实验6

 p2
#include 
#include    
#include <string>
#include 
using namespace std;

int main() {
    char filename1[10], filename2[10], newfilename[10];
    
    cout << "输入要合并的两个文件名: " ;
    gets(filename1); gets(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;
    
    fin.close(); 
    
    fout<<"\nmerge successfully."<<endl;
    fout.close();

    system("pause");
    
    return 0;
}

实验6_第1张图片

 

p3
#include
#include <string>
#include 
#include
#include
#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 "< 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;
}

utils.h

#include <string>
using std::string;
string getCurrentDate();

 

utils.cpp

#include "utils.h"
#include 
using std::string;

const int SIZE = 20;

string getCurrentDate() {

time_t time_seconds = time(0);
struct tm now_time;
localtime_s(&now_time, &time_seconds); 
char date[SIZE];

strftime(date, SIZE, "%Y%m%d", &now_time);

return (string(date))

实验6_第2张图片

p4

#include
#include
#include<string>
using namespace std;
int main()
{
    string fn,temp;
    char tch;
    ifstream fin;
    int charcnt,wordcnt,linecnt;
    
    cout<<"输入要统计的英文文本文件名:";
    cin>>fn;
    
    fin.open(fn);
    if(!fin.is_open())
    {
        cerr << "fail to open file"< endl;
        system("pause");
        exit(0);    
    
    }  
        charcnt = 0;
     while(fin.get(tch))
     charcnt++;
     charcnt--;
     
     wordcnt=0;
     fin.close();
     fin.open(fn); 
     while(fin>>temp)
     wordcnt++;
     
     linecnt=0;
     fin.close();
     fin.open(fn) ;
     while(getline(fin,temp))
     linecnt++;
     
     fin.close();
     
    cout<<"字符数:"<endl;
    cout<<"单词数:"<endl;
    cout<<"行数:"<endl;
    
    system("pause");
    return 0; 
     
}

实验6_第3张图片

 

 

你可能感兴趣的:(实验6)