Exe 1

注意函数 write(char[],int n)  //n是要写入的字符的个数

 

#include <iostream>
#include <fstream>
using namespace std;

const char week[7][12]={"Monday","Tuesday","Wednesday","Thirsday","Friday","Saturday","Sunday"};

 

typedef struct
{
 char ch;  //字符
 int num;  //字符个数
}CType;

 

int Record(char s[],int count,CType cnum[])
{
 int i,j,k=0;        //k记录cnum中元素的个数,即是字符的种类
 for(i=0;i<count;i++) //对原始串元素逐个进行检查
 {
  if(k==0)      //cnum中没有元素的时候,将s[i]直接放到cnum中
  {
   cnum[k].ch = s[i];
   cnum[k].num = 1;
   k++;
  }
  else
  {   //cnum中存在元素时,查找是否有相同的字符
   for(j=0;j<k && s[i]!=cnum[j].ch;j++)
    ;     //空操作只是为了逐次检查cnum中已有的每个元素
   if(j>=k)  //s[i]不在cnum中,添加进来
   {
    cnum[k].ch = s[i];
    cnum[k].num = 1;
    k++;
   }
   else    //s[i]在cnum中,增加cnum中该元素的计数
    cnum[j].num++;
  }
 }
 return k;
}

int main()
{
 char str[10000] = {0};
 char str1[10000] = {0};
 CType cnum[128];
 int i=0;
 ifstream infile("originate.txt",ios::in);
 if(!infile)
 {
  cerr<<"原始文件打开错误!"<<endl;
  exit(1);
 }
 while(infile.get(str[i]))  
  i++;
 infile.close();
 cout<<str<<endl<<i<<endl;
 ofstream outfile("new.txt",ios::out);
 if(!outfile)
 {
  cerr<<"生成文件错误!"<<endl;
  exit(1);
 }
 for(int j=0;j<i;j++)
 {
  if(str[j]=='1')
   outfile.write(week[0],6);
  else if(str[j]=='2')
   outfile.write(week[1],7);
  else if(str[j]=='3')
   outfile.write(week[2],9);
  else if(str[j]=='4')
   outfile.write(week[3],8);
  else if(str[j]=='5')
   outfile.write(week[4],6);
  else if(str[j]=='6')
   outfile.write(week[5],8);
  else if(str[j]=='7')
   outfile.write(week[6],6);
  else
   outfile.put(str[j]);
 }
 outfile.close();
 cout<<"替换完毕,见根目录下文件'new.txt'"<<endl;
 cout<<"统计新文件中的非数字字符出现的情况如下:"<<endl;
    ifstream infile1("new.txt",ios::in);
 if(!infile1)
 {
  cerr<<"原始文件打开错误!"<<endl;
  exit(1);
 }
 i = 0;
 while(infile1.get(str1[i]))
  i++;
 ofstream outfile1("static.txt",ios::out);
 if(!outfile1)
 {
  cerr<<"生成文件错误!"<<endl;
  exit(1);
 }
 int k = Record(str1,i,cnum);
 for(j=0;j<k;j++)
 {
  if(cnum[j].ch>=65 &&cnum[j].ch<=90 || cnum[j].ch>=97 &&cnum[j].ch<=122)  //只统计字符串中字母的个数
  {
   cout<<cnum[j].ch<<":"<<cnum[j].num<<endl;
   outfile1<<cnum[j].ch<<":"<<cnum[j].num<<endl;
  }
 }
 return 0;
}

 

 

你可能感兴趣的:(ios,struct,exe)