实验6-2
6-1 矩形类
题目描述
定义一个矩形类,长和宽是它的属性,可以求出矩形的面积。定义一个比较函数,比较两个矩形的面积,把面积大的矩形对象作为引用来返回。主函数中定义两个矩形,它们的长、宽已知,分别为矩形1:长5.2、宽4.3,矩形2:长100、宽20。再由键盘输入一个矩形的长、宽。调用比较函数进行比较,找出面积大的矩形,输出其面积。
输入样例
10 8
输出样例
2000
#include<iostream>
using namespace std;
class s {
private:
double l, d;
double sq;
public:
s(double x, double y) {
l = x;
d = y;
sq = x * y;
};
double getS() {
return sq;
}
};
int main() {
s c1(5.2, 4.3);
s c2(100, 20);
double a, b;
cin >> a >> b;
s c3(a, b);
double t = (c1.getS() > c2.getS() ? c1.getS() : c2. getS()) > c3.getS() ? (c1.getS() > c2.getS() ? c1.getS() : c2. getS()) : c3.getS();
cout << t << endl;
return 0;
}
#include<iostream>
using namespace std;
class Triangle {
private:
int a, b, c;
public:
Triangle(int A,int B,int C) {
a = A;
b = B;
c = C;
}
void tape() {
bool i1 = 0, i2 = 0, i3 = 0;
if (a + b <= c || a + c <= b || b + c <= a)
cout << "Not a triangle" << endl;
else {
if (a==b&&b==c)
{
cout << "A equileteral triangle" << endl;
i1 = 1;
}
if ((a == b || a == c || b == c) && i1 == 0)
{
cout << "A isosceles triangle" << endl;
i2 = 1;
}
if ((a*a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) && i2 == 0)
{
cout << "A right triangle" << endl;
i3 = 1;
}
if (i1 == 0 && i2 == 0 && i3 == 0)
cout << "A triangle" << endl;
}
}
};
int main() {
int i, j, k;
cin >> i >> j >> k;
Triangle aaa(i, j, k);
aaa.tape();
return 0;
}
#include <iostream>
#include <cstdio>
#include<string>
using namespace std;
class Time {
private:
int year;
int month;
int day;
bool a = 0;
public:
Time(int x, int y,int z) {
year = x;
month = y;
day = z;
}
void show(int a) {
cout << year << "-" << month << "-" << day << endl;
}
void show(double a) {
cout << year << "年" << month << "月" << day << "日" << endl;
}
};
int main() {
int a, b, c;
string s;
cin >> a >> b >> c >> s;
Time clock(a, b, c);
int k = 1;
int len = s.length();
for (int i = 0; i < len; i++) {
if (s[i] == '.')
k = 0;
}
if (k)
clock.show(1);
if (k == 0)
clock.show(1.0);
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Employee {
private:
int gonghao;
string name;
int wage;
int year;
int month;
int day;
public:
Employee(int id, string n, int w, int y, int m, int d) {
gonghao = id;
name = n;
wage = w;
year = y;
month = m;
day = d;
};
void pay(int x, int y) {
int num = (x - year) * 12 + (y - month);
num *= wage;
cout << name << " " << num << endl;
}
};
int main() {
Employee h[5] = { Employee(1,"wang",5000,2000,10,23),
Employee(2,"liu",4500,2008,1,20),
Employee(3,"huo",3800,2003,7,3),
Employee(4,"ma",5300,2015,4,10),
Employee(5,"meng",6000,2016,3,16) };
int a, b, c;
cin >> a >> b >> c;
h[a - 1].pay(b, c);
return 0;
}