首页 > 其他 > 详细

实验6

时间:2019-06-12 23:32:49      阅读:189      评论:0      收藏:0      [点我收藏+]

part 1

技术分享图片

3.txt 的图片见part 2。

 

part 2

技术分享图片
#include <iostream>
#include <fstream>   
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    string file;
    ofstream fout;
    cout<<"open the file:";
    cin>>file;
    fout.open(file,ios_base::app);
    if(!fout.is_open())
    {
        cout<<"fail to open"<<endl;
        exit(0);
     } 
     fout<<endl<<"merge successfully"<<endl;
     fout.close();
     return 0;
}
2.main.cpp

技术分享图片

 

part 3

1.

技术分享图片
#include <iostream>
#include <string>
#include "utils.h"
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main() {
    
    string filename;
    
    filename = getCurrentDate();
    
    cout << filename << endl;
    
    string file;
    int n;
    ifstream fin;
    ofstream fout;
    cout<<"输入名单列表的文件名:";
    cin >>file;
    cout<<"输入随机抽取人数:";
    cin>>n; 
    fin.open(file);
    if(!fin.is_open())
    {
        cerr<<"fail to open"<<file<<endl;
        system("pause");
        exit(0);
    }
    fout.open(filename);
    if(!fout.is_open())
    {
        cerr<<"fail to open"<<filename<<endl;
        system("pause");
        exit(0);
    }
    srand(time(0));
    int line=0,ran;
    string t[100],a;
    while (getline(fin,a))
    {
        t[line++]=a;
    }
    while(n--)
    {
        ran=rand()%83;
        fout<<t[ran]<<endl;
        cout<<t[ran]<<endl;
    }
    fin.close();
    fout.close();
    return 0;
}
1.main.cpp

技术分享图片

技术分享图片

 

 2.

技术分享图片
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    string file;
    ifstream fin;
    int num=0,num1=0,line=0;
    cout<<"输入要统计的英文文本文件名:";
    cin>>file;
    fin.open(file);
    if(!fin.is_open())
    {
        cout<<"fail to open"<<endl;
        return 0;
    }
    string a;
    string s[1000];
    int x,i;
    while(getline(fin,a)){
        s[line]=a;
        line++;
        x=a.length();
        for(i=0;i<x;i++){
            if(a[i]== )
            num1++;
        }
        num1++;
    }
    for(i=0;i<line;i++)
    {
        x=s[i].length();
        num+=x;
    }
     fin.close();
     cout<<"字符数:"<<num<<endl;
     cout<<"单词数:"<<num1<<endl; 
     cout<<"行数:"<<line<<endl; 
     return 0; 
}
2.main.cpp

技术分享图片

 

summary:这次的实验主要是对输出流和输入流的实际运用,以及对不同函数,如getline(),get(),rand()等。记得对输入(出)流open时的检验,需要特殊的要求时可以添加ios::(...),同时注意最后的close()。

实验6

原文:https://www.cnblogs.com/suifeng823/p/11013401.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!