学生成绩管理系统设计-学生成绩管理系统设计思想

本文目录一览:

学生成绩管理系统设计

///////////////////以下是源程序,你稍作修改就能使用,student.txt的格式Hi我告诉你

#include iostream

#include fstream

#include string

const char * file = "student.txt";

using namespace std;

class Student

{

public:

int Num, Age, Maths, English, Cpp, Total, Rank;

string Name;

char Sex;

void Set(int num, string name, char sex,int age, int english, int maths, int cpp)

{

Num =num;

Name =name;

Sex=sex;

Age=age;

English=english;

Maths=maths;

Cpp=cpp;

Total=english+maths+cpp;

};

void total()

{

cout "The score of " Name " in total is: "

Maths + English + Cpp endl;

};

void ave()

{

cout "The score of " Name " in average is: "

(Maths + English + Cpp)/3.0 endl;

};

void display()

{

//数字设置为右对齐

cout.setf(ios_base ::right, ios_base ::adjustfield);

cout.width(3);

cout Num " ";

//字符串设置为左对齐

cout.setf(ios_base ::left, ios_base ::adjustfield);

cout.width(8);

cout Name;

cout.width(4);

cout Sex;

//数字设置为右对齐

cout.setf(ios_base ::right, ios_base ::adjustfield);

cout.width(3);

cout Age;

cout.width(8);

cout English;

cout.width(6);

cout Maths;

cout.width(4);

cout Cpp;

cout.width(6);

cout Total;

};

int rank() //返回名次

{ return Rank;};

}stu[5];

void getdatas();

void dis_age();

void dis_course();

void dis_ave();

void dis_total();

void openfile();

void callchoice();

void dis_all();

int main()

{

cout "1、输入学生信息并保存到文件" endl

"2、按年龄从大到小显示学生各项信息" endl

"3、按各门成绩由高到低显示学生信息" endl

"4、输入学号计算学生平均成绩并显示" endl

"5、输入学号计算学生总成绩并显示" endl

"6、按学生总成绩排名并显示排名结果" endl

"请选择(1-6)" endl;

callchoice();

return 0;

}

void callchoice()

{

cout "\nPress 1-6 to select your choice, and any character to terminate:_\b";

int choice;

while(cin choice)

{

switch (choice)

{

case 1 : getdatas(); callchoice(); break; //接受输入并存至文件

case 2 : dis_age(); callchoice(); break; //按年龄显示

case 3 : dis_course();callchoice(); break; //按单科成绩排名并显示

case 4 : dis_ave(); callchoice(); break; //显示某位学生的平均分

case 5 : dis_total(); callchoice(); break; //显示某位学生的总分

case 6 : dis_all(); callchoice(); break; //按总分排名并显示

default : cout "Wrong input, select again: ";

}

cin choice;

}

}

void openfile() //打开文件,并将信息复制到类成员中

{

ifstream fin(file, ios_base::in | ios_base::binary);

if(fin.is_open())

for (int i=0; i5 ;i++)

{

int num, age, english, maths, cpp;

char sex;

string name;

fin num name sex age english maths cpp;

stu[i].Set(num, name, sex, age, english, maths, cpp);

}

else cout "The file could not be open." endl;

fin.close();

}

void getdatas() //接受输入,并将信息存入文件中

{

ofstream fout(file, ios::out | ios::trunc |ios_base::binary);

if(fout.is_open())

{

cout "Num\tName\tSex\tAge\tEnglish\tMaths\tCpp\t\n";

for (int i=0; i5 ;i++)

{

cin stu[i].Num stu[i].Name stu[i].Sex stu[i].Age

stu[i].English stu[i].Maths stu[i].Cpp ;

fout stu[i].Num " " stu[i].Name " " stu[i].Sex " " stu[i].Age " "

stu[i].English " " stu[i].Maths " " stu[i].Cpp " ";

}

}

else cout "The file could not be open." endl;

cout "The data has been writen in the flie " file endl;

fout.close();

};

void dis_age() //按年龄大小排名,并显示成绩表

{

openfile();

Student temp;

for (int i=0; i5; i++)

{

for (int j=i+1; j6; j++)

{

if(stu[i].Age stu[j].Age)

{

temp = stu[i];

stu[i] = stu[j];

stu[j] = temp;

}

}

}

cout "N0. Name Sex Age English Maths C++ Total\n";

for (int i=0; i5; i++)

{ stu[i].display(); cout endl;}

};

void dis_course() //按单科成绩排名,并显示成绩表

{

openfile();

cout "Select a course(E for english,M for Maths, C for C++):_\b";

char sel;

cin sel;

while (sel!='e' sel!='E' sel!='m' sel!='M' sel!='c' sel!='C')

{

cout "Not a right choice, input again:_\b";

cin sel;

}

Student temp;

for (int i=0; i4; i++)

{

for (int j=i+1; j5; j++)

{

if (sel == 'e' or sel=='E')

{

if(stu[i].English stu[j].English)

{

temp = stu[i];

stu[i] = stu[j];

stu[j] = temp;

}

}

else if (sel == 'm' or sel=='M')

{

if(stu[i].Maths stu[j].Maths)

{

temp = stu[i];

stu[i] = stu[j];

stu[j] = temp;

}

}

else if (sel == 'c' or sel=='C')

{

if(stu[i].Cpp stu[j].Cpp)

{

temp = stu[i];

stu[i] = stu[j];

stu[j] = temp;

}

}

}

}

cout "N0. Name Sex Age English Maths C++ Total\n";

for (int i=0; i5; i++)

{ stu[i].display(); cout endl;}

};

void dis_total() //显示某位学生的总成绩

{

openfile();

cout "Input the number of a student:__\b\b";

int choice;

cin choice;

for (int i=0; i5; i++)

if (choice == stu[i].Num)

stu[i].total();

};

void dis_ave() //显示某位学生的平均分

{

openfile();

cout "Input the number of a student:__\b\b";

int choice;

cin choice;

for (int i=0; i5; i++)

if (choice == stu[i].Num)

stu[i].ave();

};

void dis_all() //按部分多少排名,并显示成绩表

{

openfile();

Student temp;

for (int i=0; i5; i++)

{

for (int j=i+1; j6; j++)

{

if(stu[i].Total stu[j].Total)

{

temp = stu[i];

stu[i] = stu[j];

stu[j] = temp;

}

}

}

for (int i=0; i5; i++)

{

stu[i].Rank=i+1;

}

cout "N0. Name Sex Age English Maths C++ Total Rank\n";

for (int i=0; i5; i++)

{

stu[i].display();

cout.width(5);

cout stu[i].rank() endl;

}

}

用C语言设计一个学生成绩管理系统

#include stdio.h

#include string.h

#include stdlib.h

#define MAX 1000

/*定义学生成绩信息结构*/

struct stu

{

char id[8];

char name[8];

扩展资料:

short:修饰int,短整型数据,可省略被修饰的int。(KR时期引入)

long:修饰int,长整型数据,可省略被修饰的int。(KR时期引入)

long long:修饰int,超长整型数据,可省略被修饰的int。(C99标准新增)

signed:修饰整型数据,有符号数据类型。(C89标准新增)

unsigned:修饰整型数据,无符号数据类型。(KR时期引入)

restrict:用于限定和约束指针,并表明指针是访问一个数据对象的唯一且初始的方式。(C99标准新增)

复杂类型关键字

struct:结构体声明。(KR时期引入)

union:联合体声明。(KR时期引入)

enum:枚举声明。(C89标准新增)

typedef:声明类型别名。(KR时期引入)

sizeof:得到特定类型或特定类型变量的大小。(KR时期引入)

inline:内联函数用于取代宏定义,会在任何调用它的地方展开。(C99标准新增)

参考资料来源:百度百科-c语言

C语言程序设计学生成绩管理系统

#include "stdio.h"

/*定义学生结构体*/

struct Student

{

char ID[20];

char Name[20];

float Mark1;

float Mark2;

float Mark3;

float Average;

};

/*声明学生数组及学生数量*/

struct Student students[1000];

int num=0;

/*求平均值*/

float Avg(struct Student stu)

{

return (stu.Mark1+stu.Mark2+stu.Mark3)/3;

}

/*通过学号返回数组下标*/

int Student_SearchByIndex(char id[])

{

int i;

for (i=0;inum;i++)

{

if (strcmp(students[i].ID,id)==0)

{

return i;

}

}

return -1;

}

/*通过姓名返回数组下标*/

int Student_SearchByName(char name[])

{

int i;

for (i=0;inum;i++)

{

if (strcmp(students[i].Name,name)==0)

{

return i;

}

}

return -1;

}

/*显示单条学生记录*/

void Student_DisplaySingle(int index)

{

printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","成绩","成绩","成绩","平均成绩");

printf("-------------------------------------------------------------\n");

printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[index].ID,students[index].Name,

students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average);

}

/*插入学生信息*/

void Student_Insert()

{

while(1)

{

printf("请输入学号:");

scanf("%s",students[num].ID);

getchar();

printf("请输入姓名:");

scanf("%s",students[num].Name);

getchar();

printf("请输入成绩:");

scanf("%f",students[num].Mark1);

getchar();

printf("请输入成绩:");

scanf("%f",students[num].Mark2);

getchar();

printf("请输入成绩:");

scanf("%f",students[num].Mark3);

getchar();

students[num].Average=Avg(students[num]);

num++;

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*修改学生信息*/

void Student_Modify()

{

float mark1,mark2,mark3;

while(1)

{

char id[20];

int index;

printf("请输入要修改的学生的学号:");

scanf("%s",id);

getchar();

index=Student_SearchByIndex(id);

if (index==-1)

{

printf("学生不存在!\n");

}

else

{

printf("你要修改的学生信息为:\n");

Student_DisplaySingle(index);

printf("-- 请输入新值--\n");

printf("请输入学号:");

scanf("%s",students[index].ID);

getchar();

printf("请输入姓名:");

scanf("%s",students[index].Name);

getchar();

printf("请输入成绩:");

scanf("%f",students[index].Mark1);

getchar();

printf("请输入成绩:");

scanf("%f",students[index].Mark2);

getchar();

printf("请输入成绩:");

scanf("%f",students[index].Mark3);

getchar();

students[index].Average=Avg(students[index]);

}

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*删除学生信息*/

void Student_Delete()

{

int i;

while(1)

{

char id[20];

int index;

printf("请输入要删除的学生的学号:");

scanf("%s",id);

getchar();

index=Student_SearchByIndex(id);

if (index==-1)

{

printf("学生不存在!\n");

}

else

{

printf("你要删除的学生信息为:\n");

Student_DisplaySingle(index);

printf("是否真的要删除?(y/n)");

if (getchar()=='y')

{

for (i=index;inum-1;i++)

{

students[i]=students[i+1];

}

num--;

}

getchar();

}

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*按姓名查询*/

void Student_Select()

{

while(1)

{

char name[20];

int index;

printf("请输入要查询的学生的姓名:");

scanf("%s",name);

getchar();

index=Student_SearchByName(name);

if (index==-1)

{

printf("学生不存在!\n");

}

else

{

printf("你要查询的学生信息为:\n");

Student_DisplaySingle(index);

}

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*按平均值排序*/

void Student_SortByAverage()

{

int i,j;

struct Student tmp;

for (i=0;inum;i++)

{

for (j=1;jnum-i;j++)

{

if (students[j-1].Averagestudents[j].Average)

{

tmp=students[j-1];

students[j-1]=students[j];

students[j]=tmp;

}

}

}

}

/*显示学生信息*/

void Student_Display()

{

int i;

printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","成绩","成绩","成绩","平均成绩");

printf("-------------------------------------------------------------\n");

for (i=0;inum;i++)

{

printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[i].ID,students[i].Name,

students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average);

}

}

/*将学生信息从文件读出*/

void IO_ReadInfo()

{

FILE *fp;

int i;

if ((fp=fopen("Database.txt","rb"))==NULL)

{

printf("不能打开文件!\n");

return;

}

if (fread(num,sizeof(int),1,fp)!=1)

{

num=-1;

}

else

{

for(i=0;inum;i++)

{

fread(students[i],sizeof(struct Student),1,fp);

}

}

fclose(fp);

}

/*将学生信息写入文件*/

void IO_WriteInfo()

{

FILE *fp;

int i;

if ((fp=fopen("Database.txt","wb"))==NULL)

{

printf("不能打开文件!\n");

return;

}

if (fwrite(num,sizeof(int),1,fp)!=1)

{

printf("写入文件错误!\n");

}

for (i=0;inum;i++)

{

if (fwrite(students[i],sizeof(struct Student),1,fp)!=1)

{

printf("写入文件错误!\n");

}

}

fclose(fp);

}

/*主程序*/

main()

{

int choice;

IO_ReadInfo();

while(1)

{

/*主菜单*/

printf("\n------ 学生成绩管理系统------\n");

printf("1. 增加学生记录\n");

printf("2. 修改学生记录\n");

printf("3. 删除学生记录\n");

printf("4. 按姓名查询学生记录\n");

printf("5. 按平均成绩排序\n");

printf("6. 退出\n");

printf("请选择(1-6):");

scanf("%d",choice);

getchar();

switch(choice)

{

case 1:

Student_Insert();

break;

case 2:

Student_Modify();

break;

case 3:

Student_Delete();

break;

case 4:

Student_Select();

break;

case 5:

Student_SortByAverage();

Student_Display();

break;

case 6:

exit();

break;

}

IO_WriteInfo();

}

}


原文链接:http://527256.com/32270.html

相关文章

访客
访客
发布于 2022-09-08 15:43:49  回复
; j++) { if(stu[i].Age stu[j].Age) { temp = stu[i];
访客
访客
发布于 2022-09-08 19:44:27  回复
cout.width(6); cout Total; }; int rank() //返回名次 { return Rank;};}stu[5];void getdatas();void dis_age();void dis_cou
访客
访客
发布于 2022-09-09 00:40:18  回复
ase ::right, ios_base ::adjustfield); cout.width(3); cout Age; cout.width(8); cout English; cout.width(6);
访客
访客
发布于 2022-09-08 20:15:16  回复
{ if (students[j-1].Averagestudents[j].Average) { tmp=students

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

返回顶部