实验六

Part1+Part2

// 合并两个文件内容到一个新文件中。
// 文件名均从键盘输入
#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.close(); // 关闭文件输入流对象fin与文件filename2的关联
fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联
ofstream file;
 file.open(newfilename,ios_base::app);
 if(!file.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出
cerr << "fail to open file " << filename2 << endl;
system("pause");
exit(0);}
 file<<endl;
 file<<"merge successfully. "<<endl;
 file.close();
system("pause");
return 0;
}
View Code

 

Part3

1.

//这个头文件里包含了可用工具函数的声明 

#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();
utils.h
#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));
} 
utils.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include "utils.h"
using namespace std;

void suiji(int x,int n,int m[]){
    int a=n,b;
    srand(int(time(NULL)));
    for(n;n>0;n--){
        m[n-1]=rand()%x;
        for(b=a;b>n;b--){
            if(m[b-1]==m[n-1]){
                n++;
                break;
            }
        }
    }
}

int main(){
    ifstream fin;
    ofstream fout;
    string filename0,line0;
    int m[1000];
    string a[1000];
    int num,n,max=0,b=0;
    cout << "输入名单列表文件名:";
    cin >> filename0;
    ifstream file;
    file.open(filename0);
    while(getline(file,a[b++])){
        max++;
    }
    cout << "输入随机抽点人数:";
    cin >> num;
    cout<<"随机抽点中..."<<endl;
    string filename;
    filename=getCurrentDate();
    ofstream file0;
    file0.open(filename);
     if(!file0.is_open()) {  
        cerr << "fail to open file " << filename << endl;
        system("pause");
        exit(0);    
    }
    suiji(max,n,m);
    for(n;n>0;n--){
        cout<<a[m[n-1]]<<endl;
        file0<<a[m[n-1]]<<endl;
    }
    system("pause");
    return 0;
}
main.cpp

2.

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

int main(){
    string textname,line0;
    char a[1000];
    cout<<"输入要统计的英文文本文件名:";
    cin>>textname;
    ifstream fin;
    int n=0,x=1,b=1;
    char ch;
    fin.open(textname); 
    if(!fin.is_open()) { 
        cerr << "fail to open file " << textname << endl;
        exit(0);    
    } 
    while(fin.get(ch)) 
      {
       
       if(ch!='\n')
         {
            n++;
            if(ch==' ')
              b++;
         }
           
       else
            {
            b++;
            x++;
            }
    }
     
    cout<<"字符数: "<<n<<endl;
    cout<<"单词数:"<<b<<endl;
    cout<<"行数:"<<x<<endl;
   return 0;
}
View Code

 

posted @ 2019-06-17 13:46  黎黎0202  阅读(121)  评论(0编辑  收藏  举报