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

📄 成绩管理系统.cpp

📁 学生成绩管理系统
💻 CPP
字号:
#include<stdio.h>
#include<io.h>
#include<iostream>
#include<conio.h>
#include <string.h> 

using namespace std;

struct student
{
	int number;
	char name[15];
	int score;
}person[1000];



void add();
void del();
void change();
int show(FILE *);
void sort();
int cmp(const void *,const void *);
int cmp1(const void *,const void *);
void sort();

int main()
{
	int choose,i=0;
	FILE *aptr;
	
	do{
		fflush(stdin);
		
		printf("\t\t\tWelcome to the student system!\n");
		printf("1.add student imformation.\n");
		printf("2.delete student's imformation.\n");
		printf("3.change student's imformation.\n");
		printf("4.show all the imformation\n");
		printf("5.sort\n");
		printf("0.exit the system\n");
		
		printf("\n\nPlease input your choose.\n");
		scanf("%d",&choose);

		if(choose==0)
		{	
			printf("感谢您的使用!");
			return 0;
		}

		if((aptr=fopen("student.dat","a+"))==NULL)
		{	
			printf("File coule not be opened!\nsorry! system error\n");
			return 0;
		}

		if(choose!=1&&choose!=2&&choose!=3&&choose!=4&&choose!=5)
		{
			printf("对不起,您的选择有误,请再做选择\n");
			choose=8;
		}
		switch(choose)
		{
		case 1:
			add();
			break;
		case 2:
			del();
			break;
		case 3:
			change();
			break;
		case 4:
			show(aptr);
			printf("回车返回!\n");
			fflush(stdin);
			while(getchar()!='\n');
			break;
		case 5:
			sort();
			break;
		default:
			break;
		}
			fclose(aptr);
		}
	while(1);

	
	return 0;

}

void add()
{
	FILE *inptr=NULL,*aptr=NULL;
	char input[10],a;
	int innum=0,choose;

	if((aptr=fopen("student.dat","a+"))==NULL)
	{	
		printf("File coule not be opened!\nsorry! system error\n");
		return ;
	}
	else
	{
		do{
			printf("Do you want to input by\n"
			   "1.file\n2.input directlly\n");
			scanf("%d",&choose);
			switch(choose)
			{
			case 1: 	
					printf("please input the file name\n");
					scanf("%s",input);
					if((inptr=fopen(input,"r"))==NULL)
						printf("File coule not be opened!\n");
					else
					{	
						while(fscanf(inptr,"%d%s%d",&person[0].number,person[0].name,&person[0].score)==3)
						{
							fprintf(aptr,"%-6d %-16s %-6d\n",person[0].number,person[0].name,person[0].score);
						}
						fclose(inptr);
					}
					break;			
			case 2:
					printf("input the student's number,name,scores.\n");
					printf("输入以#号结束\n");
					while(scanf("%d%s%d",&person[0].number,person[0].name,&person[0].score)==3)		
					{
						if(person[0].number)
						fprintf(aptr,"%-6d %-16s %-6d\n",person[0].number,person[0].name,person[0].score);
					}
					break;
			default:
					printf("输入有误!\n");
					break;
			}
			printf("是否继续添加文件(y or n)\n");
			fflush(stdin);
			scanf("%c",&a);
		}while(a=='y');
		fclose(aptr);

	} 


	return ;

}

void del()
{
	int number,i=0,n,j=0;
	char a;
	FILE *aptr=NULL;
	do
	{
		i=0;	
		if((aptr=fopen("student.dat","r"))==NULL)
		{	
			printf("File coule not be opened!\nsorry! system error\n");
			return ;
		}
		printf("input the student's numaber you want to delete\n");
		scanf("%d",&number);
		
			for(j=0;fscanf(aptr,"%d%s%d",&person[j].number,person[j].name,&person[j].score)==3;j++)
			{
				if(number==person[j].number)
				{
					printf("%-6d%-16s%-6d\n",person[j].number,person[j].name,person[j].score);
					printf("are you sure?(y or n)\n");
					fflush(stdin);
					scanf("%c",&a);
					if(a=='y')
					{
						i=1;
						printf("Success!\n");
						j--;
						continue;
					}
					else
					{
						i=2;
						break;
					}
				}
			}
		
						fclose(aptr);	
			if(i==0||i==1)
				{
					if(i==0)
					printf("the student is not exsit\n");
					else
					{					
						aptr=fopen("student.dat","w");
						n=j;
						for(j=0;j<n;j++)
							fprintf(aptr,"%-6d %-16s %-6d\n",person[j].number,person[j].name,person[j].score);
					}
				}
		
				fclose(aptr);

				printf("do you want to delete more?\n");
		fflush(stdin);
		scanf("%c",&a);
	}
	while(a=='y');
		

return ;
	
}

void change()
{
	int i=0,n,flag=0;
	char a;
	struct student * p;
	p=person;
	FILE *aptr=NULL,*temp=NULL;

	do
	{
		flag=0;
		if((aptr=fopen("student.dat","r"))==NULL)
		{	
			printf("File coule not be opened!\nsorry! system error\n");
			return ;
		}
		n=show(aptr);
		printf("\n输入您要进行修改的学生的学号(现在的)。\n");
		scanf("%c",&a);
		flag=0;
		for(i=0;i<n;i++)
		{
			if(a==person[i].number)
			{
				flag=1;
				break;
			}
		}
		if(flag==0)
			printf("您选择的学号不存在!\n");
		else
		{
			printf("请选择您需要修改的项目.\n1.学号\n2.姓名\n3.成绩\n");
			fflush(stdin);
			scanf("%c",&a);
			switch(a)
			{
			case '2':
				printf("请输入新的姓名\n");
				scanf("%s",person[i].name);
				break;
			case '1':
				printf("请输入修改后的学号");
				scanf("%d",&person[i].number);
				break;
			case '3':
				printf("请输入修改后的成绩");
				scanf("%d",person[i].score);
				break;
			default:
				printf("您输入的选项不存在!\n");
				break;
			}
		}

	
	printf("您还想继续吗?(y or n)\n");
	fflush(stdin);
	scanf("%c",&a);	
	
	}while(a=='y');	

	fclose(aptr);

	if((aptr=fopen("student.dat","w"))==NULL)
	{
		for(i=0;i<n;i++)
		{			
			fprintf(aptr,"%-6d %-16s %-6d\n",person[i].number,person[i].name,person[i].score);
		}
	}

	fclose(aptr);

	return ;

}



int show(FILE *aptr)
{
	int i=0;
	while(fscanf(aptr,"%d%s%d",&person[i].number,person[i].name,&person[i].score)==3)
	{
		printf("%-6d%-16s%-6d\n",person[i].number,person[i].name,person[i].score);
		i+=1;
	}

	return i;

}

void sort()
{
	FILE * aptr;
	int n,i;
	char c;

	if((aptr=fopen("student.dat","r"))==NULL)
		{	
			printf("File coule not be opened!\nsorry! system error\n");
			return ;
		}

		printf("原来的顺序:\n");
		n=show(aptr);

		qsort(person,n,sizeof(person[0]),cmp);

		printf("按成绩排序之后:\n");

		for(i=0;i<n;i++)
			printf("%-6d %-16s %-6d\n",person[i].number,person[i].name,person[i].score);
		fclose(aptr);

	printf("是否按当前顺序储存(y or n)或者文件按学号顺序储存\n");
	
	fflush(stdin);
	scanf("%c",&c);
	
	if(c=='y')
	{
		if((aptr=fopen("student.dat","w"))==NULL)
		{	
			printf("File coule not be opened!\nsorry! system error\n");
			return ;
		}
		
		for(i=0;i<n;i++)
		{
			fprintf(aptr,"%-6d %-16s %-6d\n",person[i].number,person[i].name,person[i].score);
		}
		printf("........");
		printf("储存成功!\n");
		fclose(aptr);
	}
	
	else
	{
		printf("文件按学号顺序储存!\n");
	
		qsort(person,n,sizeof(person[0]),cmp1);

		if((aptr=fopen("student.dat","w"))==NULL)
		{	
			printf("File coule not be opened!\nsorry! system error\n");
			return ;
		}
		
		for(i=0;i<n;i++)
		{
			fprintf(aptr,"%-6d %-16s %-6d\n",person[i].number,person[i].name,person[i].score);
		}
		printf(".........\n");
		printf("储存成功!\n");
		fclose(aptr);
		
	}
	fflush(stdin);

	printf("按回车返回\n!");
	while(getchar()!='\n');

}

int cmp(const void*a,const void*b)
{
	struct student *pa,*pb;
	pa=(struct student*)a;
	pb=(struct student*)b;
	return -(pa->score-pb->score);
}

int cmp1(const void*a,const void*b)
{
	struct student *pa,*pb;
	pa=(struct student*)a;
	pb=(struct student*)b;
	return pa->number-pb->number;
}
































⌨️ 快捷键说明

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