实验六
#include<fstream> #include<iostream> #include<string> using namespace std; int main() { ofstream out("D:\\DEV\\3.txt",ios_base::app); if (!out) { cout << "Fail to open." << endl; return 1; } out << endl; out << "merge successfully"; out.close(); return 0; }
part 3
#include<iostream> #include<fstream> #include<cstring> #include<cstdlib> #include<ctime> #include"utils.h" using namespace std; int n,i,j,k; string filename,filename1; string s,a[100]; int main() { filename1=getCurrentDate(); ifstream fin; ofstream fout; cout<<"输入名单列表文件名:"; cin>>filename; fout.open(filename1); fin.open(filename,ios_base::in); if(!fin.is_open()) { cout<<"fail to open file"<<filename<<endl; system("pause"); exit(0); } if(fin) { cout<<"输入随机抽点人数:"; cin>>n; cout<<"随机抽点中"<<filename1 <<endl; } srand((unsigned)time(0)); for( i=0;i<83;i++) { getline(fin,s); a[i]=s; } for(j=0;j<n;j++) { k=1+rand()%83; cout<<a[k]<<endl; fout<<a[k]<<endl; } fin.close(); fout.close(); system("pause"); return 0; }
#include "utils.h" #include <ctime> using std::string; const int SIZE = 20; // 函数功能描述:返回当前系统日期 // 参数描述:无参数 // 返回值描述:以string类型返回系统当前日期,格式诸如20190611 string getCurrentDate() { time_t now = time(0); // 获取当前系统日历时间 struct tm *local_time = localtime(&now); // 把系统日历时间转换为当地时间 char date[SIZE]; strftime(date, SIZE, "%Y%m%d", local_time); return (string(date)); }
//这个头文件里包含了可用工具函数的声明 #include <string> using std::string; // 函数声明 // 返回当前系统时间,格式诸如20190611 string getCurrentDate();
part 3(2)
#include<iostream> #include<fstream> #include<cstring> #include<cstdlib> using namespace std; int main() { string filename; ifstream fin; ofstream fout; cout<<"输入要打开的文件文本名:"; cin>>filename; fin.open(filename); if(!fin.is_open()) { cout<<"fail to open the file"<<endl; exit(0); } string s; int i=0,j=0,hang=0; while(getline(fin,s)) { hang++; int x; x=s.size(); i+=x; char a[x]; for(int m=0;m<x;m++) { a[m]=s[m]; } a[x-1]='\0'; for(int n=0;n<x;n++) { if(a[n]==' '&&a[n+1]!=' '||(a[n]=='.'||a[n+1]==',')&&a[n+1]!=a[n]) j++; } } cout<<"字符数:"<<i<<endl; cout<<"单词数:"<<j<<endl; cout<<"行数:"<<hang <<endl; fin.close(); system("pause"); return 0; }
1.基础练习和part(1)的逻辑都挺清楚的,只有part3(1)中随机函数不是很熟练,但是part3(2)一开始感觉挺好写的。但是后来一直有错运行不出来,然后让同学给我分析思路,按照新的思路重新写的。
2.文件的输入输出并不是特别熟练。
3.写程序之前还是需要先想好算法。
https://www.cnblogs.com/sq102217/p/11048324.html
https://www.cnblogs.com/yidaoyigexiaopenyou/p/11044639.html
https://www.cnblogs.com/99563220-fhy/p/11044559.html