计算机程序设计C++ MOOC
测试与作业C++基础练习100题
##第九周基本练习
本周内容为类的构建
- 点类
#include "iostream"
#include "cmath"
using namespace std;
class POINT
{
private:
double x, y;
public:
void set(double,double);
double distance(POINT);
void show();
};
void POINT::set(double a, double b)
{
x = a; y = b;
}
void POINT::show()
{
cout<<'('<<x<<','<<y<<')';
}
double POINT::distance(POINT a)
{
return sqrt((x - a.x)*(x - a.x) + (y - a.y)*(y - a.y));
}
int main()
{
POINT a, b;
double x, y;
cin >> x >> y;
a.set(x, y);
cin >> x >> y;
b.set(x, y);
a.show(); cout << endl;
b.show(); cout << endl;
cout << a.distance(b) << endl;
return 0;
}
- 圆类
#include "iostream"
using namespace std;
class CIRCLE
{
private:
double x, y, r;
public:
void set(double, double, double);
double area(){ return 3.14*r*r; }
double perimeter(){ return 2 * 3.14*r; }
void show();
};
void CIRCLE::set(double a, double b, double c)
{
x = a; y = b; r = c;
}
void CIRCLE::show()
{
cout << '(' << x << ',' << y << ',' << r << ')';
}
int main()
{
CIRCLE a;
double x, y, r;
cin >> x >> y >> r;
a.set(x, y, r);
a.show(); cout << endl;
cout << a.perimeter() << endl;
cout << a.area() << endl;
return 0;
}
- 矩形类
#include "iostream"
using namespace std;
class RECT
{
private:
int x1, y1, x2, y2;
public:
void set(int, int, int, int);
int area(){ return abs((x1 - x2))*abs((y1 - y2)); }
int perimeter(){ return 2 * abs((x1 - x2)) + 2 * abs((y1 - y2)); }
void showinfo();
void show();
};
void RECT::set(int a, int b, int c, int d)
{
x1 = a; y1 = b; x2 = c; y2 = d;
}
void RECT::showinfo()
{
cout << '(' << x1 << ',' << y1 << ',' << x2 <<','<< y2 << ')';
}
void RECT::show()
{
int m = abs((x1 - x2)), n = abs((y1 - y2));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << "*";
}
cout << endl;
}
}
int main()
{
RECT a;
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
a.set(x1, y1, x2, y2);
a.showinfo(); cout << endl;
cout << a.perimeter() << endl;
cout << a.area() << endl;
a.show();
return 0;
}
- 复数类
#include "iostream"
#include "cmath"
using namespace std;
class COMPLEX
{
private:
double real, imag;
public:
void set(double, double);
double getReal(){return real; }
double getImag(){ return imag; }
double module(){ return sqrt(real*real + imag*imag); }
COMPLEX add(COMPLEX);
void show();
};
void COMPLEX::set(double a, double b)
{
real = a; imag = b;
}
COMPLEX COMPLEX::add(COMPLEX a)
{
COMPLEX temp;
temp.real = real+a.real;
temp.imag = imag+a.imag;
return temp;
}
void COMPLEX::show()
{
if (imag >= 0)
cout << real << "+" << imag << 'j';
else
cout << real << imag << 'j';
}
int main()
{
COMPLEX a, b, c;
double x, y;
cin >> x >> y;
a.set(x, y);
cin >> x >> y;
b.set(x, y);
c = a.add(b);
c.show();
cout << endl;
cout << c.getReal() << endl;
cout << c.getImag() << endl;
cout << c.module() << endl;
return 0;
}
- 分数类
#include "iostream"
#include "cmath"
using namespace std;
class FRACTION
{
private:
int mol, den;
void reduce();
public:
void set(int,int);
double real(){ return double(mol)/double(den); }
void show();
};
void FRACTION::reduce()
{
if (mol == 0) return;
int m = (abs(mol) < abs(den)) ? abs(mol) : abs(den);
while (m)
{
if (mol%m == 0 && den%m == 0) break;
m--;
}
mol = mol / m;
den = den / m;
}
void FRACTION::set(int a, int b)
{
mol = a;
den = b;
reduce();
if (den < 0)
{
mol *= -1;
den *= -1;
}
}
void FRACTION::show()
{
cout << mol << '/' << den;
}
int main()
{
FRACTION a;
int x, y;
cin >> x >> y;
a.set(x, y);
a.show();
cout << endl;
cout << a.real() << endl;
return 0;
}
以上为第九周的基本练习。