?? string.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 + -