实验六

#include<iostream>
#include<fstream>
using namespace std;
int main(){
	ofstream fout("3.txt",ios_base::app);
	fout<<"merge successfuly.";
	if(!fout.is_open())
{
	cerr<<"ERROR:Cannot open txt."<<endl;
    exit(0);
	}
	system("pause");
	fout.close();
return  0;
}

  

//utils.h
//这个头文件里包含了可用工具函数的声明
#include <string>
using std::string;
// 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();

//utils.cpp
#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<iostream>
#include<fstream>
#include<istream>
#include<ctime>
#include<string>
#include"utils.h"
#include<cstdlib>
using namespace std;
//struct student
//{
// int id1,id2;
// string name,classroom;
//};
int main ()
{
string file;
string namefile=getCurrentDate();
cout<<"输入名单列表文件名:";
cin>>file;
ifstream fin;
ofstream fout;
fin.open(file);
if(!fin.is_open())
{
cerr<<"Fail to open"<<file<<endl;
exit(0);
}
fout.open(namefile);
if(!fout.is_open())
{
cerr<<"Fail to open"<<namefile<<endl;
exit(0);
}
//从文件file中抽取n个人
int n;
cout<<"输入随机抽点人数:"<<endl;
cin>>n;
//unsigned int a[n];
int id;
string s,b[84];
for(int j=1;j<=83;j++)
{
getline(fin,s);
b[j]=s;
}

srand((unsigned)time(0));
for(int i=0;i<n;i++)
{
id=1+rand()%83;  
cout<<b[id];
cout<<endl;
fout<<b[id];
fout<<endl;
}
fin.close();
fout.close();
 return 0;


}

#include<iostream>
#include<fstream>
#include<cstring>
#include<string>
#include<cstdlib>
using namespace std;
bool fun(char ch)//判断是字符还是数字
{
if(ch>='A'&&ch<+'Z'||ch>='a'&&ch<='z'||ch=='.'||ch==','||ch==' ')
    return true;
else 
    return false;
 } 
 void count(fstream &file,int a[])
 {
  char str[100];
  while(file.getline(str,100))
  {
  int t=0;
  for(int i=0;i<strlen(str);i++)
  {
  if(str[i]==' '||str[i]=='.'&&str[i+1]!='.'||str[i]=='\n')
  {
  
a[1]++; 
 }
      
  if(fun(str[i]))
  {
  a[0]++;
  t++;//用来区分是不是一空行 
 }
 
 }
 if(t!=0)
a[2]++;
 t=0;
 }
 }
int main ()
{
char filename[100];
int a[3]={0};
cout<<"请输入文件名:";
cin.getline (filename,100);
fstream fin(filename,ios::in);
if(!fin.is_open())
{
cerr<<"Fail to open "<<filename<<endl;
exit(0);
}
count(fin,a);
cout<<"字符数:"<<a[0]<<endl;
 cout<<"单词数:"<<a[1]<<endl;
 cout<<"行数:"<<a[2]<<endl;
 fin.close();
 return 0; 
}

 

posted on 2019-06-16 15:56  happy-every-day  阅读(109)  评论(0编辑  收藏  举报