⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stuf.c

📁 这是过去自己编写的一个初级Dos程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** Stuf.c***/
#include "Student.c"

STUDENT *init()
{
   return NULL;
}

/*clear screen*/
void ClrScr()
{
	int i,j;
	puttext(2,3,78,23,c);
	gotoxy(2,3);
}

/*help*/
void Help()
{
	ClrScr();
	DrawFrame(10,5,50,8,0,7);
	gotoxy(15,6);
	cprintf("click menu to run each funtion");
	getch();
	ClrScr();
}

/*version information*/
void Ver()
{
	ClrScr();
	DrawFrame(10,5,50,8,0,7); 
	gotoxy(15,6);
	cprintf("Ver 2.23 finished by LIU Binbin");
	getch();
	ClrScr();
}

/*start to input data*/
STUDENT *create()
{
	int i; int s; int staticcount=4;
	STUDENT *h=NULL,*info;
	char *ch[]={"Student Number: ","Student Name: ","Score1: ","Score2: ","Score3: "};
	ClrScr();
	if(head!=NULL)
	{
		saveChange(head);
		ClrScr();
	}
	gotoxy(2,22);
	printf("Input student's No. \"@\" to end. Don't press ESC before you finish it.");
	gotoxy(2,23);
	printf("ID no more than 11 characters. Name no more than 15 characters.");
	for(;;)
	{
		info=(STUDENT *)malloc(sizeof(STUDENT));
		if(!info)
		{
			gettext(25,9,54,12,infow);
			DrawFrame(25,9,54,12,WHITE,RED);
			gotoxy(26,10);
			printf("out of memory               ");
			gotoxy(26,11);
			printf("                            ");
			getch();
			return NULL;
		}
		for(i=0;i<5;i++)
		{
			gotoxy(5,3*i+5);
			printf("%s",ch[i]);
		}
		gettext(5,5,36,17,Fuleft1);
		gotoxy(21,5);
		inputs(info->no,11);
		if(info->no[0]=='@') break;
		gotoxy(19,8);
		inputs(info->name,15);
		s=0;
		for(i=0;i<N;i++)
		{
			do
			{
				gotoxy(13,i*3+11);
				gettext(2,3,39,20,Fuleft);
				scanf("%d",&info->score[i]);
				if(info->score[i]>100||info->score[i]<0)
				{
					gettext(25,9,54,12,infow);
					DrawFrame(25,9,54,12,WHITE,RED);
					gotoxy(26,10);
					cprintf("Bad data, try again.        ");
					gotoxy(26,11);
					cprintf("                            ");
					getch();
					puttext(25,9,54,12,infow);
					puttext(2,3,39,20,Fuleft);
				}
			}while(info->score[i]>100||info->score[i]<0);
			s=s+info->score[i];
		}
		puttext(5,5,36,17,Fuleft1);
		info->sum=s;
		info->average=(float)s/N;
		info->order=0;
		info->next=h;
		h=info;
		gotoxy(43,3);
		printf("No.   Name           sc1  sc2  sc3  ");
		gotoxy(43,staticcount);
		printf("%-6s",info->no);
		printf("%-15s",info->name);
		for(i=0;i<3;i++)
		{
			printf("%-5d",info->score[i]);
		}
		staticcount++;
	}
	return(h);
}

/*Add records*/
STUDENT  *insert(STUDENT *h)
{
	STUDENT *p,*q,*info;
	char s[11];
	int s1,i;
	char *ch[]={"Student Number: ","Student Name: ","Score1: ","Score2: ","Score3: "};
	gotoxy(5,10);
	printf("Input the No. before new record: ");
	gotoxy(15,11);
	scanf("%s",s);
	puttext(5,5,36,17,Nuleft);
	for(i=0;i<5;i++)
	{
		gotoxy(5,3*i+5);
		printf("%s",ch[i]);
	}
	gettext(2,3,39,20,Fuleft);
	gotoxy(2,22);
	printf("Don't press ESC before you finish it.");
	gotoxy(2,23);
	printf("ID no more than 11 characters. Name no more than 15 characters.");
	info=(STUDENT *)malloc(sizeof(STUDENT));
	if(!info)
	{
		gettext(25,9,54,12,infow);
		DrawFrame(25,9,54,12,WHITE,RED);
		gotoxy(26,10);
		printf("out of memory               ");
		gotoxy(26,11);
		printf("                            ");
		getch();
		return NULL;
	}
	gotoxy(21,5);
	inputs(info->no,11);
	gotoxy(19,8);
	inputs(info->name,15);
	s1=0;
	for(i=0;i<N;i++)
	{
		do
		{
			gotoxy(13,i*3+11);
			gettext(2,3,39,20,Fuleft);
			scanf("%d",&info->score[i]);
			if(info->score[i]>100||info->score[i]<0)
			{
				gettext(25,9,54,12,infow);
				DrawFrame(25,9,54,12,WHITE,RED);
				gotoxy(26,10);
				cprintf("Bad data, try again.        ");
				gotoxy(26,11);
				cprintf("                            ");
				getch();
				puttext(25,9,54,12,infow);
				puttext(2,3,39,20,Fuleft);
			}
		}while(info->score[i]>100||info->score[i]<0);
		s1=s1+info->score[i];
	}
	info->sum=s1;
	info->average=(float)s1/N;
	info->order=0;
	info->next=NULL;
	p=h;
	q=h;
	while(strcmp(p->no,s)&&p!=NULL)
	{
		q=p;
		p=p->next;
	}
	if(p==NULL)
		if(p==h)
			h=info;
		else
			q->next=info;
	else if(p==h)
	{
		info->next=p;
		h=info;
	}
	else
	{
		info->next=p;
		q->next=info;
	}
	DrawFrame(25,9,54,12,0,7);
	gotoxy(26,10);
	cprintf("have inserted %s student.",info->name);
	gotoxy(26,11);
	cprintf("Don't forget save!");
	return(h);
}

/*input data*/
inputs(char *s, int count)
{
	char p[255];
	do
	{
		gettext(2,3,39,20,Fuleft);
		if(count==11)
			gotoxy(21,5);
		else
			gotoxy(19,8);
		scanf("%s",p);
		if(strlen(p)>count)
		{
			gettext(25,9,54,12,infow);
			DrawFrame(25,9,54,12,WHITE,RED);
			gotoxy(26,10);
			cprintf("too long!                   ");
			gotoxy(26,11);
			cprintf("                            ");
			getch();
			puttext(25,9,54,12,infow);
			puttext(2,3,39,20,Fuleft);
		}
	}while(strlen(p)>count);
	strcpy(s,p);
}

/*delete a record*/
STUDENT *delete(STUDENT *h)
{
	STUDENT *p,*q;
	char s[11];
	gotoxy(2,11);
	printf("Input No. you want to delete: ");
	scanf("%s",s);
	q=p=h;
	while(strcmp(p->no,s)&&p!=NULL)
	{
		q=p;
		p=p->next;
	}
	if(p==NULL)
	{
		DrawFrame(25,9,54,12,WHITE,RED);
		gotoxy(26,10);
		cprintf("list no %s student          ",s);
		gotoxy(26,11);
		cprintf("                            ");
		getch();
	}
	else
	{
		ClrScr();
		gotoxy(43,3);
		printf("No.   Name           sc1  sc2  sc3  ");
		gotoxy(43,4);
		printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
		DrawFrame(25,9,54,12,WHITE,RED);
		gotoxy(26,10);
		cprintf("Are you sure? Y/N           ");
		gotoxy(26,11);
		cprintf("                            ");
		gotoxy(44,10);
		getchar();
		if(getchar()=='y')
		{
			if(p==h)
				h=p->next;
			else
				q->next=p->next;
			free(p);
			DrawFrame(25,9,54,12,0,7);
			gotoxy(26,10);
			cprintf("have deleted No %s student",s);
			gotoxy(26,11);
			cprintf("Don't forget save!          ");
			getch();
		}
	}
	return(h);
}

/*load file*/
STUDENT *load()
{
	STUDENT *p,*q,*h=NULL;
	FILE *fp;
	char infile[10];
	if(head!=NULL)
	{
		saveChange(head);
		ClrScr();
	}
	puttext(2,2,22,8,ssmenu);
	DrawFrame(4,4,40,6,0,7);
	BlackText(0,1,"File");
	textbackground(WHITE);
	gotoxy(5,5);
	printf("File:             ");
	gotoxy(11,5);
	textbackground(BLACK);
	textcolor(WHITE);
	cprintf("No more than 8 chs.");
	getch();
	gotoxy(11,5);
	textbackground(WHITE);
	textcolor(BLACK);
	cprintf("                             ");
	gotoxy(11,5);
	scanf("%s",infile);
	if((fp=fopen(infile,"rb"))==NULL)
	{
		gotoxy(5,5);
		printf("can not find file                  ");
		getch();
		return 0;
	}
	else
	{
		p=(STUDENT *)malloc(sizeof(STUDENT));
		if(!p)
		{
			DrawFrame(25,9,54,12,WHITE,RED);
			gotoxy(26,10);
			cprintf("out of memory!              ");
			gotoxy(26,11);
			cprintf("                            ");
			return h;
		}
		h=p;
		while(!feof(fp))
		{
			if(1!=fread(p,sizeof(STUDENT),1,fp))
			break;
			p->next=(STUDENT *)malloc(sizeof(STUDENT));
			if(!p->next)
			{
				DrawFrame(25,9,54,12,WHITE,RED);
				gotoxy(26,10);
				cprintf("out of memory!              ");
				gotoxy(26,11);
				cprintf("                            ");
				return h;
			}
			q=p;
			p=p->next;
		}
		q->next=NULL;
		fclose(fp);
		return h;
	}
}

/*save file*/
void savefile(STUDENT *h)
{
	FILE *fp;
	STUDENT *p;
	char outfile[10];
	puttext(2,2,22,8,ssmenu);
	DrawFrame(4,6,40,8,0,7);
	BlackText(0,1,"File");
	RedText(0,3,"Load               ");
	BlackText(0,5,"Save               ");
	gotoxy(5,7);
	printf("File:             ");
	gotoxy(11,7);
	textbackground(BLACK);
	textcolor(WHITE);
	cprintf("No more than 8 chs.");
	getch();
	gotoxy(11,7);
	textbackground(WHITE);
	textcolor(BLACK);
	cprintf("                             ");
	gotoxy(11,7);
	scanf("%s",outfile);
	if((fp=fopen(outfile,"wb"))==NULL)
	{
		gotoxy(5,7);
		printf("can not open file");
		getch();
		puttext(1,1,80,25,save);
	}
	p=h;
	while(p!=NULL)
	{
		fwrite(p,sizeof(STUDENT),1,fp);
		p=p->next;
	}
	fclose(fp);
}

/*Save before exit*/
void saveend(STUDENT *h)
{
	FILE *fp;
	STUDENT *p;
	char outfile[10];
	DrawFrame(25,9,54,12,0,7);
	gotoxy(26,10);
	cprintf("   Save before exit? Y/N");
	if(getch()=='n')exit(1);
	gotoxy(26,10);
	printf("file:                   ");
	gotoxy(32,10);
	textbackground(BLACK);
	textcolor(WHITE);
	cprintf("No more than 8 chs. ");
	getch();
	gotoxy(32,10);
	textbackground(WHITE);
	textcolor(BLACK);
	cprintf("                     ");
	gotoxy(32,10);
	scanf("%s",outfile);
	if((fp=fopen(outfile,"wb"))==NULL)
	{
		gotoxy(26,10);
		printf("can not open file!!");
		getch();
		puttext(1,1,80,25,save);
	}
	p=h;
	while(p!=NULL)
	{
		fwrite(p,sizeof(STUDENT),1,fp);
		p=p->next;
	}
	fclose(fp);
	gotoxy(26,10);
	cprintf("        File saved!!    ");
	getch();
}

/*print all data*/
void print(STUDENT *h)
{
	int i=0;
	STUDENT *p;
	p=h;
	ClrScr();
	gotoxy(43,3);
	printf("No.   Name           sc1  sc2  sc3  ");
	while(p!=NULL)
	{
		i++;
		gotoxy(43,i+3);
		printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
		p=p->next;
	}
}

/*calculate sum and average*/
void calculate(STUDENT *h)
{
	STUDENT *p;
	int i=0;
	long s=0;
	float average=0;
	p=h;
	ClrScr();
	if(p==NULL);
	while(p!=NULL)
	{
		s+=p->sum;
		i++;
		p=p->next;
	}
	average=(float)s/i;
	gotoxy(43,3);
	printf("All students' sum score is:%ld",s);
	gotoxy(43,4);
	printf("All students' average is %5.2f",average);
	gotoxy(43,5);
	printf("No.   Name           Sum     Average");
	i=0;
	p=h;
	while(p!=NULL)
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -