实验6

给定内容仅包含'p4'及转载链接,无关键信息,无法生成有效摘要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 p2
#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); 
    if(!fin.is_open()) {  
        cerr << "fail to open file " << filename1 << endl;
        system("pause");
        exit(0);    
    }
    
    fout.open(newfilename);     
    if(!fin.is_open()) { 
        cerr << "fail to open file " << newfilename << endl;
        system("pause");
        exit(0);
    }
    
    char ch;
    
    while(fin.get(ch)) 
        fout << ch;
    
    fin.close();  
    
    fout << endl; 
    
    fin.open(filename2); 
    if(!fin.is_open()) { 
        cerr << "fail to open file " << filename2 << endl;
        system("pause");
        exit(0);    
    }
    
    while(fin.get(ch))
        fout << ch;
    
    fin.close(); 
    
    fout<<"\nmerge successfully."<<endl;
    fout.close();

    system("pause");
    
    return 0;
}

 

p3
#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;
}

utils.h

#include <string>
using std::string;
string getCurrentDate();

 

utils.cpp

#include "utils.h"
#include <ctime>
using std::string;

const int SIZE = 20;

string getCurrentDate() {

time_t time_seconds = time(0);
struct tm now_time;
localtime_s(&now_time, &time_seconds); 
char date[SIZE];

strftime(date, SIZE, "%Y%m%d", &now_time);

return (string(date))

p4

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    string fn,temp;
    char tch;
    ifstream fin;
    int charcnt,wordcnt,linecnt;
    
    cout<<"输入要统计的英文文本文件名:";
    cin>>fn;
    
    fin.open(fn);
    if(!fin.is_open())
    {
        cerr << "fail to open file"<<fn<< endl;
        system("pause");
        exit(0);    
    
    }  
        charcnt = 0;
     while(fin.get(tch))
     charcnt++;
     charcnt--;
     
     wordcnt=0;
     fin.close();
     fin.open(fn); 
     while(fin>>temp)
     wordcnt++;
     
     linecnt=0;
     fin.close();
     fin.open(fn) ;
     while(getline(fin,temp))
     linecnt++;
     
     fin.close();
     
    cout<<"字符数:"<<charcnt<<endl;
    cout<<"单词数:"<<wordcnt<<endl;
    cout<<"行数:"<<linecnt<<endl;
    
    system("pause");
    return 0; 
     
}

 

 

转载于:https://www.cnblogs.com/sora5934/p/11044594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值