| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 322 人关注过本帖
标题:我解决了一天的文件问题 还没解决 真求了!
只看楼主 加入收藏
GUnever
Rank: 2
等 级:论坛游民
帖 子:59
专家分:25
注 册:2012-3-3
结帖率:95.24%
收藏
已结贴  问题点数:30 回复次数:3 
我解决了一天的文件问题 还没解决 真求了!
#include
#define len sizeof(Linklist)
typedef    struct worker
    {
        int num;
        char name[20];
        char sex;
        int age;
        char tele[12];
        char xueli[20];
        int salary;
        struct worker*next;
    }Linklist;
void main()
    {
        Linklist*creat();       /**********创建链表************/   
        void menu();             /**********菜单界面************/   
        void show();               /**********输出全部职工信息************/   
        Linklist* insert();             /**********添加职工************/   
        void  revise();                 /**********修改职工的信息************/   
        void ccode();             /***********修改密码***********/   
        
        menu();
    }
void menu()   
{
        int choose,i;               
        static flag=0,flag1=0;
        char *p1;
        int *p2;            /*密码  和 标志  都改变其值*/
        char code[10],change[]="520",ch;
        Linklist *head;   
        p1=change;
        p2=&flag;
Start:                                                                      /*******这里的 goto语句 为了让密码 不在重置******/
        printf("                        Workers Information Query System\nPlease give the code to enter the System:");
        for(;flag==0;)
    {
        for(i=0;(ch=getch())!='\r'&&i<10;i++)                /*********职工进入密码的一段代码***********/
        {
            code[i]=ch;
            printf("*");
        }
        code[i]='\0';                                     /*  记住在这里的后面加上一个结束的标识符*/
        if(strcmp(code,p1)==0)                    /*这里用不用指针都没有关系*/
        {
            flag=1;
        }
        else
            printf("\nSorry! the code you give is wrong,please re-enter it as confirmation:\n");
    }
    if(flag1==0)            /********这里是创建一个链表 每次从文件里面取出数据进行创建,每次运行程序的时候只能创建一次  所有标志(尾插法创建链表)**********/
    {
        head=creat();
        flag1++;
    }
        system("cls");
        printf("                          Workers Information Query System\nPlease choose the survice as follow:\n  1.Information for all workers\n  2.Workers search\n  3.Change the information of workers\n");
        printf("  4.Adding workers\n  5.Password reset\n  6.Dismiss workers\n  7 End");
        scanf("%d",&choose);
        switch(choose)
    {
        case 1:show(head);break;    /*************输出全部职工的信息 用链表的方法***************/
        case 3:revise(head);
                break;        /*****************用链表 修改职工里面的信息******************/
        case 4:head=insert(head);            /********这里是添加职工信息 用的还是链表的插入法************/
                system("pause");
                system("cls");
                menu();
                break;
        case 5:ccode(p1,p2);goto Start;            /*********修改密码的一个函数**********/
        


    }
}
void show(Linklist*head)
{
    Linklist*p1;
    p1=head;
    system("cls");
    printf(" num  name       sex   age");
    for(;p1!=NULL;)
    {
        printf("\n%2d    %-11s% 2c%7d",p1->num,p1->name,p1->sex,p1->age);    /**********输出整个链表************/   
        p1=p1->next;
    }
    system("pause");
    system("cls");
    menu();

}
void revise(Linklist*head)      /*现在只是执行了 找一次的程序*/
{
    Linklist*p1;
    int num,i,choose,age;
    char sex,name[10];
    FILE*fp;
    if((fp=fopen("d:\z.txt","w"))==NULL)
    {
        printf("open error");
        exit(1);
    }
    p1=head;
    system("cls");
    printf("Please choose which ways you wanna seek workers to revise their information?\n1.num  2.name");      /***********这里按照名字和数字的方法查找修改***********/   
    scanf("%d",&choose);
    switch(choose)                /*这里没有选择的bug也要进行修改, 第 2次进行 选择修改的 时候指针的志向要进行修改一下 */
    {
        case 1:printf("\nPlease input the num :");       /***********按照数字的方法查找***********/   
                scanf("%d",&num);
                for(;p1!=NULL;p1=p1->next)
                {
                    if(p1->num==num)
                    {
                        printf("\nTHE original information of the worker:");
                        printf("\n%2d    %-11s% 2c%7d",p1->num,p1->name,p1->sex,p1->age);
                        printf("\nwhich do you wanna to revise?\n 1.name 2.age 3.sex\n");       /**********修改的哪一个方面************/   
                        scanf("%d",&choose);
                        switch(choose)
                        {
                            case 1:printf("Please give the new name:");          /***********把修改的数据再存入次链表当中***********/   
                            scanf("%s",name);
                            strcpy(p1->name,name);
                            break;
                            case 2:printf("Please give the new age:");
                            scanf("%d",&age);
                            p1->age=age;
                            break;
                            case 3:printf("Please give the new sex:");
                            fflush(stdin);
                            scanf("%c",&sex);
                            p1->sex=sex;
                            break;
                        }
                        break;  /**********找到后可以直接跳出了***********/
                    }
                }
                break;
        case 2:    printf("Please input the worker's name :");         /**********按照名字的方式查找并且修改************/   
                    scanf("%s",name);
                    for(;p1!=NULL;p1=p1->next)
                    {
                        if(strcmp(p1->name,name)==0)
                        {
                            printf("\nTHE original information of the worker:");
                            printf("\n%2d    %-11s% 2c%7d",p1->num,p1->name,p1->sex,p1->age);
                            printf("\nwhich do you wanna to revise?\n 1.name 2.age 3.sex\n");
                            scanf("(%d)",&choose);
                            switch(choose)
                            {
                                case 1:printf("Please give the new name:");
                                scanf("%s",name);
                                strcpy(p1->name,name);
                                break;
                                case 2:printf("Please give the new age:");
                                scanf("%d",&age);
                                p1->age=age;
                                break;
                                case 3:printf("Please give the new sex:");
                                fflush(stdin);
                                scanf("%c",&sex);
                                p1->sex=sex;
                                break;
                            }
                        }
                    }
    }
    for(p1=head;p1!=NULL;p1=p1->next)         /****** 将改变后的链表存入文件********/
    {
        if(fwrite(p1,sizeof(Linklist),1,fp)!=1)
        {
        printf("eqweq");
         exit(1);
        }
    }
    fclose(fp);
    system("pause");
    system("cls");
    menu();   
    printf("Succee!\n");
}

 Linklist*insert(Linklist*head)
{
    FILE *fp;
    Linklist*p1,*p2,*p3;            /*p3进行创建一个新的节点*/
    int k,n,flag,num,age,salary;
    char tele[12],name[10],sex,xueli[10];
    system("cls");
    if((fp=fopen("d:\z.txt","w"))==NULL)
    {
        printf("open error");
        exit(1);
    }
    printf("             Welcome to the adding system\n How many workers do you wanna add in?\n");
    p1=p2=head;
    p1=p1->next;
    scanf("%d",&n);
    for(k=0;knum&&num>p2->num)           /***********找到了刚好在中间的位置***********/   
            {
                flag=1;
                break;
            }
            if(p1==NULL)         /***********并没有找到中间的位置 输入的编号直接按照大小直接放在最后面***********/   
            {
                flag=2;
                break;
            }
            if(num==p2->num||num==p2->num)                         /**********这里是输入了相同的编号 指针初始化 要求重新输入 在进行插入*********/
            {
                printf("the num has been register,Please input another one:");
                scanf("%d",&num);
                p1=p2=head;
                p1=p1->next;
            }
        }
        p3=(Linklist*)malloc(len);            /*********存入插入的节点数据**********/
        p3->num=num;
        
        printf("name:");
        scanf("%s",name);
        strcpy(p3->name,name);
        
        printf("\nage:");
        scanf("%d",&age);
        p3->age=age;
        getchar();
        
        printf("\nsex:");
        scanf("%c",&sex);
        p3->sex=sex;
        
        printf("\ntele:");
        scanf("%s",tele);
        strcpy("p3->tele.tele");
        
        printf("\nxueli:");
        scanf("%s",xueli);
        strcpy(p3->xueli,xueli);
        
        printf("\nsalary:");
        scanf("%d",&salary);
        p3->salary=salary;
        if(flag==1)                   /**********中间位置插入*********/
        {
            p2->next=p3;
            p3->next=p1;
        }
        if(flag==2)              /*********末尾插入**********/
        {
            p2->next=p3;
            p3->next=NULL;
        }
        p1=p2=head;
        p1=p1->next;
    }
    for(p1=head;p1!=NULL;p1=p1->next)         /****** 将改变后的链表存入文件********/
    {
        if(fwrite(p1,sizeof(Linklist),1,fp)!=1)
        {
        printf("eqweq");
         exit(1);
        }
    }
        fclose(fp);
        return head;
}



void ccode(char *p1,int *p2)
{
    char old[10], new[10];
    printf("Please input the old password:");           /*********修改密码系统比较简单**********/
    /*gets(old);*/
    scanf("%s",old);
    if(strcmp(old,p1)!=0)
    {
        menu();                    /*如果输入不正确 就要返回菜单 这个设置 应该在完善一下*/
    }
    else
    {
        printf("Please input the new password:");
        /*gets(new);*/
        scanf("%s",new);
        strcpy(p1,new);
        *p2=0;
    }
    printf("Succee!\n");
    system("pause");
    system("cls");

}

Linklist*creat()
{
    Linklist*head,*p1,*p2;
    FILE*fp;
    if((fp=fopen("d:\z.txt","r"))==NULL)
    {
        printf("open error");
        exit(1);
    }
    p1=p2=(Linklist*)malloc(len);
    p1->num=0;
    strcpy(p1->name,"Edward");
    p1->sex='M';
    p1->age=19;
    strcpy(p1->tele,"83355367");
    strcpy(p1->xueli,"daxue");
    p1->salary=10;
    head=p1;
    p1=(Linklist*)malloc(len);
    p2->next=p1;
    p2=p1;
    p2->num=1;
    strcpy(p1->name,"peter");
    p1->sex='M';
    p1->age=19;
    strcpy(p1->tele,"83312467");
    strcpy(p1->xueli,"daxue");
    p2->salary=10;
    p1=(Linklist*)malloc(len);
    while(fread(p1,sizeof(Linklist),1,fp)==1)           /*********再从文件读入数据进行插入链表  现在文件暂时为空**********/
    {
        p2->next=p1;
        p2=p1;
        p1=(Linklist*)malloc(len);
    }
    p2->next=NULL;
    free(p1);
    fclose(fp);    /******最后还开辟了一个空间但是没有使用*******/
    return head;
}


   
   
        
        这段程序是在是TC下写的(文件暂时什么东西都没)
   关键问题在于修改函数。每次修改了数据以后 在执行了menu();以后就自动把我head释放了 太奇怪了 再修改函数还没有增加文件之前 都是正常的
  跪求啊 解决了一天了! 30分
搜索更多相关主题的帖子: void 问题 include 
2012-04-14 21:24
love24114
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:223
专家分:399
注 册:2011-7-11
收藏
得分:30 
用stl,C实现数据结构这个太烦。
2012-04-14 21:36
GUnever
Rank: 2
等 级:论坛游民
帖 子:59
专家分:25
注 册:2012-3-3
收藏
得分:0 
回复 2楼 love24114
还是没解决问题啊!
2012-04-14 21:43
love24114
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:223
专家分:399
注 册:2011-7-11
收藏
得分:0 
给下要求,我写份代码给你
2012-04-14 21:45
快速回复:我解决了一天的文件问题 还没解决 真求了!
数据加载中...
 
   



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

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