| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 646 人关注过本帖
标题:这个程序怎么改才对
只看楼主 加入收藏
lklqlk1991
Rank: 2
等 级:论坛游民
帖 子:32
专家分:16
注 册:2009-10-15
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:7 
这个程序怎么改才对
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
struct op
{
char name[20];
int age;
char sex;
};
typedef struct op opp;
opp a,*b=&a;
struct lk
{
char name[20];
int age;
char sex;
struct lk*next;
};
typedef struct lk lkk;
lkk* del(lkk*head)
{
char a[20];
lkk*d,*dd;
if(head==0)
{
printf(" no data for find\n");
getch();
exit(0);
}
d=dd=head;

printf("input the name to find\n");
do
{
fflush(stdin);
scanf("%s",a);
if(strlen(a)>19)
   printf(" you put too long\n");
}while(strlen(a)>19);
while(dd!=0)
{

   if(strcmp(dd->name,a)==0)
   {
    printf("%s\t%d\t%c\n",dd->name,dd->age,dd->sex);
    getch();
    printf("the date is del!\n");
    break;
    }
    d=dd;
    dd=dd->next;
    if(dd==0)
    {
     printf("no find\n");
     getch();
     exit(0);}
    }
    if(dd==head)
    {
     head=head->next;
      free(dd);
    }
    else
    {
     d->next=dd->next;
     free(dd);}
     return(head);
     }
lkk*vget(FILE*fp)

{
lkk*head,*p,*q;
p=(lkk*)malloc(sizeof(lkk));
if(p==0)
{
printf("error bnbn\n");
getch();
exit(0);
}
fread(p,sizeof(opp),1,fp);

head=p;
p->next=0;
fseek(fp,sizeof(opp),0);
while(!feof(fp))
{
q=(lkk*)malloc(sizeof(lkk));
if(q==0)
{
   printf("error nnnn\n");
   getch();
   exit(0);
}
   fread(q,sizeof(opp),1,fp);
   p->next=q;
   q->next=0;
   p=q;
}
return(head);
}还是这里有问题???


lkk*creat(int n)
{
lkk*head,*p,*q;
int i;
p=(lkk*)malloc(sizeof(lkk));
if(p==0)
{
   printf("error\n");
   getch();
   exit(0);
   }
   head=p;
   printf("input the 1 stdunt date\n");
   do
   {
   printf("name:");
   scanf("%s",p->name);
   i=strlen(p->name);
   if(i>19)
    printf("too long\n");
   }while(i>19);
   do
   {
   fflush(stdin);
   printf("age:");
   scanf("%d",&p->age);
   if(p->age<0)
    printf("error\n");
    }while(p->age<0);
    do
    {
   printf("sex: b for boy g for gile\n");
   while(getchar()!='\n')
   ;
    scanf("%c",&p->sex);
   if(p->sex!='b'&&p->sex!='g')
    printf("input error\n");
   }while(p->sex!='b'&&p->sex!='g');
   p->next=0;
   for(i=1;i<n;i++)
   {
    if(n==1)
      break;
    q=(lkk*)malloc(sizeof(lkk));
    if(q==0)
    {
     printf("error error\n");
     getch();
     exit(0);
     }
     p->next=q;
     printf("input the %d stdunt date\n",i+1);
    do
   {
   printf("name:");
   scanf("%s",q->name);
   i=strlen(q->name);
   if(i>19)
    printf("too long\n");
   }while(i>19);
   do
   {
   printf("age:");
   scanf("%d",&q->age);
   if(q->age<0)
    printf("error\n");
   }while(q->age<0);
    do
    {
    getchar();
   printf("sex: b for boy g for gile\n");
   scanf("%c",&q->sex);
   if(q->sex!='b'&&q->sex!='g')
    printf("input error\n");
   }while(q->sex!='b'&&q->sex!='g');
   q->next=0;
   p=q;
   }
   return(head);
   }
   void print(lkk*head)
   {
    if(head==0)
     {
      printf("no data\n");
      getch();
      exit(0);
      }
    while(head!=0)
    {
     printf("%s\t%d\t%c\n",head->name,head->age,head->sex);
     head=head->next;
     }
     }
main()
{
FILE*fp;
lkk*head=0,*p,*d;
int i,j,k;
char c,cc,ccc,a[20];
lk:do
{
printf("1.---------- input -----------\n");
printf("2.---------- find -----------\n");
printf("3.---------- del -----------\n");
printf("4.---------- save -----------\n");
printf("5.---------- exit -----------\n");
printf("7.---------- open -----------\n");
printf("\n\n\n");
printf("6.---------input 1-5 to contuie--------\n");
fflush(stdin);
c=getchar();
getchar();
switch(c)
{
   case '1':printf("input the stdunt numbers\n");scanf("%d",&i);head=creat(i);
            getchar();p=head;break;
   case '2':   printf("1.|------find age ----------|\n");
               printf("2.|------find sex ----------|\n");
               printf("3.|------find age and sex---|\n");
               printf("4.|------ find name -------|\n");
               fflush(stdin);
               cc=getchar();
               getchar();d=head;
               if(head==0)
                {
                 printf("head==0\n");
                 getch();
                 exit(0);
                 }
               switch(cc)
               {
                case '1':printf("the age:");scanf("%d",&j);
                         head=p;
                         d=head;
                         while(d!=0)
                         {
                          if(d->age==j)
                          {
                           printf("name:%s\tage:%d\tsex:%c\n",d->name,d->age,d->sex);
                             }
                            d=d->next;
                          }
                             break;
                 case '2':printf("the sex:");fflush(stdin);scanf("%c",&cc);
                          head=p;
                         d=head;
                         while(d!=0)
                         {
                          if(d->sex==cc)
                          {
                           printf("name:%s\tage:%d\tsex:%c\n",d->name,d->age,d->sex);
                          }
                          d=d->next;
                             }
                           break;
                  case '4':printf("input the name\n");fflush(stdin);
                           do
                           {
                           scanf("%s",a);
                           if(strlen(a)>19)
                           {
                            printf(" out\n");
                            }
                            fflush(stdin);
                            }while(strlen(a)>19);
                            while(head!=0)
                            {
                            if(strcmp(head->name,a)==0)
                             printf("%s\t%d\t%c\n",head->name,head->age,head->sex);
                             head=head->next;
                            if(head==0)
                            {
                             printf(" no find\n");
                             getch();
                             break;}
                             }
                             break;
                   default:printf("error error\n");getch();
                  }break;
         case '5':exit(0);
         case '4':
                  printf("input the file\n");
                  scanf("%s",a);
                  fp=fopen(a,"wb");
                  if(fp==0)
                  {
                   printf("can not open\n");
                   getch();
                   exit(0);
                   }
                   if(head==0)
                   {
                    printf("no date to find\n");
                    getch();
                    goto lk;
                    }
                    while(head!=0)
                    {
                    strcpy(b->name,head->name);
                    b->age=head->age;
                    b->sex=head->sex;
                    fwrite(b,sizeof(opp),1,fp);
                    head=head->next;难道说这里错了???
                    }
                    printf("date is save!\n");
                    fclose(fp);
                    break;

         case '7':printf("input the file to open\n");
                  scanf("%s",a);
                  fp=fopen(a,"rb+");
                  if(fp==0)
                  {
                   printf("cannot open files\n");
                   getch();
                   goto lk;
                   }
                  p=head=vget(fp);调用VGET打开一个文件
                  printf("now type the file\n");
                  getch();
                  print(head);显示出的后面有乱码
                   break;
         case '3':
                   head=del(head);
                   break;
         default:printf("error rrrr\n");
         }
       }while(1);
}
 
搜索更多相关主题的帖子: include 
2009-10-27 19:55
lklqlk1991
Rank: 2
等 级:论坛游民
帖 子:32
专家分:16
注 册:2009-10-15
收藏
得分:0 
哪位大吓能不能仔细看一下.感激不尽!!
2009-10-28 17:11
m456m654
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:783
专家分:2806
注 册:2009-9-17
收藏
得分:2 
too long ,too 难
2009-10-28 18:36
忆水流年
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2009-9-17
收藏
得分:2 
太长了,看着就恐怖得很
2009-10-28 19:17
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
这程序问题太严重了.

数组非常容易溢出

struct lk
{
    char   name[20];
    int    age;
    char   sex;
    struct lk* next;
};

    do
    {
        printf("name:");
        scanf("%s",p->name);
        i = strlen(p->name);
        if(i > 19)
            printf("too long\n");
   // 如果出现这个说明数组已经溢出.
    }while(i>19);

输入太多字符程序一般都出错停止了.

—>〉Sun〈<—
2009-10-28 19:18
dengjun000
Rank: 2
等 级:论坛游民
帖 子:66
专家分:77
注 册:2009-10-14
收藏
得分:12 
。。。。
2009-10-28 19:28
lklqlk1991
Rank: 2
等 级:论坛游民
帖 子:32
专家分:16
注 册:2009-10-15
收藏
得分:0 
哪位高人给我一个思路,能不能把一组存储学生信息的链表存入一文件,然后可以打开它,对它进行排序,删除指定学生的信息.例如用malloc开辟一块内存储存学生信息,然后用链表的思路处理。或者给一别的好的思路也行啊,我写的这个主要是在储存和打开文件时犯了难。老是出错。最好给一源码和详细的解释。我是菜鸟。多多包涵,指点一二也可以啊。
2009-10-29 20:20
华飞
Rank: 2
等 级:论坛游民
帖 子:18
专家分:18
注 册:2009-10-23
收藏
得分:4 
太有才了
2009-10-29 22:52
快速回复:这个程序怎么改才对
数据加载中...
 
   



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

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