实验六

1.验证性实验

 1 // 合并两个文件内容到一个新文件中。
 2 // 文件名均从键盘输入
 3 
 4 #include <iostream>
 5 #include <fstream>   
 6 #include <string>
 7 #include <cstdlib>
 8 using namespace std;
 9 
10 int main() {
11     string filename1, filename2, newfilename;
12     
13     cout << "输入要合并的两个文件名: " ;
14     cin >> filename1 >> filename2;
15     cout << "输入合并后新文件名: " ;
16     cin >> newfilename;
17     
18     ofstream fout;        // 输出文件流对象 
19     ifstream fin;        // 输入文件流对象 
20     
21     
22     fin.open(filename1);  // 将输入文件流对象fin与文件filename1建立关联 
23     if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出 
24         cerr << "fail to open file " << filename1 << endl;
25         system("pause");
26         exit(0);    
27     }
28     
29     fout.open(newfilename,ios_base::app);    // 将输出文件流对象fout与文件newfilename建立关联 
30     if(!fin.is_open()) {  // 如果创建/打开文件失败,输出错误提示信息并退出  
31         cerr << "fail to open file " << newfilename << endl;
32         system("pause");
33         exit(0);
34     }
35     
36     char ch;
37     
38     // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 
39     while(fin.get(ch)) 
40         fout << ch;
41     
42     fin.close();  // 关闭文件输入流对象fin与文件filename1的关联 
43     
44     fout << endl; // 向文件输出流对象fout中插入换行 
45     
46     
47     fin.open(filename2);  // 将输入文件流对象fin与文件filename2建立关联 
48     if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出
49         cerr << "fail to open file " << filename2 << endl;
50         system("pause");
51         exit(0);    
52     }
53     
54     // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
55     while(fin.get(ch))
56         fout << ch;
57     
58     fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
59     fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联
60 
61     system("pause");
62     
63     return 0;
64 }
View Code

2.基础练习

 1 #include<iostream>
 2 #include<fstream>
 3 #include<ostream>
 4 #include<cstring> 
 5 using namespace std;
 6 int main(){
 7     string file;
 8     string str[100];
 9     cout<<"请选择一个文件,输入文件名:";
10     cin>>file; 
11     cout<<endl;
12     cout<<"请输入想要添加的内容:";
13     int t=0,m=-1;
14     for(int i=0;t==0;i++){
15         cin>>str[i];
16         m++;
17         if(cin.get()=='\n')
18         t=1;
19     }
20     ofstream out(file,ios_base::app);
21     if(out.is_open()){
22         out<<endl<<endl;
23         for(int i=0;i<=m;i++){
24         out<<str[i];    
25     }
26     out.close();
27         system("pause");
28         return 0;
29 }
30 }
View Code

3.编程1

更改了main函数

 1 #include<iostream>
 2 #include<string>
 3 #include<fstream>
 4 #include<ctime>
 5 #include "utils.h"
 6 #define N 100 
 7 using namespace std;
 8 
 9 int main() {
10     
11     string filename,file0;
12     int n,num;
13     char a[83][N];
14     char s;
15     
16     filename = getCurrentDate();
17     
18     ifstream fin;
19     ofstream out;
20     
21     
22     cout<<"输入将要检索的列表文件名:"; 
23     cin>>file0;
24     
25     cout<<"请输入希望在该列表中抽取的人数:";
26     cin>>n;
27     
28     fin.open(file0);
29     if(fin.is_open())
30     for (int i=0;i<83;i++)
31     {
32         int t=0; 
33         for (int j=0;j<N&&t==0;j++) {
34             s=fin.get();
35             a[i][j]=s;
36             if(s=='\n')
37                 t=1;
38             }
39         }
40         
41     fin.close();
42     
43     out.open(filename);
44     if(!out.is_open())
45     {
46         cerr << "fail to open file."<< endl;
47         system("pause");
48         exit(0);    
49     }
50     else{
51         srand(time(0));
52         for(int i=0;i<n;i++){
53             num=rand()%83;
54             cout<<a[num];
55             out<<a[num];
56         }    
57     }
58     out.close();
59     system("pause");
60     return 0;
61 }
main.cpp
 1 #include "utils.h"
 2 #include <ctime>
 3 using std::string;
 4 
 5 const int SIZE = 20;
 6 
 7 // 函数功能描述:返回当前系统日期 
 8 // 参数描述:无参数
 9 // 返回值描述:以string类型返回系统当前日期,格式诸如20190611 
10 string getCurrentDate() {
11     
12     time_t now = time(0);  // 获取当前系统日历时间
13     
14     struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
15     
16     char date[SIZE];
17 
18     strftime(date, SIZE, "%Y%m%d", local_time);
19     
20     return (string(date));
21 }
utils.cpp
1 //这个头文件里包含了可用工具函数的声明 
2 
3 #include <string>
4 using std::string;
5 
6 // 函数声明
7 // 返回当前系统时间,格式诸如20190611
8 string getCurrentDate();
utils.h

实验过程中出现过乱码,稍后重试就没有了不知道哪里有问题……

4.编程2

 1 #include <iostream>
 2 #include <fstream>   
 3 #include <string>
 4 using namespace std;
 5 int main() {
 6     string filename,file;
 7      
 8     cout<<"输入要统计的文本文件名:" ;
 9     cin>>filename;
10     cout<<"设置结果文本的存储路径:";
11     cin>>file; 
12 
13     ifstream fin;
14     ofstream out;
15     
16     fin.open(filename);
17     if(!fin.is_open()) {
18         cerr << "fail to open file "<<endl;
19         system("pause");
20         exit(0);    
21     }
22     else{
23     char ch;
24     int str=0,word=1,line=1;
25     while(fin.get(ch)){
26         if(ch==' '){
27             word++;
28             str++;
29         }
30         else if(ch=='\n'){
31             line++;
32             word++;
33         } 
34         else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
35             str++;
36         }
37         else
38         str++;
39         }
40         out.open(file);
41         if(!out.is_open()) {
42         cerr << "fail to open file "<<endl;
43         system("pause");
44         exit(0);    
45     }
46     else{
47         cout<<"字符数:"<<str<<endl<<"单词数:"<<word<<endl<<"行数:"<<line<<endl;
48         out<<"字符数:"<<str<<endl<<"单词数:"<<word<<endl<<"行数:"<<line<<endl;
49         fin.close();
50     }
51         return 0;
52 }
53 } 
View Code

实验反思:

啊好难a……其中算法能写出来。验证性实验中"merge successfully"中间空格输出不了,这里有疑问,改了几次感觉更糟糕了,到最后只能输出“merge”了,又默默改回去……编程练习一开始会输出乱码我的妈a……程序反复看来看去都没有问题。开了文件之后一定要记得.close(),close一定要放对位置,还总是打不开文件,各种拉文件各种删文件,还是写实验前掌握不够……不过大概除了算法的部分都遇到了问题,都做了改正。写mergesuccessfully的时候有去偷看同学的程序,为什么输出是直接cout……不应该由键盘键入吗,然后就出现了现在的程序……过程狗血,自当勉励。

 

posted @ 2019-06-12 13:36  DADABud  阅读(122)  评论(1编辑  收藏  举报