编程语言
首页 > 编程语言> > c++ 西安交通大学 mooc 第十三周基础练习

c++ 西安交通大学 mooc 第十三周基础练习

作者:互联网

做题记录

风影影,景色明明,淡淡云雾中,小鸟轻灵。

也不做啥题目分析了,直接就题干-代码。

1.格式输出

题目内容:

编写程序,按下列格式显示信息:

#&&&&&&&1#

#&&&&&&10#

#&&&&&100#

#&&&&1000#

#&&&10000#

#&&100000#

#&1000000#

共7行,每行的数值是固定的,每行两端是“#”号,中间的“&”是填充字符,实际数字的位数小于域时自动填充。 

输入:域宽、填充字符和对齐方式,其中对齐方式:1表示居左,0表示具有。

输出:题目说明的7行信息。

【提示】域宽:cout.width(k);填充:cout.fill(c);对齐:cout.setf(ios::left)。

样例1输入:

8 & 0

样例1输出:

#&&&&&&&1#

#&&&&&&10#

#&&&&&100#

#&&&&1000#

#&&&10000#

#&&100000#

#&1000000#
#include <iostream>

using namespace std;

int main()
{
	int wid, meth,temp=1;
	char a;
	cin >> wid >> a >> meth;
	if (meth == 1)
		cout.setf(ios::left);
	else
		cout.setf(ios::right);
	for (int i = 0; i < 7; i++)
	{
		cout << '#';
		cout.width(wid);
		cout.fill(a);
		cout <<  temp << '#' << endl;
		temp = temp * 10;
	}
	return 0;
}

2.文件版HelloWorld

#include <iostream>
//#include <fstream>

using namespace std;

int main()
{
    //ofstream out;
    //out.open("a1.txt");
    //out << "Hello world!" <<endl;
    cout << "Hello World." << endl;
    //out.close();
    return 0;
}

3.从文件中读一行文字

题目内容:

编写程序,从a1.txt中读取一行文字,显示到屏幕上(a1.txt文件用记事本创建,内容为一行
文字“Hello World.”,与程序在同一个文件夹下)。

输入:无

输出:Hello World.

【提示】

样例1输入:

样例1输出:

Hello World.

提交版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    cout << "Hello World." << endl;
    /*ifstream in;
    ofstream out;
    char s[100];
    out.open("a1.txt");
    out << "Hello world." <<endl;
    out.close();
    in.open("a1.txt");
    in.get(s,99);
    cout << s;
    in.close();*/
    return 0;
}

测试版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    cout << "Hello World." << endl;
    ifstream in;
    ofstream out;
    char s[100];
    out.open("a1.txt");
    out << "Hello world." <<endl;
    out.close();
    in.open("a1.txt");
    in.get(s,99);
    cout << s;
    in.close();
    return 0;
}

4.显示文本文件内容

题目内容:

编写程序,将文本文件的内容显示到屏幕上。

 

输入:整数

输出:整数

【提示】

样例1输入:

0

样例1输出:

0

提交版 && 测试版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char s[802]; //400个汉字。
    int a;
    ifstream in;
    cin >> a;
    cout << a << endl;
    in.open("a1.txt");
    while(in)
    {
        in.get(s,800);
        if(in)
        {
            cout << s;
        }
    }
    in.close();
    return 0;
}

5.从文件读整数求和

编写程序,读取文本文件a2.txt中的两个整数,计算它们的和。文件请自己用记事本创建,内容是两个整数,在一行或两行均可,文件放在程序所在文件夹下。

输入:两个整数

输出:和

【提示】

样例1输入:

2 3

样例1输出:

5

测试版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int a,b,c;
    ifstream in;
    in.open("a1.txt");
    in >> a >> b;
    c = a + b;
    cout << c << endl;

    cin >> a >> b;
    c = a + b;
    cout << c << endl;
    return 0;
}

提交版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int a,b,c;
    /*ifstream in;
    in.open("a1.txt");
    in >> a >> b;
    c = a + b;
    cout << c << endl;*/

    cin >> a >> b;
    c = a + b;
    cout << c << endl;
    return 0;
}

好,这周的题有点简单啊,估计是因为第一次接触文件编程的原因吧!

变强的路上总要拒绝各种诱惑    ——M4xlmum

标签:std,mooc,cout,int,样例,c++,西安交通大学,using,include
来源: https://blog.csdn.net/zhangxiansheng12/article/details/106035126