实验六
part 2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main() { string filename1, filename2, newfilename; cout << "输入要合并的两个文件名: "; cin >> filename1 >> filename2; cout << "输入合并后新文件名: "; cin >> newfilename; ofstream fout; ifstream fin; fin.open(filename1); if (!fin.is_open()) { cerr << "fail to open file " << filename1 << endl; system("pause"); exit(0); } fout.open(newfilename); if (!fin.is_open()) { cerr << "fail to open file " << newfilename << endl; system("pause"); exit(0); } char ch; while (fin.get(ch)) fout << ch; fin.close(); fout << endl; fin.open(filename2); if (!fin.is_open()) { cerr << "fail to open file " << filename2 << endl; system("pause"); exit(0); } while (fin.get(ch)) fout << ch; fout << endl; fout << "merge sucessfully."; fin.close(); fout.close(); system("pause"); return 0; }
截图
part 3
1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//这个头文件里包含了可用工具函数的声明 #include <string> using std::string; // 函数声明 // 返回当前系统时间,格式诸如20190607 string getCurrentDate();
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "utils.h" #include <ctime> using std::string; const int SIZE = 20; // 函数功能描述:返回当前系统时间 // 参数描述:无参数 // 返回值描述:以string类型返回系统当前日期,格式诸如20190611 string getCurrentDate() { time_t time_seconds = time(0); struct tm now_time; localtime_s(&now_time, &time_seconds); // 使用了更安全的localtime_s() char date[SIZE]; strftime(date, SIZE, "%Y%m%d", &now_time); return (string(date)); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<fstream> #include <string> #include <cstdlib> #include<sstream> #include<ctime> #include "utils.h" using namespace std; int main() { ifstream fin; ofstream fout; string filename1; int num, totalline = 0; cout << "输入名单列表文件名:"; cin >> filename1; cout << "输入随机抽点人数:"; cin >> num; fin.open(filename1, ios_base::in); if (!fin.is_open()) { cerr << "fail to open file " << filename1 << endl; system("pause"); exit(0); } string temp; string a[100]; if (fin) { while (getline(fin, temp)) { a[totalline++] = temp; } fin.close(); } string filename; filename = getCurrentDate() + ".txt"; cout << "new file name is " << filename << endl; srand(std::time(0)); for (int i = 0; i < num; i++) { int line; int random = rand(); line = rand() % totalline; cout << a[line] << endl; fout.open(filename, ios_base::app); fout << a[line] << endl; fout.close(); } system("pause"); return 0; }
截图
2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<string.h> #include<fstream> #include<iostream> using namespace std; int main() { ifstream in("article.txt"); long ln=0,cn=0,wn=0; char str[1000]; while (in.getline(str, 1000)) { for (int i=0;i<strlen(str);i++) { cn++; if (str[i] == ' '||str[i]=='...'||(str[i]=='.'&&str[i + 1]!= '.'&&str[i - 1] != '.') || str[i] == ',') { wn++; } } ln++; } wn--; cout<< "行数:" << ln<< endl << "字符数:" << cn<< endl <<"单词数:"<<wn<< endl; in.close(); system("pause"); return 0; }
截图