实验六

验证性实验

基础练习

#include<iostream>
#include<fstream>
using namespace std;
int main(){
    ofstream a("3.txt",ios_base::app);
    if(!"3.txt")
    {
        cout<<"fail to open"<<endl;
        return 1;
    }
    a<<'\n'<<"merge successfully"<<endl;
    a.close();
    return 0;
}

实验三1

#include <iostream>
#include <string>
#include<ctime>
#include<fstream>
#include<stdlib.h>
#include "utils.h"
using namespace std;
typedef struct tongxue{
    int xvhao;
    string xuehao;
    string xingming;
    string banji;
};
int main() {
    string b;
    ifstream a;
    if(!a)
    {
        cout<<"faile to open"<<endl;
        return 1;
    }
    cout<<"输入名单列表文件名";
    cin>>b;
    a.open(b);
    int c,i;
    cout<<"输入随机抽点人数";
    cin>>c;
    cout<<"随机抽点中..."<<endl;
    tongxue shu[83];
    for(i=0;i<83;i++)
        a>>shu[i].xvhao>>shu[i].xuehao>>shu[i].xingming>>shu[i].banji;
    string filename;
    filename = getCurrentDate()+".txt";
    ofstream aa;
    if(!aa)
    {
        cout<<"fail to open"<<endl;
        return 1;
    }
    aa.open(filename);
    srand(time(0));
    for(i=0;i<c;i++){
        int x=rand()%(83)-1;
        cout<<shu[x].xvhao<<" "<<shu[x].xuehao<<" "<<shu[x].xingming<<" "<<shu[x].banji<<endl;
        aa<<shu[x].xvhao<<" "<<shu[x].xuehao<<" "<<shu[x].xingming<<" "<<shu[x].banji<<"\n";
    }
    a.close();
    aa.close();
    return 0;
}
 

没有进行重复清理,然后也只能服务于这一个文本。

实验三2

#include<iostream>
#include<string>
#include<fstream> 
using namespace std;
int main(){
    ifstream fin;
    string wenjian;
    char zi;
    int i=0,j=0,k=0;
    int a,b,c;
    cout<<"输入要统计的英文文本文件名:"; 
    cin>>wenjian;
    fin.open(wenjian);
    if(!fin){
        cout<<"sorry"<<endl;
        return 1;
    }
    string yi[100];
    while(getline(fin,yi[i])){
        if(yi[i]=="\n")
        break;
        else
        i++;
    }
    for(a=0;a<i;a++){
        b=yi[a].length();
        for(c=0;c<b;c++){
            if(yi[a][c]==' ')
            j++;
            else
            k++;
        }
    }
    k+=j;
    j+=i;
    cout<<"字符数:"<<k<<endl;
    cout<<"单词数:"<<j<<endl;
    cout<<"行数:"<<i<<endl;
    return 0;
} 

我是先计算出行数,然后用行数的几个string类型做的。

开始时候没有计算空格在计算字符数的时候。

posted on 2019-06-18 21:12  吵闹机器人  阅读(93)  评论(0编辑  收藏  举报