大一时候的C++/VC++课程设计,“学生成绩管理系统”。老师:“你这个管理系统有什么特点”。我:“这个系统可以动态的调整科目的数量,还能把成绩存储到文件里…”(当时我自己觉得好高大上,好牛逼啊)。
那是一个初春,年后的日子,学期刚开始。寒冷的北方春天像北方的春天一样寒冷。我拿着厚厚的一沓传单,行走在某城的某街,麻木的往行人手中塞着单子。有一天下午,一个姑娘来找我借一块钱,说坐车没钱了,需要现金,加微信转给我。emmm借给她了,然后让她帮我分摊了几张传单。嗯,并没加好友。
早出晚归的日子很快过去,这段代码也是那个时候写下的。白天兼职,晚上敲代码,拖着疲惫的身体到处溜达。
// StudentScoreManage.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
// ceshi.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include
#include "process.h"
#include
#include
#include
#include "stdio.h"
#define clear() system("cls")
#define foreground_bright() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY)
#define foreground_green() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN)
#define foreground_red() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED)
#define foreground_blue() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE)
#define foreground_white() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE)
#define background_bright() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY)
#define background_green() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_GREEN)
#define background_red() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED)
#define background_blue() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_BLUE)
#define background_black() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_GREEN | FOREGROUND_BLUE)
using namespace std;
int NUMBER = 4;
int open = 0;
class Student_Score_Manage {
private:
string* head = new string[NUMBER];
string* str = new string[NUMBER];
float* score = new float[NUMBER];
static int NUM;
public:
void SET_HEAD(int n, string s) { *(head + n) = s; }
void SET_STR(int n, string s) { *(str + n) = s; }
void SET_SCORE(int n, float s) { *(score + n) = s; }
string GET_HEAD(int n) { return *(head + n); }
string GET_STR(int n) { return *(str + n); }
float GET_SCORE(int n) { return *(score + n); }
static void set_num(int i) { NUM += i; }
static int get_num() { return NUM; }
Student_Score_Manage();
Student_Score_Manage(string[], string[], float[], int);
~Student_Score_Manage();
void display(Student_Score_Manage*, int);
void print_table_head(Student_Score_Manage*);
float get_one_sum(Student_Score_Manage*, int);
};
//初始化所有数据
int Student_Score_Manage::NUM = 0;
Student_Score_Manage::Student_Score_Manage()
{
for (int i = 0; i < NUMBER; i++)
{
*(head + i) = "NULL";
*(str + i) = "NULL";
*(score + i) = 0;
}
}
Student_Score_Manage::Student_Score_Manage(string he[], string st[], float sc[], int n = 2)
{
for (int i = 0; i < NUMBER; i++)
* (head + i) = he[i];
for (int j = 0; j < n; j++)
{
*(str + j) = st[j];
}
for (int k = n; k < NUMBER - n; k++)
* (score + k) = sc[k];
}
Student_Score_Manage::~Student_Score_Manage()
{
delete[] head;
delete[] str;
delete[] score;
}
void Student_Score_Manage::display(Student_Score_Manage* s, int n = 2)//前n个为string,后NUMBER-n个为float
{
for (int i = 0; i < NUMBER; i++)
{
if (i < n) cout << s->GET_STR(i) << "\t";
else
{
cout << s->GET_SCORE(i) << "\t";
}
}
cout << endl;
}
float Student_Score_Manage::get_one_sum(Student_Score_Manage* s, int n = 2)//从score[n]个加起
{
float sum = 0;
for (int i = n; i < NUMBER; i++)
sum += s->GET_SCORE(i);// *(score + i);
return sum;
}
//打印所有学生成绩信息
void Student_Score_Manage::print_table_head(Student_Score_Manage* s)
{
foreground_green();
for (int i = 0; i < NUMBER; i++)
cout << s->GET_HEAD(i) << "\t";
cout << endl;
foreground_white();
}
void print_all(Student_Score_Manage* s, int n = 2)
{
//foreground_green();
//background_black();
cout << "编号\t";
//foreground_white();
s->print_table_head(s);
for (int i = 0; i < s->get_num(); i++)
{
cout.width(8);
cout.setf(ios::left);
foreground_blue();
cout << (i + 1);
foreground_white();
(s + i)->display(s + i, n);
}
cout.unsetf(ios::left);
}
void swap(Student_Score_Manage* m, Student_Score_Manage* n)
{
Student_Score_Manage* p = new Student_Score_Manage;
for (int i = 0; i < NUMBER; i++)
{
p->SET_HEAD(i, (m->GET_HEAD(i)));
p->SET_STR(i, (m->GET_STR(i)));
p->SET_SCORE(i, (m->GET_SCORE(i)));
m->SET_HEAD(i, (n->GET_HEAD(i)));
m->SET_STR(i, (n->GET_STR(i)));
m->SET_SCORE(i, (n->GET_SCORE(i)));
n->SET_HEAD(i, (p->GET_HEAD(i)));
n->SET_STR(i, (p->GET_STR(i)));
n->SET_SCORE(i, (p->GET_SCORE(i)));
}
delete p;
}
void sort(Student_Score_Manage* s, int j, int ud = 0)//j为第几科目从0开始
{
int i = s->get_num();
for (int h = 0; h < i - 1; h++)
{
for (int n = h + 1; n < i; n++)
{
switch (ud)
{
case 0:
if ((s + h)->GET_STR(j) < (s + n)->GET_STR(j) || (s + h)->GET_SCORE(j) < (s + n)->GET_SCORE(j))
swap((s + h), (s + n));
break;
case 1:
if ((s + h)->GET_STR(j) > (s + n)->GET_STR(j) || (s + h)->GET_SCORE(j) > (s + n)->GET_SCORE(j))
swap((s + h), (s + n));
break;
default:break;
}
}
}
}
void Main_page(Student_Score_Manage* s, string head[], int n)
{
void A_page(Student_Score_Manage * s, string str[], int n = 2);
void B_page(Student_Score_Manage * s, string str[], int n = 2);
void C_page(Student_Score_Manage * s, string str[], int n = 2);
int D_page(Student_Score_Manage * s, string str[], int n = 2);
void E_page(Student_Score_Manage * s, string str[], int n = 2);
void Q_page(Student_Score_Manage * s, int n);
cout << "\t\t\t\t请 选 择 系 统 功 能 项:\n"
<< "\t\t\t\t\t\ta、成绩录入\n"
<< "\t\t\t\t\t\tb、成绩显示\n"
<< "\t\t\t\t\t\tc、成绩排序\n"
<< "\t\t\t\t\t\td、成绩修改\n"
<< "\t\t\t\t\t\te、成绩统计\n"
<< "\t\t\t\t\t\tq、退出系统\n" << endl;
cout << endl << endl;
cout << "请选择编号(a,b,c,d,e,q)后按 ENTER:" << endl;
switch (getchar())
{
case 'a':A_page(s, head, n); break;
case 'b':B_page(s, head, n); break;
case 'c':C_page(s, head, n); break;
case 'd':
if (D_page(s, head, n) != 0)
{
cout << "系统内暂无数据,请先录入或导入数据!\n" << endl;
cout << "按任意键返回主界面。" << endl;
system("pause>nul");
Main_page(s, head, n);
}
break;
case 'e':E_page(s, head, n); break;
case 'q':Q_page(s, n);
default:clear(); Main_page(s, head, n);
}
}
int Read_line()
{
int number = 0;
ifstream in("SCORE.txt", ios::in);
char c;
while (in.get(c))
{
if (c == '\n')
number++;
}
in.close();
return number;
}
void back(Student_Score_Manage* s, string head[], int n)
{
if (s->get_num() == 0)
{
clear();
cout << "无任何数据,请先录入或导入数据,再执行此操作!!!" << endl;
system("echo 按任意键返回主界面。&pause>nul");
Main_page(s, head, n);
}
}
void A_page(Student_Score_Manage* s, string str[], int n = 2)
{
void Read(Student_Score_Manage * s, int n);
if (open == 0)
{
if (Read_line() > 0)
{
cout << "发现文件中有" << Read_line() << "条记录,是否从文件中导入数据?( Y \\ N )" << endl;
getchar();
if (getchar() == 'Y')
{
Read(s, n);
for (int i = 0; i < s->get_num(); i++)
for (int j = 0; j < NUMBER; j++)
(s + i)->SET_HEAD(j, *(str + j));
system("echo 导入成功!&echo 按任意键返回主界面 & pause>null");
open = 1;
Main_page(s, str, n);
}
}
}
cout << "在行首输入quit结束录入,按Tab键隔开数据:\n";
s->print_table_head(s);
string* strl = new string[n];
float* scorel = new float[NUMBER];
while (1)
{
//cout << "全局个数:" << s->get_num() << endl;
cin >> *strl;
if (*strl == "quit" || *strl == "QUIT") {
system("pause");
break;
}
(s + s->get_num())->SET_STR(0, *strl);
for (int k = 0; k < NUMBER; k++)
(s + s->get_num())->SET_HEAD(k, str[k]);
for (int i = 1; i < NUMBER; i++)
{
if (i < n)
{
cin >> *(strl + i);
(s + s->get_num())->SET_STR(i, *(strl + i));
}
else
{
cin >> *(scorel + i);
(s + s->get_num())->SET_SCORE(i, *(scorel + i));
}
}
s->set_num(1);
}
delete[] strl; delete[] scorel;
Main_page(s, str, n);
}
void B_page(Student_Score_Manage* s, string head[], int n = 2)
{
back(s, head, n);
clear();
cout << "\t\t\t全部信息:\n" << endl;
print_all(s, n);
system("echo 按任意键返回主界面 & pause>nul");
Main_page(s, head, n);
}
void C_page(Student_Score_Manage* s, string head[], int n = 2)
{
back(s, head, n);
for (int i = 0; i < NUMBER; i++)
cout << i + 1 << "." << s->GET_HEAD(i) << "\t";
cout << endl << "选择要排序的字段:" << endl;
int w;
while (cin >> w && (w<1 || w>NUMBER));
int t;
cout << "请选择升降序: 0,降序 1,升序\n请选择:" << endl;
while (cin >> t && (t > 1 || t < 0));
sort(s, w - 1, t);
cout << "排序完毕!" << endl;
system("echo 按任意键返回主界面。& pause>nul");
Main_page(s, head, n);
}
int D_page(Student_Score_Manage* s, string head[], int n = 2)
{
back(s, head, n);
if (s->get_num() == 0) return -1;
cout << "\t\t\t\t\t\t程序修改:\n";
print_all(s, n);
cout << "请选择要修改的数据 '编号',然后按ENTER键\n" << endl;
int num; cin >> num;
if (num > (s->get_num()) || num <= 0) {
clear();
system("echo 编号超出范围,按任意键重新输入。pause>nul");
D_page(s, head, n);
}
clear();
cout << "请选择字段前编号以修改字段对应内容。" << endl;
for (int i = 0; i < NUMBER; i++)
cout << i + 1 << "." << s->GET_HEAD(i) << "\t";
cout << endl;
int m;
while (cin >> m && (m > NUMBER || m <= 0));
clear();
cout << "请输入新值:" << endl;
string stl; float scl;
if (m < n)
{
cin >> stl; (s + num - 1)->SET_STR(m - 1, stl);
cout << (s->GET_HEAD(m - 1)) << "修改成功!" << endl;
}
else
{
cin >> scl; (s + num - 1)->SET_SCORE(m - 1, scl);
cout << (s->GET_HEAD(m - 1)) << "修改成功!" << endl;
}
system("echo 按任意键返回主界面。 & pause>nul");
Main_page(s, head, n);
return 0;
}
float get_average(Student_Score_Manage* s, int n)
{
float sum = 0;
for (int i = 0; i < (s->get_num()); i++)
sum += (s + i)->GET_SCORE(n);
return (sum / (float)(s->get_num()));
}
int get_gl_average_number(Student_Score_Manage* s, int n, int ud = 0)//n为第几科,ud为升降序
{
int num = 0;
float average = get_average(s, n);
for (int i = 0; i < s->get_num(); i++)
{
if (ud == 0)
{
if ((s + i)->GET_SCORE(n) > average) num++;
}
else
if ((s + i)->GET_SCORE(n) < average) num++;
}
return num;
}
Student_Score_Manage* find(Student_Score_Manage* s, string str/*int y*/, int n)//n为字段
{
int i;
for (i = 0; i < s->get_num(); i++)
if ((s + i)->GET_STR(n) == str)
break;
if (i < s->get_num())
return (s + i);
return NULL;
}
string get_most(Student_Score_Manage* s, int n, int y = 0) //n第几科,y为最大最小
{
float most = s->GET_SCORE(n);
string most_user = s->GET_STR(0);
for (int i = 1; i < s->get_num(); i++)
{
if (y == 0)
{
if ((s + i)->GET_SCORE(n) > most)
{
most = (s + i)->GET_SCORE(n);
most_user = (s + i)->GET_STR(0);
}
}
else
if ((s + i)->GET_SCORE(n) < most)
{
most = (s + i)->GET_SCORE(n);
most_user = (s + i)->GET_STR(0);
}
}
return most_user;
}
void fun_E_1(Student_Score_Manage* s, int n)
{
clear();
int y = 0;
string b[2] = { "高","低" };
print_all(s);
cout << endl << endl;
cout << "请选择:0,查找显示最高分 1,查找显示最低分" << endl;
while (cin >> y && (y > 1 || y < 0));
cout << endl << endl << endl;
for (int i = n; i < NUMBER; i++)
{
cout << s->GET_HEAD(i) << "最" << b[y] << "分:\t" << find(s, get_most(s, i, y), 0)->GET_SCORE(i) << endl;
s->print_table_head(s);
find(s, get_most(s, i, y), 0)->display(find(s, get_most(s, i, y), 0));
cout << endl;
}
}
void fun_E_2(Student_Score_Manage* s, int n) {
clear();
print_all(s);
cout << endl << endl << endl;
cout << "\t\t\t\t\t各科平均成绩:" << endl;
cout << "科 目:\t";
for (int i = n; i < NUMBER; i++)
cout << s->GET_HEAD(i) << "\t";
cout << endl;
cout << "平均分:\t\t";
for (int j = n; j < NUMBER; j++)
cout << get_average(s, j) << "\t";
cout << endl;
}
void fun_E_3(Student_Score_Manage* s, int n)
{
clear();
int y;
cout << "0,高于平均分 1,低于平均分\n请选择:" << endl;
string st[2] = { "高于平均分","低于平均分" };
while (cin >> y && (y > 1 || y < 0));
print_all(s, n);
cout << endl;
fun_E_2(s, n);
cout << endl;
for (int k = n; k < NUMBER; k++)
cout << s->GET_HEAD(k) << " :" << st[y] << get_gl_average_number(s, k, y) << endl;
}
void E_page(Student_Score_Manage* s, string head[], int n)
{
back(s, head, n);
cout << "\t\t\t\t\t\t\t成绩统计\n"
<< "\t\t\t\n请选择:\n"
<< "1.显示每门课程成绩最高的学生的基本信息\n"
<< "2.显示每门课程的平均成绩\n"
<< "3.显示超过某门课程平均成绩的学生人数"
<< endl;
int w;
while (cin >> w && (w > 3 || w < 1));
switch (w)
{
case 1:
fun_E_1(s, n);
cout << endl;
system("echo 按任意键返回主界面。& pause>nul");
Main_page(s, head, n);
break;
case 2:
fun_E_2(s, n);
cout << endl;
system("echo 按任意键返回主界面。& pause>nul");
Main_page(s, head, n);
case 3:
fun_E_3(s, n);
cout << endl;
system("echo 按任意键返回主界面。& pause>nul");
Main_page(s, head, n);
break;
default:
clear();
Main_page(s, head, n);
}
}
void Q_page(Student_Score_Manage* s, int n)
{
void Write(Student_Score_Manage * s, int n, int h);
clear();
cout << "\t\t\t\t\t\t数据保存" << endl << endl << endl;
cout << "1,追加在原有数据之后保存\n" << endl
<< "2,清空之前内容之后保存数据\n" << endl
<< "3, 清空之前所有数据并不保存此次操作的数据\n" << endl
<< "4, 不保存任何数据" << endl << endl;
cout << "请选择:" << endl;
int a;
while (cin >> a && (a > 4 || a < 1));
if (a == 1 || a == 2)
{
Write(s, n, a);
cout << "\t\t\t\t\t\t数据保存成功!" << endl;
}
else
if (a == 3)
{
ofstream file("SCORE.txt", ios::out | ios::trunc);
file.close();
}
delete[]s;
clear();
for (int h = 5; h >= 1; h--)
{
cout << '\a' << endl;
cout << "谢谢使用!!!\n\t\t\t\t\t 再见!!!\n\n\n\n\n\n\n\n\n\t" << h << "秒后程序自动退出。" << endl;
Sleep(999);
clear();
}
exit(1);
}
void Read(Student_Score_Manage* s, int n)
{
ofstream out("SCORE.txt", ios::out | ios::app);
out.close();
ifstream in("SCORE.txt", ios::in);
while (!in.eof())
{
string str;
float scor;
for (int i = 0; i < n; i++)
{
in >> str;
if (str == "") break;
(s + s->get_num())->SET_STR(i, str);
}
if (str == "") break;
for (int i = n; i < NUMBER; i++)
{
in >> scor;
(s + s->get_num())->SET_SCORE(i, scor);
}
s->set_num(1);
}
//s->set_num(-1);
in.close();
}
void Write(Student_Score_Manage* s, int n, int h)
{
if (s->get_num() > 0)
{
ofstream out;
if (h == 0)
out.open("SCORE.txt", ios::out | ios::trunc);
else
out.open("SCORE.txt", ios::out | ios::app);
for (int k = 0; k < (s->get_num()); k++)
{
for (int i = 0; i < n; i++)
out << ((s + k)->GET_STR(i)) << "\t";
for (int i = n; i < NUMBER; i++)
out << ((s + k)->GET_SCORE(i)) << "\t";
out << endl;
}
out.close();
}
}
int main()
{
NUMBER = 4;
string* str = new string[NUMBER];
*str = "学号";
*(str + 1) = "姓名";
*(str + 2) = "数学";
*(str + 3) = "语文";
//*(str + 4) = "英语";
//system("color 0a");
int many;
cout << "请输入学生人数!尽量大一些\n:" << endl;
cin >> many;
Student_Score_Manage* s = new Student_Score_Manage[many];
for (int i = 0; i < NUMBER; i++)
(s + 0)->SET_HEAD(i, *(str + i));
Main_page(s, str, 2);
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件