实验六

part2

// 合并两个文件内容到一个新文件中。
// 文件名均从键盘输入

#include <iostream>
#include <fstream>   
#include <string>
#include <cstdlib>
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);  // 将输入文件流对象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<<"\nmerge successfully."<<endl;
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联

    system("pause");
    
    return 0;
} 
main.cpp

part3

#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 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));
utils.cpp
#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>
#include<ctime>
#include "utils.h"

using namespace std;

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

int main() {
    string number[1000];
    int jud[1000];
    string name1;
    int a=0,b;
    int max=0;
    int n;


    cout<<"输入名单列表文件名:";
    cin>>name1;
    ifstream file;


    file.open(name1);
    while(getline(file,number[a++])){
        max++;
    }

    cout<<"输入随机抽点人数:";
    cin>>n;
    cout<<"随机抽点中..."<<endl;



    string filename;
    
    filename = getCurrentDate();
    

    ofstream fileo;
    fileo.open(filename);
    suiji(max,n,jud);
    for(n;n>0;n--){
        cout << number[jud[n-1]] << endl;
        fileo<< number[jud[n-1]] << endl;
    }

    system("pause");
    
    return 0;
}
main.cpp

 

 

posted @ 2019-06-16 11:04  顾少鹏  阅读(138)  评论(0编辑  收藏  举报