标题:[原创]管理联络方式的小系统
只看楼主
summoner
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1622
专家分:0
注 册:2005-3-3
 问题点数:0 回复次数:9 
[原创]管理联络方式的小系统

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct linkinfo)

struct linkinfo
{
struct linkinfo *first;
char name[20];
char place[128];
char birthday[11];
char telephone[16];
struct linkinfo *next;
};
int datacount = 0,sortname,sorttype;
struct linkinfo *p_main,*p_first=NULL,*p_tail = NULL;
int checkempty(char temp[])
{
int flag = 0,i;
if(strlen(temp) == 0)
flag = 1;
for(i=0;i<strlen(temp);i++)
{
if((int)temp[i] == 32 || (int)temp[i] == 129)
{
flag = 1;
}
else
flag = 0;
}
return(flag);
}
struct linkinfo *create(int number)
{
struct linkinfo *new;
int i,flag = 0;
char temp[20];
for(i=0; i<number; i++)
{
new = (struct linkinfo *)malloc(LEN);
fflush(stdin);
printf("Please input name:");
gets(new->name);
strcpy(temp,new->name);
flag = checkempty(temp);
if(flag == 1)
{
free(new);
break;
}

printf("Please input place:");
gets(new->place);
printf("please input birthday:");
gets(new->birthday);
printf("Please input telephone:");
gets(new->telephone);
new->next = NULL;
new->first = NULL;
if(p_first == NULL)
{
p_first = new;
}
else
{
new->first = p_tail;
p_tail->next = new;
}
p_tail = new;
datacount++;
}
}
void showtopmenu()
{
fflush(stdin);
printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- Menu -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**\n");
printf("1 add *\n");
printf("2 update *\n");
printf("3 delete *\n");
printf("4 showall *\n");
printf("5 showone *\n");
printf("6 sort *\n");
printf("------------------------------------------------------------------------*\n");
printf("7 read data from file *\n");
printf("8 write data to file *\n");
printf("------------------------------------------------------------------------*\n");
printf("0 exit *\n");
printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
printf("Please choice a command>>");
}
void process_add()
{
create(1);
showtopmenu();
}
void process_update()
{
int i = 1,index,flag;
char temp[20];
printf("Please input the data index that you want to update:");
fflush(stdin);
scanf("%d",&index);
p_main = p_first;
if(index > datacount || index < 1)
printf("----------------No Data-----------------\n");
else
{
while(p_main != NULL)
{
if(i == index)
{
printf("----------------------------\n");
printf("Number%d's name:\t%s\n",index,p_main->name);
printf("Number%d's place:\t%s\n",index,p_main->place);
printf("Number%d's birthday:\t%s\n",index,p_main->birthday);
printf("Number%d's telephone:\t%s\n",index,p_main->telephone);
fflush(stdin);
printf("Please input new name:");
gets(temp);
flag = checkempty(temp);
if(flag == 1)
{
printf("New name input error!\n");
break;
}
else
strcpy(p_main->name,temp);
printf("Please input new place:");
gets(p_main->place);
printf("Please input new birthday:");
gets(p_main->birthday);
printf("Please input new telephone:");
gets(p_main->telephone);
break;
}
else
{
i++;
p_main = p_main->next;
}
}
}
showtopmenu();
}
void process_delete()
{
int i = 1,index;
printf("Please input the data index that you want to delete:");
scanf("%d",&index);
p_main = p_first;
if(index <= datacount && index > 0)
{
while(p_main != NULL)
{
if(index == 1)
{
if(p_main->next == NULL)
{
datacount = 0;
p_first = NULL;
p_tail = NULL;
}
else
{
p_first = p_main->next;
datacount--;
}
break;
}
else if(i == index-1)
{
if(p_main->next->next != NULL)
{
p_main->next->next->first = p_main;
}
p_main->next = p_main->next->next;
if(index == datacount)
p_tail = p_main;
datacount--;
break;
}
else
{
i++;
p_main = p_main->next;
}
}
printf("Data %d deleted!\n",index);
}
else
printf("----------------No Data-----------------\n");
showtopmenu();
}
void process_showall()
{
int i = 0;
p_main = p_first;
if(p_main ==NULL)
printf("----------------No Data-----------------\n");
else
printf("%2s %20s %20s %10s %16s\n\n","No","Name","Place","Birthday","Telephone");

while(p_main != NULL)
{
printf("%2d %20s %20s %10s %16s\n",i+1,p_main->name,p_main->place,p_main->birthday,p_main->telephone);
i++;
p_main = p_main->next;
}
showtopmenu();
}
void process_showone()
{
int i = 1,index;
printf("Please input the data index that you want to show:");
scanf("%d",&index);
p_main = p_first;
while(p_main != NULL)
{
if(i == index)
{
printf("----------------------------\n");
printf("Number%d's name:\t\t%s\n",i,p_main->name);
printf("Number%d's place:\t%s\n",i,p_main->place);
printf("Number%d's birthday:\t%s\n",i,p_main->birthday);
printf("Number%d's telephone:\t%s\n",i,p_main->telephone);
break;
}
else
{
i++;
p_main = p_main->next;
}
}
if(index > datacount || index < 1)
printf("----------------No Data-----------------\n");
showtopmenu();
}
void subprocess_exchange(int j)
{
struct linkinfo *temp = NULL,*p_temp = NULL;
p_temp = p_main->next;
if(j==0)
p_first = p_main->next;
temp->first = p_main->first;
temp->next = p_main->next;
if(p_main->first != NULL)
p_main->first->next = p_main->next;
if(p_main->next->next != NULL)
p_main->next->next->first = p_main;
p_main->first = p_main->next;
p_main->next = p_main->next->next;
p_temp->first = temp->first;
p_temp->next = p_main;
}
int bigthan(char *string1,char *string2)
{
int i,len,result=0;
len = strlen(string1);
for(i=0;i<len;i++)
{
if(string1[i]>string2[i])
{
return(1);
}
else if(string1[i]<string2[i])
{
return(0);
}
}
return(result);
}
void subprocess_sort(int sortname,int sorttype)
{
int i,j;
p_main = p_first;
for(i=0; i<datacount; i++)
{
p_main = p_first;
for(j=0; j<datacount-i-1; j++)
{
if(sortname == 1 && sorttype ==1)
{
if(bigthan(p_main->name,p_main->next->name))
{
subprocess_exchange(j);
}
else
p_main = p_main->next;
}
else if(sortname == 1 && sorttype == 2)
{
if(!bigthan(p_main->name,p_main->next->name))
{
subprocess_exchange(j);
}
else
p_main = p_main->next;
}
else if(sortname == 2 && sorttype == 1)
{
if(bigthan(p_main->birthday,p_main->next->birthday))
{
subprocess_exchange(j);
}
else
p_main = p_main->next;
}
else if(sortname == 2 && sorttype == 2)
{
if(!bigthan(p_main->birthday,p_main->next->birthday))
{
subprocess_exchange(j);
}
else
p_main = p_main->next;
}
}
if(j == datacount-1)
p_tail = p_main;
}
printf("Sort Completed!\n");
}
void process_sort()
{
char confirmflag;
sortname = 1;
sorttype = 1;
printf("***sort choice***\n");
printf("please select sort's name.\n");
printf("1:name 2:birthday\n>");
scanf("%d",&sortname);
printf("Please select sort's position\n");
printf("1:↓ 2:↑\n");
scanf("%d",&sorttype);
printf("***Please check***\n");
if(sortname==1)
{
if(sorttype==1)
{
printf("name ↓\n");
}
else if(sorttype == 2)
{
printf("name ↑\n");
}
else
{
printf("sort's position choice error!\n");
showtopmenu();
return;
}
}
else if(sortname==2)
{
if(sorttype == 1)
{
printf("birthday ↓\n");
}
else if(sorttype == 2)
{
printf("birthday↑\n");
}
else
{
printf("sort's position choice error!\n");
showtopmenu();
return;
}
}
else
{
printf("sort's name choice error!!\n");
showtopmenu();
return;
}
fflush(stdin);
printf("Are you sure(y/n)");
scanf("%c",&confirmflag);
if(confirmflag =='y' || confirmflag =='Y')
{
subprocess_sort(sortname,sorttype);
}
else
{
sortname = 0;
sorttype = 0;
}
showtopmenu();
}
void process_readfile()
{
FILE *fr;
int i,dataindex = 0;
char out,temp[LEN],*p_data,*exchangechar = ",";
struct linkinfo *new;
fr = fopen("01-2-5.csv","r");
if(fread == NULL)
{
printf("Openfile Error!\n");
}
else
{
p_first = NULL;
p_tail = NULL;
datacount = 0;
while(!feof(fr))
{
dataindex = 0;
out = getc(fr);
if(out == EOF)
break;
else
fseek(fr,-1,1);
fgets(temp,LEN+4,fr);
for(i=0; i<strlen(temp); i++)
{
if(temp[i] == '\n')
temp[i] = '';
}
new = (struct linkinfo *)malloc(LEN);
p_data = strtok(temp,exchangechar);
while(p_data)
{
switch(dataindex)
{
case 0: strcpy(new->name,p_data);break;
case 1: strcpy(new->place,p_data);break;
case 2: strcpy(new->birthday,p_data);break;
case 3: strcpy(new->telephone,p_data);break;
}
dataindex++;
p_data = strtok(NULL,exchangechar);
}
new->next = NULL;
if(p_first == NULL)
{
p_first = new;
}
else
{
p_tail->next = new;
}
p_tail = new;
datacount++;
}
fclose(fr);
}
printf("%d Data geted!\n",datacount);
showtopmenu();
}
void process_writefile()
{
FILE *fr;
fr = fopen("01-2-5.csv","w");
if(fread == NULL)
{
printf("Openfile Error!\n");
}
else
{
p_main = p_first;
while(p_main != NULL)
{
fputs(p_main->name,fr);
fputc(',',fr);
fputs(p_main->place,fr);
fputc(',',fr);
fputs(p_main->birthday,fr);
fputc(',',fr);
fputs(p_main->telephone,fr);
fputc('\n',fr);
p_main = p_main->next;
}
fclose(fr);
printf("wirte in sucess!\n");
}
showtopmenu();
}
void process_exit()
{
char check;
printf("Are you sure to exit?(Y/N):");
check = getchar();
if(check == 'y' || check == 'Y')
exit(0);
else
showtopmenu();
}

main()
{
int choice;
showtopmenu();
while(1)
{
fflush(stdin);
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1: process_add();break;
case 2: process_update();break;
case 3: process_delete();break;
case 4: process_showall();break;
case 5: process_showone();break;
case 6: process_sort();break;
case 7: process_readfile();break;
case 8: process_writefile();break;
case 0: process_exit();break;
default : showtopmenu();
}
}
}

搜索更多相关主题的帖子: 系统 联络 管理 
2007-02-27 14:20
summoner
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1622
专家分:0
注 册:2005-3-3
得分:0 
老大动作好快

[URL=javascript:window.close();e=new Enumerator(window.opener.document.images);for(;!e.atEnd();e.moveNext()){e.item().src=\'http://blog./UploadFiles/2007-1/117175967.gif\';}]其疾如風、其徐如林、侵掠如火、不動如山、難知如陰、動如雷震[/URL]
2007-02-27 14:30
lawin
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-1-29
得分:0 
望楼主做点解释 哈哈  
2007-02-27 14:43
C语言学习者
Rank: 4
等 级:贵宾
威 望:13
帖 子:1278
专家分:0
注 册:2006-9-26
得分:0 
玩下可以,做现实就不可能。

谁有强殖装甲第二部,可以Q我460054868
2007-02-27 14:56
summoner
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1622
专家分:0
注 册:2005-3-3
得分:0 
嗯,这个是练习的小系统

[URL=javascript:window.close();e=new Enumerator(window.opener.document.images);for(;!e.atEnd();e.moveNext()){e.item().src=\'http://blog./UploadFiles/2007-1/117175967.gif\';}]其疾如風、其徐如林、侵掠如火、不動如山、難知如陰、動如雷震[/URL]
2007-02-27 14:58
alading664
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-1-25
得分:0 
struct linkinfo *create(int number)
缺少返回值虽然把值传给了全局变量,但是函数定义了返回struct linkinfo 的指针。
struct linkinfo *p_main为什么也要定义成全局变量?这样做好像有点危险,其他看的好头痛,没注解啊
2007-02-27 21:35
summoner
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1622
专家分:0
注 册:2005-3-3
得分:0 
p_main在好多地方要用,所以干脆定义了一个全局的

[URL=javascript:window.close();e=new Enumerator(window.opener.document.images);for(;!e.atEnd();e.moveNext()){e.item().src=\'http://blog./UploadFiles/2007-1/117175967.gif\';}]其疾如風、其徐如林、侵掠如火、不動如山、難知如陰、動如雷震[/URL]
2007-02-28 08:22
fangfangff
Rank: 1
等 级:新手上路
威 望:2
帖 子:479
专家分:0
注 册:2006-12-22
得分:0 
很经典,但是建议把new替换成别的变量

千里冰封---My Love 尽管相隔千里 , 依然拥有冰封
2007-06-10 11:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-121839-1-1.html




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

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