/*设计一个Student(学生)类,并使Student类具有以下特点:
该类具有学生姓名、学号、程序设计、信号处理、数据结构三门课程的成绩。
学生全部信息由键盘输入,以提高程序的适应性。
通过成员函数分别统计每个学生的平均成绩和每门课程的平均成绩,当课程 数量增加或减少时成员函数无需修改仍可求取平均成绩。
输出学生的各科成绩与平均成绩。
学生对象的定义采用对象数组实现。
统计不及格学生人数。
表格输出整齐美观。
能以最方便的方式实现对课程数量和学生人数的修改。
采用多文件方式组织程序。
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):1
请依次输入该学生的相关信息:
学号:1001
姓名:张三
信号处理:67
数据结构:65
C语言:87
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):1
请依次输入该学生的相关信息:
学号:1002
姓名:王龙龙
信号处理:78
数据结构:89
C语言:98
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):1
请依次输入该学生的相关信息:
学号:1003
姓名:王丽丽
信号处理:76
数据结构:89
C语言:90
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):3
学号:1001 姓名:张三 信号处理:67 数据结构:65 C语言:87 总分:219 平均分:73
学号:1002 姓名:王龙龙 信号处理:78 数据结构:89 C语言:98 总分:265 平均分:88.3333
学号:1003 姓名:王丽丽 信号处理:76 数据结构:89 C语言:90 总分:255 平均分:85
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):4
不及格学生人数: 0
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):2
请输入要修改的学生学号:
1001
该学号的学生信息如下:
学号:1001 姓名:张三 信号处理:67 数据结构:65 C语言:87 总分:219 平均分:73
请输入新的学生信息:
学号:1001
姓名:张三
信号处理:67
数据结构:75
C语言:40
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):3
学号:1001 姓名:张三 信号处理:67 数据结构:75 C语言:40 总分:182 平均分:60.6667
学号:1002 姓名:王龙龙 信号处理:78 数据结构:89 C语言:98 总分:265 平均分:88.3333
学号:1003 姓名:王丽丽 信号处理:76 数据结构:89 C语言:90 总分:255 平均分:85
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):4
不及格学生人数: 1
1.添加 2.修改 3.显示 4.不及格学生人数 5.排序
请根据你的需要选择(1~5):7
谢谢使用!!!
*/
#include <iostream>
#include <string>
using namespace std
struct Student //声明一个名为student的结构体类型
{
string name //姓名
long number //学号
double xhcl //信号处理
double sjjg //数据结构
double Cyuyan //C语言
double ZF //总分
double PJF //平均分
}
class Students //声明类Students
{
public:
Students(){n=0} //构造函数
void add()//添加
void show()//显示
void change()//修改
void PX()//排序
void bjg()//统计不及格学生人数
private:
Student stu[1000]//定义一个结构体类型Students的变量stu,其最大值为100
int n
}
void Students::add()//添加
{
cout<<"学号:"
cin>>stu[n].number
cout<<"姓名:"
cin>>stu[n].name
cout<<"信号处理:"
cin>>stu[n].xhcl
cout<<"数据结构:"
cin>>stu[n].sjjg
cout<<"C语言:"
cin>>stu[n].Cyuyan
stu[n].ZF= stu[n].xhcl+ stu[n].sjjg+ stu[n].Cyuyan
stu[n].PJF=stu[n].ZF/3
n++//学生个数增加一个
}
void Students::show()//显示
{
for(int i=0i<ni++)
cout<<"学号:"<<stu[i].number<<' '<<"姓名:"<<stu[i].name<<' '<<"信号处理:"<<stu[i].xhcl<<' '<<"数据结构:"<<stu[i].sjjg<<' '<<"C语言:"<<stu[i].Cyuyan<<' '<<"总分:"<<stu[i].ZF<<' '<<"平均分:"<<stu[i].PJF<<endl
}
void Students::change() //修改
{long num
cout<<"请输入要修改的学生学号:"<<endl
cin>>num
for(int i=0i<ni++)
if(stu[i].number==num)
{cout<<"该学号的学生信息如下:"<<endl
cout<<"学号:"<<stu[i].number<<' '<<"姓名:"<<stu[i].name<<' '<<"信号处理:"<<stu[i].xhcl<<' '<<"数据结构:"<<stu[i].sjjg<<' '<<"C语言:"<<stu[i].Cyuyan<<' '<<"总分:"<<stu[i].ZF<<' '<<"平均分:"<<stu[i].PJF<<endl
cout<<"请输入新的学生信息:"<<endl
cout<<"学号:"
cin>>stu[i].number
stu[n].number=stu[i].number
cout<<"姓名:"
cin>>stu[i].name
stu[n].name=stu[i].name
cout<<"信号处理:"
cin>>stu[i].xhcl
stu[n].xhcl=stu[i].xhcl
cout<<"数据结构:"
cin>>stu[i].sjjg
stu[n].sjjg=stu[i].sjjg
cout<<"C语言:"
cin>>stu[i].Cyuyan
stu[n].Cyuyan=stu[i].Cyuyan
stu[i].ZF= stu[i].xhcl+ stu[i].sjjg+ stu[i].Cyuyan
stu[i].PJF=stu[i].ZF/3
break
}
if(stu[i].number!=num)
cout<<"无此学生信息!!"<<endl
}
void Students::PX() //排序
{cout<<"请选择排序方式:"
cout<<"1.按学生总分排序"
cout<<"2.按学生平均分排序"
cout<<"3.返回"<<endl
int k
cin>>k
switch (k)
{
case 1://按学生总分排序
{
Student p
for(int i=0i<ni++)
for(int j=0j<n-ij++)
{
if(stu[j].ZF<stu[j+1].ZF)
{
p=stu[j]
stu[j]=stu[j+1]
stu[j+1]=p
}
}
cout<<"学号 姓名 信理 数构 C语言 平均分"<<endl
for(int m=0m<nm++)
cout<<stu[m].number<<" "<<stu[m].name<<" "<<stu[m].xhcl<<" "<<stu[m].sjjg<<" "<<stu[m].Cyuyan<<""<<stu[m].PJF<<endl
}
break
case 2://按学生平均分排序
{ Student p//定义一个Student类型的变量p
for(int i=0i<ni++)
for(int j=0j<n-ij++)
{
if(stu[j].PJF<stu[j+1].PJF)
{
p=stu[j]
stu[j]=stu[j+1]
stu[j+1]=p
}
}
cout<<"学号 姓名 信号处理 数据结构 C语言 平均分"<<endl
for(int m=0m<nm++)
cout<<stu[m].number<<" "<<stu[m].name<<" "<<stu[m].xhcl<<" "<<stu[m].sjjg<<" "<<stu[m].Cyuyan<<""<<stu[m].PJF<<endl
}break
case 3:return//返回
}
}
void Students::bjg()//统计不及格学生人数
{
int count=0
for(int m=0m<nm++)
if(stu[m].xhcl<60||stu[m].sjjg<60||stu[m].Cyuyan<60)
count++
cout<<"不及格学生人数: "<<count<<endl
}
void main()
{
Students person
int b
do
{
cout<<" 1.添加 2.修改 3.显示 4.不及格学生人数 5.排序 "<<endl
cout<<"请根据你的需要选择(1~5):"
cin>>b
switch(b)
{
case 1:
cout<<"请依次输入该学生的相关信息:"<<endl
person.add()
break
case 2:
person.change()
break
case 3:
person.show()
break
case 4:
person.bjg()
break
case 5:
person.PX()
break
case 6:
cout<<"谢谢使用!!!"<<endl
}
}while(b<7)
}
#include <stdio.h>#include <stdlib.h>
typedef struct stu{
int id
char name[40]
int mark[4]
float total
} STU
int id
int m1,m2,m3,m4
char str[40]
void main()
{
int i,j,N=0
FILE *fin, *fout
STU tmp
STU *s
fin = fopen("score.txt","r")
while (1){
if ( fscanf(fin,"%*d %*s %*d %*d %*d %*d") ==EOF) break
N++
}
fclose(fin)
s = (STU *) malloc(N * sizeof(STU))
if (!s) {
printf("no enough memory for STU\n")exit(0)
}
fin = fopen("score.txt","r")
for (i=0i<Ni++){
fscanf(fin,"%d %s %d %d %d %d",&id,&str[0],&m1,&m2,&m3,&m4)
s[i].id = id
strcpy(&s[i].name[0],str)
s[i].mark[0]= m1
s[i].mark[1]= m2
s[i].mark[2]= m3
s[i].mark[3] = m4
s[i].total = 0.2*m1 + 0.6*m2 + 0.5 * m3 + 0.15*m4
}
fclose(fin)
for (i=0i<N-1i++){
for (j=i+1j<Nj++){
if (s[j].total >s[i].total){
tmp = s[j]s[j]=s[i]s[i]=tmp
}
}
}
fout = fopen("score_Q.txt","w")
for(i=0i<Ni++){
fprintf(fout,"%d %s %d %d %d %d\n",s[i].id,s[i].name,s[i].mark[0],s[i].mark[1],s[i].mark[2],s[i].mark[3])
}
fclose(fout)
exit(0)
}
参考实例源码如下://#include <stdlib.h>
#include <iostream>
using namespace std
int main()
{
int i,j,k,l=0,n
cout<<"小学数学测试"<<endl
cout<<"请选择想测试的题型:1.+ 2.- 3.* 4./:"
cin>>i
if(i==1)
{
cout<<"请输入测试题的个数:"
cin>>j
for(int k1=0k1<jk1++)
{
srand((unsigned)time(NULL))
int x1=rand()%100
int x2=rand()%100
cout<<x1<<"+"<<x2<<"=:"
cin>>k
if(k==x1+x2){
cout<<"回答正确!"<<endl
l++
}
else
cout<<"回答错误!正确答案为:"<<x1+x2<<endl
}
}
else if(i==2)
{
cout<<"请输入测试题的个数:"
cin>>j
for(int k1=0k1<jk1++)
{
srand((unsigned)time(NULL))
int x1=rand()%100
int x2=x1+rand()%50
cout<<x2<<"-"<<x1<<"=:"
cin>>k
if(k==x2-x1){
cout<<"回答正确!"<<endl
l++
}
else
cout<<"回答错误!正确答案为:"<<x2-x1<<endl
}
}
else if(i==3)
{
cout<<"请输入测试题的个数:"
cin>>j
for(int k1=0k1<jk1++)
{
srand((unsigned)time(NULL))
int x1=rand()%100
int x2=rand()%100
cout<<x1<<"*"<<x2<<"=:"
cin>>k
if(k==x1*x2){
cout<<"回答正确!"<<endl
l++
}
else
cout<<"回答错误!正确答案为:"<<x1*x2<<endl
}
}
else if(i==4)
{
cout<<"请输入测试题的个数:"
cin>>j
for(int k1=0k1<jk1++)
{
srand((unsigned)time(NULL))
int x1=rand()%100+1
int x2=x1*(rand()%10)
cout<<x2<<"/"<<x1<<"=:"
cin>>k
if(k==x2/x1){
cout<<"回答正确!"<<endl
l++
}
else
cout<<"回答错误!正确答案为:"<<x2/x1<<endl
}
}
cout<<"这次测试你作对的题目的个数为"<<l<<"个"<<endl
cout<<"是否继续测试:0.退出 1.继续:"
cin>>n
if(n==1){
return main()}
//system("PAUSE")
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)