红联Linux门户
Linux帮助

E 信息个人助理(一阶段功能模块代码)

发布时间:2009-05-23 14:21:52来源:红联作者:busixiaozi
[i=s] 本帖最后由 busixiaozi 于 2009-5-23 14:23 编辑 [/i]

red hat linux 下编译通过

[attach]14919[/attach]

3 天前 06:57
参考书籍:

1.Linux系统下c程序基础

2.UNIX/LINUX编程
(Understanding unix/linux programming A guide to theory and practice)

3. Linux网络体系结构
4.Linux内核中网络协议的设计与实现

(The linux network architecture.Design and implementation of network protocols in the linux kernel)
[attach]14920[/attach]
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define HEADER1 " ---------------------------E Personal Information Assigment------------------------\n"
#define HEADER2 " | num | name | password | address |\n"
#define HEADER3 " |-------------|---------------|------------------|--------------------------------|\n"
#define FORMAT " |%-13s|%-15s|%-18s|%-32s|\n"
#define DATA p->num,p->name,p->password,p->address
#define END " -----------------------------------------------------------------------------------\n"
#define N 100 /*define the maxsize of pda is 100*/
int saveflag=0;
typedef struct epia /* epia:E Personal Information Assigment*/
{
char num[4];
char name[10];
char password[20];
char address[20];
}EPIA; /* epia:E Personal Information Assigment*/
void menu()
{
clearenv();
printf("\n -----------------------------------------------------------------------------------\n");
printf("\n The E Personal Information Assigment\n");
printf("\n *1: Input record 2: Display record*\n");
printf("\n *3: Delete record 4: Search record*\n"); printf("\n *5: Modify record 6: Insert record*\n");
printf("\n *7: Sort record 8: Save record*\n");
printf("\n *0: Quit system*\n");
printf("\n -----------------------------------------------------------------------------------\n");
}
void printheader()
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(EPIA pp)
{
EPIA* p;
p=&pp;
printf(FORMAT,DATA);
}
void Disp(EPIA temp[],int n)
{
int i;
if(n==0)
{
printf("\n =====>Not E-information record<=====\n");
getchar();
return;
}
printf("\n\n");
printheader();
i=0;
while(i{
printdata(temp[i]);
i++;
printf(HEADER3);
}
getchar();
}
void Wrong()
{
printf("\n\n **************Error:input has wrong! press any key to continue*************\n");
getchar();
}
void Nofind()
{
printf("\n =====>Not find this E-information record!<=====\n");
}
int Locate(EPIA temp[],int n,char findmess[],char nameoraddress[])
{
int i=0;
if(strcmp(nameoraddress,"address")==0)
{
while(i {
if(strcmp(temp[i].address,findmess)==0)
return i;
i++;
}
}
else if(strcmp(nameoraddress,"name")==0)
{
while(i {
if(strcmp(temp[i].name,findmess)==0)
return i;
i++;
}
}
return -1;
}
void stringinput(char *t,int lens,char *notice)
{
char n[255];
do{
printf(notice);
scanf("%s",n);
if(strlen(n)>lens)
printf("\n Exceed the required length!\n ");
}while(strlen(n)>lens);
strcpy(t,n);
}
int Add(EPIA temp[],int n)
{
char ch,num[10];
int i,flag=0;
clearenv();
Disp(temp,n);

while(1)
{
while(1)
{
stringinput(num,10,"input number(press '0' return menu):");
flag=0;
if(strcmp(num,"0")==0)
{
return n;
}
i=0;
while(i {
if(strcmp(temp[i].num,num)==0)
{
flag=1;
break;
}
i++;
}
if(flag==1)
{
getchar();
printf(" ==>The numbere %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return n;
}
else
{
break;
}
}
strcpy(temp[n].num,num);
stringinput(temp[n].address,20,"Address :");
stringinput(temp[n].name,15,"Name :");
stringinput(temp[n].password,15,"Password:");
saveflag=1;
n++;
}
return n;

}
void Qur(EPIA temp[],int n)
{
int select;
char searchinput[20];
int p=0;
if(n<=0)
{
clearenv();
printf("\n = ========>No telephone record!<=========\n");
getchar();
return;
}
clearenv();
printf("\n 1: Search by name 2: Search by address\n");
printf("\n Please choice [1,2]:");
scanf("%d",&select);
if(select==1)
{

stringinput(searchinput,10,"\nInput the existing name:");
p=Locate(temp,n,searchinput,"name");

if(p!=-1)
{
printheader();
printdata(temp[p]);
printf(END);
printf("\n Press any key to return\n");
// getchar();
}
else
Nofind();
getchar();
}
else if(select==2)
{
stringinput(searchinput,15,"\ninput the existing address:");
p=Locate(temp,n,searchinput,"address");
if(p!=-1)
{
printheader();
printdata(temp[p]);
printf(END);
printf("\n Press any key to return\n");
// getchar();
}
else
Nofind();
getchar();
}
else
Wrong();
getchar();
}
int Del(EPIA temp[],int n)
{
int sel;
char findmess[20];
int p=0,i=0;
if(n<=0)
{
clearenv();
printf("\n ====>No E-Information record!<====\n");
getchar();
return n;
}
clearenv();
Disp(temp,n);
printf("\n 1: Delete by name 2: Delete by address\n"); printf("\n Please choice [1,2]:");
scanf("%d",&sel);
if(sel==1)
{
stringinput(findmess,10,"Input the exsiting name:");
p=Locate(temp,n,findmess,"name");
getchar();
if(p!=-1)
{
for(i=p+1;i<1;i++)
{
strcpy(temp[i-1].num,temp[i].num);
strcpy(temp[i-1].name,temp[i].name);
strcpy(temp[i-1].password,temp[i].password);
strcpy(temp[i-1].address,temp[i].address);
}
printf("\n ===>Delete success!<===\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
else if(sel==2)
{
stringinput(findmess,15,"\nInput the existing address:");
p=Locate(temp,n,findmess,"address");
getchar();
if(p!=-1)
{
for(i=p+1;i {

strcpy(temp[i-1].num,temp[i].num);
strcpy(temp[i-1].name,temp[i].name);
strcpy(temp[i-1].password,temp[i].password);
strcpy(temp[i-1].address,temp[i].address);
}
printf("\n ===>Delete success!<===\n");
n--;
saveflag=1;
}
else
Nofind();
getchar();
}
return n;
}
void Modify(EPIA temp[],int n)
{
char findmess[20];
int p=0;
if(n<=0)
{
clearenv();
printf("\n =========>No E-Information record!<=========\n");
getchar();
return ;
}
clearenv();
printf("\n Modify E-Information recorder!\n");
Disp(temp,n);
stringinput(findmess,10,"\nInput the existing name:");
p=Locate(temp,n,findmess,"name");
if(p!=-1)
{
printf("\nNumber:%s,\n",temp[p].num);
printf("\nName :%s,\n",temp[p].name);
stringinput(temp[p].name,15,"\nInput new name :\n");

printf("\nName :%s,\n",temp[p].password);
stringinput(temp[p].password,15,"\nInput new password:\n");

printf("Name:%s,",temp[p].address);
stringinput(temp[p].address,30,"\nInput new address :");

printf("\n ===>Modify success!<===\n");
getchar();
Disp(temp,n);
getchar();
saveflag=1;
}
else
{
Nofind();
getchar();
}
return ;
}
int Insert(EPIA temp[],int n)
{
char ch,num[10],s[10];
EPIA newinfo;
int flag=0,i=0,kkk=0;
clearenv();
Disp(temp,n);
while(1)
{
stringinput(s,10,"\nPlease input insert location after the Number:");
flag=0;i=0;
while(i {
if(strcmp(temp[i].num,s)==0)
{kkk=i;flag=1;break;}
i++;
}
if(flag==1)
break;
else
{
getchar();
printf("\n=======>The number %s is not existing,try again?(y/n):",s);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{continue;}
else
{return n;}
}
}
while(1)
{
stringinput(num,10,"\nInput new Nunber:");
i=0;flag=0;
while(i {
if(strcmp(temp[i].num,num)==0)
{flag=1;break;}
i++;
}
if(flag==1)
{
getchar();
printf("\n===>Sorry,The number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{continue;}
else
{return n;}
}
else
break;
}
strcpy(newinfo.num,num);
stringinput(newinfo.address,20,"Address:");
stringinput(newinfo.name,15,"Name:");
stringinput(newinfo.password,15,"Password:");

saveflag=1;

for(i=n-1;i>kkk;i--)
{
strcpy(temp[i+1].num,temp[i].num);
strcpy(temp[i+1].address,temp[i].address);
strcpy(temp[kkk+1].num,newinfo.num);
strcpy(temp[i+1].name,temp[i].name);
strcpy(temp[i+1].password,temp[i].password);
}
strcpy(temp[kkk+1].num,newinfo.num);
strcpy(temp[kkk+1].address,newinfo.address);
strcpy(temp[kkk+1].name,newinfo.name);
strcpy(temp[kkk+1].password,newinfo.password);
n++;
Disp(temp,n);
printf("\n\n");
getchar();
return n;
}
void SelectSort(EPIA temp[],int n)
{
int i=0,j=0,flag=0,indexmin,select;
char charflag[10];
EPIA newinfo;
if(n<=0)
{
clearenv();
printf("\n===========Not E-Information record!\n");
getchar();
return;
}
clearenv();
Disp(temp,n);
printf(" 1: Sort by number 2:Sort by address\n");
printf(" Please choice[1,2]");
scanf("%d",&select);
if(select==1)
{
for(i=0;i {
flag=32767;indexmin=0;
for(j=i;j {
if(atoi(temp[j].num) {
flag=atoi(temp[j].num);
indexmin=j;
}
}
strcpy(newinfo.num,temp[i].num);

strcpy(newinfo.address,temp[i].address);
strcpy(newinfo.name,temp[i].name);
strcpy(newinfo.password,temp[i].password);

strcpy(temp[i].num,temp[indexmin].num);
strcpy(temp[i].address,temp[indexmin].address);
strcpy(temp[i].name,temp[indexmin].name);
strcpy(temp[i].password,temp[indexmin].password);

strcpy(temp[indexmin].num,newinfo.num);
strcpy(temp[indexmin].address,newinfo.address);
strcpy(temp[indexmin].name,newinfo.name);
strcpy(temp[indexmin].password,newinfo.password);
}
Disp(temp,n);
saveflag=1;
printf("\n ======> Sort complete!\n");
getchar();
return;
}
else if(select==2)
{
for(i=0;i {
charflag[0]=255;indexmin=0;
for(j=i;j {
if(strcmp(temp[j].name,charflag)>0)
{
charflag[0]=temp[j].name;
indexmin=j;
}
}
strcpy(newinfo.num,temp[i].num);
strcpy(newinfo.address,temp[i].address);
strcpy(newinfo.name,temp[i].name);
strcpy(newinfo.password,temp[i].password);

strcpy(temp[i].num,temp[indexmin].num);
strcpy(temp[i].address,temp[indexmin].address);
strcpy(temp[i].name,temp[indexmin].name);
strcpy(temp[i].password,temp[indexmin].password);

strcpy(temp[indexmin].num,newinfo.num);
strcpy(temp[indexmin].address,newinfo.address);
strcpy(temp[indexmin].name,newinfo.name);
strcpy(temp[indexmin].password,newinfo.password);
}
Disp(temp,n);
saveflag=1;
printf("\n =====>Sort complete!\n");
getchar();
return;
}
else
{
Wrong();
getchar();
getchar();
return;
}
}
void Save(EPIA temp[],int n)
{
FILE* fp;
int i=0;
fp=fopen("/home/pda","w");
if(fp==NULL)
{
printf("\n =======Open file error!\n");
getchar();
return ;
}
for(i=0;i {
if(fwrite(&temp[i],sizeof(EPIA),1,fp)==1)
{
continue;
}
else
{break;}
}
if(i>0)
{
getchar();
printf("\n\n====>Save file complete,total saved's record number is:%d\n",i);
getchar();
saveflag=0;
}
else
{
clearenv();
printf("\nThe current link is empty,no telephone record is saved!\n");
getchar();
}
fclose(fp);
}
int main()
{
EPIA pda[N]; /* pda:personal digital assigment*/
FILE *fp; /*the file pointer*/
int select; /*save the choice variable*/
char ch; /*save the choice (y,Y,n,N)*/
int count=0; /*save the file record number or the element number*/

fp=fopen("/home/epda","a+");/*open a file,if the file do exist,creat a new*/
if(fp==NULL)
{
printf("\n=====>can not open file!\n");
exit(0);
}

while(!feof(fp))
{
if(fread(&pda[count],sizeof(EPIA),1,fp)==1)
count++;/*read a record one time*/
}
fclose(fp);
printf("\n ==>open file sucess,the total records number is:%d<==\n",count);
while(1)
{
clearenv();
menu();
printf("\n Please enter your choice(0~9):");
scanf("%d",&select);
if(select==0)
{
if(saveflag==1)
{
getchar();
printf("\n ==>Whether save the modified record to file?(y/n):");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(pda,count);
}
printf("\n ===>thank you for useness<===!\n");
getchar();
break;
}
switch(select)
{
case 1: count=Add(pda,count);break;
case 2: clearenv();Disp(pda,count);break;
case 3: count=Del(pda,count);break;
case 4: Qur(pda,count);break;
case 5: Modify(pda,count);break;
case 6: count=Insert(pda,count);break;
case 7: SelectSort(pda,count);break;
case 8: Save(pda,count);break;
default: Wrong();getchar();break;
}
}
}
文章评论

共有 1 条评论

  1. Laifus 于 2009-05-23 15:35:28发表:

    收下先