C++程序设计B 实验3

文章包含多个C++编程示例,包括定义计数器类实现加减操作,阶乘类计算阶乘,重载`sort`函数分别实现整数和浮点数数组的冒泡排序和选择排序,编写函数求两个或三个正数的最大值,设计时间类判断闰年并输出,以及三角形类计算周长和面积。此外,还展示了处理int、double和string类型的重载函数。
摘要由CSDN通过智能技术生成

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;
        else
            cout << "不是闰年" << endl;
    } else
        cout << "是闰年" << 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;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再见以前说再见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值