码迷,mamicode.com
首页 > 其他好文 > 详细

实验六

时间:2019-06-11 15:05:49      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:ESS   显示   spl   退出   ide   fst   app   click   换行符   

1.追加

技术图片
 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 using namespace std;
 5 #include<cstdlib>
 6 
 7 int main() {
 8     string filename3 = "3.txt";
 9     ofstream fout;
10 
11     fout.open(filename3, ios_base::app);
12     if (!fout.is_open()) {
13         cerr << "fail to open file " << filename3 << endl;
14         system("pause");
15         exit(0);
16     }
17 
18     fout << endl << "merge successfully.";
19     fout.close();
20     return 0;
21 }
View Code

 

技术图片

 技术图片

 

2.

 

技术图片
 1 #include <iostream>
 2 #include <string>
 3 #include "utils.h"
 4 #include<cstdlib>
 5 using namespace std;
 6 #include<fstream>
 7 #include<vector>
 8 #include<ctime>
 9 /*1. 已知名单列表文件list.txt。
10 编写一个应用程序,实现从名单中随机抽点n位同学(n由键盘输入),
11 在屏幕上显示结果,同时也将结果写入文本文件,文件名自动读取当天系统日期,
12 如20190611.txt。*/
13 int main() {
14 
15     int n;
16     int num = 0;//记录总人数 
17     string filename, filename1;
18     string cun[100];//用于存储学生信息,并且编号 
19     int jilu[100];//用于判断是否已经被读过 
20 
21     //初始化
22     for (int i = 0; i < 100; i++) {
23         jilu[i] = 0;
24     }
25 
26     filename = getCurrentDate();
27 
28     cout << "输入名单列表文件名:";
29     cin >> filename1;
30     cout << "输入随机抽点人数:";
31     cin >> n;
32     cout << "随机抽点中..." << endl;
33     filename = filename + ".txt";
34 
35     //输入文件流对象 
36     fstream fin(filename1);
37 
38     // 如果打开文件失败,则输出错误提示信息并退出   
39     if (!fin) {
40         cerr << "fail to open file " << filename1 << endl;
41         system("pause");
42         exit(0);
43     }
44 
45     //输出文件流对象 
46     ofstream fout(filename);
47 
48     // 如果打开文件失败,则输出错误提示信息并退出   
49     if (!fout) {
50         cerr << "fail to open file " << filename << endl;
51         system("pause");
52         exit(0);
53     }
54     vector<string>aa;
55     string a;
56     while (getline(fin, a)) {
57         num++;
58         aa.push_back(a);
59     }
60 
61     srand(time(0));
62     for (int i = 1; i <= n;) {
63         int k = rand() % num;
64         if (jilu[k] != 1) {
65             cout << aa[k] << endl;
66             fout << aa[k] << endl;
67             i++;
68             jilu[k] = 1;
69         }
70     }
71 
72 
73 
74     return 0;
75 }
main

技术图片

技术图片

 

 

3.

技术图片
 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<string>
 5 #include<fstream>
 6 int tongji(string);
 7 
 8 int main() {
 9     int numc = 0, nums = 0, numw;//分别是字符,字符串,单词个数 
10     fstream fin;
11     fin.open("D:\\c++老师编程\\c++实验\\实验六\\article.txt");
12     if (!fin) {
13         cout << "fail to open!" << endl;
14         return 1;
15     }
16 
17     char a;
18     while (fin.get(a)) {
19         numc++;
20     }
21 
22     fin.close();
23     fin.open("D:\\c++老师编程\\c++实验\\实验六\\article.txt");
24     if (!fin) {
25         cout << "fail to open!" << endl;
26         return 1;
27     }
28 
29     vector<string>word;
30     string aa;
31     while (getline(fin, aa)) {
32         word.push_back(aa);
33         nums++;
34     }
35 
36     for (int i = 0; i < nums; i++) {
37         numw = numw + tongji(word[i]);
38     }
39 
40     cout << "选择要统计的数:1--统计字符数, 2--统计行数  ,3--统计单词数";
41     cout << ",4--统计字符行数 ,5--统计字符 单词,6--统计行数 单词数,7--统计字符 行数 单词数" << endl;
42     int t;
43     cin >> t;
44     if (t == 1) {
45         cout << "字符数:" << numc - nums + 1 << endl;
46     }
47     else if (t == 2) {
48         cout << "行数:" << nums << endl;
49     }
50     else if (t == 3) {
51         cout << "单词数:" << numw << endl;
52     }
53     else if (t == 4) {
54         cout << "单词数:" << numc - nums + 1 << " 行数:" << nums << endl;
55     }
56     else if (t == 5) {
57         cout << "字符数:" << numc - nums + 1 << " 单词数:" << numw << endl;
58     }
59     else if (t == 6) {
60         cout << "行数:" << nums << " 单词数:" << numw << endl;
61     }
62     else if (t == 7) {
63         cout << "字符数:" << numc - nums + 1 << " 行数: " << nums << " 单词数:" << numw << endl;
64     }
65     return 0;
66 }
67 
68 int tongji(string a) {
69 
70     int k = 1, num = 0;
71     for (int i = 0; i < a.size(); i++) {
72         if (a[i] ==  )
73             k = 1;
74         else {
75             if (k == 1) {
76                 num++;
77                 k = 0;
78             }
79         }
80     }
81     return num;
82 }
View Code

技术图片

 

实验过程中会踩的坑:在第三个中,统计字符个数时,一开始运行结果比实际多了两个,后来我察觉到时因为换行符的问题。

                                      还有在统计单词个数的时候,for(int i=0 ;i<a.size();i++)这一句中间我一开始定义的是a[i]!=‘\0’;这很明显是错误的 。

实验六

标签:ESS   显示   spl   退出   ide   fst   app   click   换行符   

原文地址:https://www.cnblogs.com/sqcmxg/p/11003452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!