/*
*File name: classmate.c
*Author: Vinco Zhang e-mail: xuyunzhang693@qq.com
*Description: this program is written for the course design of "创新实验",it have been tested in
*Red Hat 5(a linux os).when run it, firstly it create a file classmate.ini, then input the data of your *classmate, it will be recorded into the classmate.ini, finally it will print the data in the file. you also *can clean the file in the last by the function fclean()
*Develop environment: linux red hat 5 //gcc -o classmate classmate.c
*Copyright: All Reserved
*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
#include<malloc.h>
/*****************************************************/
#define LIST_INIT_SIZE 60
#define LEN sizeof(struct classmate)
//#define DEF_INIT_P(p) ClassMate p=(ClassMate )malloc(LEN)
//#define DEF_NULL_P(p) ClassMate p=NULL
//#define llong long long
#define llong __int64
typedef struct classmate
{
unsigned llong No;
char name[30];
char cellphone[15];
//you can add more members such as QQ, home address .etc
struct classmate* next;
}* ClassMate;
//struct classmate classmate;
int n;
ClassMatelist_creat(ClassMate head);
ClassMate list_insert(ClassMate head,ClassMate classmate);
ClassMate list_delete(ClassMate head, unsigned llong No);
void list_print(ClassMatehead);
int list_write(ClassMate head);
int list_read(ClassMate head);
int parse_line_to_list(char* line,ClassMate head);
int fclean(void);
int time_print(void);
/*****************************************************/
int main(void)
{
ClassMate head,p;
//DEF_INIT_P(head);
//DEF_INIT_P(p);
struct classmate tp={2007142103,"li","15307135540",p};
ClassMate classmate=&tp;
unsigned llong No=2007142105;
time_print();
printf("len=sizeof(struct classmate)=%d\n",LEN);
head=list_creat(head);
head=list_delete(head,No);
list_print(head);
list_write(head);
list_read(head);
list_print(head);
//free(head);
//free(p);
//fclean();
return 0;
}
/*****************************************************/
int fclean(void)
{
FILE* fclm;
int i=0;
char line[128];
if((fclm=fopen("classmates.ini","w"))==NULL)//w+
{
printf("classmates.ini open fail!!\n");
return 0;
}
printf("the file have been cleaned now!\n");
fclose(fclm);
return 1;
}
ClassMate list_creat(ClassMate head)
{
//p1 point to the last one elem of the list (the elem inputed just now)
//p2 point to the second last elem of the list
ClassMate p1=NULL,p2=NULL;
n=0;
p1=(ClassMate)malloc(LEN);
p2=(ClassMate)malloc(LEN);
//DEF_INIT_P(p1);
//DEF_INIT_P(p2);
printf("please input as follw format,or enter 0 for exit from input model:\nNo:\tname:\t\tcellphone:\n");
scanf("%ld",&p1->No);if(p1->No==0) goto exit;
scanf("%s",p1->name);
scanf("%s",p1->cellphone);
printf("you have input:\nNo:%ld\tname:%s\t\tcellphone:%s\n",p1->No,p1->name,p1->cellphone);
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
head=list_insert(head,p1);
list_print(head);
while((p1->No)!=0)
{
n=n+1;
p1=(ClassMate )malloc(LEN);
printf("please input as follw format,or enter 0 for exit from input model:\nNo:\tname:\t\tcellphone:\n");
scanf("%ld",&p1->No);if(p1->No==0) goto exit;
scanf("%s",p1->name);
scanf("%s",p1->cellphone);
printf("you have input:\nNo:%ld\tname:%s\t\tcellphone:%s\n",p1->No,p1->name,p1->cellphone);
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
head=list_insert(head,p1);
list_print(head);
}
exit:
printf("you have input '0',so exit from the input now!!\n");
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
free(p1);
free(p2);
return (head);
}
ClassMate list_insert(ClassMate head,ClassMate classmate)
{
//p0 point to the data will be insert into the list
//p1 point to the last one elem of the list
//p2 point to the second last elem of the list
ClassMate p0,p1,p2;
//DEF_INIT_P(p0);
//DEF_INIT_P(p1);
//DEF_INIT_P(p2);
p1=head;
p0=classmate;
if(head==NULL)
{
head=p0;;
p0->next=NULL;
}
else
{
while((p0->No > p1->No) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->No <= p1->No)
{
if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
printf("you have inserted the following into the list just now!\nNo:%ld\tname:%s\t\tcellphone:%s\n",classmate->No,classmate->name,classmate->cellphone);
n=n+1;
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
//free(p0);
//free(p1);
//free(p2);
return(head);
}
ClassMate list_delete(ClassMate head, unsigned llong No)
{
//p1 point to the last one elem of the list
//p2 point to the second last elem of the list
ClassMate p1;
ClassMate p2;
printf("you want to detele classmate from the list whose No=%ld;\n",No);
//DEF_INIT_P(p1);
//DEF_INIT_P(p2);
if(head==NULL)
{
printf("\nthe list is NULL,delete fail!!!\n");
goto end;
}
p1=head;
while(No != p1->No && p1->next != NULL)
{
p2=p1;
p1=p1->next;
}
if(No==p1->No)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("you have deleted the following from the list just now:\nNo:%ld\tname:%s\t\tcellphone:%s\n",p1->No,p1->name,p1->cellphone);
n=n-1;
}
else printf("but %ld not been found,delete fail!!!\n",No);
end:
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
free(p1);
free(p2);
return(head);
}
void list_print(ClassMate head)
{
//ClassMate p;
DEF_INIT_P(p);
p=head;
printf("print the list record now!\n");
if(head!=NULL)
{
do
{
printf("No:%ld\tname:%s\t\tcellphone:%s\n",p->No,p->name,p->cellphone);
p=p->next;
}while(p!=NULL);
}
free(p);
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
}
int list_write(ClassMate head)
{
ClassMate p=head;
FILE* fclm;
char line[128+1];
int i=0;
if((fclm=fopen("classmates.ini","a"))==NULL)//a+
{
printf("classmates.ini open fail!!\n");
return 0;
}
if(head!=NULL)
{
do
{
snprintf(line,128+1,"No:%ld\tname:%s\t\tcellphone:%s\n",p->No,p->name,p->cellphone);
fprintf(fclm,"%s",line);
//printf("No:%ld\tname:%s\t\tcellphone:%s\n",p->No,p->name,p->cellphone);
printf("you have writed the following line to the file:\n%s\n",line);
p=p->next;
}while(p!=NULL);
}
fclose(fclm);
return 1;
}
int list_read(ClassMate head)
{
ClassMate p=head;
FILE* fclm;
char line[128+1];
char ch;
int i=0;
if((fclm=fopen("classmates.ini","r"))==NULL)
{
printf("classmates.ini open fail!!\n");
return 0;
}
//fseek(fclm,0,SEEK_SET);
rewind(fclm);
printf("read the file now!\n");
ch=fgetc(fclm);
do
{
fseek(fclm,-1,SEEK_CUR);
fgets(line,128+1,fclm);
++i;
printf("get the line[%d]=%s",i,line);//fputs(line,stdout);
parse_line_to_list(line,head);
ch=fgetc(fclm);
}while(ch!= EOF);
fclose(fclm);
return 1;
}
int parse_line_to_list(char* line,ClassMate head)
{
//ClassMate classmate;
DEF_INIT_P(classmate);
char* p=line;
int i;
unsigned llong No=0,tmp;
char name[30];
char cellphone[15];
p=strstr(p,"No:");
p=strstr(p,":");
if(p==NULL) return 0;
while(!isdigit(*p)) p++;
while(isdigit(*p))
{
tmp=(*p)-'0';
No=10*No+tmp;
p++;
}
p=strstr(p,"name:");
p=strstr(p,":");
p++;
i=0;
while(!isalnum(*p)) p++;
while(isalnum(*p))
{
name[i++]=*(p++);
}
name[i]='\0';
p=strstr(p,"cellphone:");
p=strstr(p,":");
p++;
i=0;
while(!isdigit(*p)) p++;
while(isdigit(*p))
{
cellphone[i++]=*(p++);
}
cellphone[i]='\0';
classmate->No=No;
//strncpy(classmate->name,name,strlen(name));
//strncpy(classmate->cellphone,cellphone,strlen(cellphone));
strcpy(classmate->name,name);
strcpy(classmate->cellphone,cellphone);
head=list_insert(head,classmate);
//free(classmate);
return 0;
}
int time_print(void)
{
time_t t=time(0);
char tmp[64];
strftime(tmp,sizeof(tmp),"date:%Y-%m-%d %A \ntime:%X %j %z %Z",localtime(&t));
puts(tmp);
return 0;
}
*File name: classmate.c
*Author: Vinco Zhang e-mail: xuyunzhang693@qq.com
*Description: this program is written for the course design of "创新实验",it have been tested in
*Red Hat 5(a linux os).when run it, firstly it create a file classmate.ini, then input the data of your *classmate, it will be recorded into the classmate.ini, finally it will print the data in the file. you also *can clean the file in the last by the function fclean()
*Develop environment: linux red hat 5 //gcc -o classmate classmate.c
*Copyright: All Reserved
*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
#include<malloc.h>
/*****************************************************/
#define LIST_INIT_SIZE 60
#define LEN sizeof(struct classmate)
//#define DEF_INIT_P(p) ClassMate p=(ClassMate )malloc(LEN)
//#define DEF_NULL_P(p) ClassMate p=NULL
//#define llong long long
#define llong __int64
typedef struct classmate
{
unsigned llong No;
char name[30];
char cellphone[15];
//you can add more members such as QQ, home address .etc
struct classmate* next;
}* ClassMate;
//struct classmate classmate;
int n;
ClassMatelist_creat(ClassMate head);
ClassMate list_insert(ClassMate head,ClassMate classmate);
ClassMate list_delete(ClassMate head, unsigned llong No);
void list_print(ClassMatehead);
int list_write(ClassMate head);
int list_read(ClassMate head);
int parse_line_to_list(char* line,ClassMate head);
int fclean(void);
int time_print(void);
/*****************************************************/
int main(void)
{
ClassMate head,p;
//DEF_INIT_P(head);
//DEF_INIT_P(p);
struct classmate tp={2007142103,"li","15307135540",p};
ClassMate classmate=&tp;
unsigned llong No=2007142105;
time_print();
printf("len=sizeof(struct classmate)=%d\n",LEN);
head=list_creat(head);
head=list_delete(head,No);
list_print(head);
list_write(head);
list_read(head);
list_print(head);
//free(head);
//free(p);
//fclean();
return 0;
}
/*****************************************************/
int fclean(void)
{
FILE* fclm;
int i=0;
char line[128];
if((fclm=fopen("classmates.ini","w"))==NULL)//w+
{
printf("classmates.ini open fail!!\n");
return 0;
}
printf("the file have been cleaned now!\n");
fclose(fclm);
return 1;
}
ClassMate list_creat(ClassMate head)
{
//p1 point to the last one elem of the list (the elem inputed just now)
//p2 point to the second last elem of the list
ClassMate p1=NULL,p2=NULL;
n=0;
p1=(ClassMate)malloc(LEN);
p2=(ClassMate)malloc(LEN);
//DEF_INIT_P(p1);
//DEF_INIT_P(p2);
printf("please input as follw format,or enter 0 for exit from input model:\nNo:\tname:\t\tcellphone:\n");
scanf("%ld",&p1->No);if(p1->No==0) goto exit;
scanf("%s",p1->name);
scanf("%s",p1->cellphone);
printf("you have input:\nNo:%ld\tname:%s\t\tcellphone:%s\n",p1->No,p1->name,p1->cellphone);
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
head=list_insert(head,p1);
list_print(head);
while((p1->No)!=0)
{
n=n+1;
p1=(ClassMate )malloc(LEN);
printf("please input as follw format,or enter 0 for exit from input model:\nNo:\tname:\t\tcellphone:\n");
scanf("%ld",&p1->No);if(p1->No==0) goto exit;
scanf("%s",p1->name);
scanf("%s",p1->cellphone);
printf("you have input:\nNo:%ld\tname:%s\t\tcellphone:%s\n",p1->No,p1->name,p1->cellphone);
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
head=list_insert(head,p1);
list_print(head);
}
exit:
printf("you have input '0',so exit from the input now!!\n");
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
free(p1);
free(p2);
return (head);
}
ClassMate list_insert(ClassMate head,ClassMate classmate)
{
//p0 point to the data will be insert into the list
//p1 point to the last one elem of the list
//p2 point to the second last elem of the list
ClassMate p0,p1,p2;
//DEF_INIT_P(p0);
//DEF_INIT_P(p1);
//DEF_INIT_P(p2);
p1=head;
p0=classmate;
if(head==NULL)
{
head=p0;;
p0->next=NULL;
}
else
{
while((p0->No > p1->No) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->No <= p1->No)
{
if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
printf("you have inserted the following into the list just now!\nNo:%ld\tname:%s\t\tcellphone:%s\n",classmate->No,classmate->name,classmate->cellphone);
n=n+1;
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
//free(p0);
//free(p1);
//free(p2);
return(head);
}
ClassMate list_delete(ClassMate head, unsigned llong No)
{
//p1 point to the last one elem of the list
//p2 point to the second last elem of the list
ClassMate p1;
ClassMate p2;
printf("you want to detele classmate from the list whose No=%ld;\n",No);
//DEF_INIT_P(p1);
//DEF_INIT_P(p2);
if(head==NULL)
{
printf("\nthe list is NULL,delete fail!!!\n");
goto end;
}
p1=head;
while(No != p1->No && p1->next != NULL)
{
p2=p1;
p1=p1->next;
}
if(No==p1->No)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("you have deleted the following from the list just now:\nNo:%ld\tname:%s\t\tcellphone:%s\n",p1->No,p1->name,p1->cellphone);
n=n-1;
}
else printf("but %ld not been found,delete fail!!!\n",No);
end:
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
free(p1);
free(p2);
return(head);
}
void list_print(ClassMate head)
{
//ClassMate p;
DEF_INIT_P(p);
p=head;
printf("print the list record now!\n");
if(head!=NULL)
{
do
{
printf("No:%ld\tname:%s\t\tcellphone:%s\n",p->No,p->name,p->cellphone);
p=p->next;
}while(p!=NULL);
}
free(p);
printf("[++++++++++++++++++++++++++++++++++++++++++++++]\n");
}
int list_write(ClassMate head)
{
ClassMate p=head;
FILE* fclm;
char line[128+1];
int i=0;
if((fclm=fopen("classmates.ini","a"))==NULL)//a+
{
printf("classmates.ini open fail!!\n");
return 0;
}
if(head!=NULL)
{
do
{
snprintf(line,128+1,"No:%ld\tname:%s\t\tcellphone:%s\n",p->No,p->name,p->cellphone);
fprintf(fclm,"%s",line);
//printf("No:%ld\tname:%s\t\tcellphone:%s\n",p->No,p->name,p->cellphone);
printf("you have writed the following line to the file:\n%s\n",line);
p=p->next;
}while(p!=NULL);
}
fclose(fclm);
return 1;
}
int list_read(ClassMate head)
{
ClassMate p=head;
FILE* fclm;
char line[128+1];
char ch;
int i=0;
if((fclm=fopen("classmates.ini","r"))==NULL)
{
printf("classmates.ini open fail!!\n");
return 0;
}
//fseek(fclm,0,SEEK_SET);
rewind(fclm);
printf("read the file now!\n");
ch=fgetc(fclm);
do
{
fseek(fclm,-1,SEEK_CUR);
fgets(line,128+1,fclm);
++i;
printf("get the line[%d]=%s",i,line);//fputs(line,stdout);
parse_line_to_list(line,head);
ch=fgetc(fclm);
}while(ch!= EOF);
fclose(fclm);
return 1;
}
int parse_line_to_list(char* line,ClassMate head)
{
//ClassMate classmate;
DEF_INIT_P(classmate);
char* p=line;
int i;
unsigned llong No=0,tmp;
char name[30];
char cellphone[15];
p=strstr(p,"No:");
p=strstr(p,":");
if(p==NULL) return 0;
while(!isdigit(*p)) p++;
while(isdigit(*p))
{
tmp=(*p)-'0';
No=10*No+tmp;
p++;
}
p=strstr(p,"name:");
p=strstr(p,":");
p++;
i=0;
while(!isalnum(*p)) p++;
while(isalnum(*p))
{
name[i++]=*(p++);
}
name[i]='\0';
p=strstr(p,"cellphone:");
p=strstr(p,":");
p++;
i=0;
while(!isdigit(*p)) p++;
while(isdigit(*p))
{
cellphone[i++]=*(p++);
}
cellphone[i]='\0';
classmate->No=No;
//strncpy(classmate->name,name,strlen(name));
//strncpy(classmate->cellphone,cellphone,strlen(cellphone));
strcpy(classmate->name,name);
strcpy(classmate->cellphone,cellphone);
head=list_insert(head,classmate);
//free(classmate);
return 0;
}
int time_print(void)
{
time_t t=time(0);
char tmp[64];
strftime(tmp,sizeof(tmp),"date:%Y-%m-%d %A \ntime:%X %j %z %Z",localtime(&t));
puts(tmp);
return 0;
}