⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.c

📁 string.rar是我们自己班上的一个程序 然后
💻 C
字号:
#include<stdio.h>
#include<string.h>

#define MAXSIZE 256
#define N  30

typedef struct {
       char str[MAXSIZE];
       int length ; 
} string;  

/*文本统计*/
void txttotal(char fname[], int *space, int *word ,int *num)
{ string s;
  FILE  *fp;
  int i, flag;
  *space=0;
  *word=0;
  *num=0;
  fp=fopen(fname,"rb");
  if (fp==NULL)
     printf("\nthe txtfile can't open!\n");
  else
   {  
        while (!feof(fp))
         { strcpy(s.str,'\0');
           fread (&s.str,sizeof(s.str),1,fp);
           s.length=strlen(s.str);
           if ( s.length!=0)
           {
               i=0;/*在C语言中数组下标从0开始计*/
               while (i<=s.length)
                { 
                      if (s.str[i]==' ')
                           { *space=(*space)+1;}
                      if ( 'A'<=s.str[i]&&s.str[i]<='Z'|| 'a'<= s.str[i]&&s.str[i]<='z')
                       {
                               while('A'<=s.str[i]&&s.str[i]<='Z'||'a'<= s.str[i]&&s.str[i]<='z')
                                          i++;
                               *word=(*word)+1;
                               i=i-1;
                       }
                      if ('0'<=s.str[i]&&s.str[i]<='9')
                       { 
                            flag=0;
                            {  
                              while  ('0'<=s.str[i]&&s.str[i]<='9' || s.str[i]=='.')
                              { i++;
                                   if ( s.str[i]=='.')
                                           flag=flag+1;
                                   if (flag==2)
                                           break;
                              }
                              *num=(*num)+1;
                              i=i-1;
							}
                         }
                        i=i+1;
                }
           }
         }
   }
   fclose (fp);
}

/* 建立一个文本文件*/
void createtxtfile( char fname[]  )
{
 string s;
 FILE *fp;
 fp=fopen(fname,"wb");
 if (fp==NULL)
   printf("\nthe txtfile can't create!\n");
 else
 { 
    printf("enter text (end of ");
    printf("\"");
    printf("end");
    printf("\"");
    printf("):\n");
    gets(s.str);
    while (strcmp(s.str, "end")!=0)
    { 
        fwrite(&s.str, sizeof(s.str),1,fp);
        gets(s.str);
    }
  }
fclose (fp);
printf("created the new txtfile!\n");
}


/*数据的输出:
  将用户输入的文件及其文件的单词,数字和空格的数量输出到屏幕上. */
void printtxtfile( char fname[])
{  
   string s;
   FILE  *fp;
   fp=fopen(fname,"rb");
   if (fp==NULL)
       printf("\nthe txtfile can't open!\n");
   else
   {  
        while (!feof(fp))
          { 
            strcpy(s.str,'\0');
            fread (&s.str,sizeof(s.str),1,fp);
            if ( (s.length=strlen(s.str))!=0)
            puts(s.str);
         }
     }
}


/*主函数*/
void main()
{
 char filename[N];
 int select;
 int s=0,w=0,n=0;
 while( 1 )
 {
     printf("1. create txtfile!\n");
     printf("2. total !\n");
     printf("3. print the result !\n");
     printf("4. exit !\n");
     printf("enter your select (1-4):");
     scanf("%d", &select);
     switch(select)
       { 
         case 1:  printf("enter create new file's name:");
                  scanf("%s",filename);
                  getchar();
                  createtxtfile(filename);
                  break;
         case 2:  txttotal(filename, &s,&w,&n);
                  break;
         case 3:  printf("the txt file is :\n");
                  printtxtfile(filename);
                  printf("the result of total is:\n");
                  printf(" the num of space : %d\n",s);
                  printf(" the num of word: %d\n", w);
                  printf(" the num of num : %d\n", n);
                  break;
         case 4:  return ;
         default: printf("your select is error ,retry(1-4):\n");
       }
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -