| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3195 人关注过本帖
标题:课程设计:c语言通讯录(在linux下用c写的、有菜单的、用链表做的退出就会丢 ...
只看楼主 加入收藏
maoniu
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2010-4-7
结帖率:57.14%
收藏
 问题点数:0 回复次数:10 
课程设计:c语言通讯录(在linux下用c写的、有菜单的、用链表做的退出就会丢失数据) 链表、指针的练习
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"
                                                                                                                                               
void storage();                //建立用户记录表
void add();                    //添加用户记录函数
void del();                    //删除用户记录函数
void search();                 //查找用户记录函数
void change();                 //查找用户记录函数
void print();                  //打印用户记录函数
void printt();                 //显示主菜单函数
void yanshi();                 //延时函数
void help();                   //关于此系统介绍的函数
void quit1();                  //自动返回主菜单函数
void quit2();                  //按任意键返回主菜单函数
int m = 0;
struct people
{
        char *name;
        char *tell;
        struct people *next;
};
struct people *head;
                                                                                                                                               
int main()
{
        char s;
        int i = 0;
        printf("\033[2J");
        printt();
do
        {
        scanf("%c",&s);
        if(s == 'j')    storage();
        else if(s == 't')       add();
        else if(s == 's')       del();
        else if(s == 'g')       change();
        else if(s == 'c')       search();
        else if(s == 'd')       print();
        else if(s == 'h')       help();
        else if(s == 'q')       i = 1;
        else    printf("输入错误\n");
        }while(i == 0);
        printf("您已退出系统!\n");
                                                                                                                                               
}
                                                                                                                                               
void printt()
{
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("|        主菜单:          |\n");
        printf("|                          |\n");
        printf("|     建立用户记录:(j)     |\n");
        printf("|                          |\n");
        printf("|     添加用户记录:(t)     |\n");
        printf("|                          |\n");
        printf("|     查找用户记录:(c)     |\n");
        printf("|                          |\n");
        printf("|     删除用户记录:(s)     |\n");
        printf("|                          |\n");
        printf("|     更改用户记录:(g)     |\n");
        printf("|                          |\n");
        printf("|     查看所有用户记录:(d) |\n");
        printf("|                          |\n");
        printf("|     关于此系统:(h)       |\n");
        printf("|                          |\n");
        printf("|     退出系统:(q)         |\n");
        printf("|                          |\n");
        printf("============================\n");
}
                                                                                                                                               
                                                                                                                                               
void help()    //关于系统介绍
{
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("|此系统是在链表的基础上建立|\n");
        printf("|的,所以退出系统就会丢失所|\n");
        printf("|有数据,本系统只为练习c语 |\n");
        printf("|言指针、链表所编写。请大家|\n");
        printf("|多多提意见!               |\n");
        printf("|           制作者:jc.mima|\n");
        printf("|           QQ:594216070 |\n");
        printf("============================\n");
        printf("\n按任意键回到主菜单");
        quit2();
}
                                                                                                                                               
void storage()     //建立用户记录表
{
        int n,i;
        struct people *p,*p1,*p2;
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("|建立用户记录:             |\n\n");
        printf("要添加的用户数:");
        scanf("%d",&n);
        if(n < 100 && n > 0)
        {
                p = (struct people *)malloc(sizeof(struct people));
                p2 = (struct people *)malloc(sizeof(struct people));
                for(i = 0; i < n;i ++)
                {
                p1 = (struct people *)malloc(sizeof(struct people));
                if(i) ;
                else
                p = p1;
                printf("姓名:");
                p1->name = (char *)malloc(14);
                scanf("%s",p1->name);
                printf("%s 的电话号码:",p1->name);
                p1->tell = (char *)malloc(14);
                scanf("%s",p1->tell);
                p2->next = p1;
                p2 = p1;
                p1->next = NULL;
}
                  head = p;
        printf("添加成功  三秒后将自动回到主菜单");
        }
        else printf("添加未成功  三秒后将自动回到主菜单");
        printf("\n");
        quit1();
}
                                                                                                                                               
                                                                                                                                               
                                                                                                                                               
void add()                  //添加用户记录
{
        int i,n;
        struct people *p,*p1,*p2,*p3;
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 添加用户记录:            |\n\n");
        printf("要添加的用户数:");
        scanf("%d",&n);
        if(n > 0 && n < 100)
        {
                if(head == NULL)
                {
                        p = (struct people *)malloc(sizeof(struct people));
                        p1 = (struct people *)malloc(sizeof(struct people));
                         for(i = 0; i < n; i ++)
                        {
                                p2=(struct people *)malloc(sizeof(struct people));
                                if(i)   ;
                                else p = p2;
                                printf("请输入姓名:");
                                p2->name = (char *)malloc(14);
                                scanf("%s",p2->name);
                                printf("请输入号码:");
                                p2->tell = (char *)malloc(14);
                                scanf("%s",p2->tell);
                                p1 = p2;
                                p1->next = p2;
                                p2->next = NULL;
                        }
                        head = p;
                        printf("添加成功! 3秒后将自动回到主菜单");
                }
                 else
                {
                        p3 = (struct people *)malloc(sizeof(struct people));
                        p3 = head;
                        while(p3->next != NULL)
                        {
                                p3 = p3->next;
                        }
                         p = (struct people *)malloc(sizeof(struct people));
                         p2 = (struct people*)malloc(sizeof(struct people));
                         for(i = 0; i < n; i ++)
                                 {
                                p1=(struct people *)malloc(sizeof(struct people));
                                if(i)   ;
                                else    p = p1;
                                printf("请输入姓名:");
                                p1->name = (char *)malloc(14);
                                scanf("%s",p1->name);
                                printf("请输入号码:");
                                p1->tell = (char *)malloc(14);
                                scanf("%s",p1->tell);
                                p2->next = p1;
                                p2 = p1;
                                p1->next = NULL;
                        }
                                 p3->next = p;
                                 printf("添加成功! 3秒后将自动回到主菜单");
                }
        }
        else printf("输入错误 3秒后将自动回到主菜单");
        printf("\n");
        quit1();
}



void print()   //打印用户记录函数
{
        struct people *p;
        int i;
        p = head;
                if(head != NULL)
                {
                        printf("\033[2J");
                        printf("=============通讯录========\n");
                        printf("|                          |\n");
                        printf("|                          |\n");
                        printf("|  所有用户记录:           |\n");
                        do
                        {
                                printf("|  %-12s            |\n",p->name);
                                printf("|  %-12s            |\n",p->tell);
                                p = p->next;
                        }while(p != NULL);
                printf("|                          |\n");
                printf("|                          |\n");
                printf("============================\n");
                printf("\n按任意键回到主菜单");
                quit2();
                }
                else
                        {
                        printf("\033[2J");
                        printf("=============通讯录========\n");
                        printf("|                          |\n");
                        printf("| 没有用户记录!            |\n");
                        for(i = 0; i < 3; i ++)
                        printf("|                          |\n");
                        printf("============================\n");
                        printf("\n按任意键回到主菜单");
                        quit2();
                }
}


void del()          //删除用户记录函数
{
        char *a;
        void deln();
        void delt();
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 删除用户记录:            |\n\n");
        printf("按名字删除(n)\n按号码删除(其它):");
        a = (char *)malloc(sizeof(char));
        scanf("%s",a);
        if(*a == 'n')
                deln();
        else delt();
                                                                                                                                               
}
void deln()          //删除用户记录函数(按用户姓名)
{
        struct people *p1,*p2;
        char *name;
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("|删除用户记录(按用户姓名): |\n\n");
        printf("要删除用户的姓名:");
        name=(char *)malloc(14);
        scanf("%s",name);
        if(head != NULL)
                        {
                p1 = head;
                while(strcmp(name,p1->name) != 0 && p1->next != NULL)
                        {p2 = p1; p1 = p1->next;}
                if(strcmp(name,p1->name) == 0)
                        {
                        if(p1 == head)  head = p1->next;
                        else p2->next = p1->next;
                        printf("删除成功 三秒后将自动回到主菜单");
                        printf("\n");
                        quit1();
                        }
                else
                {
                        printf("没有记录 三秒后将自动回到主菜单\n");
                        quit1();
                }
                m --;
        }
        else
        {
                printf("没有记录 三秒后将自动回到主菜单\n");
                quit1();
        }
                                                                                                                                               
}
                                                                                                                                               
void delt()          //删除用户记录函数
{
        struct people *p1,*p2;
        char *tell;
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 删除用户记录(按号码):    |\n\n");
        printf("要删除的号码:");
        tell = (char *)malloc(14);
        scanf("%s",tell);
        if(head != NULL)
           {
                p1 = head;
                while(strcmp(tell,p1->tell) != 0 && p1->next != NULL)
                        {p2 = p1; p1 = p1->next;}
                if(strcmp(tell,p1->tell) == 0)
                        {
                        if(p1 == head)  head = p1->next;
                        else p2->next = p1->next;
                        printf("删除成功 三秒后将自动回到主菜单 ");
                        printf("\n");
                        quit1();
                        }
                else
                {
                        printf("没有记录 三秒后将自动回到主菜单\n");
                        quit1();
                }
        }
        else
        {
                printf("没有记录 三秒后将自动回到主菜单\n");
                quit1();
        }
                                                                                                                                               
}
void search()    //查找用户记录函数
{
                                                                                                                                               
        char *a;
        void searcht();
        void searchn();
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 查找用户记录:            |\n\n");
        printf("按名字名字查询(n)\n按电话号码查询(其它):");
        a=(char *)malloc(sizeof(char));
        scanf("%s",a);
        if(*a == 'n')
                searchn();
        else searcht();
                                                                                                                                               
}
                                                                                                                                               
                                                                                                                                               
                                                                                                                                               
void searchn()    //查找用户记录函数 (按姓名)
{
struct people *p1,*p2;
        char *name;
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 查找用户记录(按姓名):  |\n\n");
        printf("要查找的用户名:");
        name = (char *)malloc(14);
        scanf("%s",name);
        if(head!=NULL)
                        {
                p1=head;
                while(strcmp(name,p1->name)!=0&&p1->next!=NULL)
                        {p2=p1; p1=p1->next;}
                if(strcmp(name,p1->name)==0)
                {               printf("姓名:%s  电话号码:%s\n",p1->name,p1->tell);
                                printf("\n按任意键回到主菜单");
                                quit2();
                }
                else
                {
                        printf("未找到! 三秒后将自动回到主菜单");
                        quit1();
                }
        }
        else {printf("没有记录 三秒后将自动回到主菜单");
                 quit1();}
                                                                                                                                               
}
void searcht()    //查找用户记录函数 (按电话号码)
{
        struct people *p1,*p2;
        char *tell;
        void yanshi();
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("|查找用户记录(按电话号码): |\n\n");
        printf(" 电话号码:");
        tell=(char *)malloc(14);
        scanf("%s",tell);
        if(head!=NULL)
                        {
                p1=head;
                while(strcmp(tell,p1->tell)!=0&&p1->next!=NULL)
                        {p2=p1; p1=p1->next;}
                if(strcmp(tell,p1->tell)==0)
                {               printf("姓名:%s  电话号码:%s\n",p1->name,p1->tell);
                                printf("\n按任意键回到主菜单");
                                quit2();
                }
                else
                {
                        printf("未找到! 三秒后将自动回到主菜单");
                        quit1();
                }
        }
        else {printf("没有记录 三秒后将自动回到主菜单");
                 quit1();}
}
                                                                                                                                               
                                                                                                                                               
                                                                                                                                               
void change()        //更改用户记录函数
{
        char *a;
        void changet();
        void changen();
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 更改用户记录:            |\n\n");
        printf("更改用户姓名(n) 更改用户号码(其它)");
        a=(char *)malloc(sizeof(char));
        scanf("%s",a);
        if(*a=='n')
                changen();
        else changet();
}
void changet()    //更改用户记录函数(更改电话号码)
{
        struct people *p1,*p2;
        char *name,*tell,a;
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 更改用户记录(更改号码):  |\n\n");
        printf("要更改的用户名:");
        name=(char *)malloc(14);
        scanf("%s",name);
        if(head!=NULL)
                        {
                p1=head;
                while(strcmp(name,p1->name)!=0&&p1->next!=NULL)
                        {p2=p1; p1=p1->next;}
                if(strcmp(name,p1->name)==0)
                {               printf("姓名:%s  电话号码:%s\n",p1->name,p1->tell);
                                printf("电话号码改为:");
                                tell=(char *)malloc(14);
                                scanf("%s",tell);
                                p1->tell=tell;
                                printf("更改成功\n");
                                printf("按任意键回到主菜单");
                                quit2();
                }
                else
                {
                        printf("未找到! 三秒后将自动回到主菜单");
                        printf("\n");
                        quit1();
                }
        }
        else {printf("没有记录 三秒后将自动回到主菜单");
                printf("\n");
                 quit1();}
                                                                                                                                               
}
                                                                                                                                               
                                                                                                                                               
void changen()    //更改用户记录函数(更改姓名)
{
        struct people *p1,*p2;
        char *name,*tell,a;
        printf("\033[2J");
        printf("=========通讯系统===========\n");
        printf("|                          |\n");
        printf("| 更改用户记录(更改姓名):  |\n\n");
        printf("要更改的用户名:");
        tell=(char *)malloc(14);
        scanf("%s",tell);
        if(head!=NULL)
                        {
                p1=head;
                while(strcmp(tell,p1->tell)!=0&&p1->next!=NULL)
                        {p2=p1; p1=p1->next;}
                if(strcmp(tell,p1->tell)==0)
                {               printf("姓名:%s  电话号码:%s\n",p1->name,p1->tell);
                                printf("其姓名改为:");
                                name=(char *)malloc(14);
                                scanf("%s",name);
                                p1->name=name;
                                printf("更改成功\n");
                                printf("按任意键回到主菜单");
                                quit2();
                }
                else
                {
                        printf("未找到! 三秒后将自动回到主菜单");
                        printf("\n");
                        quit1();
}
        }
        else {printf("没有记录 三秒后将自动回到主菜单");
                printf("\n");
                 quit1();
                }
                                                                                                                                               
}
                                                                                                                                               
                                                                                                                                               
                                                                                                                                               
                                                                                                                                               
void quit1()
{
        getchar();
        sleep(3);
        printf("\n");
        printf("\033[2J");
        printt();
}
                                                                                                                                               
void quit2()
{
        getchar();
        getchar();
        printf("\n");
        printf("\033[2J");
        printt();
}
搜索更多相关主题的帖子: 链表 c语言 通讯录 指针 课程 
2010-06-03 22:41
maoniu
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2010-4-7
收藏
得分:0 
void  yanshi();  没用 linux下直接可以用sleep();
2010-06-03 22:42
a13412206216
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-6-11
收藏
得分:0 
为什么在TC  或VC++ 下 运行不了    有什么问题?
2010-06-12 00:34
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
很不怎么样。
2010-06-12 22:12
goyoyo315
Rank: 1
等 级:新手上路
帖 子:12
专家分:3
注 册:2006-5-25
收藏
得分:0 
可以考虑用文件把结果保存起来,然后运行程序时,就打开这个文件,读取数据到内存,退出时,再把数据写入文件。这样子就不会丢失数据了。
不然,别人辛苦录入的数据,一退出就丢失。不合理的。
2010-06-13 15:14
maoniu
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2010-4-7
收藏
得分:0 
回复 3楼 a13412206216
可以的  只要把linux下的清频函数printf("\033[2J");改成system("cls");就可以的
2010-06-19 19:09
maoniu
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2010-4-7
收藏
得分:0 
回复 5楼 goyoyo315
就是 有待改进!
2010-06-19 19:10
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
可以考虑用文件把结果保存起来,然后运行程序时,就打开这个文件,读取数据到内存,退出时,再把数据写入文件。这样子就不会丢失数据了。
不然,别人辛苦录入的数据,一退出就丢失。不合理的。
//
这个建议不错,实现起来也不难,将文件分块处理就行了。

我就是真命天子,顺我者生,逆我者死!
2010-06-19 19:29
楼主
Rank: 1
等 级:新手上路
帖 子:21
专家分:3
注 册:2010-6-18
收藏
得分:0 
为什么复制到Win-TC运行不了?????
2010-06-19 19:46
楼主
Rank: 1
等 级:新手上路
帖 子:21
专家分:3
注 册:2010-6-18
收藏
得分:0 
void add();                    //添加用户记录函数
-------
这句有问题???
2010-06-19 19:48
快速回复:课程设计:c语言通讯录(在linux下用c写的、有菜单的、用链表做的退出 ...
数据加载中...
 
   



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

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