📄 mailmanagementsystem.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct addbook)
#define MAX 100
void InitDialBook(void);
void UserInterface(void);/*The Menu Function*/
void Load(void);
void Add(void);
void Find(void);
void Modify(void);
void Delete(void);
void ShowAll(void);
void Save(void);
void Quit(void);
struct addbook{
int record;
char name[15];
char age[4];
char addr[20];
char phone[15];
/*Personal Signature*/
struct addbook *link;
};
FILE *fp;
struct addbook *p,*p1,*head,*s,*t;
main () /*(int argc, char * argv[])*/
{
/*FILE *fp;
int err=0;
if (DEBUG)
printf("\nNum of arguments = %d\n",argc);
if (argc==2)
{
err=init_file(&fp,argv[1]);
if (DEGUG)
printf("\nOpen file returned %d\n",err);*/
int flag;
int choice;
if((fp = fopen("zzxx.txt","rb"))==NULL)
{ system("cls");
printf("\n\n\t\t\tThe file is not exist!\n");
if((fp = fopen("zzxx.txt","wb"))==NULL)
{
printf("Error on opening files!\n");
getchar();
exit(0);
}
}
if(fgetc(fp)==-1)
{
printf("\n\t\tNo information in the system file, please add the information.\n");
InitDialBook();
printf("\n\t\t\tadding was successful!\n");
printf("\n\t\tIf you want to add more information, please restart this program.\n");
getchar();
}
rewind(fp); /* Initail file pointer */
Load();
fclose(fp);
/* printf("\nthis is the%dmail.\n",p->record); */
flag = 1;
while(flag)
{
UserInterface();
printf("Please enter your choice:(1-6):");
scanf("%d",&choice);
getchar();
if(choice>=0&&choice<6)
{
switch(choice)
{
case 1:
Add();break;
case 2:
Find();break;
case 3:
ShowAll();break;
case 4:
Delete();break;
case 5:
Modify();break;
case 6:
Quit();break;
flag = 0;
system("cls");
printf("\n\n\n\t\t\tThank you for using my programe!!!");
printf("\n\n\t\tIf you have any advice, please contact:");
printf("\n\n\t\t\tseptemwsh@hotmail.com or 021892984");
getchar();
break;
}
}
else{
printf("Wrong type of input,please enter again!\n");
}
}
fp = fopen("zzxx.txt","wb");
Save();
fclose(fp); system("cls");
printf("\n\n\n\t\t\tPress any key to quit!\n");
getchar();
return 0;
}
void InitDialBook(void)/*Starting file function*/
{
p->record = 1;
printf("\n\n\t\tPlease enter Name:");
gets(p->name);
printf("\n\t\tPlease enter Address:");
gets(p->addr);
printf("\n\t\tPlease enter Phone number:");
gets(p->phone);
p->link = NULL;
head = p;
fwrite(p,LEN,1,fp);
fclose(fp);
}
void UserInterface(void)/*Menu*/
{ system("cls");
printf("\n\t\t*************************************************\n");
printf("\t\tWellcome to Wang Lei's mail management system");
printf("\n\t\t*************************************************\n");
printf("\n\n\t\t\t******Please choose your opinion******\n");
printf("\t\t\t +-------------------------------+");
printf("\t\t\t\t\t\t | 1 Add record |\n");
puts(" \t\t\t | 2 Find record |");
printf("\t\t\t | 3 Display all records |\n");
printf("\t\t\t | 4 Delete record |\n");
printf("\t\t\t | 5 Modify record |\n");
puts(" \t\t\t | 6 Exit |");
printf("\t\t\t +-------------------------------+\n");
printf("\n\n\tPlease Notice!!The address and name enter must bu in English!!\n\n");
}
void Load(void)/*Reading from file*/
{
fread(head,LEN,1,fp);
p = head;
while(p->link!=NULL){
p1 = (struct addbook*)malloc(LEN);
p1 = p->link;
fread(p1,LEN,1,fp);
p = p->link;
}
}
void Add(void)/*input function*/
{
p1 = (struct addbook*)malloc(LEN);
p1->record = p->record + 1; system("cls");
printf("\n\n\t\t\t-------------------------------------------\n");
printf("\t\t\t\t\tPlease Enter Information\n");
printf("\t\t\t-------------------------------------------\n");
printf("\n\t\t\tThis is the %d mail information",p->record+1);
printf("\n\t\t\tPlease Enter Name:");
gets(p1->name);
printf("\n\t\t\tPlease Enter Address:");
gets(p1->addr);
printf("\n\t\t\tPlease Enter Phone Number:");
gets(p1->phone);
p1->link = NULL;
p->link = p1;
p = p1;
}
void ShowAll(void)/*Show all the functions*/
{
s = head;
system("cls");
printf("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf(" \tNO. NAME ADDRESS PHONE");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
do{
printf("\t %-6d%-10s%-8s%-7s%-10s%-10s%-s%-s\n",s->record,s->name,s->addr,s->phone);
s = s->link;
}while(s!=NULL);
printf("\n\n\n\t\t\tPress any KEY to QUIT");
getchar();
return;
}
void Find(void)/*Find function*/
{
char c;
struct addbook who;
s = head;
system("cls");
printf("\n\t\tPlease enter the name you want to find:");
gets(who.name);
while((s->link!=NULL)&&(strcmp(s->name,who.name)))
{
s = s->link;
}
if((s->link==NULL)&&(strcmp(s->name,who.name)))
{
printf("\"%s\" Not exist!\n",who.name);
printf("Please check the name and search again\n");
getchar();
return;
}
system("cls");
puts(" The following is the information of the record");
printf("\n******************************************************\n");
printf("\t NO. NAME ADDRESS PHONE ");
printf("\n******************************************************\n");
printf("\t %-6d%-10s%-8s%-7s%-8s%-10s%-s%-s\n",s->record,s->name,s->addr,s->phone);
puts(" delete modify or quit");
printf("\n\tPlease enter your choice(d/m/q):");
scanf("%c",&c);
switch(c)
{
case 'D':
case 'd':Delete();break;
case 'M':
case 'm':Modify();break;
}
}
void Modify(void)
{ getchar();
printf("\n\t\tPlease enter New Name:");
gets(s->name);
printf("\n\t\tPlease enter New Address:");
gets(s->addr);
printf("\n\t\tPlease enter New Phone number:");
gets(s->phone);
printf("\n\t\tModify was successful!\n");
getchar();
}
void Delete(void)/*Delete function*/
{
int find_prev;
int find_record;
char del;
char del_member_name[15];
struct addbook *p_del_prev;
strcpy(del_member_name,s->name);
getchar();
printf("Are you SURE you want to delete %s(Y/N)?",del_member_name);
scanf("%c",&del);
if(del=='n'||del=='N')
{
return;
}
find_prev = s->record - 1;
find_record = 1;
p_del_prev = head;
while((p_del_prev->link!=NULL)&&(find_record<find_prev))
{
p_del_prev = p_del_prev->link;
find_record++;
}
/* If delete the first mail information*/
if(s->record==1)
{
if(s->link!=NULL)
{
head = head->link;
s = head;
while(s!=NULL)
{
s->record = s->record - 1;
s = s->link;
}
printf("%d,%s",p->record,p->name);
}
else if(s->link==NULL){ /* If only one mail information in the file */
p = NULL;
head = p;
if(remove("zzxx.txt")==0){
printf("You already deteled last information, file is now empty!\n");
printf("So, you must rebuild information file\n");
printf("Please open the programe again.\n");
}
printf("\n\n\t\tPress any key to quit.\n");
getchar();
exit(0);
}
}
/* If delete the last information */
else if(s->record==p->record){
p_del_prev->link = NULL;
s->link = NULL;
p = p_del_prev;
}
else{
p_del_prev->link = s->link;
s->link = NULL;
while(p_del_prev->link!=NULL)
{
p_del_prev = p_del_prev->link;
p_del_prev->record = p_del_prev->record -1;
}
printf("%d\n%d\n",p->record,p->link);
}
printf("Mail Information\"%s\" has been deleted.\n ",del_member_name);
getchar();
}
void Save(void)/*Save file*/
{
s = head;
do{
fwrite(s,LEN,1,fp);
s = s->link;
}while(s!=NULL);
}
void Quit(void)
{
exit(0);
printf("************End of Mail manage system*****\n");
printf("\t\t\t\t\t\tPress any key to exit...");
fflush(stdin);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -