c++实验六

实验1:

源代码:

// 文件名均从键盘输入

#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<<endl;
    fout<<"merge successfully"<<endl;
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联

    system("pause");
    
    return 0;
} 

// 说明:
// 这个简单示例中,合并两个文件的具体方法,是逐个读取文件中的字符直到文件末尾,并写入到新文件中
// 还可以一次读取一行
// 或者,直接利用标准模板库的成员函数,一次性将整个文件内容读取至缓冲区
// 等过了期末考,时间宽松一些的时候可以学习体验更多标准模板库的内容,对后续专业课的学习(如数据结构、算法、操作系统等)也会有帮助 

运行截图:

实验二:

main源代码:

#include <iostream>
#include <string>
#include "utils.h"
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int main() {
    string filename; 
    filename = getCurrentDate();
    cout << filename << endl;
    char file0[100];
    cout<<"输入名单列表文件名:";
    gets(file0) ;
    int n;
    cout<<"请输入抽取人数:";
    cin>>n;
    ofstream fout;
    ifstream fin;
    fin.open(file0);
    if(!fin.is_open()){
        cerr<<"fail to open file"<<file0<<endl;
        system("pause");
        exit(0);}
    char date[100];
    strcpy(date,filename.c_str());
    char *txt=".txt";
    strcat(date,txt);
    cout<<"输出文件名:"<<date<<endl;
    fout.open(date);
    if(!fout.is_open()){
        cerr<<"fail to open file"<<date<<endl;
        system("pause");
        exit(0);}
    int num=0;
    string x[100];
    ifstream infile(file0);
    while(!infile.eof()){
        getline(infile,x[num],'\n');
        num++;
    }
    int y[100];
    for(int i=0;i<n;i++){
        y[i]=0;
    }
    srand(time(0));
    for(int j=0;j<n;){
        int z=rand()%num;
        if(y[z]!=1) {
            cout<<x[z]<<endl;
            fout<<x[z]<<endl;
            y[z]=1;
            j++;
        }
    }
    return 0;
}
 

运行截图:

实验三:

源代码:

 

#include <iostream> 
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
    char filename[100],file[100];
    strcpy(filename,".txt");
    strcpy(file,".txt");
    cout<<"输入要统计的英文文本文件名:";
    cin>>filename;
    cout<<"输出结果:";
    cin>>file;
    ifstream fin;
    ofstream fout;
    fin.open(filename) ;
    if(!fin.is_open()){
        cerr<<"fail to open file"<<endl;
        system("pause");
        exit(0);
    }
    char ch;
    int a=0,b=1,c=1;
    while(fin.get(ch)){
        if(ch==' '){
            b++;
            a++;
        }
        else if(ch=='\n'){
            c++;
            b++;
        }
        else if((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')){
            a++;
        }
        else{
            a++;
        }
        fout.open(file);
        if(!fout.is_open()){
            cerr<<"fail to open file"<<endl;
            system("pause");
            exit(0) ;
        }
    }
        cout<<"字符数:"<<a<<endl<<"单词数:"<<b<<endl<<"行数:"<<c<<endl;
        fout<<"字符数:"<<a<<endl<<"单词数:"<<b<<endl<<"行数:"<<c<<endl;
        fin.close();
    return 0;
}

运行截图:

转载于:https://www.cnblogs.com/csl-40/p/11030252.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值