| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 557 人关注过本帖
标题:【求助】编程问题,关于重要程序管理源代码
取消只看楼主 加入收藏
huang6896
Rank: 1
等 级:新手上路
帖 子:8
专家分:9
注 册:2010-6-24
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
【求助】编程问题,关于重要程序管理源代码
题目是要求编个重要的什么什么管理程序,可是对于我难度不小,特此求救
以下的错误太多,我自己找不出来,渴望同为C爱好者的各位帮忙指导讨论
我比较菜,所以错误处请详细指出。。。。
#include "stdio.h"     
#include "malloc.h"   
#include "stdlib.h"   
#include "string.h"   

#define N 30           

typedef struct stu     
{
    long int no;
    char address[N];
    char score[N];
    char name[N];
    long int order;
    struct stu *next;
}STUDENT;

/*菜单函数,返回值为整数*/
int menu_select()
{
    char s[3];
    int c;
    printf("\n         ***************主菜单**************\n");
    printf("             1. 输入记录\n");
    printf("             2. 显示所有记录\n");  
    printf("             3. 按账号名查找记录并显示\n");
    printf("             4. 插入记录\n");
    printf("             5. 删除记录\n");
    printf("             6. 将所有记录保存到文件\n");
    printf("             7. 从文件中读入所有记录\n");
    printf("             8. 退出\n");
    printf("         ***********************************\n\n");
    do
    {
        printf("         请选择操作(1-8):");
        scanf("%s",s);
        c=atoi(s);
    }while(c<0||c>8);
    return(c);
}


/*创建链表,完成数据录入功能,新结点在表头插入*/
STUDENT *create()
{
     
   
    STUDENT *h=NULL,*info;            
    for(;;)
    {
        info=(STUDENT *)malloc(sizeof(STUDENT));  
        if(!info)                                 
        {
             printf("\n内存分配失败");                 
             return NULL;                        
        }
        printf("\n请按如下提示输入相关信息.\n\n");
        printf("输入编号(输入'#'结束):");
        scanf("%ld",info->no);   
        if(info->no=='#') break;              
        printf("输入账号位置:");
        scanf("%s",info->address);      
        
         info->next=NULL;            
        info->next=h;              
        h=info;            
    }
    return(h);                  
}


/*显示模块*/
void print(STUDENT *h)
{
     int i;              
    STUDENT *p;            
    p=h;                  
    if(p==NULL)
    {
        printf("\n    很遗憾,空表中没有任何记录可供显示!\n");
    }
    else
    {
        printf("********************************** STUDENT ****************************************\n");
        printf("  编号   账号位置         账号描述             账号名            密码             \n");
        printf("-----------------------------------------------------------------------------------\n");
        while(p!=NULL)
        {
            i++;
            printf("    %d %5s %5s %5s %ld \n",  p->no,p->address,p->score,p->name,p->order);
            p=p->next;
        }
        printf("***********************************************************************************\n\n");
    }
}


/*查找记录模块*/
void search(STUDENT *h)
{
    STUDENT *p;                     
    char s[15];                     
    printf("请输入您要查找的账号名:\n");
    scanf("%s",s);                  
    p=h;                           
    while(p!=NULL&&strcmp(p->name,s))
        p=p->next;                  
    if(p==NULL)                     
        printf("\n您要查找的是%s,很遗憾,查无此账号!\n",s);
    else                             
    {  
        printf("*********************************** Found *****************************************\n");
        printf("   编号   账号位置         账号描述             账号名            密码        \n");
        printf("-----------------------------------------------------------------------------------\n");
        printf("    %d %5s %5s %5s %ld \n",  p->no,p->address,p->score,p->name,p->order);
        printf("***********************************************************************************\n");
    }
}

/*在链表头部添加记录*/
STUDENT  *insert(STUDENT *h)
{  
    STUDENT *info;                  
    int n=0;
   
    printf("\n请添加新记录!\n");
    info=(STUDENT *)malloc(sizeof(STUDENT));
    if(!info)
    {  
        printf("\n内存分配失败!");
        return NULL;                       
    }
    printf("输入编号:");
    scanf("%d",info->no);  
    printf("输入账号位置:");
    scanf("%s",info->address);
    printf("输入账号名:\n",N);
    scanf("%s",info->name);
    printf("输入密码:\n");
    scanf("%d",info->order);
               
    info->next=NULL;         

    info->next=h;            
    h=info;                  

    printf("\n ----已经添加 %s 到链表头部!----\n",info->name);
    return(h);               
}

/*删除记录模块*/
STUDENT *delete1(STUDENT *h)
{
    char k[5];                    
    STUDENT *p,*q;               
    char s[10];                    
    printf("请输入要删除数据的账号名:\n");     
    scanf("%d",s);               
    q=p=h;                        
    {  
        q=p;                    
        p=p->next;               
    }
    if(p==NULL)                    
        printf("\n很遗憾,链表中没有您要删除数据的账号!\n",s);
    else                        
    {
        printf("*********************************** Found *****************************************\n");
        printf("   编号          账号位置       账号描述        账号名          密码         \n");
        printf("-----------------------------------------------------------------------------------\n");
        printf("    %d %5s %5s %5s %ld \n",  p->no,p->address,p->score,p->name,p->order);
        printf("***********************************************************************************\n");
        
        do{
            printf("您确实要删除此记录吗?(y/n):");
            scanf("%s",k);
        }while(k[0]!='y'&&k[0]!='n');
        
        if(k[0]!='n')               
        {
            if(p==h)               
                h=p->next;            
            else
                q->next=p->next;   
            free(p);               
            printf("\n 已经成功删除账号为%d的记录!\n",s);
        }
    }
    return(h);                        
}

/*保存数据到文件模块*/
void save(STUDENT *h)
{
   FILE *fp;                                      
   STUDENT *p;                                    
   char outfile[20];                             
   printf("请输入导出文件名,例如:d:\\xds\\score.txt:\n");
   scanf("%s",outfile);
   if((fp=fopen(outfile,"wb"))==NULL)            
   {
      printf("不能打开文件,文件保存失败!\n");
   }
   else
   {
       p=h;                                      
       while(p!=NULL)                             
       {
          fwrite(p,sizeof(STUDENT),1,fp);         
          p=p->next;                             
       }
       fclose(fp);                              
       printf("-----所有记录已经成功保存至文件%s中!-----\n",outfile);   
   }
}

/*导入信息模块*/
STUDENT *load()
{
   STUDENT *p,*q,*h=NULL;                        
   FILE *fp;                                      
   char infile[20];                              
   printf("请输入导入文件名,例如:d:\\xds\\score.txt:\n");
   scanf("%s",infile);                           
   if((fp=fopen(infile,"rb"))==NULL)            
   {  
        printf("文件打开失败!\n");              
        return h;
   }
   p=(STUDENT *)malloc(sizeof(STUDENT));         
   if(!p)
   {  
        printf("内存分配失败!\n");               
        return h;                                 
   }
   h=p;                                          
   while(!feof(fp))                              
   {
       if(1!=fread(p,sizeof(STUDENT),1,fp))
            break;                                 
        p->next=(STUDENT *)malloc(sizeof(STUDENT));
        if(!p->next)
        {
             printf("内存分配失败!\n");            
             return h;
        }
        q=p;                                       
        p=p->next;                                 
    }
    q->next=NULL;                                 
    fclose(fp);                                   
    printf("已经成功从文件%s导入数据!!!\n",infile);
    return h;                                    
}

/******主函数开始*******/
main()
{
    {
 char password[10]={"123456"};
 char key[40];
 int i=0;
 while(1)
 {
    printf("\n请输入密码\n");
    gets(key);
    if(strcmp(key,password)!=0)
         printf("\n密码错误!\n");
    else
        break;
   
    i++;
    if(i==3)
        exit(0);
 }
 printf("\n密码正确!\n");
}
    STUDENT *head = NULL;      
    system("color 5e");        
    for(;;)                        
    {
        switch(menu_select())   
        {
             case 1: head=create();break;        
             case 2: print(head);break;               
             case 3: search(head);break;        
             case 4: head=insert(head);break;   
             case 5: head=delete1(head);break;   
             case 6: save(head);break;           
             case 7: head=load();break;         
             case 8: exit(0);                  
        }
    }
}
搜索更多相关主题的帖子: 源代码 管理 
2010-06-24 18:44
huang6896
Rank: 1
等 级:新手上路
帖 子:8
专家分:9
注 册:2010-6-24
收藏
得分:0 
回复 2楼 suntea
不会吧?
我搞的时候可以运行啊
可是运行的时候输入数据就出现错误而跳出程序拉
2010-06-25 12:19
快速回复:【求助】编程问题,关于重要程序管理源代码
数据加载中...
 
   



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

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