请教达人两道C++题目

来源:百度知道 编辑:UC知道 时间:2024/06/18 11:08:30
1. 编写一个mutiple函数,令其判断一对整数中,第一个整数是否是第二个整数的倍数。函数取两个整数参数,并在第一个整数是第二个整数的倍数时返回 true,反之则返回 false。
2. 新建一个 Rectangle 类,该类的 length 和 width 属性默认为1,其成员函数计算长方形的 perimeter (周长) 和 area (面积)。为该类的 length 和width 设置 set 和 get 函数。set 函数应验证 length 和 width 需>0.0

正解,写的挺详细的了,如下:
第1题:
#include <iostream>
using namespace std;
bool mutiple(int a,int b){
return a%b==0?true:false;
}

void main( )
{
int a,b;
cout<<"请输入2个整数:\n";
cin>>a>>b;
if(mutiple(a,b))cout<<"第一个整数是第二个整数的倍数\n";
else cout<<"第一个整数不是第二个整数的倍数\n";

}

第2题:
#include <iostream>
using namespace std;

class Rectangle
{
public:
Rectangle(){
length=1.0;
width=1.0;
}

double perimeter(){ return 2*(length+width); }
double area(){ return length*width; }
void set(double l,double w)
{
if(l<=0.0||w<=0.0)
cout<<"输入的长或宽必须大于0!"<<endl;
else
{
length=l;
width=w;
}
}

void get()
{
cout<<"长为: "<<length<