main.cpp

来自「作品名称:班级成绩管理系统 作者:sammual 一 该系统的设计方法:其主」· C++ 代码 · 共 311 行

CPP
311
字号
#include<iostream.h>
#include"student.h"
#include<string.h>
#include<iomanip.h>
#include<fstream.h>
#include <stdio.h>
#include <memory.h>

static int n=0;//定义各变量,n为学生人数,
int number;//number用于存放用户输入的数字
int key=123456;//key为管理员密码
int id;
char name[5];
char keyname[5]="abc";//初始用户名
char username[5];//登录时用来输入用户名
int  usernumber;//登录时用来输入密码
char sex[2];
float a,b,c;//分别存放语文,数学和物理成绩
Student person[100];
int EnterInterface()//系统的登录界面
{
	cout<<endl<<"********************************班级成绩管理系统********************************"<<endl;
	cout<<"************************************欢迎使用************************************"<<endl;
    cout<<endl<<"********************请输入你的用户名:请注意!只有三次输入机会!******************"<<endl;
	cin>>username;
	for(int k=0;strcmp(username,keyname)!=0;k++)
	{	 
		if(2==k)
		{
			cout<<"对不起,你是非法用户!再见!"<<endl;
			return 0;
		}
		cout<<endl<<"你的用户名有错,请重新输入!"<<endl;
		cin>>username;
			
	}
	
    cout<<endl<<"********************************请输入你的密码:********************************"<<endl;
	cin>>usernumber;
	for(int j=0;usernumber!=key;j++)
	
	{
		if(2==j)
		{
			cout<<"对不起,你是非法用户!再见!"<<endl;
			return 0;
		}
		cout<<endl<<"你的密码有错,请重新输入!"<<endl;
		cin>>usernumber;	
		
	}
	return 1;
}

void DataOut()//输入系统标头
{
	cout<<endl<<"**********************班级成绩管理系统***********************************"<<endl;
	cout<<"1增加  2删除  3修改  4学生查询  5学生排名 6修改密码  7修改用户名  8保存 "<<endl
		<<"9帮助"<<endl;
	cout<<"*************************************************************************"<<endl;
	cout<<"选择您所要的操作:";
}

void Dispall()//输出所有学生的成绩
{   cout<<"学号 姓名 性别 语文成绩 数学成绩 物理成绩 总分  排名"<<endl;
int m=1;
for(int i=0;i<=n;i++)
{
	if(person[i].Id()!=0)
	{
		person[i].Disp();
		cout<<setw(6)<<m<<endl;
		m++;
	}
}
}
int Del()//用于删除学生的资料
{   
	cout<<"请输入要删除的学生学号:";
	cin>>number;
	for(int ii=0;ii<n;ii++)
	{   
		//temp=person[ii].num();
		if(person[ii].Id()==number)
		{
			while(ii<n)
			{
				person[ii]=person[ii+1];
				ii++;
			}
			n--;
			return 1;
		}
		
		
	}
	return 0;	
}
void Sort()//冒泡算法用于成绩的排名
{
	for(int i=0;i<(n-1);i++)
		
		for(int j=0;j<(n-i-1);j++)
			
			if(person[j].Score()<person[j+1].Score())
				
			{Student t=person[j];
			person[j]=person[j+1];
			person[j+1]=t;
			}
}



void Inquire()//用于学生的查询
{   
	Sort();
	cout<<"请输入你的学号:";
	cin>>number;	
	for(int iiii=0;iiii<n;iiii++)
	{
		if(person[iiii].Id()==number)
		{
			cout<<"您的成绩为:"<<endl;
			cout<<"学号 姓名 性别 语文成绩 数学成绩 物理成绩 总分 排名"<<endl;
			person[iiii].Disp();
			cout<<setw(6)<<(iiii+1)<<endl;
		}
	}
}

void Change()//用于修改学生的资料
{
	cout<<"请输入要修改资料的学生学号:";
	cin>>number;
	cout<<"将该学生的资料";
	for(int iii=0;iii<n;iii++)
	{
		if(person[iii].Id()==number)
		{
			person[iii].Disp();
			cout<<endl<<"修改为:";
			cin>>id>>name>>sex>>a>>b>>c;
			person[iii].Input(id,name,sex,a,b,c);
		}
	}
}
void Save()//保存所有学生的成绩
{
	if(n>0)
	{
        Sort();
		fstream f1("students.txt",ios::out);		
		for(int jj=1;jj<=n;jj++)
		{			
			char  buffer[100];
			memset( buffer, 0, 100 );
			int jjj=0;;
			jjj=sprintf(buffer,"%d",person[jj-1].Id());
	        jjj+=sprintf(buffer+jjj,"\t%s",person[jj-1].Name());
            jjj+=sprintf(buffer+jjj,"\t%s",person[jj-1].Sex());
			jjj+=sprintf(buffer+jjj,"\t%0.1f",person[jj-1].Chinese());
            jjj+=sprintf(buffer+jjj,"\t%0.1f",person[jj-1].Math());
            jjj+=sprintf(buffer+jjj,"\t%0.1f",person[jj-1].Psysic());
            jjj+=sprintf(buffer+jjj,"\t%0.1f",person[jj-1].Score());
			jjj+=sprintf(buffer+jjj,"\t%d\n",jj);			
			f1.write(buffer,jjj);
		    memset( buffer, 0, 100 );
			
		}
		f1.close();
		cout<<endl<<"保存成功!"<<endl;
	}
   else
	   cout<<endl<<"数据库为空,请先输入学生资料!"<<endl;
}
void main()
{  
	if (1==EnterInterface())
{
	
	while(1)
	{
		DataOut();
		int time=0;//改用户名时用于检查输入次数
		int w;
		cin>>w;
		switch(w)
		{
			
		case 1:
			{					
				cout<<endl<<"请按以下顺序输入学生的资料,每个数据之间用空格隔开:"<<endl
					<<"学号 姓名 性别 语文成绩 数学成绩 物理成绩"<<endl;;
				cin>>id>>name>>sex>>a>>b>>c;
				if(n>=0)
				{
					for(int k=0;k<n;k++)
					{
						if(id==person[k].Id())
						{
							cout<<"对不起,学号有重复,请重新输入学号!"<<endl;
							cin>>id;
							break;
						}
					}
				}				
				person[n].Input(id,name,sex,a,b,c);
				n++;
				Sort();
				Dispall();
				cout<<endl;
			}
			break;
			
		case 2:		
			
			if(Del())
			{
				Sort();
				Dispall();
			}
			else
				cout<<endl<<"没有该学生或者数据库为空,请先输入学生资料!"<<endl;				
			break;
			
		case 3:			
				if(n==0)cout<<endl<<"数据库为空,请先输入学生资料!"<<endl;
				else 
				{
					Change();
					Sort();
					Dispall();
				}	
				break;
			
		case 4:
			if(n!=0)
				Inquire();
			else
				cout<<"数据库为空,请先输入学生资料!"<<endl;
			break;
		case 5:
			if(n!=0)
			{				
				Sort();
				Dispall();				
			}
			else
				cout<<"数据库为空,请先输入学生资料!"<<endl;
			break;
		case 6:
			cout<<"请输入管理员之前的密码:"<<endl;
			cin>>number;
			if(number==key)
			{cout<<"请输入您的新密码:";
			cin>>number;
			cout<<"请再次输入您的新密码:";
			int temp;
			cin>>temp;
			if(temp==number)
			{
				key=temp;
				cout<<"密码修改成功!"<<endl;
			}
			else
				cout<<"两次输入的密码不相同!"<<endl;
			}
			else
				cout<<"密码错误!";
			break;
		case 7:
			cout<<"请输入管理员之前的用户名:"<<endl;
            cin>>username;
			for(;time<2&&strcmp(username,keyname)!=0;time++)
			{
				
               cout<<endl<<"用户名错误,请重新输入"<<endl;
			   cin>>username;
			}
			if(2==time)
				{
					cout<<"用户名错误!"<<endl;
					break;
				}
			else
			{
				cout<<endl<<"请输入新的用户名:"<<endl;
				cin>>username;
				strcpy(keyname,username);
				cout<<endl<<"用户名修改成功"<<endl;
			}
		   break;
		case 8:
			Save();
			break;		
		case 9:
			cout<<endl<<"1. 本系统管理员的初始密码为:123456"<<endl;
			cout<<endl<<"2. 本系统管理员的初始用户名为:abc"<<endl;
			cout<<"3. 输入学生的资料时请按照如下的格式输入:"<<endl;
			cout<<"   学号 姓名 性别 语文成绩 数字成绩 物理成绩"<<endl;						
			break;	
		default:
			cout<<"非法输入!"<<endl;
			break;
		}
	}
	}
}

⌨️ 快捷键说明

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