课程设计没有选很难,因为最近的学习内容很多,选了一个比较熟悉的课题。
昨天晚上花了几个小时写完基本功能,今天加入了文件操作和debug
用了STL之后比上学期的教师工资管理系统代码量更少,写起来也更方便一些。
部分效果图
txt文件的截图
代码由main.cpp和student.h组成
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"student.h"
using namespace std;
//-----------------------------------------------菜单--------------------------------------------------------------
void menu()
{
system("color F5");
system("mode con cols=150 lines=40");
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t\t\t\t\t\t**********欢迎使用学生成绩管理系统**********");
printf("\n\n");
printf("\n");
printf("\t\t\t\t\t\t|1.录入学生信息");
printf("\t\t\t\t\t|2.删除学生信息");
printf("\n\n");
printf("\t\t\t\t\t\t|3.浏览学生信息");
printf("\t\t\t\t\t|4.修改学生信息");
printf("\n\n");
printf("\t\t\t\t\t\t|5.查找学生信息");
printf("\t\t\t\t\t|0.退出管理系统");
printf("\n\n\n\n");
printf("\t\t\t\t\t请输入你想要的项目的序号:");
}
int main()
{
xt p;
vector<student>stu;
Read(stu);
while (1)
{
menu();
fflush(stdin);
int choise = 0;
cin >> choise;
fflush(stdin); //清理缓冲区
system("cls"); //清屏
switch (choise)
{
case 0:
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t正常退出!\n\t\t\t\t\t\t\t\t");
system("pause");
exit(0);
break;
case 1:
p.AddStu(stu);
break;
case 2:
p.DelStu(stu);
break;
case 3:
p.ShowStu(stu);
break;
case 4:
p.AltStu(stu);
break;
case 5:
p.FindStu(stu);
}
p.SortStu(stu);
Save(stu);
system("pause");
system("cls");
}
return 0;
}
#pragma once
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<sstream>
using namespace std;
const string fileName = "grade.txt";
class student {
public:
string name;
int id;
double Final;
double Mid;
double Usual;
double Grade;
}s;
bool Comp(student a, student b)
{
return a.Grade > b.Grade;//从大到小排
}
class xt
{
public:
void AddStu(vector<student>& stu);
void DelStu(vector<student>& stu);
void AltStu(vector<student>& stu);
void ShowStu(vector<student>& stu);
void FindStu(vector<student>& stu);
void CountGrade(vector<student>& stu);
void SortStu(vector<student>& stu);
};
void xt::CountGrade(vector<student>& stu)
{
vector<student>::iterator it;
for (vector<student>::iterator it = stu.begin(); it != stu.end(); it++)
{
it->Grade = (it->Final) * 0.4 + (it->Mid) * 0.2 + (it->Usual) * 0.4;
}
}
void xt::SortStu(vector<student>& stu)
{
sort(stu.begin(), stu.end(), Comp);
}
void xt::AddStu(vector<student>& stu)
{
cout << "\t\t\t\t\t 请输入学生学号:";
cin >> s.id;
cout << "\t\t\t\t\t 请输入学生姓名:";
cin >> s.name;
cout << "\t\t\t\t\t 请输入学生期中成绩:";
cin >> s.Mid;
cout << "\t\t\t\t\t 请输入学生期末成绩:";
cin >> s.Final;
cout << "\t\t\t\t\t 请输入学生平时成绩:";
cin >> s.Usual;
stu.push_back(s);
xt::CountGrade(stu);
}
void xt::DelStu(vector<student>& stu)
{
int n;
cout << "\t\t\t\t\t 输入你要删除学生的学号 :";
cin >> n;
vector<student>::iterator it;
for (it = stu.begin(); it != stu.end(); it++)
{
if ((*it).id == n)
{
stu.erase(it); return;
}
}
cout << endl << endl;
cout << "\t\t\t\t\t 没有找到这名同学!" << endl;
}
void xt::ShowStu(vector<student>& stu)
{
int t = 1;
cout << "排名\t\t学号\t\t姓名\t\t 期中成绩\t\t 期末成绩\t\t 平时成绩\t\t 总成绩" << endl;
for (vector<student>::iterator it = stu.begin(); it != stu.end(); it++,t++)
{
cout <<t<< "\t\t" << (*it).id << "\t\t" << (*it).name << "\t\t\t" << (*it).Mid << "\t\t\t" << (*it).Final << "\t\t\t" << (*it).Usual << "\t\t\t" << (*it).Grade << endl;
cout << "--------------------------------------------------------------------------------------------------------------------------------------------" << endl;
}
}
void xt::FindStu(vector<student>& stu)
{
string n;
vector<student>::iterator it; //迭代器
cout << "\t\t\t\t\t 输入你要查看学生的姓名 :";
cin >> n;
for (it = stu.begin(); it != stu.end(); it++)
{
if ((*it).name == n)
{
cout << "该名学生信息如下:"<<endl << endl;
cout << "\t\t学号\t\t姓名\t\t 期中成绩\t\t 期末成绩\t\t 平时成绩\t\t 总成绩" << endl;
cout << "\t\t" << (*it).id << "\t\t" << (*it).name << "\t\t\t" << (*it).Mid << "\t\t\t" << (*it).Final << "\t\t\t" << (*it).Usual << "\t\t\t" << (*it).Grade << endl;
cout << endl << endl;
break;
}
}
if (it == stu.end()) {
cout << endl << endl;
cout << "\t\t\t\t\t 没有找到这名同学!" << endl;
}
}
void xt::AltStu(vector<student>& stu)
{
int n;
vector<student>::iterator it; //迭代器
cout << "\t\t\t\t\t 输入你要修改学生的学号 :";
cin >> n;
for (it = stu.begin(); it != stu.end(); it++)
{
if ((*it).id == n)
{
cout << "\t\t学号\t\t姓名\t\t 期中成绩\t\t 期末成绩\t\t 平时成绩\t\t 总成绩" << endl;
cout << "\t\t" << (*it).id << "\t\t" << (*it).name << "\t\t\t" << (*it).Mid << "\t\t\t" << (*it).Final << "\t\t\t" << (*it).Usual << "\t\t\t" << (*it).Grade << endl;
cout << endl << endl;
break;
}
}
if (it == stu.end()) {
cout << endl << endl;
cout << "\t\t\t\t\t 错误的操作! :" << endl;
return;
}
int c=1;
while (c)
{
cout << "\t\t\t\t\t 选择你要修改的信息:(1学号 2姓名 3期中成绩 4期末成绩 5平时成绩 0退出修改)"<<endl;
cout << "\t\t\t\t\t ";
cin >> c;
switch (c)
{
case 1:
cout << "\t\t\t\t\t 重新输入学号:";
cin >> s.id; (*it).id = s.id;
break;
case 2:
cout << "\t\t\t\t\t 重新输入姓名:";
cin >> s.name; (*it).name = s.name;
break;
case 3:
cout << "\t\t\t\t\t 重新输入期中成绩:";
cin >> s.Mid; (*it).Mid = s.Mid;
break;
case 4:
cout << "\t\t\t\t\t 重新输入期末成绩:";
cin >> s.Final; (*it).Final = s.Final;
break;
case 5:
cout << "\t\t\t\t\t 重新输入平时成绩:";
cin >> s.Usual; (*it).Usual = s.Usual;
break;
}
cout << " -------------------------------------------------------------------------------------" << endl;
}
xt::CountGrade(stu);
cout << endl << endl;
cout << "\t\t\t\t\t 修改成功。。。" << endl;
cout << "\t\t学号\t\t姓名\t\t 期中成绩\t\t 期末成绩\t\t 平时成绩\t\t 总成绩" << endl ;
cout << "\t\t" << (*it).id << "\t\t" << (*it).name << "\t\t\t" << (*it).Mid << "\t\t\t" << (*it).Final << "\t\t\t" << (*it).Usual <<"\t\t\t"<<(*it).Grade<< endl;
}
//----------------------------------------文件操作----------------------------------------------------------------------------
void Read(vector<student>& stu)
{
ifstream in;
in.open(fileName);
string sline;//每一行
student t;
while (getline(in, sline)) //逐行读取txt文件
{
istringstream sin(sline);
sin >> t.id >> t.name >> t.Mid>>t.Final>>t.Usual>>t.Grade ;
stu.push_back(t);
}
in.close();
}
void Save(vector<student>& stu)
{
ofstream desFile(fileName, ios::out);
vector<student>::iterator it;
for (vector<student>::iterator it = stu.begin(); it != stu.end(); it++)
{
desFile << "\t\t" << (*it).id << "\t\t" << (*it).name << "\t\t\t" << (*it).Mid << "\t\t\t" << (*it).Final << "\t\t\t" << (*it).Usual << "\t\t\t" << (*it).Grade << endl;
}
desFile.close();
}
istringstream sin(sline);定义一个字符串输入流的对象sin,并调用sin的复制构造函数,将s中所包含的字符串放入sin 对象中
简单来说它的作用就是将一个含有多个空格的字符串分割开来
istringstream需要包含头文件#include<sstream>
其实是很想做ege的两个课题的,但是最近事情真的太多了,没有热情去学就没有办法做好。
游戏什么的等更厉害了再去做~