c++第六次实验

part 1 验证性实验

合并两个文件。虽说验证,但后两个实验均受该代码指导启发。

 

part 2  文末添加数据

1、代码

 1 #include<fstream>
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     ofstream out("C:\\Users\\lenovo\\Desktop\\实验六\\实验六part1\\实验六part1\\3.txt",ios_base::app);
 8     if (!out)
 9     {
10         cout << "Fail to open." << endl;
11         return 1;
12     }
13     out << endl;
14     out << "merge successfully";
15     out.close();
16     return 0;
17 }
part 2

2、程序截图

 

3、知识点

①“打开一个与输出流相关的文件时,可以指定一个open_mode标志,实现一些特定模式。用 ‘ | ’组合多个。 ”本题需要在文件尾添加数据,即“ios_base::app”。

②路径写法注意双斜杠,为了与转义字符相区别。

Part 3 

一、随机抽取

1、代码

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 #include<ctime>
 5 #include<sstream>
 6 #include<windows.h>
 7 using namespace std;
 8 string getdate();
 9 int main()
10 {
11     string filename, newfilename;//f是已存在的名单文件,new是即将存随机抽取的文件
12     int n, i;
13     
14     ofstream out;
15     ifstream in;
16     
17     cout << "输入名单列表文件名:";
18     cin >> filename;
19     cout << "输入随机抽点人数:";
20     cin >> n;
21     cout << "随机抽点中···" << endl;
22 
23     newfilename = getdate().c_str();//新建文件命名
24     
25     out.open(newfilename);
26     if (!out.is_open())
27     {
28         cout << "Fail to open." << endl;
29         return 1;
30     }
31 
32     for (i = 1; i <= n; i++)
33     {    
34         in.open(filename);
35         if (!in.is_open())
36         {
37             cout << "Fail to open." << endl;
38             return 1;
39          }
40         
41         srand((unsigned)time(NULL));
42         int N = rand() % 84;
43         
44         string s;
45         stringstream ss;
46         ss << N;
47         ss >> s;
48         
49         char c;
50         int j=1;
51         while (in.get(c))
52         {
53             if (c == '\n')
54             {
55                 j++;
56                 if (j == N)
57                 {
58                     in.get(c);
59                     while (c != '\n')
60                     {
61                         out << c;
62                         in.get(c);
63                     }
64                     out << endl;
65                     break;
66                 }
67             }
68             else
69                 continue;
70         }
71         in.close();
72         Sleep(1000);
73     }
74     out.close();
75 
76     char c;
77     ifstream nfile(newfilename);
78     while (nfile.get(c))
79         cout << c;
80     nfile.close();
81     return 0;
82 }
83 const int SIZE1 = 20;
84 string getdate()
85 {
86     time_t time_seconds = time(0);    
87     struct tm now_time;    
88     localtime_s(&now_time, &time_seconds); // 使用了更安全的localtime_s()        
89     char date[SIZE1];
90     strftime(date, SIZE1, "%Y%m%d", &now_time);        
91     return (string(date));
92 }
part 3.1

2、程序截图

3、出现的一些问题

①查找相应标号的同学,最开始想得简单,是通过简单对比数字,后来发现学号里也有数字。(有点儿傻的过分了)后来纠正的一些方法不如数行数更简便,最终就由数行数来找对应的同学。

②输入流文件打开操作 在n次抽取循环的外面,导致in.get(c)是接着数的。例如:

 

    我机智的发现应该把 打开list  和 打开20190611 两个操作都放循环里面,每次循环就可以重新打开重新数。我还特别机智的立即想到也要在里面关闭两个文件,结果只保留了最后一个抽取的情况。例如:

    循环哪个地方每次都会清空内容。翻课本时惊现不久前的笔记,输出流中隐含指定的模式是ios_base::out,当使用该模式打开已存在的文件时,内容清空。解决可以用part 2中的追加模式。

    我是只把list打开关闭操作放在循环内。

③每次随机数取的差不多。例如:

   我猜是随机数取的系统时间精度不那么高,程序运行又很快。不知道对不对地 人为的 让它每次取随机数前延迟一阵子。用了Sleep函数,感觉嗯,慢的过分了。

   最终结果就如代码 and 截图。

二、统计单词

1、代码

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     string filename;
 8     char c;
 9 
10     cout << "输入要统计的英文文本文件名:";
11     cin >> filename;
12 
13     int a = 0, b = 1, s = 1;//a字符数 b单词数 c行数
14 
15     ifstream in;
16     in.open(filename);
17     if (!in)
18     {
19         cout << "Fail to open." << endl;
20         return 1;
21     }
22 
23     while (in.get(c))
24     {
25         if (c != '\n')
26             a++;
27         if (c == '\n')
28             s++;
29         if (c == ' ' || c == '\n' || c == '\t')
30         {
31             in.get(c);
32             a++;
33             if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z'))
34             {
35                 b++;
36             }
37         }
38     }
39     cout << "字符数:" << a << endl;
40     cout << "单词数:" << b << endl;
41     cout << "行数:" << s << endl;
42     return 0;
43 }
part 3.2

2、程序结果截图

3、一点说明

   没有用getline等等,就全一个一个数的,一次性全数完。

总结

1、学流类库之前认为插入提取用<<或者>>就是刻板规定,而且学习之初形象理解认为 输出 大概用>>更形象,所以很多时候还会打反。在学习流类库的时候也混,怎么它就是输出流文件,不是往它里面打字cin吗/扶额/。尤其做完实验才有点儿理解,明白为什么cin后跟提取,cout后跟插入,也就是你提取资料是输入,你把资料筛选出的内容插入到新文件,又是输出。

2、做实验总会有做oj的错觉,我会不会少打了回车啊,好像落了句号啊.../叹气/。

3、程序错乱或者漏洞可以直接指出,谢谢。

后继更新。

回顾知识点的时候参考博客https://blog.csdn.net/w_linux/article/details/72793651

评价

1、https://www.cnblogs.com/q1831726125/p/11044621.html

2、https://www.cnblogs.com/yidaoyigexiaopenyou/p/11044639.html

3、https://www.cnblogs.com/cjj1/p/11043693.html

 

转载于:https://www.cnblogs.com/lovecpp/p/10999562.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值