c++ 西安交通大学 mooc 第十三周基础练习&第十三周编程作业

做题记录

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

c++的文件操作已经好玩起来了,不过掌握好控制结构显得更为重要了。
我这也不做啥题目分析了,直接就题干-代码。

总结——留着自己看

1.	流是指从一个位置向另一个位置传输的一连串数据的集合。
2.	标准输入流一一从标准输入设备(键盘)流向程序的数据.,一般用cin流对象进行输入
3.	“>>”,这个符号是输入ios类的符号重载函数。以回车和空格作为分隔符。就是说中间的空格符号无法获得。
4.	着重介绍:get 和 getline [cin对象所有]
5.	Char c;
则:c = cin.get();  与  cin.get(c)等价。
6.	Char ch[80];
cin.get(ch,70,”|”);  //以 | 结束取最多70个字符。
7.	cin.getline()函数与cin.get()函数的第三种情况相似。但是getline会舍弃终止的字符。不用ignore函数丢掉了。
8.	get与getline的不同之处,get函数会将指针移到终止字符处,getline会将指针移到终止字符之后。则下次继续读取时的位置不同。
9.	对文件的操作
10.	Open函数open(filename<*>,openmode<ios::in(仅in) | ios::out(仅out) | ios::binary()二进制打开>)openmode默认为仅out。
11.	>>提取符,<<输出符。跟键盘输入输出的效果是类似的。Ofstream(往文件里写),ifstream(往外输出)
12.	文本文件读写的函数与有些运算符是不能用于二进制文件的,因为大多数的二进制文件的编码方式特殊,非常有可能出现乱码。

第十三周基础练习

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;
}

第十三周编程作业

这个题上周没有发布,这周继续补上

1.计算某个正整数平方根,并按要求输出

设置小数位数总结

#include <iostream>
#include <iomanip> //头文件

using namespace std;
int main()
{
    cout << setiosflags(ios::fixed) << setprecision(2) << x;//第一种方法:
    //第二种方法:
    cout.setf(ios::fixed) ;
    cout << setprecision(2) << x;
    
    //第三种方法,看起来最简洁。应该也是最实用的了吧!
    cout << fixed << setprecision(2) << x;
    return 0;
}

代码:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    int x;
    cin >>x;
    for(int i=1;i<7;i++)
    {
        cout << fixed << setprecision(i) << sqrt(x) << endl;
    }
    return 0;
}

2.读取文件,添加行号显示

代码:
测试版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char data[5][81];
    char temp[81];
    for(int i=0;i<5;i++)
    {
        cin.getline(data[i],80);
    }
    ofstream out;
    out.open("A.txt");
    for(int i=0;i<5;i++)
    {
        out << data[i] <<endl;
    }
    out.close();
    ifstream in;
    in.open("A.txt");
    for(int i=0;i<5;i++)
    {
        cout.width(5);
        cout.setf(ios::left); //这里为了练习格式输出
        cout << i+1 ;
        in >> temp;  //这里用.get方法的话似乎是不能按行输出的。
        cout << temp << endl;
    }
    in.close();
    return 0;
}

提交版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char data[5][81];
    char temp[81];
    for(int i=0;i<5;i++)
    {
        cin.getline(data[i],80);
    }
    /*ofstream out;
    out.open("A.txt");
    for(int i=0;i<5;i++)
    {
        out << data[i] <<endl;
    }
    out.close();
    ifstream in;
    in.open("A.txt");
    for(int i=0;i<5;i++)
    {
        cout.width(5);
        cout.setf(ios::left);
        cout << i+1 ;
        in >> temp;  //这里用.get方法的话似乎是不能按行输出的。
        cout << temp << endl;
    }
    in.close();*/
    for(int i=0;i<5;i++)
    {
        cout.width(4);
        cout.setf(ios::left);
        cout << i+1 ;
        cout << data[i] << endl;
    }
    return 0;
}

3.读写文件并转换字符

代码:
测试版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char data[201];
    char temp;
    cin.getline(data,200);
    ofstream out;
    out.open("a1.txt");
    out << data;
    out.close();
    ifstream in;
    in.open("a1.txt");
    while(in)
    {
        temp = toupper(in.get()); //利用了toupper函数。
        cout << temp;
    }
    return 0;
}

提交版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char data[201];
    char temp;
    cin.getline(data,200);
    /*ofstream out;
    out.open("a1.txt");
    out << data;
    out.close();
    ifstream in;
    in.open("a1.txt");
    while(in)
    {
        temp = toupper(in.get()); //使用toupper()函数简化代码量。
        cout << temp;
    }*/
    for(int i=0;;i++)
    {
        if(data[i])
        {
            temp = toupper(data[i]);
            cout << temp;
        }
        else
        {
            break;
        }
    }
    return 0;
}

4.读文件中的数字,算平均值

代码:
测试版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    double sum=0;
    int n;
    double temp;
    cin >> n;
    /*for(int i=0;i<n;i++)
    {
        cin >> temp;
        sum = sum + temp;
    }*/
    ofstream out;
    out.open("out1.txt"); //创建并打开文件
    out << n << endl;
    for(int i=0;i<n;i++)
    {
        cin >> temp;
        out << temp << endl;
    }
    out.close();
    ifstream in;
    in.open("out1.txt");
    in >> n;
    sum = 0;
    for(int i=0;i < n;i++)
    {
        if(in)
        {
            in >> temp;
            //cout << temp << endl;
            sum = sum + temp;
        }
    }
    in.close();
    cout << "Avg=" << sum / n << endl;
    return 0;
}

提交版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    double sum=0;
    int n;
    double temp;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> temp;
        sum = sum + temp;
    }
    /*ofstream out;
    out.open("out1.txt"); //创建并打开文件
    out << n << endl;
    for(int i=0;i<n;i++)
    {
        cin >> temp;
        out << temp << endl;
    }
    out.close();*/
    /*ifstream in;
    in.open("out1.txt");
    in >> n;
    sum = 0;
    for(int i=0;i < n;i++)
    {
        if(in)
        {
            in >> temp;
            //cout << temp << endl;
            sum = sum + temp;
        }
    }
    in.close();*/
    cout << "Avg=" << sum / n << endl;
    return 0;
}

5.读文件中的字符并排序输出

代码:
测试版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    void f(int n,char* data);//排序函数
    char data[100];
    int n;
    char temp;
    cin >> n;
    ofstream out;
    out.open("A.txt"); //创建并打开文件
    for(int i=0;i < n;i++)
    {
        cin >> temp;
        out << temp << " ";
    }
    out.close();
    ifstream in;
    in.open("A.txt");
    for(int i=0;i < n;i++)
    {
        if(in)
        {
            in >> data[i];
        }
    }
    in.close();
    f(n,data);

    out.open("B.txt"); //创建并打开文件B
    for(int i=0;i < n;i++)
    {
        out << data[i];
        if(i != n-1)
        {
            cout << " ";
        }
    }
    return 0;
}

void f(int n,char* data)//排序函数
{
    char temp;
    for(int i=0;i < n-1;i++)
    {
        for(int j=0;j < n-1;j++)
        {
            if(data[j] > data[j+1])
            {
                temp = data[j];
                data[j] = data[j+1];
                data[j+1] = temp;
            }
        }
    }
}

提交版

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    void f(int n,char* data);//排序函数
    char data[100];
    int n;
    //char temp;
    cin >> n;
    /*ofstream out;
    out.open("A.txt"); //创建并打开文件
    for(int i=0;i < n;i++)
    {
        cin >> temp;
        out << temp << " ";
    }
    out.close();
    ifstream in;
    in.open("A.txt");
    for(int i=0;i < n;i++)
    {
        if(in)
        {
            in >> data[i];
        }
    }
    in.close();
    f(n,data);

    out.open("B.txt"); //创建并打开文件B
    for(int i=0;i < n;i++)
    {
        out << data[i];
        if(i != n)
        {
            cout << " ";
        }
    }*/
    for(int i=0;i < n;i++)
    {
        cin >> data[i];
    }
    f(n,data);
    for(int i=0;i < n;i++)
    {
        cout << data[i];
        if(i != n-1)
        {
            cout << " ";
        }
    }
    return 0;
}

void f(int n,char* data)//排序函数
{
    char temp;
    for(int i=0;i < n-1;i++)
    {
        for(int j=0;j < n-1;j++)
        {
            if(data[j] > data[j+1])
            {
                temp = data[j];
                data[j] = data[j+1];
                data[j+1] = temp;
            }
        }
    }
}

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

变秃是不可能变秃的    ——M4xlmum

54mp55CG5p2A5oiR

posted @ 2020-05-10 14:58  M4xlmum  阅读(473)  评论(0编辑  收藏  举报