A.Point(类与构造)
题目描述
下面是一个平面上的点的类定义,请在类外实现它的所有方法,并生成点测试它。
1 2 3 4 5 6 7 8 9 10 11 12 class Point { double x,y; public : Point (); Point (double x_value,double y_value); double getX () ; double getY () ; void setX (double x_value) ; void setY (double y_value) ; double distanceToAnotherPoint (Point p) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #include <iostream> #include <iomanip> #include <math.h> using namespace std;class Point { private : double x, y; public : Point (); Point (double x_value, double y_value); double getX () ; double getY () ; void setX (double x_value) ; void setY (double y_value) ; double distanceToAnotherPoint (Point p) ; }; Point::Point () { x = 0 , y = 0 ; } Point::Point (double x_value, double y_value) { this ->x = x_value; this ->y = y_value; } double Point::getX () { return this ->x; } double Point::getY () { return this ->y; } void Point::setX (double x_value) { this ->x=x_value; } void Point::setY (double y_value) { this ->y = y_value; } double Point::distanceToAnotherPoint (Point p) { return sqrt (pow (this ->x - p.getX (), 2 ) + pow (this ->y - p.getY (), 2 )); } int main () { int t; double x1, y1, x2, y2, distance; cin >> t; while (t--) { cin >> x1 >> y1 >> x2 >> y2; Point p1 (x1, y1) ; Point p2 (x2, y2) ; distance = p1. distanceToAnotherPoint (p2); cout << fixed << setprecision (2 ) << "Distance of Point(" << x1 << "," << y1 << ") to Point(" << x2 << "," << y2 << ") is " << distance << endl; } return 0 ; }
B.Date(类与构造)
题目描述
下面是一个日期类的定义,请在类外实现其所有的方法,并在主函数中生成对象测试之。
1 2 3 4 5 6 7 8 9 10 11 12 13 class Date { int year,month,day; public : Date (); Date (int y,int m,int d); int getYear () ; int getMonth () ; int getDay () ; void setDate (int y,int m,int d) ; void print () ; void addOneDay () ; }
注意,在判断明天日期时,要加入跨月、跨年、闰年的判断
例如9月30日的明天是10月1日,12月31日的明天是第二年的1月1日
2月28日的明天要区分是否闰年,闰年则是2月29日,非闰年则是3月1日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 #include <iostream> #include <iomanip> using namespace std;class Date { private : int year, month, day; public : Date (); Date (int y, int m, int d); int getYear () ; int getMonth () ; int getDay () ; void setDate (int y, int m, int d) ; void printf () ; void addOneDay () ; }; Date::Date () { year = 1900 , month = 1 , day = 1 ; } Date::Date (int y, int m, int d) { year = y; month = m; day = d; } int Date::getYear () { return year; } int Date::getMonth () { return month; } int Date::getDay () { return day; } void Date::setDate (int y, int m, int d) { year = y; month = m; day = d; } void Date::printf () { cout << year << "/" <<setfill ('0' )<<setw (2 ) << month << "/" <<setfill ('0' )<<setw (2 ) << day << endl; } void Date::addOneDay () { int dm; day++; if (month == 2 ) { if (year % 4 == 0 && year / 100 != 0 || year % 400 == 0 ) { dm = 29 ; } else { dm = 28 ; } } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) { dm = 31 ; } else { dm = 30 ; } if (day > dm) { day = day - dm; month++; } if (month > 12 ) { month = month - 12 ; year++; } } int main () { int t, y, m, d; cin >> t; while (t--) { cin >> y >> m >> d; if (t % 2 == 1 ) { Date a (y, m, d) ; cout << "Today is " ; a.printf (); a.addOneDay (); cout << "Tomorrow is " ; a.printf (); } else { Date a; a.setDate (y, m, d); cout << "Today is " ; a.printf (); a.addOneDay (); cout << "Tomorrow is " ; a.printf (); } } return 0 ; }
C.分数类(类与构造)
题目描述
完成下列分数类的实现:
class CFraction
{
private:
int fz, fm;
public:
CFraction(int fz_val, int fm_val) ;
CFraction add(const CFraction &r);
CFraction sub(const CFraction &r);
CFraction mul(const CFraction &r);
CFraction div(const CFraction &r);
int getGCD(); // 求对象的分子和分母的最大公约数
void print();
};
求两数a、b的最大公约数可采用辗转相除法,又称欧几里得算法,其步骤为:
交换a, b使a > b;
用a除b得到余数r,若r=0,则b为最大公约数,退出.
若r不为0,则用b代替a, r代替b,此时a,b都比上一次的小,问题规模缩小了;
继续第2步。
注意:如果分母是1的话,也按“分子/1”的方式输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 #include <iostream> #include <cmath> #include <iomanip> using namespace std;class CFraction { private : int fz, fm; public : CFraction (); CFraction (int fz_val, int fm_val) :fz (fz_val), fm (fm_val) {} CFraction add (const CFraction& r) ; CFraction sub (const CFraction& r) ; CFraction mul (const CFraction& r) ; CFraction div (const CFraction& r) ; int getGCD () ; void print () ; }; CFraction CFraction::add (const CFraction& r) { return CFraction (fz * r.fm + fm * r.fz, fm * r.fm); } CFraction CFraction::sub (const CFraction& r) { return CFraction (fz * r.fm - fm * r.fz, fm * r.fm); } CFraction CFraction::mul (const CFraction& r) { return CFraction (fz * r.fz, fm * r.fm); } CFraction CFraction::div (const CFraction& r) { return CFraction (fz * r.fm, fm * r.fz); } int CFraction::getGCD () { int r ,a,b; r = fm, a = fabs (fz), b = fabs (fm); while (a % b != 0 ) { r = a % b; a = b; b = r; } return b; } void CFraction::print () { int gcd = getGCD (); fz /= gcd; fm /= gcd; string signal; if (fz / fz == fm / fm) signal = "" ; else signal = "-" ; if (fm != 1 ) { cout << signal << fz << "/" << fm << endl; } else cout << signal << fz << "/" << "1" << endl; } int main () { int t; int fz1, fm1, fz2, fm2; char c; cin >> t; while (t--) { cin >> fz1 >> c >> fm1 >> fz2 >> c >> fm2; CFraction cs1 (fz1, fm1) ; CFraction cs2 (fz2, fm2) ; CFraction cs3 = cs1. add (cs2); cs3. print (); cs3 = cs1. sub (cs2); cs3. print (); cs3 = cs1. mul (cs2); cs3. print (); cs3 = cs1. div (cs2); cs3. print (); cout << endl; } return 0 ; }
D.Point_Array(类+构造+对象数组)
题目描述
1 2 3 4 5 6 7 8 9 10 11 12 13 class Point { double x,y; public : Point (); Point (double x_value,double y_value); double getX () ; double getY () ; void setXY (double x1,double y1) {x=x1;y=y1} void setX (double x_value); void setY (double y_value) ; double getDisTo (Point &p) ; }
上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改:
1、增加自写的析构函数;
2、将getDisTo方法的参数修改为getDisTo(const Point &p);
3、根据输出的内容修改相应的构造函数。
然后在主函数中根据用户输入的数目建立Point数组,求出数组内距离最大的两个点之间的距离值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #include <iostream> #include <iomanip> #include <math.h> using namespace std;class Point { private : double x, y; public : Point (); ~Point () { cout << "Distructor." << endl; } Point (double x_val, double y_val); double getX () ; double getY () ; void setXY (double x1, double y1) ; void setX (double x_val) ; void setY (double y_val) ; double getDisTo (const Point& p) ; }; Point::Point () { x = 0 , y = 0 ; cout << "Constructor." << endl; } Point::Point (double x_val, double y_val) { x = x_val; y = y_val; } double Point::getX () { return x; } double Point::getY () { return y; } void Point::setXY (double x1, double y1) { x = x1; y = y1; } void Point::setX (double x_val) { x = x_val; } void Point::setY (double y_val) { y = y_val; } double Point::getDisTo (const Point& p) { return sqrt (pow (x-p.x,2 )+pow (y-p.y,2 )); } int main () { int t; cin >> t; while (t--) { int n; cin >> n; int x, y, i, j; Point* p = new Point[n]; for ( i = 0 ; i < n; i++) { double x1, y1; cin >> x1 >> y1; p[i].setXY (x1, y1); } double max = 0 ; int a1, a2; double tmp; for (i = 0 ; i < n; i++) { for (j = i + 1 ; j < n; j++) { tmp = p[i].getDisTo (p[j]); if (tmp > max) { max = tmp; a1 = i; a2 = j; } } } cout << "The longeset distance is " << fixed << setprecision (2 ) << max << ",between p[" << a1 << "] and p[" << a2 << "]." << endl; delete []p; } return 0 ; }
E.Stack(类与构造)
题目描述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class CStack { public : CStack (); CStack (int s); int get (int index) ; void push (int n) ; int isEmpty () ; int isFull () ; int pop () ; ~CStack (); private : int *a; int size; int top; }
上面是栈类的定义,栈是一种具有先进后出特点的线性表,请根据注释,完成类中所有方法的实现,并在主函数中测试之。
堆栈类的说明如下:
堆栈的数据实际上是保存在数组a中,而a开始是一个指针,在初始化时,根据实际需求将a动态创建为数组,数组长度根据构造函数的参数决定。
size实际上就是数组的长度,当使用无参构造则size为10,当使用有参构造则size为s
top表示数组下标,也表示数组中下一个存放数据的空白位置。
push操作表示堆栈的数组存放一个数据,例如一开始数组为空,则top为0,当有数据要入栈时,把数据存放在a[top]的位置,然后top加1指向下一个空白位置、数据进栈只能从栈顶进。
pop操作表示一个数据要出栈,数据出栈只能从栈顶出,先把top减1指向栈顶数据,然后把数据返回。
判断堆栈空的条件是top是否等于0,判断堆栈满的条件是top是否等于size
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 #include <iostream> using namespace std;class CStack { private : int * a; int size; int top; public : CStack () { a = new int [10 ]; size = 10 ; top = 0 ; cout << "Constructor." << endl; } CStack (int s) { a = new int [s]; size = s; top = 0 ; cout << "Constructor." << endl; } int get (int index) { return *(a + index); } void push (int n) { a[top++] = n; } int isEmpty () { return top == 0 ; } int isFull () { return top == size; } int pop () { return *(a + (--top)); } ~CStack () { delete []a; cout << "Destructor." << endl; } }; int main () { int i, j, t, size_1, m; cin >> t; for (i = 0 ; i < t; i++) { cin >> size_1; CStack CStack1 (size_1) ; for (j = 0 ; j < size_1; j++) { cin >> m; CStack1. push (m); } if (CStack1. isEmpty () == 1 ) { cout << "empty" << endl; } else if (CStack1. isFull () == 1 ) { for (j = 0 ; j < size_1-1 ; j++) { cout << CStack1. pop () << " " ; } cout << CStack1. pop () << endl; } } return 0 ; }