| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 590 人关注过本帖
标题:程序改错,求赐教。
只看楼主 加入收藏
unilin540
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-9-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
程序改错,求赐教。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define NULL 0

struct carinfo
{
    char num[18];
    char name[12];
    char type[5];
    double price;
    int date;
    char chexing[20];
    char corporation[20];
    struct carinfo *next;
};
int n=10;
struct carinfo *head;



void main()
{

    int i=0,j;
    int cishu=3;
    char name[15],password[10];
    struct carinfo car[10];
    strcpy(car[0].num,"CTXER5673423KI980");
    strcpy(car[0].name,"audi");
    strcpy(car[0].type,"S8");
    car[0].price=228.43;
    car[0].date=2010;
    strcpy(car[0].chexing,"haohuaxing");
    strcpy(car[0].corporation,"audi");

   strcpy(car[1].num,"ATRFNL33458719L43");
   strcpy(car[1].name,"audi");
   strcpy(car[1].type,"A5");
   car[1].price=50.45;
   car[1].date=2009;
   strcpy(car[1].chexing,"shushixing");
   strcpy(car[1].corporation,"audi");

   strcpy(car[2].num,"IOJENLF3777908KE4");
   strcpy(car[2].name,"baoma");
   strcpy(car[2].type,"3");
   car[2].price=54.45;
   car[2].date=2010;
   strcpy(car[2].chexing,"zhongxing");
   strcpy(car[2].corporation,"baoma");

   strcpy(car[3].num,"KEIFDS32545LI6574");
   strcpy(car[3].name,"baoma");
   strcpy(car[3].type,"1");
   car[3].price=37.70;
   car[3].date=2010;
   strcpy(car[3].chexing,"jincouxing");
   strcpy(car[3].corporation,"baoma");

   strcpy(car[4].num,"EIWOQ3411087JH567");
   strcpy(car[4].name,"benchi");
   strcpy(car[4].type,"CLS");
   car[4].price=46.44;
   car[4].date=2010;
   strcpy(car[4].chexing,"dazhongxing");
   strcpy(car[4].corporation,"benchi");

   strcpy(car[5].num,"QIEOW327658JK4567");
   strcpy(car[5].name,"benchi");
   strcpy(car[5].type,"C");
   car[5].price=34.00;
   car[5].date=2009;
   strcpy(car[5].chexing,"zhongxing");
   strcpy(car[5].corporation,"benchi");

   strcpy(car[6].num,"OUEND34675L763335");
   strcpy(car[6].name,"dongfeng");
   strcpy(car[6].type,"H30");
   car[6].price=6.58;
   car[6].date=2008;
   strcpy(car[6].chexing,"jincouxing");
   strcpy(car[6].corporation,"dongfeng");

   strcpy(car[7].num,"KDLSH32575K843167");
   strcpy(car[7].name,"dongfeng");
   strcpy(car[7].type,"S30");
   car[7].price=9.98;
   car[7].date=2009;
   strcpy(car[7].chexing,"jincouxing");
   strcpy(car[7].corporation,"dongfeng");

   strcpy(car[8].num,"EWODLS32564L26676");
   strcpy(car[8].name,"fengtian");
   strcpy(car[8].type,"RAV4");
   car[8].price=31.88;
   car[8].date=2009;
   strcpy(car[8].chexing,"weixing");
   strcpy(car[8].corporation,"fengtian");

   strcpy(car[9].num,"NCMDL445687K32154");
   strcpy(car[9].name,"fengtian");
   strcpy(car[9].type,"4Runner");
   car[9].price=69.00;
   car[9].date=2009;
   strcpy(car[9].chexing,"paoche");
   strcpy(car[9].corporation,"fengtian");

   head=&car[0];
   car[0].next=&car[1];
   car[1].next=&car[2];
   car[2].next=&car[3];
   car[3].next=&car[4];
   car[4].next=&car[5];
   car[5].next=&car[6];
   car[6].next=&car[7];
   car[7].next=&car[8];
   car[8].next=&car[9];
   car[9].next=NULL;
   void print();
   void save();
   struct carinfo *del();
   struct carinfo *add();
   void search();
   void update();
   void order();
   void display();
   void open();
    printf("********************************************************************************\n");
    printf("\n");
    printf("=====================欢-迎-进-入-汽-车-信-息-管-理-系-统========================\n");
    printf("\n");
    printf("********************************************************************************\n");
   again1: printf("请输入您的用户名,您还有%d次机会:\n",cishu--);
    scanf("%s",name);   
   if(strcmp(name,"administrator")!=0&&strcmp(name,"user")!=0&&i<2)
    {   
        printf("\n");
        printf("用户名错误!\n");
        i++;
        goto again1;
    }
    cishu=3;
    i=0;
    if(strcmp(name,"administrator")==0)
    {
    again2:printf("请输入管理员密码,您还有%d次机会:\n",cishu--);
        scanf("%s",password);
       if(strcmp(password,"123456")!=0&&i<2)
        {
            printf("\n");
            printf("密码错误!\n");
            i++;
            goto again2;
       }
      if(strcmp(password,"123456")==0)
       while(1)
       {
           printf("--------------------------------\n");
           printf("       请选择所需功能:          \n");
           printf("--------------------------------\n");
           printf(" 1. 显示记录\n 2. 查找\n 3. 添加记录\n 4. 删除记录\n 5. 更新记录\n 6. 代号排序\n 7. 按类别显示记录\n 8. 保存记录到磁盘\n 9. 读入磁盘文件\n 0. 退出\n");
           printf("--------------------------------\n");
           scanf("%d",&j);
           switch(j)
           {
              case 1:print();
                  break;
              case 2:search();
                  break;
              case 3:head=add();
                  break;
              case 4:head=del();
                  break;
              case 5:update();
                  break;
              case 6:order();
                  break;
              case 7:display();
                  break;
              case 8:save();
                  break;
              case 9:open();
                  break;
              case 0 : exit(0);
              default :
                  break;
           }
       }
    }
         
    if(strcmp(name,"user")==0)
    while(1)
    {     
           printf("--------------------------------\n");
           printf("        请选择所需功能:         \n");
           printf("--------------------------------\n");
           printf(" 1. 显示记录\n 2. 查找\n 0. 退出\n");
            printf("--------------------------------\n");
           scanf("%d",&j);
           switch(j)
           {
              case 1:print();
                  break;
              case 2:search();
                  break;
              case 0:exit(0);
              default :
                  break;
           }
    }           
}

   
void print()
{
    struct carinfo *p;
    p=head;
    printf("%d条记录:\n",n);
    if(head!=NULL)
    do
    {
        printf("%s %-10s %-8s %-6.2f %-5d %-14s %-10s\n",p->num,p->name,p->type,p->price,p->date,p->chexing,p->corporation);
        p=p->next;
    }while(p!=NULL);
}



struct carinfo *add()
{
    struct carinfo *p0,*p1;
    char choice;
     begin4:
       p1=head;
       p0=(struct carinfo *)malloc(sizeof(struct carinfo));
       printf("请输入:代号 车名 型号 价格/万元 日期 车型 公司\n");
       scanf("%s %s %s %f %d %s %s",p0->num,p0->name,p0->type,&p0->price,&p0->date,p0->chexing,p0->corporation);
       if(p0->num!=p1->num)
       {
          if(p1!=NULL)
              p0->next=p1;
          else
              p0->next=NULL;
          head=p0;
          printf("成功添加,现存%d条记录\n",++n);
       }
       else
           printf("该车已存在!\n");
          printf("继续添加吗?y/n\n");
          choice=getchar();
         choice=getchar();
           putchar('\n');
         if(choice=='y')
            goto begin4;
    return(head);
}



struct carinfo *del()
{
    struct carinfo *p1,*p2;
    char num[17];
    char choice='y';
    if(head==NULL)
    {
        printf("无车辆可删除!\n");
        return(head);
    }
    else
    {
       begin3:
            printf("请输入要删除的代号:\n");
            scanf("%s",num);
            p1=head;
             while((strcmp(num,p1->num)!=0)&&(p1->next!=NULL))
            {
                 p2=p1;
                p1=p1->next;
            }
            if(strcmp(num,p1->num)==0)
            {
                if(p1==head)
                    head=p1->next;
                else
                    p2->next=p1->next;
                printf("车辆%s已删除,现存%d条记录\n",num,--n);
            }
            else
                printf("无要删除车辆!\n");
            printf("继续删除吗?y/n\n");
            choice=getchar();
             choice=getchar();
            putchar('\n');
            if(choice=='y')
            goto begin3;
       }
    return(head);
}


void save()
{
    struct carinfo c[50],*p;
    FILE *fp;
    char filename[15];
    int i=0;
    p=head;
    while(p!=NULL)
    {
        strcpy(c[i].num,p->num);
        strcpy(c[i].name,p->name);
        strcpy(c[i].type,p->type);
        c[i].price=p->price;
        c[i].date=p->date;
        strcpy(c[i].chexing,p->chexing);
        strcpy(c[i].corporation,p->corporation);
        p=p->next;
        i++;
    }
start4:printf("请输入要保存的文件名:\n");
    scanf("%s",filename);
    if((fp=fopen(filename,"wb"))==NULL)
    {
        printf("无法打开文件\n");
        goto start4;
    }
    for(i=0,p=head;p!=NULL;p=p->next,i++)
        if(fwrite(&c[i],sizeof(struct carinfo),1,fp)!=1)
            printf("文件写入失败\n");
        fclose(fp);
}


void search()
{
    struct carinfo *p;
    char num[18];
    char choice;
begin1:
        printf("请输入要查询的车辆代号:\n");
        scanf("%s",num);
         p=head;
        while((strcmp(num,p->num)!=0)&&(p->next!=NULL))
        {
            p=p->next;
        }
        if(strcmp(num,p->num)==0)
        {
             printf("该车辆信息为:\n");
            printf("%s %s %s %f %d %s %s\n",p->num,p->name,p->type,p->price,p->date,p->chexing,p->corporation);
        }
        else
             printf("该车辆不存在!\n");
        printf("继续查找吗?y/n\n");
        choice=getchar();
        choice=getchar();
        putchar('\n');
        if(choice=='y')
            goto begin1;
}


void update()
{
    char num[17];
    char choice;
    struct carinfo *p;
    begin2:
        p=head;
         printf("请输入要更新的车辆代号:\n");
        scanf("%s",num);
        while((strcmp(num,p->num)!=0)&&(p->next!=NULL))
        {
            p=p->next;
        }
        if(strcmp(num,p->num)==0)
        {
            printf("\n%s %s %s %f %d %s %s\n",p->num,p->name,p->type,p->price,p->date,p->chexing,p->corporation);
            printf("请输入更新后的信息:价格/万元,日期\n");
            scanf("%f,%d",&p->price,&p->date);
            printf("更新成功!更新后的信息为:\n");
            printf("\n%s %s %s %f %d %s %s\n",p->num,p->name,p->type,p->price,p->date,p->chexing,p->corporation);
        }
        else
            printf("无该车辆代号!\n");
          printf("继续更新吗?y/n\n");
         choice=getchar();
        choice=getchar();
        putchar('\n');
        if(choice=='y')
            goto begin2;
}


void display()
{
    char choice,company[20];
    struct carinfo *p;
    int m=0;
   loop1:
    printf("请输入要查询的公司名:\n");
    gets(company);
    gets(company);
     p=head;
    if(p==NULL)
        printf("无车记录\n");
    while(p!=NULL)
    {
      if(strcmp(p->corporation,company)!=0)  
      p=p->next;
      else
      {
            printf("%s %-10s %-8s %-6.2f %-5d %-14s %-10s\n",p->num,p->name,p->type,p->price,p->date,p->chexing,p->corporation);
            m++;
            p=p->next;
      }
    }
   if(m==0)
       printf("无该公司的车!\n");
    printf("继续操作吗?y/n\n");
    choice=getchar();
    choice=getchar();
    putchar('\n');
    if(choice=='y')
        goto loop1;
 }



void open ()
{
  FILE *fp;
  int i=0;
  char retry ;
  struct carinfo temp[50];
  char datafile[15];
  loop:
    printf("请输入要打开的文件名:\n ");
    scanf("s%",datafile);
   if((fp=fopen(datafile,"rb"))==NULL)
   {
        printf(" open file s% fail\n",datafile);
        printf ("do you want to open datafile again? (y\n)\n");
         scanf("c%",&retry);
        if(retry=='y'||retry=='Y')
            goto loop ;
        fclose(fp);
   }
 else  
 printf("the content of s% as follow\n",datafile);
 
while (fread(&temp[i++],sizeof(struct carinfo),1,fp)!=NULL)
{
  printf("%s %-10s %-8s %-6.2f %-5d %-14s %-10s\n",temp->num,temp->name,temp->type,temp->price,temp->date,temp->chexing,temp->corporation);
}
fclose(fp);
 
}
  

void order()
{
    struct carinfo c[50],*p;
    struct carinfo temp;
    int i;
    int j;
    p=head;
    while(p!=NULL)
    {
        strcpy(c[i].num,p->num);
        strcpy(c[i].name,p->name);
        strcpy(c[i].type,p->type);
        c[i].price=p->price;
        c[i].date=p->date;
        strcpy(c[i].chexing,p->chexing);
        strcpy(c[i].corporation,p->corporation);
        p=p->next;
        i++;
    }
  for(i=0;i<50 ;i++)
      for(j=0;j<50-i;j++)
      {
          if(strcmp(c[j].num,c[j+1].num)>0)
              strcpy(temp.num,c[j].num);
        strcpy(temp.name,c[j].name);
        strcpy(temp.type,c[j].type);
        temp.price=c[j].price;
        temp.date=c[j].date;
        strcpy(temp.chexing,c[j].chexing);
        strcpy(temp.corporation,c[j ].corporation);
                 strcpy(c[j].num,c[j+1].num);
        strcpy(c[j].name,c[j+1].name);
        strcpy(c[j].type,c[j+1].type);
        c[j].price=c[j+1].price;
        c[j].date=c[j+1].date;
        strcpy(c[j].chexing,c[j+1].chexing);
        strcpy(c[j].corporation,c[j+1].corporation);
       strcpy(c[j+1].num,temp.num);
        strcpy(c[j+1].name,temp.name);
        strcpy(c[j+1].type,temp.type);
        c[j+1].price=temp.price;
        c[j+1].date=temp.date;
        strcpy(c[j+1].chexing,temp.chexing);
        strcpy(c[j+1].corporation,temp.corporation);

      }
 printf("the sorted info is:\n");
 for(i=0;i<50;i++)
     printf("%s %s %s %d %d %s %s\n",c[i].num,c[i].name,c[i].type,c[i].price,c[i].date,c[i].chexing,c[i].corporation);
  }
问题:1:功能6不能实现,求调试;2:怎么对链表节点进行排序?
搜索更多相关主题的帖子: 改错 
2010-09-08 20:45
king_tao
Rank: 1
等 级:新手上路
帖 子:3
专家分:5
注 册:2010-9-8
收藏
得分:4 
我很想看看,可是我是初学者,这个太深奥了
2010-09-08 22:18
m564522634
Rank: 2
等 级:论坛游民
帖 子:34
专家分:16
注 册:2010-9-1
收藏
得分:4 
回复 楼主 unilin540
大哥你C学了多长时时中呀,给一点经验呀。
2010-09-08 22:46
kevin_zh
Rank: 4
来 自:农村
等 级:业余侠客
威 望:1
帖 子:129
专家分:250
注 册:2010-8-14
收藏
得分:4 
蛮完整的一个程序,刚学c还无法看懂全部代码,只是意思知道

"please input a number :\n"
2010-09-09 07:51
ppfly
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:297
专家分:1956
注 册:2009-5-17
收藏
得分:4 
回复 4楼 kevin_zh
void order()函数里面有几处错误:

1  第一个while循环之前 i 没有初始化,所以int i=0;
2  冒泡排序里面 i<50,及 j<50-i 是不对的,因为50可能导致越界,修改方法有两种,第一种是加上一个计数器p,使得i<p,j<p-i-1;另一种方法就是在定义struct carinfo c[50]时采用malloc动态分配。建议采用第二种。
3  if()后面所有的语句都是其判断条件下的执行语句,所以要加上大括号。
4  最后一条printf语句里面,注意c[i].price是一个double型的变量,前面应该是%f。

下面是照你的思想修改后的代码,调试通过。
程序代码:
void order()
{
    struct carinfo c[50],*p;
    struct carinfo temp;
    int i=0,lp;
    int j;
    p=head;
    while(p!=NULL)
    {
        strcpy(c[i].num,p->num);
        strcpy(c[i].name,p->name);
        strcpy(c[i].type,p->type);
        c[i].price=p->price;
        c[i].date=p->date;
        strcpy(c[i].chexing,p->chexing);
        strcpy(c[i].corporation,p->corporation);
        p=p->next;
        i++;
    }

    lp=i;    //加上计数器

  for(i=0;i<lp;i++)
      for(j=0;j<lp-i-1;j++)
      {
          if(strcmp(c[j].num,c[j+1].num)>0)
          {
                strcpy(temp.num,c[j].num);
                strcpy(temp.name,c[j].name);
                strcpy(temp.type,c[j].type);
                temp.price=c[j].price;
                temp.date=c[j].date;
                strcpy(temp.chexing,c[j].chexing);
                strcpy(temp.corporation,c[j].corporation);

                strcpy(c[j].num,c[j+1].num);
                strcpy(c[j].name,c[j+1].name);
                strcpy(c[j].type,c[j+1].type);
                c[j].price=c[j+1].price;
                c[j].date=c[j+1].date;
                strcpy(c[j].chexing,c[j+1].chexing);
                strcpy(c[j].corporation,c[j+1].corporation);

                strcpy(c[j+1].num,temp.num);
                strcpy(c[j+1].name,temp.name);
                strcpy(c[j+1].type,temp.type);
                c[j+1].price=temp.price;
                c[j+1].date=temp.date;
                strcpy(c[j+1].chexing,temp.chexing);
                strcpy(c[j+1].corporation,temp.corporation);
          }

      }
    printf("the sorted info is:\n");
    for(i=0;i<lp;i++)
     printf("%s %s %s %f %d %s %s\n",c[i].num,c[i].name,c[i].type,c[i].price,c[i].date,c[i].chexing,c[i].corporation);
  }

很纠结很有耐心的看了代码,所以别忘了给分~~

[ 本帖最后由 ppfly 于 2010-9-9 13:59 编辑 ]

********多贴代码,少说空话*******
2010-09-09 13:43
gongyaping
Rank: 4
来 自:广东肇庆怀集
等 级:业余侠客
帖 子:174
专家分:257
注 册:2010-8-1
收藏
得分:4 
回帖得分顺便学习下,顶了。
2010-09-09 16:04
快速回复:程序改错,求赐教。
数据加载中...
 
   



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

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