#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();
}
}
}