| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1012 人关注过本帖, 1 人收藏
标题:飞机订票系统 一直死循环,求大神解决
只看楼主 加入收藏
xxyuan
Rank: 1
等 级:新手上路
帖 子:9
专家分:3
注 册:2012-9-14
结帖率:50%
收藏(1)
已结贴  问题点数:20 回复次数:11 
飞机订票系统 一直死循环,求大神解决
#include<stdio.h>
#include<string.h>   
#include<stdlib.h>     
#define N 1000  
struct air         
{
    int num;
    char start[20];
    char over[20];
    char time[10];
    int count;
}s[N];
int i;
int m=0;/*全局变量,计算输入数据数*/
#define PRINT "%d%12s%12s%10s%12d\n",s[i].num,s[i].start,s[i].over,s[i].time,s[i].count

void input();    /*输入信息*/
void print();    /*输出信息*/
void save();     /*保存信息*/
void read();     /*读取信息*/
void search();   /*查找信息*/
void delete();  /*删除信息*/
void buy();    /*订票信息*/
void refund();  /*退票信息*/
void modify();   /*修改信息*/

void main()
{
 
    int j;
   
        printf("                           ★ ***  reserve ticket system ***  ★");
        printf("           

****************************************************************************");
        printf("                     1.Enter plane information      2.Scan plane information    \n  

");
                             
        printf("                     3.Modify plane information     4.Search plane information  \n  

");
                              
        printf("                     5.Delete plane information     6.Buy ticket                \n  

");
                                    
        printf("                     7.Refund ticket            0.Exit                      \n  ");
        printf("  ****************************************************************************\n");

        printf("Please input number 0-7and press ENTER.\n");
        do{
        scanf("%d",&j);

 }while(j<0||j>7);
        switch(j)
       {
            case 1:input();   break;
            case 2:print();   break;
            case 3:modify();  break;
            case 4:search();  break;
            case 5:delete();  break;
            case 6:buy();     break;
            case 7:refund();  break;
            case 0:;           break;
        }
   
printf("Thank you,goodbye!");
}

void input()
{
    int y;
    printf("                  Please input plane informations:\n");
    printf("*****************************************************************************\n");
    for(i=0;i<N;i++)
    {
        printf("Please input the plane number:\n");
        scanf("%d",&s[i].num);
        printf("Please input the upcity:\n");
        scanf("%s",&s[i].start);
        printf("Please input the downcity:\n");
        scanf("%s",&s[i].over);
        printf("Please input the uptime:\n");
        scanf("%s",&s[i].time);
        printf("How many ticket you want:\n");
        scanf("%d",&s[i].count);
        m++;/*统计输入信息个数*/
  
        printf("the %dth information have completed,do you want to continue input the next

information?\n",m);
        printf("YES.....please press 1 and ENTER.\n");
        printf("NO......please press 0 and ENTER.\n");
        scanf("%d",&y);
        if(y==0)
        {
        save();
        print();
        break;
        }
    }
}

void save()
{   
    FILE *fp,*fp1;
    if((fp=fopen("C:\\sun.txt","wb"))==NULL)/*打开文件并判断是否出错*/
    {
    printf("Set up file fail!\n");
    return;
    }
    if((fp1=fopen("C:\\ru.txt","wb"))==NULL)
    {
    printf("Set up file fail,please press any key quit.\n");
    getchar();
    return;
    }
    for(i=0;i<m;i++)
    if(fwrite(&s[i],sizeof(struct air),1,fp)==0)/*向文件写入数据,并判断是否出错*/
    printf("WRONG!\n");
    fprintf(fp1,"%d",m);
    fclose(fp);
    fclose(fp1);
}


void read()
{
    FILE *fp,*fp1;
    if((fp=fopen("C:\\sun.txt","rb"))==NULL)
    {
    printf("Wrong,please check up the file fp,press any key return.\n");
    getchar();
    }
    if((fp1=fopen("C:\\ru.txt","rb"))==NULL)
    {
    printf("Set up file fail!\n");
    getchar();
    return;
    }
    fscanf(fp1,"%d",&m);
    fclose(fp1);
    for(i=0;i<m;i++)
    {
    fread(&s[i],sizeof(struct air),1,fp);/*从文件中读取信息*/
    }
    fclose(fp);
}
void print()
{
    char w[10];
    read();
    printf("Plane_number\t\tupcity\t\tdowncity\t\ttime\t\tthe_number_of_ticket\n");
    for(i=0;i<m;i++)
    {
        printf("PRINT\n");
    }
    printf("Please press any key+ENTER to return last menu:\n");
    scanf("%s",&w);
}
   
void search()
{
    char name1[20];
    char name2[20];
    char ii[10];
    int n,no;
    do
    {
        printf("Please choice the way to search:\n");/*打印查询方式菜单*/
        printf("1......by plane number\n");
        printf("2......by downcity\n");
        printf("3......by upcity and downcity\n");
        printf("0......return\n");
        printf("input number 0-3:\n");
        printf("press else key to return main menu:\n");
        scanf("%d",&n);
        if(n==0)
            break;
        switch(n)
        {
            case 1:
                printf("Please input plane number:\n");
                scanf("%d",&no);
                break;
            case 2:
                printf("Please input downcity:\n");
                printf("%s",&name2);
                break;
            case 3:
                printf("Please input upcity:\n");
                scanf("%s",&name1);
                printf("Please input downcity:\n");
                scanf("%s",&name2);
                break;
        }
        read();
        for(i=0;i<m;i++)
        {
            if(strcmp(s[i].over,name1)==0||strcmp(s[i].over,name2)==0)
            {
                printf("Search plane informotion accomplish.");
                printf("plane_numeber\t\tupcity\t\tdowncity\t\ttime\t\tthe_number_of_ticket\n");
                printf("PRINT\n");
                break;
            }
            if(s[i].num==no)/* 按航班号判断输出条件 */
            {
            printf("Search plane informotion accomplish.");
            printf("plane_numeber\t\tupcity\t\tdowncity\t\ttime\t\tthe_number_of_ticket\n");
            printf("PRINT\n");
            break;
            }
        }
        no=0;/*将航班号赋值为0*/
        printf("Search finish:");
        printf("Do you want to continue?Please input yes or no + ENTER.");
        scanf("%s",&ii);
    }while(strcmp(ii,"yes")==0);
}

void delete()
{
    char name1[20];
    char name2[20];
    char ii[10];
    int no,n;

    do
    {
        printf("Please choice the way of delete:");
        printf("1......by plane number");
        printf("2......by upcity and downcity");
        printf("0......return");
        printf("please input number 0-2 + ENTER:");
        scanf("%d",&n);
        if(n==0)
            break;   
        switch(n)
        {
            case 1:
                printf("Please input plane number:");
                scanf("%d",&no);
                read();
                break;
            case 2:
                printf("Please input upcity:");
                scanf("%s",&name1);
                printf("Please input downcity:");
                scanf("%s",&name2);
                read();
                break;
        }   
        for(i=0;i<m;i++)
        {
            if(s[i].num==no||strcmp(s[i].start,name1)==0&&strcmp(s[i].over,name2)==0)/*判断输入信息是

否存在 */
            {      
                s[i]=s[m-1];
                m--;
            }
        }
        printf("search finish.");
        printf("Delete the plane information,are you sure?");
        printf("Please input yes or no + ENTER.");
        scanf("%s",&ii);
        save();
        if(!strcmp(ii,"yes"))     
        printf("Please press any key to return the last menu.");
        break;
   }while(n==1||n==2||n==0);
 
}
 

 

void buy()
{
    int n;
    char a[10];
    do
    {
        search();
        printf("How many tickets you want?Please input.");
        scanf("%D",&n);
        if(n<0)
        {
        printf("Please input number again.");
        break;
        }
        if(s[i].count!=0&&s[i].count>=n)/* 判断是否出错 */
        {
            s[i].count=s[i].count-n;
            save();
            printf("Buy ticket succeed.");
            break;
        }
        if(s[i].count<n)/* 判断是否出错 */
        {
            printf("Please a useful number you want ticket.");
            break;
        }
        printf("Are you continue? Please input yes or no +ENTER");
        scanf("%s",&a);
    }while(!strcmp(a,"yes"));
}

void refund()
{
    int n;
    char a[10];
    do
    {
        search();
        printf("How many tickets you want to refund?");
        scanf("%d",&n);
        if(n<0)
            printf("Please input a useful number again:");
        scanf("%d",&n);
        s[i].count=s[i].count+n;
        save();
        printf("Refund succeed.");
        printf("Are you continue?Please input yes or no + ENTER.");
        scanf("%s",&a);
    }while(!strcmp(a,"yes"));
    getchar();
}

void modify()
{
    struct modify
    {  
    int no;
    char name1[20];
    char name2[20];
    char time[20];
    int count;
    }mo[1];
    char j[10];
    int n;
    read();
    do
    {
        printf("Please choice the way of modify.\n");
        printf("1.......by plane number");
        printf("2.......by upcity and downcity");
        printf("Please input 1 or 2 + ENTER.\n");
        scanf("%d",&n);
        switch(n)
        {
            case 1:
                    printf("Please input plane number:\n");
                    scanf("%s",&mo[0].no);
                    break;
            case 2:
                    printf("Please input upcity:\n");
                    scanf("%s",&mo[0].name1);
                    printf("Please input downcity:\n");
                    scanf("%s",&mo[0].name2);
                    break;
        }
        for(i=0;i<m;i++)
        {   
            if(strcmp(s[i].over,mo[0].name1)==0&&strcmp(s[i].over,mo[0].name2)==0)/* 判断输出条件 */
            {
                    printf("plane_numeber\t\tupcity\t\tdowncity\t\ttime\t\tthe_number_of_ticket\n");
                    printf("PRINT");
                    break;
            }
            if(s[i].num==mo[0].no)/* 判断输出条件 */
            {  
                    printf("plane_numeber\t\tupcity\t\tdowncity\t\ttime\t\tthe_number_of_ticket\n");
                    printf("PRINT");
                    break;
            }
        }
        mo[0].no=0;            /* 将结构体中的号为零 */
        printf("Please input new plane_number,upcity,downcity,time and the number of_ticket:\n");
        scanf("%s,%s,%s,%s,%s",&mo[0].no,&mo[0].name1,&mo[0].name2,&mo[0].time,&mo[0].count);
        s[i].num=mo[0].no;/* 替换航班号 */
        strcpy(s[i].start,mo[0].name1);/* 替换其始站 */     /*拷贝字符串*/
        strcpy(s[i].over,mo[0].name2);/* 替换终点站 */
        strcpy(s[i].time,mo[0].time);/* 替换时间 */
        s[i].count=mo[0].count;/* 替换机票数 */
        save();
        printf("Modify succeed!Do you want to continue?Please press yes or no + ENTER.\n");
        scanf("%s",&j);
    }while(strcmp(j,"yes")==0);  
 
}



一直死循环   求大神帮忙解决
搜索更多相关主题的帖子: count search include void 信息 
2012-09-14 15:52
xxyuan
Rank: 1
等 级:新手上路
帖 子:9
专家分:3
注 册:2012-9-14
收藏
得分:0 
咋没人来帮忙看下啊
2012-09-14 16:31
xxyuan
Rank: 1
等 级:新手上路
帖 子:9
专家分:3
注 册:2012-9-14
收藏
得分:0 
自己顶
2012-09-14 16:31
张朝成
Rank: 4
等 级:业余侠客
帖 子:149
专家分:265
注 册:2012-3-22
收藏
得分:2 
帮你顶下。
2012-09-15 14:34
xtjopt
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:89
专家分:168
注 册:2012-9-12
收藏
得分:2 
额 偶是新手,死循环这种错误还是自己找的好
2012-09-16 16:43
雪狼MJ
Rank: 8Rank: 8
来 自:甘肃
等 级:蝙蝠侠
威 望:4
帖 子:267
专家分:853
注 册:2012-5-27
收藏
得分:2 
呵呵,这种程序最简单了,出了问题就找是哪个模块出了问题,肯定是某个函数出错了!你把出错的地方说一下,然后按照功能在哪个函数里实现的,找到出错的函数,然后去改正,这不很容易吗?你啥也不说,直接把这么长的代码贴上来,这不吓人嘛!怪不得浏览很多,回复很少。。。。。

Edsger Dijkstra:算法+数据结构=程序
2012-09-16 16:52
zhu224039
Rank: 8Rank: 8
等 级:贵宾
威 望:17
帖 子:862
专家分:792
注 册:2012-7-29
收藏
得分:2 
俺看了这 实在上打不起精神  看完

我要成为嘿嘿的黑客,替天行道
2012-09-16 20:31
wzh2005022
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2012-9-16
收藏
得分:2 
表示压力很大,   不懂,  太深了
2012-09-17 10:09
freecast
Rank: 2
等 级:论坛游民
帖 子:12
专家分:10
注 册:2012-7-29
收藏
得分:2 
哎呀,我去。你这个do--while语句好给力哦。不会死循环才怪。你输入的合法字符(也就是0到7之间的字符),它会一直在do--while语句里面死循环,switch语句也没有一个默认操作。仔细看看下面的代码:
printf("Please input number 0-7and press ENTER.\n");
 do{
    scanf("%d",&j);
}while(j<0||j>7);
switch(j)
{
      case 1:input();   break;
      case 2:print();   break;
      case 3:modify();  break;
      case 4:search();  break;
      case 5:delete();  break;
      case 6:buy();     break;
      case 7:refund();  break;
      case 0:;           break;
}
个人觉得把switch语句放在do--while语句里面。switch语句最后一句改为default:或者在case 0:后面添加default
2012-09-17 10:15
huafeizhijia
Rank: 2
等 级:论坛游民
帖 子:13
专家分:34
注 册:2012-9-17
收藏
得分:2 

huafeizhijia  。com话费之家
2012-09-17 14:24
快速回复:飞机订票系统 一直死循环,求大神解决
数据加载中...
 
   



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

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