C++实验6

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

int main() {
    ofstream fout;
    fout.open("3.txt",ios_base::app);
    fout << "merge successfully.";
    system("pause");
    return 0;
}
View Code

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

#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190607
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<sstream>
#include<ctime>
#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 "<<filename << 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;
}
 
main.cpp

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main() {
    ifstream fin;
    fin.open("article.txt"); 
    if (!fin.is_open()) { 
        cerr << "fail to open file " << "article.txt" << endl;
        system("pause");
        exit(0);
    }
    char ch;
    int count = 0;
    while (fin.get(ch)){
        if (ch >= 'A'&&ch <= 'z') {
            count++;
            while (ch >= 'A'&&ch <= 'z') {
                fin.get(ch);

            }
        }
    }
    fin.close();

    cout << count << "words" << endl;
    system("pause");

    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Yyaoyyy/p/11005822.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值