基本操作 原题
#include<iostream>
#pragma warning (disable:4996)
using namespace std;
class Score{
public:
Score(const char *the_course, const char *the_id, int the_normal, int the_midterm, int the_end_of_term)
: course(the_course), normal(the_normal), midterm(the_midterm) , end_of_term(the_end_of_term){
// ERROR **********found**********
strcpy(the_id,student_id);
}
const char *getCourse()const{ return course; } //返回课程名称
// ERROR **********found**********
const char *getID()const{ return &student_id; } //返回学号
int getNormal()const{ return normal; } //返回平时成绩
int getMidterm()const{ return midterm; } //返回期中考试成绩
int getEndOfTerm()const{ return end_of_term; } //返回期末考试成绩
int getFinal()const; //返回总评成绩
private:
const char *course; //课程名称
char student_id[12]; //学号
int normal; //平时成绩
int midterm; //期中考试成绩
int end_of_term; //期末考试成绩
};
//总评成绩中平时成绩占20%,期中考试占30%,期末考试占50%,最后结果四舍五入为一个整数
// ERROR **********found**********
int getFinal()const{
return (int)(normal*0.2+midterm*0.3+end_of_term*0.5+0.5);
}
int main(){
char English[]="英语";
Score score(English,"12345678",68,83,92);
cout<<"学号:"<<score.getID()<<" ";
cout<<"课程:"<<score.getCourse()<<" ";
cout<<"总评成绩:"<<score.getFinal()<<endl;
return 0;
}
基本操作 答案
#include<iostream>
#pragma warning (disable:4996)
using namespace std;
class Score{
public:
Score(const char *the_course, const char *the_id, int the_normal, int the_midterm, int the_end_of_term)
: course(the_course), normal(the_normal), midterm(the_midterm) , end_of_term(the_end_of_term){
// ERROR **********found**********
strcpy(student_id,the_id);
}
const char *getCourse()const{ return course; } //返回课程名称
// ERROR **********found**********
const char *getID()const{ return student_id; } //返回学号
int getNormal()const{ return normal; } //返回平时成绩
int getMidterm()const{ return midterm; } //返回期中考试成绩
int getEndOfTerm()const{ return end_of_term; } //返回期末考试成绩
int getFinal()const; //返回总评成绩
private:
const char *course; //课程名称
char student_id[12]; //学号
int normal; //平时成绩
int midterm; //期中考试成绩
int end_of_term; //期末考试成绩
};
//总评成绩中平时成绩占20%,期中考试占30%,期末考试占50%,最后结果四舍五入为一个整数
// ERROR **********found**********
int Score::getFinal()const{
return (int)(normal*0.2+midterm*0.3+end_of_term*0.5+0.5);
}
int main(){
char English[]="英语";
Score score(English,"12345678",68,83,92);
cout<<"学号:"<<score.getID()<<" ";
cout<<"课程:"<<score.getCourse()<<" ";
cout<<"总评成绩:"<<score.getFinal()<<endl;
return 0;
}
简单操作 原题
//proj2.cpp
#include "shape.h"
#include <iostream>
using namespace std;
//**********found**********
______________________________ // show函数的函数头(函数体以前的部分)
{
cout<<"此图形是一个" << shape->name()
<< ", 周长="<<shape->perimeter()
<< ", 面积="<<shape->area()<<endl;
}
int main( )
{
Shape *s = new Shape;
Triangle *tri = new Triangle(Point(0,2),Point(2,0),Point(0,0));
show(s);
show(tri);
return 0;
}
//shape.cpp
#include "shape.h"
#include <cmath>
double length(Point p1,Point p2)
{
return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+
(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
}
double Triangle::perimeter()const
{ //一个return语句,它利用length函数计算并返回三角形的周长
//**********found**********
________________________________________________________________________ ;
}
double Triangle::area()const
{
double s=perimeter()/2.0;
return sqrt(s*(s-length(point1,point2))*
(s-length(point2,point3))*
(s-length(point3,point1)));
}
//shape.h
class Shape{
public:
virtual double perimeter()const { return 0; } //返回形状的周长
virtual double area()const { return 0; } //返回形状的面积
virtual const char* name()const { return "抽象图形"; } //返回形状的名称
};
class Point{ //表示平面坐标系中的点的类
double x;
double y;
public:
//**********found**********
Point (double x0,double y0):____________________{ }//用x0、y0初始化数据成员x、y
double getX()const{ return x;}
double getY()const{ return y;}
};
class Triangle: public Shape{
//**********found**********
_____________________________________ ; //定义三角形的三个顶点
public:
Triangle(Point p1,Point p2,Point p3):point1(p1),point2(p2),point3(p3){}
double perimeter()const;
double area()const;
const char* name()const{ return "三角形"; }
};
简单操作 答案
//proj2.cpp
#include "shape.h"
#include <iostream>
using namespace std;
//**********found**********
void show(Shape * shape) // show函数的函数头(函数体以前的部分)
{
cout<<"此图形是一个" << shape->name()
<< ", 周长="<<shape->perimeter()
<< ", 面积="<<shape->area()<<endl;
}
int main( )
{
Shape *s = new Shape;
Triangle *tri = new Triangle(Point(0,2),Point(2,0),Point(0,0));
show(s);
show(tri);
return 0;
}
//shape.cpp
#include "shape.h"
#include <cmath>
double length(Point p1,Point p2)
{
return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+
(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
}
double Triangle::perimeter()const
{ //一个return语句,它利用length函数计算并返回三角形的周长
//**********found**********
return length(point1,point2)+length(point2,point3)+length(point1,point3);
}
double Triangle::area()const
{
double s=perimeter()/2.0;
return sqrt(s*(s-length(point1,point2))*
(s-length(point2,point3))*
(s-length(point3,point1)));
}
//shape.h
class Shape{
public:
virtual double perimeter()const { return 0; } //返回形状的周长
virtual double area()const { return 0; } //返回形状的面积
virtual const char* name()const { return "抽象图形"; } //返回形状的名称
};
class Point{ //表示平面坐标系中的点的类
double x;
double y;
public:
//**********found**********
Point (double x0,double y0):x(x0),y(y0){ }//用x0、y0初始化数据成员x、y
double getX()const{ return x;}
double getY()const{ return y;}
};
class Triangle: public Shape{
//**********found**********
Point point1,point3,point2; //定义三角形的三个顶点
public:
Triangle(Point p1,Point p2,Point p3):point1(p1),point2(p2),point3(p3){}
double perimeter()const;
double area()const;
const char* name()const{ return "三角形"; }
};
综合应用 原题
//main.cpp
#include "Array.h"
//交换数组a中前后位置对称的元素的值
template<class Type, int m>
void Array<Type,m>::Contrary() { //补充函数体
//********333********
//********666********
}
int main(){
int s1[5]={1,2,3,4,5};
double s2[6]={1.2,2.3,3.4,4.5,5.6,8.4};
Array<int,5> d1(s1,5);
Array<double,8> d2(s2,6);
int i;
d1.Contrary(); d2.Contrary();
cout<<d1.Length()<<' '<<d2.Length()<<endl;
for(i=0;i<4;i++) cout<<d1[i]<<", "; cout<<d1[4]<<endl;
for(i=0;i<7;i++) cout<<d2[i]<<", "; cout<<d2[7]<<endl;
writeToFile(".\\"); //不用考虑此语句的作用
return 0;
}
//Array.h
#include<iostream>
using namespace std;
template<class Type, int m>
class Array { //数组类
public:
Array(Type b[], int mm) { //构造函数
for(int i=0; i<m; i++)
if(i<mm) a[i]=b[i]; else a[i]=0;
}
void Contrary(); //交换数组a中前后位置对称的元素的值
int Length() const{ return m; } //返回数组长度
Type operator [](int i)const { //下标运算符重载为成员函数
if(i<0 || i>=m) {cout<<"下标越界!"<<endl; exit(1);}
return a[i];
}
private:
Type a[m];
};
void writeToFile(const char *); //不用考虑此语句的作用
综合应用 答案
//main.cpp
#include "Array.h"
//交换数组a中前后位置对称的元素的值
template<class Type, int m>
void Array<Type,m>::Contrary() { //补充函数体
//********333********
Type b[m];
for(int i=0;i<m;i++)
b[i] = a[i];
for(int j=0;j<m;j++)
a[j] = b[m-j-1];
//********666********
}
int main(){
int s1[5]={1,2,3,4,5};
double s2[6]={1.2,2.3,3.4,4.5,5.6,8.4};
Array<int,5> d1(s1,5);
Array<double,8> d2(s2,6);
int i;
d1.Contrary(); d2.Contrary();
cout<<d1.Length()<<' '<<d2.Length()<<endl;
for(i=0;i<4;i++) cout<<d1[i]<<", "; cout<<d1[4]<<endl;
for(i=0;i<7;i++) cout<<d2[i]<<", "; cout<<d2[7]<<endl;
writeToFile(".\\"); //不用考虑此语句的作用
return 0;
}