求编一个c++程序(要有两个类,最后是要有两个cpp文件和.h文件)急
Writeaprogramthatasksyou10recordsofstudents.Eachrecordconsistsofaname(w/ospace),andsc...
Write a program that asks you 10 records of students. Each record consists of
a name (w/o space), and scores for three courses (in integer, 1 to 5). Output a
list as the next page and calculate average score (in float) of each student and
each course. Output the lowest and highest score for each course.
no name score1 score2 score3 average
1 K.Weng 5 5 5 5
2 T.Dixon 4 3 2 3
3 V.Chu 3 4 5 4
4 L.Tson 4 3 4 3.66667
5 L.Lee 3 4 3 3.33333
6 I.Young 4 2 5 3.66667
7 K.Hiro 4 2 1 2.33333
8 G.Ping 4 4 4 4
9 H.Gu 2 3 4 3
10 J.Jon 5 4 3 4
average 3.8 3.4 3.6
min 2 2 1
max 5 5 5
两个cpp文件和两个.h文件 展开
a name (w/o space), and scores for three courses (in integer, 1 to 5). Output a
list as the next page and calculate average score (in float) of each student and
each course. Output the lowest and highest score for each course.
no name score1 score2 score3 average
1 K.Weng 5 5 5 5
2 T.Dixon 4 3 2 3
3 V.Chu 3 4 5 4
4 L.Tson 4 3 4 3.66667
5 L.Lee 3 4 3 3.33333
6 I.Young 4 2 5 3.66667
7 K.Hiro 4 2 1 2.33333
8 G.Ping 4 4 4 4
9 H.Gu 2 3 4 3
10 J.Jon 5 4 3 4
average 3.8 3.4 3.6
min 2 2 1
max 5 5 5
两个cpp文件和两个.h文件 展开
2个回答
展开全部
//CStudent.h
#ifndef _CSTUDENT_HEAD_
#define _CSTUDENT_HEAD_
#define N_SCORES 3
#define SCORE1 0
#define SCORE2 1
#define SCORE3 2
class CStudent {
public:
char * name;
unsigned int scores[N_SCORES];
float getAverage();
CStudent(char * name,int score1,int score2,int score3);
~CStudent();
};
#endif
//CStudent.cpp
#include <stdlib.h> //for malloc() free()
#include <string.h> //for strlen() strcpy()
#include "CStudent.h"
//
CStudent::CStudent(char * name,int score1,int score2,int score3){
// copy name
this->name = (char*)malloc(strlen(name)+1);
strcpy(this->name,name);
//
this->scores[SCORE1]=score1;
this->scores[SCORE2]=score2;
this->scores[SCORE3]=score3;
}
CStudent::~CStudent(){
if(NULL != this->name){
free(this->name);
this->name = NULL;
}
}
float CStudent::getAverage(){
int sum = 0;
for(int i=0;i < N_SCORES;++i){
sum = sum + this->scores[i];
}
return ((float)sum)/N_SCORES;
}
//CStudent_group.h
#ifndef _CSTUDENT_GROUP_HEAD_
#define _CSTUDENT_GROUP_HEAD_
#include "CStudent.h"
#define MAX_STUDENT_NUM 10
class CStudent_group{
public:
CStudent * stdArray[MAX_STUDENT_NUM];
int iCurNum;
void AddStudent(CStudent* stu);
void PrintAnalyseInfo();
float getScoreNAverage(int scoren);
int getScoreNmin(int scoren);
int getScoreNmax(int scoren);
CStudent_group();
~CStudent_group();
};
#endif
//CStudent_group.cpp
#include <stdio.h> //for printf()
#include "CStudent_group.h"
CStudent_group::CStudent_group(){
iCurNum = 0;
}
CStudent_group::~CStudent_group(){
for(int i = 0; i < iCurNum;++i){
delete stdArray[i];
stdArray[i] = NULL;
}
}
void CStudent_group::AddStudent(CStudent* stu){
if(iCurNum >= MAX_STUDENT_NUM){
return;
}
stdArray[iCurNum++] = stu;
}
float CStudent_group::getScoreNAverage(int scoren){
int sum = 0;
if(iCurNum <= 0){
return 0.0;
}
for(int i = 0; i < iCurNum; i++){
sum = sum + stdArray[i]->scores[scoren];
}
return ((float)sum)/iCurNum;
}
int CStudent_group::getScoreNmin(int scoren){
int min = stdArray[0]->scores[scoren];
for(int i = 1; i < iCurNum; i++){
if(stdArray[i]->scores[scoren] < min){
min = stdArray[i]->scores[scoren];
}
}
return min;
}
int CStudent_group::getScoreNmax(int scoren){
int max = stdArray[0]->scores[scoren];
for(int i = 1; i < iCurNum; i++){
if(stdArray[i]->scores[scoren] > max){
max = stdArray[i]->scores[scoren];
}
}
return max;
}
void CStudent_group::PrintAnalyseInfo(){
if(iCurNum <= 0){
return;
}
printf("\tno\tname\tscore1\tscore2\tscore3\taverage\n");
for(int i = 0; i < iCurNum;++i){
printf("%d\t%s\t%d\t%d\t%d\t%f\n",1+i,stdArray[i]->name,
stdArray[i]->scores[SCORE1],
stdArray[i]->scores[SCORE2],
stdArray[i]->scores[SCORE3],
stdArray[i]->getAverage());
}
printf(" \taverage\t%f\t%f\t%f\n",
getScoreNAverage(SCORE1),
getScoreNAverage(SCORE2),
getScoreNAverage(SCORE3));
printf(" \tmin\t%d\t%d\t%d\n",
getScoreNmin(SCORE1),
getScoreNmin(SCORE2),
getScoreNmin(SCORE3));
printf(" \tmax\t%d\t%d\t%d\n",
getScoreNmax(SCORE1),
getScoreNmax(SCORE2),
getScoreNmax(SCORE3));
}
//main_test.cpp
#include "CStudent.h"
#include "CStudent_group.h"
int main(int argc,char* argv[]){
CStudent_group* psg = new CStudent_group();
psg->AddStudent(new CStudent("K.Weng",5,5,5));
psg->AddStudent(new CStudent("T.Dixon",4,3,2));
psg->AddStudent(new CStudent("V.Chu",3,4,5));
psg->AddStudent(new CStudent("L.Tson",4,3,4));
psg->AddStudent(new CStudent("L.Lee",3,4,3));
psg->AddStudent(new CStudent("I.Young",4,2,5));
psg->AddStudent(new CStudent("K.Hiro",4,2,1));
psg->AddStudent(new CStudent("G.Ping",4,4,4));
psg->AddStudent(new CStudent("H.Gu",2,3,4));
psg->AddStudent(new CStudent("J.Jon",5,4,3));
psg->PrintAnalyseInfo();
return 0;
}
编译测试过程及结果:
F:\hiapp_text>cl main_test.cpp CStudent.cpp CStudent_group.cpp 编译命令
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
main_test.cpp
CStudent.cpp
CStudent_group.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:main_test.exe
main_test.obj
CStudent.obj
CStudent_group.obj
F:\hiapp_text>main_test 执行
no name score1 score2 score3 average
1 K.Weng 5 5 5 5.000000
2 T.Dixon 4 3 2 3.000000
3 V.Chu 3 4 5 4.000000
4 L.Tson 4 3 4 3.666667
5 L.Lee 3 4 3 3.333333
6 I.Young 4 2 5 3.666667
7 K.Hiro 4 2 1 2.333333
8 G.Ping 4 4 4 4.000000
9 H.Gu 2 3 4 3.000000
10 J.Jon 5 4 3 4.000000
average 3.800000 3.400000 3.600000
min 2 2 1
max 5 5 5
#ifndef _CSTUDENT_HEAD_
#define _CSTUDENT_HEAD_
#define N_SCORES 3
#define SCORE1 0
#define SCORE2 1
#define SCORE3 2
class CStudent {
public:
char * name;
unsigned int scores[N_SCORES];
float getAverage();
CStudent(char * name,int score1,int score2,int score3);
~CStudent();
};
#endif
//CStudent.cpp
#include <stdlib.h> //for malloc() free()
#include <string.h> //for strlen() strcpy()
#include "CStudent.h"
//
CStudent::CStudent(char * name,int score1,int score2,int score3){
// copy name
this->name = (char*)malloc(strlen(name)+1);
strcpy(this->name,name);
//
this->scores[SCORE1]=score1;
this->scores[SCORE2]=score2;
this->scores[SCORE3]=score3;
}
CStudent::~CStudent(){
if(NULL != this->name){
free(this->name);
this->name = NULL;
}
}
float CStudent::getAverage(){
int sum = 0;
for(int i=0;i < N_SCORES;++i){
sum = sum + this->scores[i];
}
return ((float)sum)/N_SCORES;
}
//CStudent_group.h
#ifndef _CSTUDENT_GROUP_HEAD_
#define _CSTUDENT_GROUP_HEAD_
#include "CStudent.h"
#define MAX_STUDENT_NUM 10
class CStudent_group{
public:
CStudent * stdArray[MAX_STUDENT_NUM];
int iCurNum;
void AddStudent(CStudent* stu);
void PrintAnalyseInfo();
float getScoreNAverage(int scoren);
int getScoreNmin(int scoren);
int getScoreNmax(int scoren);
CStudent_group();
~CStudent_group();
};
#endif
//CStudent_group.cpp
#include <stdio.h> //for printf()
#include "CStudent_group.h"
CStudent_group::CStudent_group(){
iCurNum = 0;
}
CStudent_group::~CStudent_group(){
for(int i = 0; i < iCurNum;++i){
delete stdArray[i];
stdArray[i] = NULL;
}
}
void CStudent_group::AddStudent(CStudent* stu){
if(iCurNum >= MAX_STUDENT_NUM){
return;
}
stdArray[iCurNum++] = stu;
}
float CStudent_group::getScoreNAverage(int scoren){
int sum = 0;
if(iCurNum <= 0){
return 0.0;
}
for(int i = 0; i < iCurNum; i++){
sum = sum + stdArray[i]->scores[scoren];
}
return ((float)sum)/iCurNum;
}
int CStudent_group::getScoreNmin(int scoren){
int min = stdArray[0]->scores[scoren];
for(int i = 1; i < iCurNum; i++){
if(stdArray[i]->scores[scoren] < min){
min = stdArray[i]->scores[scoren];
}
}
return min;
}
int CStudent_group::getScoreNmax(int scoren){
int max = stdArray[0]->scores[scoren];
for(int i = 1; i < iCurNum; i++){
if(stdArray[i]->scores[scoren] > max){
max = stdArray[i]->scores[scoren];
}
}
return max;
}
void CStudent_group::PrintAnalyseInfo(){
if(iCurNum <= 0){
return;
}
printf("\tno\tname\tscore1\tscore2\tscore3\taverage\n");
for(int i = 0; i < iCurNum;++i){
printf("%d\t%s\t%d\t%d\t%d\t%f\n",1+i,stdArray[i]->name,
stdArray[i]->scores[SCORE1],
stdArray[i]->scores[SCORE2],
stdArray[i]->scores[SCORE3],
stdArray[i]->getAverage());
}
printf(" \taverage\t%f\t%f\t%f\n",
getScoreNAverage(SCORE1),
getScoreNAverage(SCORE2),
getScoreNAverage(SCORE3));
printf(" \tmin\t%d\t%d\t%d\n",
getScoreNmin(SCORE1),
getScoreNmin(SCORE2),
getScoreNmin(SCORE3));
printf(" \tmax\t%d\t%d\t%d\n",
getScoreNmax(SCORE1),
getScoreNmax(SCORE2),
getScoreNmax(SCORE3));
}
//main_test.cpp
#include "CStudent.h"
#include "CStudent_group.h"
int main(int argc,char* argv[]){
CStudent_group* psg = new CStudent_group();
psg->AddStudent(new CStudent("K.Weng",5,5,5));
psg->AddStudent(new CStudent("T.Dixon",4,3,2));
psg->AddStudent(new CStudent("V.Chu",3,4,5));
psg->AddStudent(new CStudent("L.Tson",4,3,4));
psg->AddStudent(new CStudent("L.Lee",3,4,3));
psg->AddStudent(new CStudent("I.Young",4,2,5));
psg->AddStudent(new CStudent("K.Hiro",4,2,1));
psg->AddStudent(new CStudent("G.Ping",4,4,4));
psg->AddStudent(new CStudent("H.Gu",2,3,4));
psg->AddStudent(new CStudent("J.Jon",5,4,3));
psg->PrintAnalyseInfo();
return 0;
}
编译测试过程及结果:
F:\hiapp_text>cl main_test.cpp CStudent.cpp CStudent_group.cpp 编译命令
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
main_test.cpp
CStudent.cpp
CStudent_group.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:main_test.exe
main_test.obj
CStudent.obj
CStudent_group.obj
F:\hiapp_text>main_test 执行
no name score1 score2 score3 average
1 K.Weng 5 5 5 5.000000
2 T.Dixon 4 3 2 3.000000
3 V.Chu 3 4 5 4.000000
4 L.Tson 4 3 4 3.666667
5 L.Lee 3 4 3 3.333333
6 I.Young 4 2 5 3.666667
7 K.Hiro 4 2 1 2.333333
8 G.Ping 4 4 4 4.000000
9 H.Gu 2 3 4 3.000000
10 J.Jon 5 4 3 4.000000
average 3.800000 3.400000 3.600000
min 2 2 1
max 5 5 5
更多追问追答
追问
不管怎样先谢谢你,我试试不懂再问你
追答
简单非集成环境快速测试:
“开始”|运行,输出cmd打开命令行
创建测试目录: mkdir c:\cpptest
将上述代码相应保存到五个文件放与c:\cpptest目录下
输入执行"C:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT“初始化命令行编译环境,VC根据自身版本和安装目录而定,都是在VC目录的Bin子目录下有该批文件。
输入编译: cl main_test.cpp CStudent.cpp CStudent_group.cpp
执行程序: main_text.exe 得到测试结果。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询