📄 class.cpp
字号:
#include "dos.h"
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "graphics.h"
#define NUMLEN 12
#define NAMELEN 12
#define ADLEN 12
#define DORMLEN 5
#define TELLEN 8
struct stu *add_a_record(void);
void add(struct stu *); /*Main add*/
void window_set(void);
void list(struct stu *);
void sort(struct stu*,struct stu*);
void modify(struct stu *);
void modify_a_record(struct stu *);
void exiting(void);
void file(struct stu*);
void help(void);
int check(struct stu *,int,int);
int check_c(char,char *);
char *inputnum(int);
char *inputchar(int);
void print(struct stu *);
void textprint(char *);
void search(struct stu *); /*Main search fuction*/
void searchnum(struct stu*); /*search functions*/
void searchname(struct stu*);
void searchtel(struct stu*);
void searchdorm(struct stu*);
void searchad(struct stu*);
void searchbirthday(struct stu*);
struct stu
{
char num[NUMLEN+1];
char name[NAMELEN+1];
char sex;
struct
{
int year;
int month;
int day;
}birthday;
char ad[ADLEN+1];
char dorm[DORMLEN+1];
char tel[TELLEN+1];
struct stu *next,*pre;
};
static int BKCOLOR=0,TBCOLOR=0,TCOLOR=10;
static int Record_len=0; /*Used to show the length of record*/
void main(void)
{
char a,date[30];
struct date *datep;
struct time *timep;
struct stu *head;
textmode(2);
textbackground(BKCOLOR);
textcolor(TCOLOR);
clrscr();
head=(struct stu*)malloc(sizeof(struct stu));
head->next=NULL;
LA:
clrscr();
gotoxy(1,1);
textprint("Super Student Manager Ver 1.2 ");
cprintf("1.File 2.Add 3.Search 4.List 5.Modify 6.Help 7.Window ");
gotoxy(1,4);
cprintf("--Input your choice:");
do
{ gotoxy(35,3);
getdate(datep);
gettime(timep);
sprintf(date,"%4d-%2d-%2d %2d:%2d:%2d",datep->da_year,(int)datep->da_mon,
(int)datep->da_day,(int)timep->ti_hour,(int)timep->ti_min,(int)timep->ti_sec);
cputs(date);
}while(!kbhit());
a=getch();
while(!check_c(a,"1234567faslmhwFASLMHW"))
a=getch();
switch(a)
{
case 'f':case 'F':
case '1':file(head);break;
case 'a':case 'A':
case '2':add(head);break;
case 's':case 'S':
case '3':search(head);break;
case 'l':case 'L':
case '4':list(head);break;
case 'm':case 'M':
case '5':modify(head);break;
case 'h':case 'H':
case '6':help();break;
case 'w':case 'W':
case '7':window_set();break;
default:;
}
head->birthday.year=Record_len;
goto LA;
}
void exiting(void) /*EXITING*/
{
clrscr();
gotoxy(1,1);
textprint("Warning!");
printf("Make sure that you have save your files!\nPress any key to goback,Esc to exit!");
if(getch()==27)
{
closegraph();
exit(0);
}
else return;
}
/*This function is used to input a date with special requirements.\
Ruturn a struct stu type node for other functions. */
struct stu *add_a_record(void)
{
char sex;
struct stu *pt;
pt=(struct stu *)malloc(sizeof(struct stu));
printf("Num(12):"); strcpy(pt->num,inputnum(NUMLEN));
printf("Name(10):"); strcpy(pt->name,inputchar(NAMELEN));
printf("sex:(m/f,M/F)");
sex=getch();
while(sex!='m' &&sex!='M'&&sex!='F'&&sex!='f')
sex=getch();
if(sex=='m'||sex=='M')
pt->sex='M';
else pt->sex='F';
putchar(pt->sex);printf("\n");
printf("adress(10):");strcpy(pt->ad,inputchar(ADLEN));
printf("birthday:(2003 12 31)");
scanf("%d%d%d",&pt->birthday.year,&pt->birthday.month,&pt->birthday.day);
while(pt->birthday.year>2003||pt->birthday.month>12||pt->birthday.day>31)
{ printf("Input error!(The maximum of birthday is 2003 12 31)!\n");
/*'s is set to show wether the input is right*/
printf("birthday:(2003 12 31)");
scanf("%d%d%d",&pt->birthday.year,&pt->birthday.month,&pt->birthday.day);
}
getchar();
printf("dorm(5):");strcpy(pt->dorm,inputnum(DORMLEN));
printf("tel.num(8):");strcpy(pt->tel,inputnum(TELLEN));
return pt;
}
void add(struct stu *P) /*ADD-list*/
{
struct stu *pt,*temp;
char sign,s;
do
{ temp=P->next;
clrscr();
gotoxy(1,1);
textprint("Add list:");
pt=add_a_record();
textprint("LIST");
print(pt);
if(temp==NULL) /*if the list is NULL*/
{ pt->next=NULL;
pt->pre=P;
P->next=pt;
}
else /*if the list isn't NULL*/
{ while(temp!=NULL)
if( strcmp(pt->num,temp->num)>0 )
if(temp->next==NULL)
{ pt->next=NULL;
pt->pre=temp;
temp->next=pt;
break;
}
else temp=temp->next;
else if(strcmp(pt->num,temp->num)==0) /*if nums of two record are same*/
{ printf("\nWarning:Num:%s already exist in list,cover it?(Y/N)\n",pt->num);
s=getch();
if (s=='Y'||s=='y') /*if the operater choosed 'Y' */
if(temp->next==NULL) /*if "temp" is in the end of the list*/
{ temp->pre->next=pt;
pt->next=NULL;
pt->pre=temp->pre;
free(temp);
}
else /*if "temp" is in other place*/
{ pt->next=temp->next;
pt->pre=temp->pre;
temp->pre->next=pt;
temp->next->pre=pt;
free(temp);
}
else free(pt); /*if the operater make other choice*/
break;
}
else /* if the find the right place*/
{ pt->next=temp;
pt->pre=temp->pre;
temp->pre->next=pt;
temp->pre=pt;break;
}
}
Record_len++;
/*When add a record,counter "Record_len" add 1*/
printf("\tContinue(y/n;Y/N):");sign=getch();
}while(sign=='Y'||sign=='y');
printf("\n\tPress any key to go back!"); getch();
}
void print(struct stu *pp) /*PRINT*/
{
printf("%-13s %-13s %c %4d/%2d/%2d %-13s %-7s %-9s\n",pp->num,pp->name,pp
->sex,pp->birthday.year,
pp->birthday.month,pp->birthday.day,pp->ad,pp->dorm,pp->tel);
}
void file(struct stu *PF) /*FILE*/
{
char c,a,test;
static char file[80];
static n=1;
int i,count;
struct stu *pf=PF->next,*pread,*plast;
FILE *P;
if(n==1)
file[0]='\0';
pread=(struct stu*)malloc(sizeof(struct stu));
plast=PF;
clrscr();
gotoxy(1,1);
textprint("File");
printf("1. save 2.open 3.new 4.exit\n");
printf("Input your choice:\n");
c=getch();
while(!check_c(c,"1234soneSONE")) c=getch();
if((c=='1'||c=='s'||c=='S')&&PF->next==NULL) /*if the user want to save a null file*/
{ printf("Empty list,could not be saved.\n");
printf("Press any key to go back!\n");
getch();
return;
}
if((c=='2'||c=='o'||c=='O') && PF->next!=NULL) /*if open a file before save the original one*/
{ textprint("Warning");
printf("You must save your files before you open a new file.\n");
printf("Or you will lose the records you inputted just before!\n");
printf("Continue?(Y/N)");
a=getch();
if(!(a=='y' || a=='Y')) return;
}
if((c=='3'||c=='n'||c=='N')&& PF->next!=NULL)
{
printf("There are records in the memory.\nDo you want to build new one and cover these records?(Y/N)");
a=getch();
if(!(a=='Y'||a=='y'))
return;
}
switch(c)
{ case 's':
case 'S':
case '1':if(file[0]!='\0')
{ printf("\nInput file path and name(%s):\n",file);
printf("-<Press <Enter> to use the old file path,other key to save in new path>-");
test=getche();
if(test!=13)
{ putchar(test);
gets(&file[1]);
file[0]=test;
}
}
else
{printf("Input file path and name:\n");
gets(file);
}
if((P=fopen(file,"wb"))==NULL)
{ printf("Can not create file.\n");
printf("Please check your file path and name:%s,then try again.",file);
break;
}
else
{ fprintf(P,"%d\n",Record_len);
count=0;
while(pf!=NULL)
{
fprintf(P,"%s\n",pf->num);
fprintf(P,"%s\n",pf->name);
fprintf(P,"%c\n",pf->sex);
fprintf(P,"%d %d %d\n",pf->birthday.year,pf->birthday.month,pf->birthday.day);
fprintf(P,"%s\n",pf->ad);
fprintf(P,"%s\n",pf->dorm);
fprintf(P,"%s\n",pf->tel);
pf=pf->next;
gotoxy(1,7);
count++;
printf("%3d/%3d have saved...\n",count,Record_len);
}
textprint("File saved!");
}
fclose(P);
break;
case 'o':case 'O':
case '2': printf("Input file path and name(c:\\tc):\n");
gets(file);
if((P=fopen(file,"rb"))==NULL)
{ printf("Can not open file.\n");
break;
}
else
{ fscanf(P,"%d",&Record_len);
for(i=0;i<Record_len;i++)
{ pread=(struct stu*)malloc(sizeof(struct stu));
fscanf(P,"%s\n",pread->num);
fscanf(P,"%s\n",pread->name);
fscanf(P,"%c\n",&pread->sex);
fscanf(P,"%d %d %d\n",&pread->birthday.year,
&pread->birthday.month,&pread->birthday.day);
fscanf(P,"%s\n",pread->ad);
fscanf(P,"%s\n",pread->dorm);
fscanf(P,"%s\n",pread->tel);
pread->next=NULL;
pread->pre=plast;
plast->next=pread;
plast=plast->next;
}
textprint("File loaded!");
printf("%d records has been found in file %s\n",Record_len,file);
}
fclose(P);
break;
case 'n':case 'N':
case '3': PF->next=NULL;
Record_len=0;
break;
case 'e':case 'E':
case '4':exiting();break;
default:;
}
n++;
printf("\n\tPress any key to go back!");getch();
}
void search(struct stu *PS) /*SEARCH*/
{
char se,*s;
int x;
clrscr();
gotoxy(1,1);
textprint("Search");
printf("1.number 2.name 3.adress 4.dorm 5.tel 6.birthday 7.exit\n");
printf("Input your choice:\n");
se=getch();
while(!check_c(se,"1234567")) se=getch();
switch(se)
{
case '1':searchnum(PS);break;
case '2':searchname(PS);break;
case '3':searchad(PS);break;
case '4':searchdorm(PS);break;
case '5':searchtel(PS);break;
case '6':searchbirthday(PS);break;
case '7':return;
default:;
}
printf("\tPress any key to go back!");getch();
}
void list(struct stu *PS)
{
int length=0,m=0;
struct stu *pl;
char c,a[NUMLEN+1],b[NUMLEN+1];
pl=PS->next;
clrscr();
gotoxy(1,1);
textprint("List");
printf(" 1.List all\n\t2.List (m-n)\n\t3.List (m-)\n\t4.List (-n)\n\t5.Exit\n");
printf("Input your choice:\n");
c=getch();
while(c>'5' ||c<'1') c=getch();
switch(c)
{case '1':
printf("\nNum |Name |Sex|Birthday |Ad |Room |Tel \n");
while(pl!=NULL)
{ print(pl);
if(pl->sex=='M') m++;
length++;
pl=pl->next;
}
printf("\n\tTotal:%4d Male:%4d Famale:%4d",length,m,length-m);
break;
case '2':
printf("From:");
strcpy(a,inputnum(NUMLEN));
printf("To:");
strcpy(b,inputnum(NUMLEN));
printf("\nNum |Name |Sex|Birthday |Ad |Room |Tel \n");
while(pl!=NULL)
{if(strcmp(pl->num,a)>=0 && strcmp(pl->num,b)<=0 )
{ print(pl);
if(pl->sex=='M') m++;
length++;
}
pl=pl->next;
}
printf("\n\tTotal:%4d Male:%4d Famale:%4d",length,m,length-m);
break;
case '3':
printf("From:");
strcpy(a,inputnum(NUMLEN));
printf("\nNum |Name |Sex|Birthday |Ad |Room |Tel \n");
while(pl!=NULL)
{if (strcmp(pl->num,a)>=0)
{ print(pl);
if(pl->sex=='M') m++;
length++;
}
pl=pl->next;
}
printf("\n\tTotal:%4d Male:%4d Famale:%4d",length,m,length-m);
break;
case '4':
printf("To:");
strcpy(b,inputnum(NUMLEN));
printf("\nNum |Name |Sex|Birthday |Ad |Room |Tel \n");
while(pl!=NULL)
{ if( strcmp(pl->num,b)<=0 )
{ print(pl);
if(pl->sex=='M') m++;
length++;
}
pl=pl->next;
}
printf("\n\n\tTotal:%4d Male:%4d Famale:%4d",length,m,length-m);
break;
case '5':return;
default:;
}
printf("\n\t Press any key to goback! ");getch();
}
void modify(struct stu *PS)
{
char ms[NUMLEN+1],s1,s2;
int flag=0;
struct stu *pm;
pm=PS->next;
clrscr();
gotoxy(1,1);
textprint("Modify");
printf("Input the num. of the records you want to modify:\n");
gets(ms);
while(pm!=NULL)
{ if(strcmp(pm->num,ms)==0)
{ flag=1;
break;
}
pm=pm->next;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -