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

📄 kesheguanli.cpp

📁 这是我大一暑假的课设作业。。做的通讯录。。。喜欢跟大家交流~~
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/**************************************************************************************************************
******2007年12月5日,由00748151 梁诗鸣与00748004 史秦青合作编制历时一个星期的学生选课系统程序最终完成了!******
***************************************************************************************************************/

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>
#include<cmath>

#define FILENAME "record.txt"     //此文件用于记录学生的信息(包括学号,密码,选课情况(选了该门课记录为1,为选该门课记录为0))
#define PWFILE "password.txt"    //此文件用于存放教务密码

const int MAXPWLEN=8;

struct record 
{
	char sno[100];
	char pw[10];
	int mark[5];
};


//假定教务只定出了5门课供学生选择

char courses[5][100]={"Computer",
                      "  Maths  ",
					  " Physics ",
				      "   P.E   ",
			    	  " English"};


void mainInterface();
void dean();
void student();
char * getPasswordFromFile();
char *inputPassword();
void showAllStudents();
void appendStudent();
void removeStudent();
void queryStudent();
void changCode();
void ret2MainInterface();


// *****************以下对于文件的操作是在老师授课之前,通过大量的学习,讨论,摸索学会的!

void initFile()      //文件初始化,
{
	ofstream outDate(FILENAME, ios::ate);

	//check if the file is empty

	int pos=outDate.tellp();
	if(pos==0)
	{
		//initilize amount to 0

		outDate<<setw(8)<<0<<endl;        //用8个字节存放学生总数,应该够用了哦!
	}

	outDate.close();
}


void inputRecord(ifstream inDate, struct record *pRec)       //从文件中学生信息导入
{
	inDate>>pRec->sno;
	inDate>>pRec->pw;
	int i;
	for(i=0; i<5; i++)
	{
		inDate>>pRec->mark[i];
	}
}

void outputRecord(ofstream outDate, struct record rec)   //存放数据到文件中时文件格式的组织
{
	outDate<<rec.sno;
	outDate<<' '<<setw(MAXPWLEN)<<rec.pw;
	int i;
	for(i=0; i<5; i++)
	{
		outDate<<' '<<rec.mark[i];
	}
	outDate<<endl;
}

int loadDate(struct record** pArr)    //导入整个文件的内容,返回的amount即为学生的总人数
{
	ifstream inDate(FILENAME, ios::in);

	int amount;
	inDate>>amount;

	struct record* arr=new record[amount];

	int i;
	for(i=0; i<amount; i++)
	{
		inputRecord(inDate, arr+i);
	}

	inDate.close();

	*pArr=arr;
	return amount;
}

void storeDate(struct record* arr, int amount)    //存储所有的信息到文件中
{
	ofstream outDate(FILENAME, ios::out);
	outDate<<setw(8)<<amount<<endl;
	
	int i;
	for(i=0; i<amount; i++)
	{
		outputRecord(outDate, arr[i]);
	}
	outDate.close();
}


//***************************************

void main()
{
	initFile();
	mainInterface();
}

void mainInterface()                    //主界面
{
	int choice;
	cout<<"************欢迎进入选课系统!希望我们的程序能为大家带来便利!***********"<<endl<<endl;
	cout<<"教务人员请输入1, 学生请输入2,退出请输入3 : "<<endl;
	cin>>choice;
	switch(choice)
	{
	case 1:
		dean();
		break;
	case 2:
		student();
		break;
	case 3:
		exit(0);
		break;
	}
}


void dean()   //教务处主界面
{
	char *inputPW;
	char *correctPW=getPasswordFromFile();
	
	system("cls");
	cout<<"请输入教务登录密码: "<<endl;
	int i;
	for(i=0; i<3; i++)
	{
		inputPW=inputPassword();           //检查输入正确的密码才可进入教务的管理页面
		if(strcmp(inputPW, correctPW)==0)
		{
			cout<<"登录成功,输入任意键进入系统..."<<endl;
     		getch();
			system("cls");
			break;
		}
		system("cls");
		cout<<"密码输入错误,请重新输入:"<<endl;
	}

	if(i==3)
	{
		cout<<"密码输错3次,自动返回主界面"<<endl;
		system("pause");
		system("cls");
		mainInterface();
		return;
	}



	while(true)
	{
		system("cls");
		//show all student numbers whose records are existing
		showAllStudents();
	
		int choice;
		cout<<"添加输入1, 删除输入2, 查看输入3,修改密码输入4,返回主界面输入5"<<endl;
		cin>>choice;
		switch(choice)
		{
		case 1:
			appendStudent();
			break;
		case 2:
			removeStudent();
			break;
		case 3:
			queryStudent();
			break;
		case 4:
			changCode();
			break;
		case 5:
			ret2MainInterface();
			return;
		}
	}
}


char * getPasswordFromFile()      //从保存文件中导出密码
{
	ifstream inDate(PWFILE, ios::in);
	char * pw=new char[100];

	//get password
	inDate>>pw;
	inDate.close();

	return pw;
}

char *inputPassword()             //用户输入密码
{
	char *pw=new char[100];

	int len=0;
	char ch;

	while((ch=getch())!='\r')    //输入密码时,不回显用户的输入信息,用"*"代替,符合现实哦,通过MSDN学会的^_^
	{
		cout<<'*';
		flush(cout);
		pw[len++]=ch;
	}
	cout<<endl;

	pw[len]=0;           //记录用户的输入信息,便于比较
	return pw;
}


void showAllStudents()    //教务界面中显示所有在档案中的学生学号(学号是按从小到大的顺序排列的,便于阅读)
{
	ifstream inDate(FILENAME, ios::in);

	int amount;
	inDate>>amount;

	struct record* arr=new record[amount];
    struct record t;
	int i,j;
	for(i=0; i<amount; i++)
	{
		inputRecord(inDate, arr+i);
	}

	inDate.close();

	for(i=0;i<amount;i++)         //按学号大小排序          
	{
		for(j=0;j<amount-i;j++)
		{
			if(strcmp(arr[j].sno,arr[j+1].sno)>0)
			{
				t=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=t;
			}
		}
	}
	for(i=0;i<amount;i++)
	{
		cout<<arr[i].sno<<endl;
	}

}

bool checkIfTheStudentIsExisting(struct record* arr, int size, char *sno)       //该函数用于检查是否存在某学生的档案
{
    int i;
    for(i=0; i<size; i++)
    {
       if(strcmp(arr[i].sno, sno)==0)
	   {
        return true;
	   }
    }
    return false;
}

void appendStudent()          //教务的强大功能之一:添加学生档案(用于新生入学时)
{
    char sno[100];
    cout<<"请输入所需添加的学生的学号"<<endl; 
	cin>>sno;
    struct record *arr;
    int amount=loadDate(&arr);

    if(checkIfTheStudentIsExisting(arr, amount, sno))    //避免学号的重复
	{
       cout<<"该学号的学生已存在!"<<endl;
       system("pause");
       return;
	}
  
  
    //write the record to the file

    ofstream outDate(FILENAME, ios::ate);
    outDate<<sno;
    int i;

	outDate<<' ';
	for(i=0; i<8; i++)
	{
		outDate<<0;
	}
    for(i=0; i<5; i++)
	{
       outDate<<' '<<0;
	}
    outDate<<endl;

    //update the amount in the file

    outDate.seekp(0, ios::beg);      //指向文件的开头,用于存放amount,花了大量的时间才学会这短短一行的操作啊!^_^
    outDate<<setw(8)<<(amount+1);
    outDate.close();
   
}

bool removeInMem(struct record* arr, int size, char *sno)     //判断是否真的删除了该学生档案
{
	int i;
	for(i=0; i<size; i++)
	{
		if(strcmp(arr[i].sno, sno)==0)
		{
		    cout<<"确认删除请按y,按其他任意键继续: ";
			char ch;
			cin>>ch;
			if(ch=='y')
			{
				int j;
			    for(j=i+1; j<size; j++)     
				{
			    	arr[j-1]=arr[j];   //删除后,文件中所有在该学生后头的学生档案应该上移一行,否则,文件会乱了!
				}
		    	return true;
			}
		}
	}
	return false;
}

void removeStudent()                     //教务的强大功能之二: 删除学生档案 (用于学生退学,毕业时)
{
	cout<<"输入要删除信息的学生的学号"<<endl;
	char sno[100];
	cin>>sno;

	struct record *arr;
	int amount=loadDate(&arr);
    

	if(!checkIfTheStudentIsExisting(arr, amount, sno))
	{
       cout<<"不存在该学号的学生!"<<endl;
       system("pause");
       return;
	}

	//remove in the memory(remember to update amount)
	if(removeInMem(arr, amount, sno)) amount--;

	//store to the file
	storeDate(arr, amount);
}

void queryStudent()             // 教务的强大功能之三:查询学生的选课信息
{
	cout<<"输入要查查询的学生的学号"<<endl;
	char sno[100];
	cin>>sno;

	//load information into the memory
	struct record *arr;
	int amount=loadDate(&arr);

	int i;
	for(i=0; i<amount; i++)
	{
		if(strcmp(arr[i].sno, sno)==0)    //找到该学生的学号后,根据文件中记录课程的“0”和“1”来判断学生是否选了该门课
		{
			int j,flag=0;
			
			for(j=0; j<5; j++)
			{
				if(arr[i].mark[j]==1)
				{
					flag=1;

				}
			}
			if(flag==0) cout<<"该学生还没有进行选课!"<<endl;
			else
			{
				cout<<"该学生已选了如下的课程:"<<endl;
				for(j=0; j<5; j++)
				{
			    	if(arr[i].mark[j]==1)
					{
					   cout<<courses[j]<<"  ";
  
					}
				}
				cout<<endl;
			}
			break;

		}
	}
	if(i==amount) cout<<"没有你所查询的学生信息"<<endl;
	system("pause");
}

void changCode()                     //教务的强大功能之四:修改密码
{
	char *inputPW1;
	char *inputPW2;

	while(1)
	{
		system("cls");
	cout<<"请输入教务新密码: "<<endl;
	inputPW1=inputPassword();
	cout<<"请再次输入教务新密码: "<<endl;
	inputPW2=inputPassword();
	if(strcmp(inputPW1, inputPW2)==0)
	{
		ofstream outDate(PWFILE);        //两次密码输入一致,就把新密码存储到文件中
        outDate<<inputPW1;
		cout<<"密码修改成功!"<<endl;
     	getch();
		break;
	}
	else
	{
		cout<<"两次输入密码不一致!"<<endl
		    <<"返回教务主界面请输入y,输入其他任意键继续更改密码"<<endl;
	    char choice ;
		cin>>choice;
		if(choice=='y'||choice=='Y')
			break;
	}
	}
	system("pause");
	void dean();

⌨️ 快捷键说明

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