c语言课程设计第二次查找出现死循环
001.rar
(1.8 MB)
第二次查找替换时候出现了死循环
这是源代码 #include <io.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> BOOL SetConsoleColor(WORD wAttributes) //获取console窗口句柄 { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsole == INVALID_HANDLE_VALUE) return FALSE; return SetConsoleTextAttribute(hConsole, wAttributes); } int count=0,C[20]={0},len=0;; int A[100]={0},B[50]={0}; char string[10]; void printFile(FILE *fp); void print(FILE *fp,int len,int shift); void write_Section(FILE *fp,FILE *fp1,int len,int shift); void write_All(FILE *fp,FILE *fp1); void print_Change(char *string); void menu(); void view(); void statistic(); void find(int *len); void change(); int main() { menu(); return 0; } void menu()//菜单 { int a=211,i,j,s; system("color 0A"); s=(a-1)/2; for(i=0;i<a;i++){ for(j=0;j<abs(s-i);j++){ printf(" "); } for(j=0;j<abs(a-2*abs(s-i));j++){ printf("*"); } putchar('\n'); } system("cls"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("\n\n\n # WELCOME TO THE SYSTEM #\n"); printf(" ------------------------------------------------------\n\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf(" # 0 # --> ");SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);printf("View the file\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf(" # 1 # --> ");SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);printf("Count characters\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf(" # 2 # --> ");SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);printf("Find & change some characters\n\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf(" ******************************************************\n"); printf(" any other keys to exit\n\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); scanf("%d",&a); if(a<0 || a>2){exit(0);} system("cls"); switch(a){ case 0:view();menu(); case 1:statistic();menu(); case 2:find(&len);change();menu(); default:exit(0); } } void view()//预览原有文件 { FILE *fp; if((fp=fopen("data.txt","r"))==NULL){ printf("Cannot open the file.\n"); exit(1); } printFile(fp); fclose(fp); } void statistic()//统计 { FILE *fp; char ch; int a[26]={0},i,k=0; if((fp=fopen("data.txt","r"))==NULL){ printf("Cannot open the file.\n"); exit(1); } SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("原文为:\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); while(!feof(fp)){ ch=fgetc(fp); printf("%c",ch); if(ch>='A'&&ch<='Z'){ i=ch-65; a[i]=a[i]+1; k++; } if(ch>='a'&&ch<='z'){ i=ch-97; a[i]=a[i]+1; k++; } } SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("\n\n一共有%d个字母.\n",k); printf("\n字母\t出现次数\n"); for(i=0;i<26;i++) printf("%c(%c)\t%d\n",65+i,97+i,a[i]); fclose(fp); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); } void find(int *len)//查找字符串 { FILE *fp; char ch; int i=0,n=0,cor=0; if((fp=fopen("data.txt","r"))==NULL){ SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Cannot open the file.\n"); exit(1); } SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("Input what do you want to find:\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); scanf("%s",string); *len=(int)strlen(string); while(!feof(fp)){ for(i=0;i<*len;i++){ ch=fgetc(fp);n++; if(ch=='\n'){cor++;} if(ch!=string[i]){break;} } if(i==*len){A[count]=n+cor-*len;count++;} } if(count!=0){printf("%d matched.\n",count);} else{SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf("No match find.\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);exit(1);} rewind(fp); putchar(10); for(i=0;i<count;i++){ print(fp,*len,A[i]); print_Change(string); } printFile(fp); putchar(10); fclose(fp); } void change()//字符串替换 { FILE *fp,*fp1; char string[10]; int i=0,n=0; SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("which do you want to change(to %d):\n",count); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); do{ scanf("%d",&C[i++]); }while(getchar()!='\n');n=i; if(C[0]==0){SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf("Nothing will be changed.\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);exit(1);} SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("What do you want change it into:\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); scanf("%s",string); if((fp1=fopen("data.txt","r"))==NULL){ SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Cannot open the source file.\n"); exit(0); } if((fp=fopen("data0.txt","w"))==NULL){ SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Cannot write in the file.\n"); exit(0); } for(i=0;i<n;i++){ write_Section(fp,fp1,len,A[C[i]-1]); fputs(string,fp); } write_All(fp,fp1); fclose(fp); fclose(fp1); if(!access("data.bak",0)){remove("data.bak");} rename("data.txt","data.bak"); rename("data0.txt","data.txt"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Change & Save successfully\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); } void printFile(FILE *fp)//输出全文 { char ch; ch=fgetc(fp); while(!feof(fp)){putchar(ch);ch=fgetc(fp);} } void print(FILE *fp,int len,int shift)//输出文章到shift位置 { while(ftell(fp)!=shift){putchar(fgetc(fp));} fseek(fp,len,SEEK_CUR); } void write_Section(FILE *fp,FILE *fp1,int len,int shift)//写入文件到shift位置 { while(ftell(fp1)!=shift){ fputc(fgetc(fp1),fp); } fseek(fp1,len,SEEK_CUR); } void write_All(FILE *fp,FILE *fp1)//写入到末尾 { char ch; ch=fgetc(fp1); while(!feof(fp1)){ fputc(ch,fp); ch=fgetc(fp1); } } void print_Change(char *string)//输出查找到的字符 { SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("%s",string); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); }
#include <io.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <conio.h> BOOL SetConsoleColor(WORD wAttributes) //获取console窗口句柄 { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsole == INVALID_HANDLE_VALUE) return FALSE; return SetConsoleTextAttribute(hConsole, wAttributes); } int count=0,C[20]={0},len=0;; int A[100]={0},B[50]={0}; char string[10]; void printFile(FILE *fp); void print(FILE *fp,int len,int shift); void write_Section(FILE *fp,FILE *fp1,int len,int shift); void write_All(FILE *fp,FILE *fp1); void print_Change(char *string); void menu(); void view(); void statistic(); void find(int *len); void change(); int main() { menu(); return 0; } void menu()//菜单 { int a=211,i,j,s; system("color 0A"); s=(a-1)/2; for(i=0;i<a;i++){ for(j=0;j<abs(s-i);j++){ printf(" "); } for(j=0;j<abs(a-2*abs(s-i));j++){ printf("*"); } putchar('\n'); } system("cls"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("\n\n\n # WELCOME TO THE SYSTEM #\n"); printf(" ------------------------------------------------------\n\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf(" # 0 # --> ");SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);printf("View the file\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf(" # 1 # --> ");SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);printf("Count characters\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf(" # 2 # --> ");SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);printf("Find & change some characters\n\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf(" ******************************************************\n"); printf(" any other keys to exit\n\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); scanf("%d",&a); if(a<0 || a>2){exit(0);} system("cls"); switch(a){ case 0:view();menu();break; case 1:statistic();menu();break; case 2:find(&len);change();menu();break; default:exit(0); } } void view()//预览原有文件 { FILE *fp; if((fp=fopen("data.txt","r"))==NULL){ printf("Cannot open the file.\n"); exit(1); } printFile(fp); fclose(fp); } void statistic()//统计 { FILE *fp; char ch; int a[26]={0},i,k=0; if((fp=fopen("data.txt","r"))==NULL){ printf("Cannot open the file.\n"); exit(1); } SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("原文为:\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); while(!feof(fp)){ ch=fgetc(fp); printf("%c",ch); if(ch>='A'&&ch<='Z'){ i=ch-65; a[i]=a[i]+1; k++; } if(ch>='a'&&ch<='z'){ i=ch-97; a[i]=a[i]+1; k++; } } SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("\n\n一共有%d个字母.\n",k); printf("\n字母\t出现次数\n"); for(i=0;i<26;i++) printf("%c(%c)\t%d\n",65+i,97+i,a[i]); fclose(fp); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); getch(); } void find(int *len)//查找字符串 { FILE *fp; char ch; int i=0,n=0,cor=0; if((fp=fopen("data.txt","r"))==NULL){ SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Cannot open the file.\n"); exit(1); } SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("Input what do you want to find:\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); scanf("%s",string); *len=(int)strlen(string); while(!feof(fp)){ for(i=0;i<*len;i++){ ch=fgetc(fp);n++; if(ch=='\n'){cor++;} if(ch!=string[i]){break;} } if(i==*len){A[count]=n+cor-*len;count++;} } if(count!=0){printf("%d matched.\n",count);} else{SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf("No match find.\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);exit(1);} rewind(fp); putchar(10); for(i=0;i<count;i++){ print(fp,*len,A[i]); print_Change(string); } printFile(fp); putchar(10); fclose(fp); } void change()//字符串替换 { FILE *fp,*fp1; char string[10]; int i=0,n=0; SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("which do you want to change(to %d):\n",count); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); do{ scanf("%d",&C[i++]); }while(getchar()!='\n');n=i; if(C[0]==0){SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);printf("Nothing will be changed.\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);exit(1);} SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("What do you want change it into:\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); scanf("%s",string); if((fp1=fopen("data.txt","r"))==NULL){ SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Cannot open the source file.\n"); exit(0); } if((fp=fopen("data0.txt","w"))==NULL){ SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Cannot write in the file.\n"); exit(0); } for(i=0;i<n;i++){ write_Section(fp,fp1,len,A[C[i]-1]); fputs(string,fp); } write_All(fp,fp1); fclose(fp); fclose(fp1); if(!access("data.bak",0)){remove("data.bak");} rename("data.txt","data.bak"); rename("data0.txt","data.txt"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); printf("Change & Save successfully\n"); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); getch(); } void printFile(FILE *fp)//输出全文 { char ch; ch=fgetc(fp); while(!feof(fp)){putchar(ch);ch=fgetc(fp);} getch(); } void print(FILE *fp,int len,int shift)//输出文章到shift位置 { while(ftell(fp)!=shift){putchar(fgetc(fp));} fseek(fp,len,SEEK_CUR); } void write_Section(FILE *fp,FILE *fp1,int len,int shift)//写入文件到shift位置 { while(ftell(fp1)!=shift){ fputc(fgetc(fp1),fp); } fseek(fp1,len,SEEK_CUR); } void write_All(FILE *fp,FILE *fp1)//写入到末尾 { char ch; ch=fgetc(fp1); while(!feof(fp1)){ fputc(ch,fp); ch=fgetc(fp1); } } void print_Change(char *string)//输出查找到的字符 { SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("%s",string); SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); }