C++程序设计B 实验3
1、定义计数器类Counter。要求具有以下成员:计数器值;可进行增值和减值记数(每次增1或减1即可);可提供记数值。(要求使用构造函数对数据成员初始化)
#include <iostream>
using namespace std;
class Counter {
private:int num;
public:Counter(int a);void jia();void jian();void show();
};
Counter::Counter(int a) {num = a;
}
void Counter::show() {cout << num;
}
void Counter:: jia() {num++;
}
void Counter:: jian() {num--;
}
int main() {int a;cout<<"请输入计数器的初始值:"<<endl;cin >> a;Counter C1(a);cout << "C1=";C1.show();C1.jia();cout << endl << "完成加数操作后C1=";C1.show();C1.jian();cout << endl << "完成减数操作后C1=";C1.show();cout << endl;return 0;
}
2、定义一个阶乘类Cfactorial实现阶乘的计算和显示。 (要求使用构造函数对数据成员初始化)
#include <iostream>
using namespace std;
class Cfactorial {
private:int n;
public:Cfactorial(int a);int F(int b);
};
Cfactorial::Cfactorial(int a) {n = a;
}
int Cfactorial::F(int b) {if(b == 0) return 1;else return F(b - 1) * b;
}
int main() {int n;cout << "请输入n:";cin >> n;Cfactorial a(n);cout << n << "的阶乘为:" << a.F(n) << endl;return 0;
}
3、写两个重载函数sort,分别实现对int和double 数组进行排序。两个函数分别采用冒泡排序和选择排序。在main函数中调用这两个函数。(注:本题不需要定义类)
#include <iostream>
using namespace std;
void Sort(int a[5], int n) { //冒泡排序int i, j, t;for(i = 0; i < n - 1; i++) {for(j = 0; j < n - i - 1; j++) {if(a[j] > a[j + 1]) {t = a[j + 1];a[j + 1] = a[j];a[j] = t;}}}cout << "冒泡排序结果如下:" << endl;for(i = 0; i < n; i++) {cout << a[i] << " ";if(i == n - 1) cout << endl;}
}
void Sort(double b[5], int n) { //选择排序int i, j, minn;double t;for (i = 0; i < n - 1; i++) {minn = i;for (j = i + 1; j < n; j++)if (b[j] < b[minn])minn = j;if (minn != i) {t = b[i];b[i] = b[minn];b[minn] = t;}}cout << "选择排序结果如下:" << endl;for(i = 0; i < n; i++) {cout << b[i] << " ";if(i == n - 1) cout << endl;}
}
int main() {int n, i;cout << "请输入n:";cin >> n;int a[n];cout << "请依次输入a[n]:" << endl;for(i = 0; i < n; i++) {cin >> a[i];}Sort(a, n);double b[n];cout << "请依次输入b[n]:" << endl;for(i = 0; i < n; i++) {cin >> b[i];}Sort(b, n);return 0;
}
4、编写一个函数,求两个或三个正数的最大值。
要求:用带默认值的函数实现。(注:本题不需要定义类)
#include <iostream>
using namespace std;
int maxx(int a, int b, int c = 0) {int t;if(a > b) t = a;else {t = b;if(t < c) {t = c;}}
}
int main() {int a, b, c;cout << "请输入a,b,c:";cin >> a >> b >> c;cout <<"前两个正数的最大值:"<< maxx(a, b) << endl;cout <<"前三个正数的最大值:"<< maxx(a, b, c) << endl;return 0;
}
5、设计一个时间类,其中数据成员:年 月 日
成员函数有三个:(1)构造函数设置年月日的具体值;(2)判断该年是否为闰年;(3)将年月日输出。编写主函数,实现并测试这个类。
#include <iostream>
using namespace std;
class Time {int y, m, d;
public:Time() {cin >> y >> m >> d;}void show();void leap();
};void Time::show() {cout << y << "年" << m << "月" << d << "日" << endl;
}void Time::leap() {cout << y;if(y % 100 == 0) {if(y % 400 == 0)cout << "是闰年" << endl;elsecout << "不是闰年" << endl;} elsecout << "是闰年" << endl;
}
int main() {cout << "请输入年 月 日:" << endl;Time a;a.leap();a.show();return 0;
}
6、重做之前的一道题,要求使用构造函数。
题目要求如下:定义三角形类,完成:为三边置值、取三边的值并输出、求三角形周长、求三角形面积、输出三角形周长和面积。要求使用构造函数为三边赋值。提示:可以定义一个无参的构造函数,在此构造函数中通过键盘输入的方法输入三边的值。
#include <iostream>
#include <cmath>
using namespace std;class Triangle {double a, b, c;
public:Triangle(){cin >> a >> b >> c;}void Get() {cout << "三边的值:" << a << " " << b << " " << c << endl; //取三边的值并输出}double l() {return a + b + c; //求三角形周长}double s() {return (sqrt((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a)) / 4); //求三角形面积}
};int main() {double x, y, z;cout << "请输入三边的值(x,y,z)" << endl;Triangle A;A.Get();cout << "三角形周长:" << A.l() << endl;cout << "三角形面积:" << A.s() << endl;
}
7、创建一个函数plus,它把两个数值加在一起,返回它们的和,提供处理int、double和string类型的重载版本。
(注:本题不需要定义类) (注意函数名plus在VC++6.0编译系统下能调试通过,但在一些编译系统下会报错,因为在某些编译系统下被处理成了关键字,报错时换个名字即可,比如改成plus1等)
#include <iostream>
#include <cstring>
using namespace std;
void plus1(int a, int b) {cout << a + b << endl;
}
void plus1(double a, double b) {cout << a + b << endl;
}
void plus1(string a, string b) {cout << a + b << endl;
}
int main() {int x, y;cout << "请输入两个int值" << endl;cin >> x >> y;plus1(x, y);double m, n;cout << "请输入两个double值" << endl;cin >> m >> n;plus1(m, n);string i, j;cout << "请输入两个string值" << endl;cin >> i >> j;plus1(i, j);return 0;
}