实验五 结构体和指针

该博客介绍了两个C++编程练习,一是利用结构体和指针实现画图功能,包括画圆和长方形,用户交互输入起点和终点,程序根据输入绘制图形。二是设计一个所得税计算程序,用户输入规则和收入,程序根据规则计算并输出应缴税额。这两个例子展示了结构体在参数传递和函数复用中的应用,以及处理用户输入和计算逻辑的技巧。
摘要由CSDN通过智能技术生成

实验五 结构体和指针

1 实验目的

(1)学习如何使用结构体的指针作为函数的参数,通过传地址的方式,在被调用函数中修改主调函数中的多个结构体变量的方法。

(2)学习如何把逻辑结构相同的部分抽象为函数,以提高代码的可重用性,达到提高程序的可维护性的目的。

2 实验内容

2.1 模拟画图功能

问题描述

模拟计算机的画图功能,能够模拟画圆和长方形的功能。程序主要功能如下:

① 提供一个如下的主菜单。


\1. Circle (圆)

\2. Rectangle (长方形)

\0. Exit (退出)


② 不断接受用户的选择(整数),直至用户选择0为止。如果用户输入了系统尚未支持的选择(比如3),则给出相应的提示信息,并继续选择。

③ 如果用户选择了圆或长方形,则进一步提示用户输入两个点,分别称为起点和终点,如下图所示。坐标仅考虑正整数的情况。要求终点的坐标大于起点的坐标,否则给出相关的提示信息并返回主菜单。

图1. 显示屏的坐标系与起点、终点位置

④ 模拟画出圆和长方形。画圆时,要计算startPoint和endPoint所构成的正方形的内切圆的圆心和半径。若startPoint和endPoint所构成的不是正方形,则给出相关的提示信息并返回主菜单。

问题要求

① 定义一个Point结构体,用来表示显示屏上的点;

② 实现以下函数(图2),使得主程序(图3)输出图4中的结果。

图2. 需要实现的函数雏形

主程序如图3所示。

图3. 主程序

运行结果如图4所示。

图4. 运行结果

注意,上述结果未能列出所有可能的执行情况。请认真思考,相关的提示信息应该分别在由哪些函数中输出?

2.2 计算所得税

问题描述

一个国家计算所得税的规则由如下的不确定多条规则构成:

其中,n是规则的条数,由用户输入。对于任意的 , 表示个人收入中在区间 的部分的税率,它是一个整数,代表百分之几。 为税率起征点, 。

问题要求

① 请定义一个结构体,用来存放一条规则。

② 根据用户输入的规则条数,使用结构体的数组存放所有的规则,并将条数与数组定义为全局变量。

③ 定义一个子函数inputRules(),根据用户输入的规则条数,从键盘得到所有规则,存放至数组。

④ 定义一个子函数计算所得税。

// 参数income表示个人收入

double computeTax(int income);

⑤ 在主函数中,一次录入规则后,可以循环计算任意多个人的所得税,直到输入的收入为-1为止。

⑥ 请至少测试以下情况

Ÿ 输入规则后,在录入收入时,直接录入-1;

Ÿ 收入为0;

Ÿ 收入小于 ;

Ÿ 收入等于 的各种情况;

Ÿ 收入大于 ;

Ÿ 收入在 和 之间的情况。

程序运行要求

程序的一次运行结果如下:

请输入规则的条数:3

请输入第 1 条规则:800 3

请输入第 2 条规则:2000 4

请输入第 3 条规则:5000 3

纳税规则如下:

纳税线 税率

800 3

2000 4

5000 3

请输入您的收入:0

您的收入是:0,应缴所得税:0.00元。

请输入您的收入:800

您的收入是:800,应缴所得税:0.00元。

请输入您的收入:801

您的收入是:801,应缴所得税:0.03元。

请输入您的收入:2000

您的收入是:2000,应缴所得税:36.00元。(2000 – 800)* 3/100

请输入您的收入:1999

您的收入是:1999,应缴所得税:35.97元。

请输入您的收入:5000

您的收入是:5000,应缴所得税:156.00元。[(2000 – 800)*3+(5000 – 2000)*4]/100

请输入您的收入:10000

您的收入是:10000,应缴所得税:306.00元。[(2000 – 800)*3+(5000 – 2000)*4+(10000 – 5000)*3] / 100

请输入您的收入:-1

再见

请按任意键继续···

3 代码

模拟画图功能

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct Point {
    Point(int a = 0, int b = 0) {
        x = a;
        y = b;
    }

    int x;
    int y;
};

void displayMenu() {
    cout << "\n***********************\n";
    cout << " 1.Circle\n";
    cout << " 2.Rectangle\n";
    cout << " 0.Exit\n";
    cout << "***********************\n";
    cout << "Please select the shape: ";
}

bool getTowPoints(struct Point *a, struct Point *b) {
    int sx, sy, ex, ey;
    cout << "Please input the coordinate (x,y) of the start point: ";
    cin >> sx >> sy;
    cout << "Please input the coordinate (x,y) of the end point: ";
    cin >> ex >> ey;
    if (ex <= sx || ey <= sy) { return false; }
    else {
        a->x = sx, a->y = sy, b->x = ex, b->y = ey;
        return true;
    }
};

void printPoint(struct Point *a) { cout << "(" << a->x << "," << a->y << ")"; }

void drawCircle(struct Point *a, struct Point *b) {
    int dx = b->x - a->x, dy = b->y - a->y;
    if (dx != dy) {

        cout << "Not a circle,Select again!\n";
        return;
    } else {
        cout << "Draw a circle at center ";
        struct Point *p = new Point(a->x + dx / 2, a->y + dy / 2);
        printPoint(p);
        delete
                p;
        cout << "with radius " << dx / 2 <<
             endl;
    }
}

void drawRectangle(struct Point *a, struct Point *b) {
    cout << "Draw a rectangle at topleft ";
    printPoint(a);
    cout << ", whose width is " << (b->x) - (a->x) << " and height is " << (b->y) - (a->y) << endl;
}

int main() {
    int choice = 1;
    struct Point startP, endP;
    while (choice) {
        displayMenu();
        cin >> choice;
        switch (choice) {
            case 1:
                if (getTowPoints(&startP, &endP)) {
                    drawCircle(&startP, &endP);
                    break;
                } else {
                    cout << "Your input is incorrect!\n";
                    break;
                }
            case 2:
                if (getTowPoints(&startP, &endP)) {
                    drawRectangle(&startP, &endP);
                    break;
                } else {
                    cout << "Your input is incorrect!\n";
                    break;
                }
            case 0:
                cout << "Good Bye!\n";
                break;
            default:
                cout << "Not supported! Please select again!\n";
                break;
        }
    }
}

### 计算所得税

#include <cstdio>
#include <iostream>
#include <vector>
#include<cstring>
#include <string>
#include<algorithm>
#include<math.h>
#include<cmath>

using namespace std; // 因为用到了 to_string() 所以需要在 c++11 下运行!
string ans;
typedef struct Rule {
    Rule(int a = 0, int b = 0) {
        m = a;
        r = b;
    }

    int m;
    int r;
};

double computeTax(struct Rule rules[], int n, int income) {
    double tax = 0;
    if (!ans.empty()) { ans.clear(); }
    int i = 1;
    for (i = 1; i <= n; i++) {
        if (income > rules[i].m) {
            int mon;
            if (i == n) {
                mon = income - rules[i].m;
                ans = ans + "(" + to_string(income) + "-" + to_string(rules[i].m) + ")";
            }
            else {
                mon = min(income, rules[i + 1].m);
                ans = ans + "(" + to_string(mon) + "-" + to_string(rules[i].m) + ")";
                mon -= rules[i].m;
            }
            ans = ans + "*" + to_string(rules[i].r) + "+";
            tax += (mon * 1.0 * rules[i].r / 100);
        }
        else { break; }
    }
    if (tax != 0) {
        if (i > 2) {
            string ans2 = "[" + ans;
            ans = ans2;
            ans[ans.length() - 1] = ']';
            ans = ans + "/100";
        }
        else {
            ans[ans.length() - 1] = '/';
            ans = ans + "100";
        }
    }
    return tax;
}

void inputRules(struct Rule rules[], int n) {
    for (int i = 1; i <= n; i++) {
        int m, r;
        printf("请输入第 %d 条规则:", i);
        cin >> m >> r;
        rules[i].m = m;

        rules[i].
                r = r;
    }
}

void outputRules(struct Rule rules[], int n) {
    cout << "\n纳税结果如下:\n";
    printf("纳税线\t税率\n");
    for (int i = 1; i <= n; i++) {
        cout << rules[i].m << "\t";
        cout << rules[i].r << endl;;
    }
    cout << endl;
}

int main() {
    int n;
    cout << "请输入规则的条数:";
    cin >> n;
    Rule rules[n + 1];
    inputRules(rules, n);
    outputRules(rules, n);
    int x;
    cout << "请输入您的收入:";
    cin >> x;
    while (x != -1) {
        cout << "您的收入是:" << x << "," << "应缴纳所得税:";
        printf("%0.2lf元。", computeTax(rules, n, x));
        cout << ans << endl;
        cout << "请输入您的收入:";
        cin >> x;
    }
    cout << "再见\n";
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值