| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 391 人关注过本帖
标题:大家运行下,提下意见
只看楼主 加入收藏
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
收藏
已结贴  问题点数:20 回复次数:2 
大家运行下,提下意见
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
    int num;
    int age;
    char pc[20];
    char name[20];
    struct student *link;
}stud;

stud* creat(int n)
{
    stud *h,*p;
    int i;
    h=(stud *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
     printf("学号\t年龄\t姓名\t电脑型号");
    h=NULL;
    for(i=0;i<=n;i++)
    {
        
      
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
            
            
            
        {
            printf("内存不足!");
            return(0);
        }
        printf("\n");
        scanf("%d",&p->num);

        
        scanf("%d",&p->age);
        scanf("%s",&p->pc);
        scanf("%s",&p->name);
        p->link=h;
        h=p;
      
    }
    return(h);
}
  stud* insert(stud *h)
  {
      stud *p,*q,*d,*i;
      p=h;
      int n;
      q=h->link;
      d=(stud *)malloc(sizeof(stud));
      if(d==NULL)
      {
          printf("内存不足!");
      }
      printf("请输入插入位置的前一个数!");
      scanf("%d",&n);
      
      printf("学号\t年龄\t姓名\t电脑型号\n");
      if(n==p->num||n==p->age)
      {
      printf("请输入要插入的数 !");
      scanf("%d",&d->num);
      scanf("%d",&d->age);
      scanf("%s",&d->pc);
      scanf("%s",&d->name);
      i->link=p->link;
      p->link=i;
      }
      else if((n==p->num||n==p->age)&&p->link==NULL)
      {
      scanf("%d",&d->num);
      scanf("%d",&d->age);
      scanf("%s",&d->pc);
      scanf("%s",&d->name);
      q->link=p->link;
      p->link=q;
      }
      else if
      
          (n==q->num||q->age)
      {
      
      scanf("%d",&d->num);
      scanf("%d",&d->age);
      scanf("%s",&d->pc);
      scanf("%s",&d->name);
      d->link=p->link;
      p->link=d;
      }
      else if(n!=p->num&&n!=q->num)
      {
         while(q->num!=NULL&&p->num!=NULL)
         {
              
              
              p=p->link;
              q=q->link;
              if(n==q->num)
             {
              printf("请输入要插入的数字!");
              scanf("%d",&d->num);
              scanf("%d",&d->age);
              scanf("%s",&d->pc);
              scanf("%s",&d->name);
              d->link=p->link;
              p->link=d;
               }
             break;
               
                        
         }
      }
     return(h);
  }  
  void search(stud *h)
  {
    stud *p;
    int m;
    printf("请输入需查学号!");
    scanf("%d",&m);
     
    printf("查找中...\n");
    system("cls");
    p=h;
     if(p==NULL)
     printf("无此数!\n");
    while(p!=NULL)
    {
   
        if(m==p->num||m==p->age)

        {   printf("学号\t年龄\t姓名\t电脑型号\n");
            printf("%d\t %d\t%s\t%s\n",p->num,p->age,p->pc,p->name);
        
         
        }
        
         p=p->link;
        
   
     }
     
            
     
   
   
   
  }
  stud *delet(stud *h)
  {
      stud *q,*p;
      p=h;
      int n;
      q=h->link;
      printf("请输入要删除的学号!");
      scanf("%d",&n);
      if(n==p->num||n==p->age)
      {if(p->link!=NULL)
        *h=*(p->link);
        else
            free(p);
      }
      else if(n==q->num||n==q->age)
          p->link=q->link;
      else if(n!=p->num&&n!=q->num)
         while(q->link!=NULL&&p->link!=NULL)
         {
              
              
              p=p->link;
              q=q->link;        
             if(n==q->num||n==q->age)
              p->link=q->link;
         
         }
         
         
      return(h);
  }
void print(stud *h)
{
     stud *p;
     p=h;
     while(p!=NULL)
     {
         printf("%3d %d\n",p->num,p->age);
         p=p->link;
     }
}
void save(stud *h) //保存函数//
{
    stud *p;
    FILE *fp;
     p=h;//头指针赋给p//
    fp=fopen("d:\\KKK","w+");
    printf("保存中...\n");
    if(fp==NULL)
    {
        printf("memeory error!");
    }
   
    while(p!=NULL)
    {
        
        fwrite(p,sizeof(stud),1,fp);
        p=p->link;
   
  
    }
   
    fclose(fp);
   
    printf("save successful !\n");
}
stud *load()
{
    stud *p,*q,*h;
    FILE *fp;
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;//初始化头指针//
        
   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;
     if(h==NULL)
         printf("无记录!\n");
     else
     {
     printf("学号\t年龄\t姓名\t电脑型号\n");
     while(h!=NULL)
     {
         
         printf(" %2d",h->num);
         printf("%8d\t",h->age);
         printf("%s\t",h->pc);
         printf("%s",h->name);
          h=h->link;
          printf("\n");

    }
    fclose(fp);
     }
   
}
stud *cbc()
{
    stud *p,*q,*h;
    FILE *fp;

   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;//初始化头指针//
        
   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;
    p=h;//头指针赋给p//
    fp=fopen("d:\\KKK","w+");
    printf("保存中...\n");
    if(fp==NULL)
    {
        printf("memeory error!");
    }
   
    while(p!=NULL)
    {
        
        fwrite(p,sizeof(stud),1,fp);
        p=p->link;
   
  
    }
     
    fclose(fp);
     
   
}
stud *DEL()
{
    stud *p,*q,*h;
    FILE *fp;
    static n=0;
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;
        
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
      h=h->link;
      delet(h);
     save(h);
     
    fclose(fp);
}        
stud *INF()
{
    stud *p,*q,*h;
    FILE *fp;
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;
        
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;   
      insert(h);//接到返回指针,执行插入任务//
     save(h);//接到插入函数返回指针,执行保存任务//
    fclose(fp);
   
   
}   
stud *SER()
{
    stud *p,*q,*h;
    FILE *fp;
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;
        
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;   
      search(h);//获取返回指针,执行查找任务//
    fclose(fp);
   
   
}
void main()
{
    printf("载入中...\n");
    system("cls");
    int c,b,a,e,g,I;
    static n=3;
    struct student *h;
    printf("\t********************欢迎使用学生宿舍管理系统***********************\n");
    printf("   \t\t0 添加记录\n");
    printf("   \t\t1 删除记录\n");
    printf("   \t\t2 显示所有记录\n");
    printf("   \t\t3 插入新记录\n");
    printf("   \t\t4 查找记录\n");
   
 
    scanf("%d",&c);
     system("cls");
    switch(c)
    {
        
     
     case 0:h=creat(2);
            save(h);break;
     case 1:DEL();
           break;
            
     case 2:load();break;
     case 3:INF();break;
     case 4:SER();break;
     
     
}
}
搜索更多相关主题的帖子: 电脑型号 姓名 include memory return 
2011-10-26 21:53
laznrbfe
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:482
专家分:1599
注 册:2011-5-22
收藏
得分:20 
程序代码:
void main()
{
    printf("载入中...\n");
    system("cls");
    int c,b,a,e,g,I;
    static n=3;
    struct student *h;
    while(1)
    {
        printf("\t********************欢迎使用学生宿舍管理系统***********************\n");
        printf("   \t\t0 添加记录\n");
        printf("   \t\t1 删除记录\n");
        printf("   \t\t2 显示所有记录\n");
        printf("   \t\t3 插入新记录\n");
        printf("   \t\t4 查找记录\n");
        printf("   \t\t5 退出\n");
   
        scanf("%d",&c);
        system("cls");
        switch(c)
        {
       
       
        case 0:h=creat(2);
            save(h);break;
        case 1:DEL();
            break;
       
        case 2:load();break;
        case 3:INF();break;
        case 4:SER();break;
        case 5:    return;
       
        }
    }
} 
首先,你的程序只能选择一个操作。我修改了一下main()函数。
其次,你的显示操作输出的和输入的数据相反,导致当我们插入再显示时,会以为你的插入算法有问题。
2011-10-26 22:56
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
输出顺序不同,该怎么改
2011-10-27 10:03
快速回复:大家运行下,提下意见
数据加载中...
 
   



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

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