| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 774 人关注过本帖
标题:论坛有人么,能帮我看看怎么保存动态链表的问题吗谢谢
只看楼主 加入收藏
强壮的鬼
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-6-25
收藏
得分:0 
回复 9楼 菜鸟0
这。。。。。。谢谢同学。。世界真小。。
这是我现在修改后的,问题是输入没有的教师号之后程序报错,能帮我看看么。。。


#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define LEN sizeof(struct teacher)
/*         定义结构体           */
struct teacher
{
    int num;
    char name[10];
    char gender[8];
    char work[100];
    char address[100];
    char tel[10];
    float jiben;
    float jintie;
    float butie;
    float yingfa;
    float huafei;
    float shuidian;
    float fangzu;
    float shuishou;
    float weisheng;
    float gongji;
    float koukuan;
    float shifa;
    struct teacher *next;
};
typedef struct teacher AA;
FILE *fp;

//***************************************************************************************************
//***************************************************************************************************
/*  WELCOME               */
void welcome()
{
    printf("---------------------------------------\n\n");
    printf("\t2012年大一c语言课程设计\n\n\n");
    printf("\t教师工资管理系统\n\n\n");
    printf("学生:巫家竞\t 2011级电气1103班 \n\n");
    printf("---------------------------------------\n\n");
    getch();
}

//***************************************************************************************************
//***************************************************************************************************
/*       菜单             */
void menu()
{
printf("教师工资管理系统\n");
printf("1.输入教师信息\n");
printf("2.修改教师信息\n");
printf("3.删除教师信息\n");
printf("4.浏览教师信息\n");
printf("5.保存数据\n");
printf("6.退出\n");
printf("请选择:\n");
}

//***************************************************************************************
//***************************************************************************************
/*       输入函数             */
AA *input()
{
    int n;
    AA *r,*s,*h;
    h=(AA *)malloc(LEN);
    r=h;
    printf("请输入教师号:");
    scanf("%d",&n);
    while(n!=0)
    {
        s=(AA *)malloc(LEN);
        s->num=n;
        fflush(stdin);
        printf("姓名:");
        gets(s->name);
        printf("性别:");
        gets(s->gender);
        printf("单位名称:");
        gets(s->work);
        printf("家庭住址:");
        gets(s->address);
        printf("联系电话:");
        gets(s->tel);
        printf("基本工资:");
        scanf("%f",&s->jiben);
        printf("津贴:");
        scanf("%f",&s->jintie);
        printf("生活补贴:");
        scanf("%f",&s->butie);
        printf("电话费:");
        scanf("%f",&s->huafei);
        printf("水电费:");
        scanf("%f",&s->shuidian);
        printf("房租:");
        scanf("%f",&s->fangzu);
        printf("所得税:");
        scanf("%f",&s->shuishou);
        printf("卫生费:");
        scanf("%f",&s->weisheng);
        printf("公积金:");
        scanf("%f",&s->gongji);
        s->yingfa=s->jiben+s->jintie+s->butie;
        s->koukuan=s->huafei+s->shuidian+s->fangzu+s->shuishou+s->weisheng+s->gongji;
        s->shifa=s->yingfa-s->koukuan;
        r->next=s;
        r=s;
        printf("请输入下一个教师号:(按0取消输入)");
        scanf("%d",&n);
        }
    r->next='\0';
    return h;
}

//***************************************************************************************
//***************************************************************************************
/*                   修改函数                    */
void modify(AA *h)
{
int x,n;
AA *p,*q;
printf("输入要修改的教师号:");
scanf("%d",&x);
p=h;
q=h->next;
    while(q!='\0'&&q->num!=x)
    {
        p=q;q=q->next;
    }
    if(q->num==x)
    {printf("该教师信息为:\n");
     printf("教师号:%5d\t\t",q->num);
     printf("教师姓名:%-10s\n",q->name);
      printf("教师性别:%-6s\t\t",q->gender);
     printf("单位名称:%-10s\n",q->work);
     printf("家庭地址:%-10s\t",q->address);
     printf("电话号码:%8s\n",q->tel);
     printf("基本工资:%5.0f\t\t",q->jiben);
     printf("津贴:%5.0f\n",q->jintie);
     printf("生活补贴:%5.0f\t",q->butie);
     printf("\t电话费:%5.0f\n",q->huafei);
     printf("个人所得税:%5.0f\t",q->shuishou);
     printf("卫生费:%5.0f\n",q->weisheng);
     printf("水电费:%5.0f\t\t",q->shuidian);
     printf("房租:%5.0f\n",q->fangzu);
     printf("公积金:%5.0f\t\t",q->gongji);
     printf("应发工资:%5.0f\n",q->yingfa);
     printf("合计扣款:%5.0f\t\t",q->koukuan);
     printf("实发工资:%5.0f\n\n\n",q->shifa);
     printf("确认修改请按\"y\",取消请按\"n\"\n");
     n=getch();
     }
    if(n=='n'||n=='N')
    menu();
    if(n=='y'||n=='Y')
    {
        fflush(stdin);
        printf("请输入新的教师姓名:");
        gets(q->name);
        printf("性别:");
        gets(q->gender);
        printf("单位名称:");
        gets(q->work);
        printf("家庭住址:");
        gets(q->address);
        printf("联系电话:");
        gets(q->tel);
        printf("基本工资:");
        scanf("%f",&q->jiben);
        printf("津贴:");
        scanf("%f",&q->jintie);
        printf("生活补贴:");
        scanf("%f",&q->butie);
        printf("电话费:");
        scanf("%f",&q->huafei);
        printf("水电费:");
        scanf("%f",&q->shuidian);
        printf("房租:");
        scanf("%f",&q->fangzu);
        printf("所得税:");
        scanf("%f",&q->shuishou);
        printf("卫生费:");
        scanf("%f",&q->weisheng);
        printf("公积金:");
        scanf("%f",&q->gongji);
        q->yingfa=q->jiben+q->jintie+q->butie;
        q->koukuan=q->huafei+q->shuidian+q->fangzu+q->shuishou+q->weisheng+q->gongji;
        q->shifa=q->yingfa-q->koukuan;
    }
    printf("修改完毕,按任意键返回");
    getch();
}
//***************************************************************************************
//***************************************************************************************
/*       浏览             */
void display(AA *h)
{
    AA *p;
    p=h->next;
    if(p=='\0')
    {
        printf("没有教师信息,按任意键继续\n");
        getch();
    }
    else
    {
    do{
printf("教师号:%5d\t\t",p->num);
printf("教师姓名:%-10s\n",p->name);
printf("教师性别:%-6s\t\t",p->gender);
printf("单位名称:%-10s\n",p->work);
printf("家庭地址:%-10s\t",p->address);
printf("电话号码:%8s\n",p->tel);
printf("基本工资:%5.0f\t\t",p->jiben);
printf("津贴:%5.0f\n",p->jintie);
printf("生活补贴:%5.0f\t",p->butie);
printf("\t电话费:%5.0f\n",p->huafei);
printf("个人所得税:%5.0f\t",p->shuishou);
printf("卫生费:%5.0f\n",p->weisheng);
printf("水电费:%5.0f\t\t",p->shuidian);
printf("房租:%5.0f\n",p->fangzu);
printf("公积金:%5.0f\t\t",p->gongji);
printf("应发工资:%5.0f\n",p->yingfa);
printf("合计扣款:%5.0f\t\t",p->koukuan);
printf("实发工资:%5.0f\n\n\n",p->shifa);
    p=p->next;   
    }while(p!='\0');
    printf("\n 输入任意键返回\n");
    getch();
    }
}
//***************************************************************************************
//***************************************************************************************
/*         删除函数           */
void delete_info(AA *h)
{
int x;
AA *p,*q;
printf("输入删除的教师号:");
scanf("%d",&x);
p=h;
q=h->next;
    while(q!='\0'&&q->num!=x)
    {
        p=q;q=q->next;
    }
    if(q->num==x)
    {p->next=q->next;
    printf("已删除该教师信息\n按任意键返回");
    getch();
     }
    else
    {
     printf("没有此教师号\n按任意键返回");
     getch();
    }
}

//***************************************************************************************************
//***************************************************************************************************
/*              保存函数              */
AA save(AA *p)
{
    if((fp=fopen("teacher_info.txt","w"))==NULL)
     {
      printf("\n无法打开文件");
      exit (1);
     }
     p=p->next;
     while(p!='\0')
     {  
         fprintf(fp,"教师号:%d\t\t",p->num);
        fprintf(fp,"姓名:%s\n",p->name);
        fprintf(fp,"性别:%s\t\t",p->gender);
        fprintf(fp,"工作单位:%s\n",p->work);
        fprintf(fp,"地址:%s\t\t",p->address);
        fprintf(fp,"电话号码:%s\n",p->tel);
        fprintf(fp,"基本工资:%.1f\t\t",p->jiben);
        fprintf(fp,"津贴:%.1f\n",p->jintie);
        fprintf(fp,"补贴:%.1f\t\t",p->butie);
        fprintf(fp,"应发工资:%.1f\n",p->yingfa);
        fprintf(fp,"话费:%.1f\t\t",p->huafei);
        fprintf(fp,"水电费:%.1f\n",p->shuidian);
        fprintf(fp,"房租:%.1f\t\t",p->fangzu);
        fprintf(fp,"税收:%.1f\n",p->shuishou);
         fprintf(fp,"公积金:%.1f\t\t",p->gongji);
         fprintf(fp,"卫生费:%.1f\n",p->weisheng);
          fprintf(fp,"合计扣款:%.1f\t\t",p->koukuan);
         fprintf(fp,"实发工资:%.1f\n\n",p->shifa);
        p=p->next;   
    };
    printf("\n 保存成功,输入任意键返回\n");
    getch();
}
//***************************************************************************************
//***************************************************************************************
/*       主函数               */
main()
{
    int choice;
    AA *h;
    h->next='\0';
    welcome();
    while(1)
    {
    system("CLS");
    menu();
    scanf("%d",&choice);
    switch(choice)
        {
        case 1:h=input();break;
        case 2:modify(h);break;
        case 3:delete_info(h);break;
        case 4:display(h);break;
        case 5:save(h);break;
        case 6:exit(1);break;
        default:printf("请输入数字1-6,按任意键重新输入\n\n");getch();break;
        }
    }
}
2012-06-26 22:35
快速回复:论坛有人么,能帮我看看怎么保存动态链表的问题吗谢谢
数据加载中...
 
   



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

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