| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 671 人关注过本帖
标题:c语言课程设计第二次查找出现死循环
取消只看楼主 加入收藏
gy_521
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-5-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
c语言课程设计第二次查找出现死循环
001.rar (1.8 MB)
第二次查找替换时候出现了死循环
搜索更多相关主题的帖子: c语言 课程 
2015-06-23 14:17
gy_521
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-5-15
收藏
得分:0 
程序代码:
这是源代码

#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);
}
2015-06-23 14:47
gy_521
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-5-15
收藏
得分:0 
这是文本的内容

The Power of Insistence

Last night, I watched a tennis game, it was the US open, because there was a Chinese female player came to the semi-final, so I stayed up to watched the game. Unluckily, she was injured twice, though she still wanted to finish the game, her body situation did not allow her to do so. Her insistence moved so many audience, they gave her the biggest applause.

The power of insistence is great, it will help you set free your potential and keep move on. Just for the players, they will face all kinds of incidences now and then, but the will to insist will make them finish the game. Sometimes, people win the game not because of their excellent skills, but their strong will. Those who can stick on to the final line will win people’s applause.

What’s more, when people insist to finish the game, it is the respect that they show to their opponents. Their spirit deserves people’s applause. Insistence is a merit, we should keep it, no matter what we do, we must remember to insist.
2015-06-23 14:47
快速回复:c语言课程设计第二次查找出现死循环
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017243 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved