文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 高等教育 > 工学 > 安工大工商学院C++期末考试重点编程题
侵权投诉

安工大工商学院C++期末考试重点编程题

2-29 编写一个完整的程序,运行时向用户提问"你考试考了多少分?(0~100)",接收输入后判断其等级,显示出来。规则如下:


解:
#include <iostream.h>

void main()
{
int i,score;

cout << "你考试考了多少分?(0~100):";
cin >> score;
if (score>100 || score<0)
cout << "分数值必须在0到100之间!";
else
{

i = score/10;
switch (i)
{
case 10:
case 9:
cout << "你的成绩为优!";
break;
case 8:
cout << "你的成绩为良!";
break;
case 7:
case 6:
cout << "你的成绩为中!";
break;
default:
cout << "你的成绩为差!";
}
}
}
程序运行输出:
你考试考了多少分?(0~100):85
你的成绩为良!


4-8 定义一个Dog 类,包含的age、weight等属性,以及对这些属性操作的方法。实现并测试这个类。


解:
源程序:
#include <iostream.h>

class Dog
{
public:
Dog (int initialAge = 0, int initialWeight = 5);
~Dog();
int GetAge() { return itsAge;} // inline!
void SetAge (int age) { itsAge = age;} // inline!
int GetWeight() { return itsWeight;} // inline!
void SetWeight (int weight) { itsAge = weight;} // inline!
private:
int itsAge, itsWeight;
};

Dog::Dog(int initialAge, int initialWeight)
{
itsAge = initialAge;
itsWeight = initialWeight;
}

Dog::~Dog() //destructor, takes no action
{
}

int main()
{
Dog Jack(2,10);
cout << "Jack is a Dog who is " ;
cout << Jack.GetAge() << " years old and";
cout << Jack.GetWeight() << " pounds weight.\n";
Jack.SetAge(7);
Jack.SetWeight(20);
cout << "Now Jack is " ;
cout << Jack.GetAge() << " years old and";
cout << Jack.GetWeight() << " pounds weight.";

return 0;
}
程序运行输出:
Jack is a Dog who is 2 years old and 10 pounds weight.
Now Jack is 7 years old 20 pounds weight.


4-9 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。


解:
源程序:
#include <iostream.h>
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle () {}

int GetTop() const { return itsTop; }
int GetLeft() const { return itsLeft; }
int GetBottom() const { return itsBottom; }
int GetRight() const { return itsRight; }

void SetTop(int top) { itsTop = top; }
void SetLeft (int left) { itsLeft = left; }
void SetBottom (int bottom) { itsBottom = bottom; }
void SetRight (int right) { itsRight = right; }

int GetArea() const;

private:
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
}

int Rectangle::GetArea() const
{
int Width = itsRight-itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}

int main()
{
Rectangle MyRectangle (100, 20, 50, 80 );

int Area = MyRectangle.GetArea();

cout << "Area: " <

第1页

你可能喜欢

  • 大学C++期末考试题
  • C++期末试题
  • C++考试题库
  • 期末考试卷
  • 考试试卷
  • 资格考试
  • C++期末考试题及答案
  • 考试中心

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email