实验6

 

Part 2

#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);  // 将输入文件流对象fin与文件filename1建立关联 
    if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出 
        cerr << "fail to open file " << filename1 << endl;
        system("pause");
        exit(0);    
    }
    
    fout.open(newfilename);    // 将输出文件流对象fout与文件newfilename建立关联 
    if(!fin.is_open()) {  // 如果创建/打开文件失败,输出错误提示信息并退出  
        cerr << "fail to open file " << newfilename << endl;
        system("pause");
        exit(0);
    }
    
    char ch;
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 
    while(fin.get(ch)) 
        fout << ch;
    
    fin.close();  // 关闭文件输入流对象fin与文件filename1的关联 
    
    fout << endl; // 向文件输出流对象fout中插入换行 
    
    
    fin.open(filename2);  // 将输入文件流对象fin与文件filename2建立关联 
    if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出
        cerr << "fail to open file " << filename2 << endl;
        system("pause");
        exit(0);    
    }
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
    while(fin.get(ch))
        fout << ch;
        
    fin.open("3.txt",ios::app);
    fout<<"\nmerge successfully."<<endl;
    
    fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联

    system("pause");
    
    return 0;
}
View Code

 

Part 2

1:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "utils.h"

using namespace std;

static int is_choose[83]={0};

int main() {
    
    string filename,listfilename;
    
    filename = getCurrentDate();
    
    int n;
    cout<<"输入名单列表文件名:";
    cin>> listfilename;
    cout<<"输入随机抽点人数:";
    cin>>n;
    cout<<"随机抽点中..." <<endl; 
    
    ofstream fout;//输出文件流对象
    ifstream fin;//输入文件流对象
    
    fin.open(listfilename);
    if(!fin.is_open()){
        cerr<<"fail to open file"<<filename<<endl;
        system("pause");
        exit(0);
        }
        
        fout.open(filename);
        if(!fin.is_open()){
            cerr<<"fail to open file"<<filename<<endl;
            system("puase");
            exit(0);
        }
    srand((unsigned)time(NULL));
    
    vector <string> num;
    string leftcon;
    while (getline(fin,leftcon))
    num.push_back(leftcon);
    
    for(int i=1;i<=n;i++){
        int choose_num=rand()%83+1;
        if(is_choose[choose_num]==1){
            i--;
            continue;
        }
        is_choose[choose_num]=1;
        cout<<num[choose_num]<<endl;
        fout<<num[choose_num]<<endl;
    }
    
    fin.close();
    fout.close();
    
    system("pause");
    
    return 0;
}
View Code

 

 

2:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

int main(){
    string filename;
    ifstream fin;
    ofstream fout;
    
    int num=0,num1=0,line=0;
    cout<<"输入要统计的英文文本文件名:";
    cin>>filename;
    fin.open(filename);
    if(!fin.is_open()){
        cout<<"fail to open"<<endl;
        exit(0);
    } 
    
    string a;
    string b[10000];
    int i,j;
    while (getline(fin,a)){
        b[line]=a;
        line++;
        j=a.length();
        for(i=0;i<j;i++){
            if(a[i]==' ')
            num1++;
        } 
        num1++;
    }
    for(i=0;i<line;i++){
        j=b[i].length();
        num+=j; 
    }
    fin.close();
    cout<<"字符数:"<<num<<endl;
    cout<<"单词数:"<<num1<<endl;
    cout<<"行数:"<<line<<endl;
    return 0; 
}
View Code

 

 
  
 

 

posted @ 2019-06-18 10:18  GeorgeWan  阅读(94)  评论(0编辑  收藏  举报