实验六

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

 

part 3

#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include"utils.h"
using namespace std;
int n,i,j,k;
string filename,filename1;
string s,a[100];
int main()
{
    filename1=getCurrentDate();
    ifstream fin;
    ofstream fout;
    cout<<"输入名单列表文件名:";
    cin>>filename;
    fout.open(filename1);
    fin.open(filename,ios_base::in);
    if(!fin.is_open())
    {
        cout<<"fail to open file"<<filename<<endl;
        system("pause");
        exit(0);
    }
    if(fin)
    {
        cout<<"输入随机抽点人数:";
        cin>>n;
        cout<<"随机抽点中"<<filename1 <<endl;
    }
    
    srand((unsigned)time(0));
    for( i=0;i<83;i++)
    {
        getline(fin,s);
        a[i]=s;
    }
    for(j=0;j<n;j++)
    {
        k=1+rand()%83;
        cout<<a[k]<<endl;
        fout<<a[k]<<endl;
    }
        fin.close();
        fout.close();
        system("pause");
        return 0;
}
#include "utils.h"
#include <ctime>
using std::string;
const int SIZE = 20;
// 函数功能描述:返回当前系统日期
// 参数描述:无参数
// 返回值描述:以string类型返回系统当前日期,格式诸如20190611
string getCurrentDate() {
time_t now = time(0); // 获取当前系统日历时间
struct tm *local_time = localtime(&now); // 把系统日历时间转换为当地时间
char date[SIZE];
strftime(date, SIZE, "%Y%m%d", local_time);
return (string(date));
}
//这个头文件里包含了可用工具函数的声明
#include <string>
using std::string;
// 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();

part 3(2)

#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
    string filename;
    ifstream fin;
    ofstream fout;
    cout<<"输入要打开的文件文本名:";
    cin>>filename;
    fin.open(filename);
    if(!fin.is_open())
    {
        cout<<"fail to open the file"<<endl;
        exit(0);
    }
    string s;
        int i=0,j=0,hang=0;
    while(getline(fin,s))
    {
    
        hang++;
        int x;
        x=s.size();
        i+=x;
        char a[x];
        for(int m=0;m<x;m++)
        {
            a[m]=s[m];
        }
        a[x-1]='\0';
        for(int n=0;n<x;n++)
        {
            if(a[n]==' '&&a[n+1]!=' '||(a[n]=='.'||a[n+1]==',')&&a[n+1]!=a[n])
            j++;
        }
    }
       cout<<"字符数:"<<i<<endl;
        cout<<"单词数:"<<j<<endl;
        cout<<"行数:"<<hang <<endl;
        fin.close();
        system("pause");
        return 0;
}

 

1.基础练习和part(1)的逻辑都挺清楚的,只有part3(1)中随机函数不是很熟练,但是part3(2)一开始感觉挺好写的。但是后来一直有错运行不出来,然后让同学给我分析思路,按照新的思路重新写的。

2.文件的输入输出并不是特别熟练。

3.写程序之前还是需要先想好算法。

转载于:https://www.cnblogs.com/agsjg/p/11042204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值