实验六

一、

 1 #include <iostream>
 2 #include <string>
 3 #include<ctime>
 4 #include<fstream>
 5 #include<stdlib.h>
 6 using namespace std;
 7 int main()
 8 {
 9     string filename, t; cout << "输入文件名:" ;
10     getline(cin, filename);
11     cout << "输入要加入的内容:" ;
12     getline(cin, t);
13     
14     ofstream a(filename, ios::app);
15     if (!a)
16     {
17         cout << "wrong" << endl;
18     }
19     if (a)
20     {
21         a << t << endl;
22     }
23     system("pause");
24     return 0;
25 }

 

实验六

实验六

二、

 1 #include <iostream>
 2 #include <string>
 3 #include<ctime>
 4 #include<fstream>
 5 #include<stdlib.h>
 6 #include"utils.h"
 7 using namespace std;
 8 struct Student
 9 {
10     string mynumber;
11     string xuehao;
12     string myname;
13     string myclass;
14 };
15 int main()
16 {
17     string t;
18     cout << "输入文件名:";
19     cin >> t;
20     ifstream file1(t, ios_base::in);
21     if (!file1)
22     {
23         cout << "wrong" << endl;
24     }
25     int i;
26     Student a[83];
27     for (i = 0; i < 83; i++)
28     {
29         file1 >> a[i].mynumber >> a[i].xuehao >> a[i].myname >> a[i].myclass;
30     }
31     string b;int x = -1;
32     b = getCurrentDate() + ".txt";
33     ofstream file2(b, ios::out);
34     cout << "抽取人数:";
35     int m; 
36     cin >> m;
37     if (!file2)
38     {
39         cout << "wrong" << endl;
40     }
41     srand(time(0)); 
42     for (i = 0; i < m; i++) {
43         int x = rand() % (83) - 1;
44             cout << a[x].mynumber << " " << a[x].xuehao << " " << a[x].myname << " " << a[x].myclass << endl;
45             file2 << a[x].mynumber << " " << a[x].xuehao << " " << a[x].myname << " " << a[x].myclass << endl;
46         }
47         
48     file1.close();
49     file2.close();
50     system("pause");
51     return 0;
52 
53 
54 
55 
56 
57 }

实验六

实验六

 

 

 

三、

 1 #include <iostream>
 2 #include <string>
 3 #include<ctime>
 4 #include<fstream>
 5 #include<stdlib.h>
 6 using namespace std;
 7 int main()
 8 {
 9     string name,a;
10     string myarray[1000]; int m=0,i=0,count=0,sum=0;
11     cout << "输入文件名:";
12     cin >> name;
13     ifstream file1(name, ios_base::in);
14     if (!file1)
15     {
16         cout << "wrong" << endl;
17     }
18     if (file1) {
19         while (getline(file1, a))
20         {
21             myarray[m++] = a;
22             for (i = 0; i < a.length(); i++)
23             {    
24                 if (a[i] == ' '&&a[i + 1] != ' ')
25                 {
26                     count++;
27                 }
28                 
29             }
30             count++;
31         }
32     }
33         file1.close();
34     for (i = 0; i < m; i++)
35     {
36         sum += myarray[i].length();
37     }
38     cout << "字符数:" << sum << endl;
39     cout << "单词数:" << count << endl;
40     cout << "行数:" << m << endl;
41     system("pause");
42     return 0;
43 
44 }

 

实验六

总结:

这次作业总体来说不是特别难,但因为有些地方一开始不太懂,导致开始写的时候进度特别缓慢。

1、ofstream a(filename,ios::out)和ofstream a;a.open(filename)好像区别不是很大。

2、抽取人数那里的 重复问题暂时还没解决。

3、数单词的题目似乎我把它想复杂了,其实只是多一个ifstream的步骤。