| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 374 人关注过本帖
标题:求教下列代码错误所在,自己实在是看不出错误在哪里了
只看楼主 加入收藏
xking717
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-11-7
结帖率:66.67%
收藏
已结贴  问题点数:15 回复次数:3 
求教下列代码错误所在,自己实在是看不出错误在哪里了
程序代码:
#include<stdio.h>
#include<direct.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#define LEN_train sizeof(struct train)
#define LEN_char sizeof(char)
#define TRUE 1
#define FALSE 0
#define LEN 100000;


typedef struct  //这里使用typedef把train定义为struct的同义词以便下面的函数中可直接使用train来定义一个我们需要的在特定情况下的结构体数据.
{
    int type;
    char ID[6];
    int price;
    char date[11];
    char s_station[20];
    char t_station[20];
    int num_passenger;
    int MAX_passenger;
    char b_seat[91];
}train;

 train t_data[LEN];

 int t_num=0;  //t_num用来记录t_data structure的总数量,同时方便在后面检索数据库以寻找需要的具体的一个t_data
typedef struct 
{
    char name[20];
    char ID[6];
    char payment[5];
    char re_date[5];
    char seat[4];
    char date[11];
    int num_passenger;
    int MAX_passenger;
    int price;
    int k; //此处k代表顾客购买票的数量
    char s_station[20];
    char t_station[20];
}passenger;

 passenger p_data[LEN];

 int p_num=0;//p_num用处同t_num
 void PUT_t(int i)
    {
        printf("train ID:%s\n",t_data[i].ID);
        printf("Start station:%20s\n",t_data[i].s_station);
        printf("Terminal station:%s\n" ,t_data[i].t_station);
        printf("Departure date:%s\n",t_data[i].date);
        printf("Capacity:%d",t_data[i].MAX_passenger);
        printf("Num of reserved passenger:%d\n",t_data[i].num_passenger);
        printf("Price(per ticket):%d\n",t_data[i].price);
           
     }
void PUT_p(int j)
    {
        ("train ID:%s\n"
                  "Start station:%s\n"
                  "Terminal station:%s\n"
                  "Departure date:%s\n"
                  "Name:%s\n"
                  "Reservation date:%s\n"
                  "Price(per ticket):%d\n",
                  p_data[j].ID,p_data[j].s_station,p_data[j].t_station,p_data[j].date,p_data[j].name,p_data[j].re_date,p_data[j].price);
}


 int readf_t()
{
    FILE *fpt;
    if ((fpt=fopen("data_train.txt","a+"))==NULL)
    {
        printf("\nFailed to load the database,tap to continue");
        getchar();
        exit(1);
    }
    fscanf(fpt, "%d", &t_num);
    fread(t_data, sizeof(passenger),t_num,fpt);
    fclose(fpt);
    return 0;
}

 int savef_t()

 {
     FILE *fpt;
     if((fpt=fopen("data_train.txt","wb"))==NULL)
     {
         puts("\nFailed to load the database,tap to continue");
         getchar();
         exit(1);
     }
     fprintf(fpt,"%d",t_num);
     if(fwrite(t_data,sizeof(train),t_num,fpt)==0)
     {
         puts("\nFailed to record data in the file!");
         exit(1);
     }
     puts("\nData successfully recorded...");
     fclose(fpt);
     return 0;

 }

 

 int readf_p()
{
    FILE *fpp;
    if ((fpp = fopen("data_passenger.txt", "a+")) == NULL)
    {
        printf("\nFailed to load the database,tap to continue");
        getchar();
        exit(1);
    }
    fscanf(fpp, "%d", &p_num);
    fread(p_data, sizeof(passenger),p_num,fpp);
    fclose(fpp);
    return 0;
}


 int savef_p()

 {
     FILE *fpp;
     if((fpp=fopen("data_passenger.txt","wb"))==NULL)
     {
         puts("\nFailed to load the database,tap to continue");
         getchar();
         exit(1);
     }
     fprintf(fpp,"%d",p_num);
     if(fwrite(p_data,sizeof(passenger),p_num,fpp)==0)
     {
         puts("\nFailed to record data in the file!");
         exit(1);
     }
     puts("\nData successfully recorded...");
     fclose(fpp);
     return 0;

 }

short strequal(char *str1, char* str2)
{
    int i=0;
    if (strlen(str1)!=strlen(str2))
        return 0;
    for (i;*(str1+i)!='\0';i++)
    {
        if (*(str1+i)!=*(str2+i))
            return 0;
    }
    return 1;
}
void create_train(void)
{  
    int i, j;
    char choice;
    train create_t_data;
    for (i=1;i<LEN;i++)
    {
        memset(create_t_data.b_seat,'0',91);
        printf("************************Create Train No.%d************************\n",i);
A:        printf("Please enter the TYPE of the new train(1 or 2):\n");
        fflush(stdin);
        scanf("%d",create_t_data.type);
        if (create_t_data.type=='1')
        {
            create_t_data.MAX_passenger=40;
        }
        else
            if (create_t_data.type=='2')
            {
            create_t_data.MAX_passenger=90;
            }
            else
                printf("please enter a valid number!");
                goto A;
        printf("Please enter the ID:\n");
        fflush(stdin);
        scanf("%s",create_t_data.ID);
        printf("Please enter the TICKET PRICE:\n");
        fflush(stdin);
        scanf("%d",create_t_data.price);
        printf("Please enter the DEPARTURE DATE (Year-Month-Date):\n");
        fflush(stdin);
        scanf("%s",create_t_data.date);
        printf("Please enter the STARTING STATION:\n");
        fflush(stdin);
        scanf("%s",create_t_data.s_station);
        printf("Please enter the TERMINAL STATION:\n");
        fflush(stdin);
        scanf("%d",create_t_data.t_station);
        printf("Caution:Are you sure you want to save the data? Any key(Yes) N(No) ");
        fflush(stdin);
        scanf("%s",&choice);
        if (choice == 'N' || choice == 'n')
        {
            printf("\nData not saved...");
         }
        else
        {
          for (i=0;i<t_num;i++)
                if (t_data[i].ID==create_t_data.ID)
                {
                    printf("\n%dhas been used,please change another number...\n", create_t_data.ID);
                    puts("\nData not saved...");
                    PUT_t(i);
                    break;
                }
            if (i==t_num)
            {
                t_data[t_num]=create_t_data;
                t_num++;
                PUT_t(i);
                savef_t();
            }
        }
        printf("Continue to create new trains? Any key(Yes)     N(no) ");
        fflush(stdin);
        scanf("%c", &choice);
        if (choice == 'N' || choice == 'n')
        {
          printf("\nTap to return to previous menu... ");
          fflush(stdin);
          getchar();
        }
    }
}
void edit_line(void)
{
    train e_data;
    char choice;
    int i,j;
    int e_train,T=0,L=0;//L用来判断是否新ID已被使用,T用来判断是否存在寻找的ID
    do
    {
      puts("\n1.Search the train ID\n"
             "2.Return to previous menu");
      printf("\nPlease choose 1 or 2 to continue:");
    fflush(stdin);
    scanf("%c",&choice);
    if (choice=='1')
    {
    printf("Please enter the train ID:\n");
    fflush(stdin);
    scanf("%d",&e_train);
    for (i=0;i<t_num;i++)
    {
        if (t_data[i].ID==e_data.ID)
        T=1;
        PUT_t(i);
A:  printf("Please enter a new train ID:");
    fflush(stdin);
    scanf("%d",&e_data.ID);
    for(i=0,L=0;i<t_num;i++)
    if (t_data[i].ID==e_data.ID)
    {
        printf("\n%d has been used,please change another ID...\n");
        printf("Data not saved...");
       
        PUT_t(i);
        L=1;
        goto A;
    }
    else
    if (L==0);
    {
        printf("Please enter a start station:");
        fflush(stdin);
        scanf("%s",e_data.s_station);
        printf("Please enter a terminal station:");
        fflush(stdin);
        scanf("%s",e_data.t_station);
        printf("Please enter a departure date(Year/Month/Date):");
        fflush(stdin);
        scanf("%s",e_data.date);
        printf("Please enter a type number (1 or 2) for the new train:");
        fflush(stdin);
        scanf("%d",&e_data.type);
        if (e_data.type=='1')
            e_data.MAX_passenger=40;
        else
        if (e_data.type=='2')
            e_data.num_passenger=90;
        else
            printf("please enter a valid number(1 or 2)");
        printf("Are you sure you want to save the data of the new train? Y(yes) Any key(no) ");
       fflush(stdin);
       scanf("%c", &choice);
       if (choice == 'Y' || choice == 'y')
         {
             e_data.num_passenger=t_data[i].num_passenger;
             t_data[i]=e_data;
             savef_t();
          puts("\n Information for the new train:");
          PUT_t(i);
       }
       else
       {
        puts("\nData not saved,failed to edit...");
       }
       break;
      }
     }
    }
    if (T==0)
    {
        puts("\n sorry,no information for this ID in database...");
    }
    if (choice=='2');
    {
        printf("\ntap to return to previous menu...");
        fflush(stdin);
        getchar();
        break;
    }

        printf("\nContinue to edit data? Any key(Yes) N(no):");
        fflush(stdin);
        scanf("%c",&choice);
        if (choice == 'n' || choice == 'N')
        {
            printf("\ntap to return to previous menu... ");
            fflush(stdin);
            getchar();
            break;
        }
    }
    while(1);
}




void search()
{
    char choice;
    int i, flag=0;//flag用来判断是否有查询航班的信息,若执行查询过程中无障碍,则其值增加,否则保持为0
    train s_data;
    do
    {
        puts("\n1.Search by ID\n"
               "2.Search by Start&Teriminal stations\n"
               "3.Search by departure date\n"
               "4.Back to previous menu\n");
        printf("Choose one option and continue with enter:");
        fflush(stdin);
        scanf("%c",&choice);
        fflush(stdin);
        switch(choice)
        {
        case '1':printf("Please enter an ID:");
                 fflush(stdin);
                 scanf("%d",&s_data.ID);
                 for(i=0;i<t_num;i++)
                     if(t_data[i].ID==s_data.ID)
                     {
                       PUT_t(i);
                       flag++;
                     } break;
        case'2':printf("Please enter the name of start station:");
                fflush(stdin);
                scanf("%s",s_data.s_station);
                printf("Please enter the name of terminal station:");
                fflush(stdin);
                scanf("%s",s_data.t_station);
                for (i=0;i<t_num;i++)
                    if((strcmp(t_data[i].s_station,s_data.s_station)==0)&&
                        (strcmp(t_data[i].t_station,s_data.t_station)==0))  //strcmp用来判断两个str的关系,若返回值为0,则两者相等
                    {
                        PUT_t(i);
                        flag++;
                    } break;
        case '3':printf("Please enter the departure date(Year/Month/Date");
                 fflush(stdin);
                 scanf("%s",s_data.date);
                 for (i=0;i<t_num;i++)
                     if(strcmp(t_data[i].date,s_data.date)==0)
                     {
                         PUT_t(i);
                         flag++;
                     } break;
        case '4':return;
        default: printf("\nError!Please enter an integer(1-4):");

        continue;
        }
        if(flag==0)
        {
         puts("\nSorry,no required information in database...");
        }
        printf("\nContinue to search? Any key(Yes) N(no):");
        fflush(stdin);
        scanf("%c",&choice);
    }
    while((choice!='n')&&(choice!='N'));
}

void reserve()
{
    int i,j,k,m,n,L=0; //i,j用来表示t_data[i],p_data[j]某个特定的结构体数据,k代表购票的数量,L用来判断用户是否为第一次买票
    passenger re_data;
    char choice;
    do
    {
        L=0;
        printf("\nPlease enter the train ID you need:");
        fflush(stdin);
        scanf("%d",&re_data.ID);
        for(i=0;i<t_num;i++)
            if(t_data[i].ID==re_data.ID)
            {
             m=i;
             printf("\nPlease enter your name:");
             fflush(stdin);
             scanf("%s",&re_data.name);
             printf("\nPlease enter the date(reservation date:");
             fflush(stdin);
             scanf("%s",&re_data.re_date);
             printf("\nPlease enter the number of tickets you need:");
               fflush(stdin);
             scanf("%d",&k);
             if (k<=0)
             {
                 puts("\nPlease enter a valid number!");
                 goto B;
             }
             if(t_data[i].num_passenger+k>t_data[i].MAX_passenger)
             {
                 printf("\nSorry,but you can only but %d ticket(s) at most.\n",t_data[i].MAX_passenger - t_data[i].num_passenger);
                 goto B;
             }
             for(j=0;j<p_num;j++)
                 if(p_data[j].ID==re_data.ID)
            if (strcmp(p_data[j].name,re_data.name)&&(strcmp(p_data[j].re_date,re_data.re_date)));
             {
                 t_data[i].num_passenger+=k;
                 savef_t();
                 p_data[j].k+=k;
                 savef_p();
                 L=1;
             }
             if(L==0)
             {
                 j=p_num;
                 p_data[j]=re_data;
                 p_num++;
                 p_data[j].k=k;
                 t_data[i].num_passenger+=k;
                 p_data[j].num_passenger=t_data[m].num_passenger;
                 p_data[j].price=t_data[m].price;
                 strcpy(p_data[j].s_station,t_data[m].s_station);
                 strcpy(p_data[j].t_station,t_data[m].t_station);
                 savef_t();
                 savef_p();
                 PUT_p(j);
             }
             break;
            }
            else
            {
                printf("No such ID in the database...");
                goto B;
            }

B:    printf("\nContinue to reserve? Any key(Yes) N(no):");
      fflush(stdin);
      scanf("%c", &choice);
    }
  while (choice != 'n' && choice != 'N');
}

void edit_reserve(void)
{
}
void manage(void)
{
}
void menu_admin(void)
{
    char n[2];
    do
    {
        system("cls");
        printf("Please input an interger (1-3):\n"
           "*******************ADMINISTRATOR******************\n"
           "* (1) Creat New Railway Lines\n"
           "* (2) Edit A Railway Line\n"
           "* (3) Back to Main Menu\n"
           "**************************************************\n");
        gets(n);
        switch(*n)
        {
            case '1':system("cls");create_train();break;
            case '2':system("cls");edit_line();break;
            default:break;
        }
    }while(*n!='3');
}
void menu_agent(void)
{
    char n[2];
    do
    {
        system("cls");
        printf("Please input an interger (1-6):\n"
           "*******************TICKET AGENT*******************\n"
           "* (1) Search A Line\n"
           "* (2) Reserve tickets\n"
           "* (3) Edit Reservation Information\n"
           "* (4) Back to Main Menu\n"
           "**************************************************\n");
        gets(n);
        switch(*n)
        {
            case '1':system("cls");search();break;
            case '2':system("cls");reserve();break;
            case '3':system("cls");edit_reserve();break;
            case '4':return;
            default:break;
        }
    }while(*n!='5');
}
void menu_manager(void)
{
    char n[2];
    do
    {
        system("cls");
        printf("Please input an interger (1-5):\n"
           "**********************MANAGER*********************\n"
           "* (1) Number of Trains for TYPE T123\n"
           "* (2) Number of Trains for TYPE T456\n"
           "* (3) Number of Passengers for A Railway Line\n"
           "* (4) Total Revenue\n"
           "* (5) Back to Main Menu\n"
           "**************************************************\n");
        gets(n);
        switch(*n)
        {
            case '1':system("cls");break;
            case '2':system("cls");break;
            case '3':system("cls");break;
            case '4':system("cls");break;
            case '5':break;
            default:break;
        }
    }while(*n!='5');
}
void menu_mode(void)
{
    printf("Please input an interger (1-4):\n"
           "********************Mode Choice*******************\n"
           "* (1) Administrator\n"
           "* (2) Ticket Agent\n"
           "* (3) Manager\n"
           "* (4) Exit\n"
           "**************************************************\n");
}
int main(void)
{
    char n[2];
    do
    {
        system("cls");
        menu_mode();
        gets(n);
        switch(*n)
        {
            case '1':system("cls");menu_admin();break;
            case '2':system("cls");menu_agent();break;
            case '3':system("cls");menu_manager();break;
            default:break;
        }
    }while(*n!='4');
    return 0;
}


25,42,153,154行有错误..但是自己找了好久实在没发现错误在哪,求指教
搜索更多相关主题的帖子: color train 
2012-12-19 15:50
jk_love
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:1
帖 子:196
专家分:965
注 册:2012-10-22
收藏
得分:15 
目测25 那里是因为
#define LEN 100000; 后面有分号
2012-12-19 16:12
crystall
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:184
专家分:809
注 册:2012-12-1
收藏
得分:0 
回复 2楼 jk_love
支持
2012-12-19 16:20
c语言新手yu
Rank: 2
等 级:论坛游民
帖 子:38
专家分:17
注 册:2012-11-5
收藏
得分:0 
哇哇哇~~~这么长??、
2012-12-19 18:32
快速回复:求教下列代码错误所在,自己实在是看不出错误在哪里了
数据加载中...
 
   



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

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