实验六

part2

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

int main(){
    string a;
    ofstream file("3.txt",ios_base::app);
    file<<"merge successfully."<<endl;
    file.close();
    return 0;
}
part2

 

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 sj(int x,int n,int c[]){
    int a=n,b;
    srand(int(time(NULL)));
    for(n;n>0;n--){
        c[n-1]=rand()%x;
        for(b=a;b>n;b--){
            if(c[b-1]==c[n-1]){
                n++;
                break;
            }
        }
    }
}

int main(){
    ifstream fin;
    ofstream fout;
    string filename0,line0;
    int c[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);
    sj(max,n,c);
    for(n;n>0;n--){
        cout<<a[c[n-1]]<<endl;
        file0<<a[c[n-1]]<<endl;
    }
    system("pause");
    return 0;
}
main.cpp

 

part3.2

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

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

 

实验总结:

随机抽取的实验看了同学的代码才能写完,对于文件的输入输出流还是没有完全理解,做起来比较困难。

posted @ 2019-06-16 15:22  mzzzy  阅读(127)  评论(2编辑  收藏  举报