几何计算器V1改进

lixinyan 爱因斯坦 2024-05-05 15:20:50 2024-05-05 15:22:37 32

#include<bits/stdc++.h> using namespace std; int main(){ while(99%2){

int o;

cout<<"请问你要计算以下那种计算:\n  1.周长 2.面积 3.表面积 4体积。\n";
cin>>o;
if(o==1){
	cout<<"请选择:1.长方形 2.正方形 3.三角形 4.平行四边形 5.梯形 6.n边形 7.圆形 8.半圆\n";
	int n;
	cin>>n;
	if(n==1){
		cout<<"请输入长方形的长and宽。\n";
		double c,k;
		cout<<"长:";
		cin>>c;
		cout<<"\n宽:";
		cin>>k;
		cout<<"\n长方形的周长为"<<(c+k)*2<<"。\n";
	}
	else if(n==2){
		cout<<"请输入正方形的边长。\n";
		double x;
		cout<<"边长:" ;
		cin>>x;
		cout<<"正方形的周长为"<<4*x<<"。\n";
	}
	else if(n==3){
		cout<<"请输入三角形的三条边长。\n";
		double k,l,j;
		cout<<"边长:";
		cin>>k>>l>>j;
		cout<<"三角形的周长为" <<k+j+l<<"。\n";
	}
	else if(n==4){
		cout<<"请输入平行四边形的底and腰。\n";
		double d,y;
		cout<<"底:";
		cin>>d;
		cout<<"腰:";
		cin>>y;
		cout<<"平行四边形的周长为"<<2*(d+y)<<"。\n";
	}
	else if(n==5){
	cout<<"请输入梯形的上,下底and2条腰。\n";
	int y1,y2,d1,d2;
	cout<<"上底:";
	cin>>d1;
	cout<<"下底:";
	cin>> d2;
	cout<<"腰:";
	cin>>y1>>y2;
	cout<<"梯形的周长为"<<d1+d2+y1+y2<<"。\n";
	}else if(n==6){
		int k=0;
		double a[20000],m;
		cout<<"这个n边形有几条边?";
		cin>>m;
		for(int i=3;i<=n;i++){
			cout<<"第"<<i-2<<"边长";
			cin>>a[i];
			cout<<"。";
			k+=a[i];
		}
		cout<<"这个n边形的周长为"<<k<<"。\n";
	}
	else if(n==7){
		cout<<"请输入圆形的半径:";
		double r;
		cin>>r;
		cout<<"这个圆形的周长为"<<3.14*2.00*r<<"。\n"; 
	} else if(n==8){
		cout<<"请输入半圆的半径:";
		double r;
		cin>>r;
		cout<<"这个半圆的周长为"<<r*2.0+r*3.14<<"。\n";
	}
	else {
		cout<<"你输入数字让我大脑瓦特,请重新输入";
	}
	cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; 
}

} return 0; }

{{ vote && vote.total.up }}

共 9 条回复

Joshua Microsoft

以下是对代码的一些改进建议:

引入合适的头文件:您可以根据需要引入特定的头文件,而不是使用<bits/stdc++.h>。例如,如果您需要使用输入输出流,可以包含;如果您需要使用数学函数,可以包含。

使用函数进行模块化:将不同的计算逻辑封装在独立的函数中,以提高代码的可读性和可维护性。例如,您可以创建一个名为calculatePerimeter的函数来计算周长,并根据不同的形状类型调用相应的函数。

添加错误处理和输入验证:在接受用户输入之前,可以添加错误处理和输入验证的逻辑,以确保用户提供有效的输入。例如,您可以检查用户输入的数字是否在有效范围内,并提示用户重新输入。

使用常量代替魔法数:在代码中使用命名的常量来代替魔法数,可以提高代码的可读性和可维护性。例如,您可以定义一个名为PI的常量来表示圆周率。

提供更友好的交互界面:可以改进用户界面,使其更加直观和友好。例如,您可以在每个计算选项后面添加相应的提示,以帮助用户理解并输入正确的值。

这些只是一些改进的建议,具体取决于您的需求和偏好。希望对您有所帮助! 我将对您的代码进行改进并使其更加模块化和易于阅读。以下是改进后的代码:

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159265358979323846;

// 函数声明
double calculateRectanglePerimeter(double length, double width);
double calculateSquarePerimeter(double sideLength);
double calculateTrianglePerimeter(double a, double b, double c);
double calculateParallelogramPerimeter(double base, double side);
double calculateTrapezoidPerimeter(double topBase, double bottomBase, double side1, double side2);
double calculatePolygonPerimeter(int sides, double lengths[]);
double calculateCirclePerimeter(double radius);
double calculateSemiCirclePerimeter(double radius);

int main() {
    while (true) {
        int option;
        cout << "请问你要计算以下那种计算:\n  1.周长 2.面积 3.表面积 4.体积。\n";
        cin >> option;

        if (option == 1) {
            cout << "请选择:1.长方形 2.正方形 3.三角形 4.平行四边形 5.梯形 6.n边形 7.圆形 8.半圆\n";
            int shapeType;
            cin >> shapeType;

            switch (shapeType) {
                case 1: {
                    double length, width;
                    cout << "请输入长方形的长:";
                    cin >> length;
                    cout << "宽:";
                    cin >> width;
                    cout << "长方形的周长为: " << calculateRectanglePerimeter(length, width) << "\n";
                    break;
                }
                case 2: {
                    double sideLength;
                    cout << "请输入正方形的边长: ";
                    cin >> sideLength;
                    cout << "正方形的周长为: " << calculateSquarePerimeter(sideLength) << "\n";
                    break;
                }
                case 3: {
                    double a, b, c;
                    cout << "请输入三角形的三条边长: ";
                    cin >> a >> b >> c;
                    cout << "三角形的周长为: " << calculateTrianglePerimeter(a, b, c) << "\n";
                    break;
                }
                case 4: {
                    double base, side;
                    cout << "请输入平行四边形的底:";
                    cin >> base;
                    cout << "腰:";
                    cin >> side;
                    cout << "平行四边形的周长为: " << calculateParallelogramPerimeter(base, side) << "\n";
                    break;
                }
                case 5: {
                    double topBase, bottomBase, side1, side2;
                    cout << "请输入梯形的上底: ";
                    cin >> topBase;
                    cout << "下底: ";
                    cin >> bottomBase;
                    cout << "两条腰: ";
                    cin >> side1 >> side2;
                    cout << "梯形的周长为: " << calculateTrapezoidPerimeter(topBase, bottomBase, side1, side2) << "\n";
                    break;
                }
                case 6: {
                    int sides;
                    cout << "这个n边形有几条边?";
                    cin >> sides;
                    double lengths[sides];
                    for (int i = 0; i < sides; ++i) {
                        cout << "" << i + 1 << "边长: ";
                        cin >> lengths[i];
                    }
                    cout << "这个n边形的周长为: " << calculatePolygonPerimeter(sides, lengths) << "\n";
                    break;
                }
                case 7: {
                    double radius;
                    cout << "请输入圆形的半径: ";
                    cin >> radius;
                    cout << "这个圆形的周长为: " << calculateCirclePerimeter(radius) << "\n";
                    break;
                }
                case 8: {
                    double radius;
                    cout << "请输入半圆的半径: ";
                    cin >> radius;
                    cout << "这个半圆的周长为: " << calculateSemiCirclePerimeter(radius) << "\n";
                    break;
                }
                default:
                    cout << "输入无效,请重新选择。\n";
                    break;
            }

            cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        } else {
            cout << "目前只支持周长计算,其他功能待开发。\n";
        }
    }

    return 0;
}

// 函数实现
double calculateRectanglePerimeter(double length, double width) {
    return 2 * (length + width);
}

double calculateSquarePerimeter(double sideLength) {
    return 4 * sideLength;
}

double calculateTrianglePerimeter(double a, double b, double c) {
    return a + b + c;
}

double calculateParallelogramPerimeter(double base, double side) {
    return 2 * (base + side);
}

double calculateTrapezoidPerimeter(double topBase, double bottomBase, double side1, double side2) {
    return topBase + bottomBase + side1 + side2;
}

double calculatePolygonPerimeter(int sides, double lengths[]) {
    double perimeter = 0;
    for (int i = 0; i < sides; ++i) {
        perimeter += lengths[i];
    }
    return perimeter;
}

double calculateCirclePerimeter(double radius) {
    return 2 * PI * radius;
}

double calculateSemiCirclePerimeter(double radius) {
    return PI * radius + 2 * radius;
}

改进点 模块化:将每种形状的周长计算独立为函数,增强代码的可读性和可维护性。 常量使用:使用常量代替魔法数,例如PI。 交互提示:在用户输入时添加了更多的提示信息,使交互界面更加友好。 这样不仅使代码更清晰,也方便以后进行扩展和维护。

jxy2012 qwq

我的计算几何板子:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const int N = 200005;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
bool equals(double a, double b) {  // 判断相等
    return fabs(a - b) < eps;
}

struct Point {  // 点 向量
    double x, y;
    Point() {}
    Point(double x, double y)
        : x(x), y(y) {}

    Point operator+(Point& b) {
        return Point(x + b.x, y + b.y);
    }

    Point operator-(Point& b) {
        return Point(x - b.x, y - b.y);
    }

    Point operator*(double k) {
        return Point(x * k, y * k);
    }

    Point operator/(double k) {
        return Point(x / k, y / k);
    }

    bool operator<(Point b) {
        return equals(x, b.x) ? y < b.y : x < b.x;
    }
};
typedef Point Vector;

double get_atan2(Point a, Point b) {  // 求极角

    Vector v = b - a;
    return atan2(v.y, v.x);
}

struct Segment {  // 线段 直线 射线

    Point p1, p2;  // 若表示射线则起点为p1
    double ang;
    Segment() {}
    Segment(Point a, Point b)
        : p1(a), p2(b), ang(get_atan2(p1, p2)) {}
};
typedef Segment Line;

struct Circle {  //    Point c;
    double r;
    Circle() {}
    Circle(Point c, double r)
        : c(c), r(r) {}
};

typedef vector<Point> Polygon;  // 多边形

double norm(Vector a) {  // 范数
    return pow(a.x, 2) + pow(a.y, 2);
}

double abs(Vector a) {  // 模长
    return sqrt(norm(a));
}

double dot(Vector a, Vector b) {  //|a||b|cos A
    return a.x * b.x + a.y * b.y;
}

double cross(Vector a, Vector b) {  //|a||b|sin A
    return a.x * b.y - a.y * b.x;
}

bool isOrthogonal(Vector a, Vector b) {  // 判断正交
    return equals(dot(a, b), 0.0);
}

bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) {
    return isOrthogonal(a1 - a2, b1 - b2);
}

bool isOrthogonal(Segment s1, Segment s2) {
    return isOrthogonal(Vector(s1.p1 - s1.p2), Vector(s2.p1 - s2.p2));
}

bool isParallel(Vector a, Vector b) {  // 判断平行

    return equals(cross(a, b), 0.0);
}

bool isParallel(Point a1, Point a2, Point b1, Point b2) {
    return isParallel(a1 - a2, b1 - b2);
}

bool isParallel(Segment s1, Segment s2) {
    return isParallel(Vector(s1.p1 - s1.p2), Vector(s2.p1 - s2.p2));
}

Point project(Point p, Segment s) {  // 求投影
    Vector base = s.p2 - s.p1;
    double r = dot(p - s.p1, base) / norm(base);
    base = base * r;
    return Point(s.p1 + base);
}

Point reflect(Point p, Segment s) {  // 求映射
    Point p1 = (project(p, s) - p) * 2.0;
    return p + p1;
}

double get_dis(Point a, Point b) {  // 求点之间距离
    return abs(a - b);
}

double get_dis(Point a, Line l) {  // 求点与直线之间距离
    return abs(cross(l.p2 - l.p1, a - l.p1) / abs(l.p2 - l.p1));
}

double get_dis_ps(Point p, Segment s) {  // 求点与线段距离
    if (dot(p - s.p1, s.p2 - s.p1) < 0.0) return abs(p - s.p1);
    if (dot(p - s.p2, s.p1 - s.p2) < 0.0) return abs(p - s.p2);
    return get_dis(p, s);
}

bool intersect(Point, Point, Point, Point);
bool intersect(Segment, Segment);

double get_dis_ss(Segment a, Segment b) {  // 求线段之间距离
    if (intersect(a, b)) return 0.0;
    return min(min(get_dis_ps(a.p1, b), get_dis_ps(a.p2, b)), min(get_dis_ps(b.p1, a), get_dis_ps(b.p2, a)));
}

// b在a的
const int CC = 1;   // 逆时针方向
const int C = -1;   // 顺时针方向
const int OB = 2;   // 方向相反
const int OF = -2;  // 方向相同 |b|>|a|
const int OS = 0;   // 方向相同 |a|>|b|

int ccw(Point p1, Point p2, Point p3) {  // 判断三点的位置关系
    Vector a = p2 - p1;
    Vector b = p3 - p1;
    if (cross(a, b) > eps) return CC;
    if (cross(a, b) < -eps) return C;
    if (dot(a, b) < -eps) return OB;
    if (norm(a) < norm(b)) return OF;
    return OS;
}

int ccw(Vector a, Vector b) {  // 判断向量的位置关系
    if (cross(a, b) > eps) return CC;
    if (cross(a, b) < -eps) return C;
    if (dot(a, b) < -eps) return OB;
    if (norm(a) < norm(b)) return OF;
    return OS;
}

bool intersect(Point a, Point b, Point c, Point d) {  // 判断两线段相交
    return ccw(a, b, c) * ccw(a, b, d) <= 0 && ccw(c, d, a) * ccw(c, d, b) <= 0;
}

bool intersect(Segment a, Segment b) {
    return intersect(a.p1, a.p2, b.p1, b.p2);
}

Point getCrossPoint_Line(Line s1, Line s2) {  // 求两直线交点
    Vector v1, v2;
    v1 = s1.p2 - s1.p1, v2 = s2.p2 - s2.p1;
    Vector u = s1.p1 - s2.p1;
    double t = cross(v2, u) / cross(v1, v2);

    Point x = v1 * t;
    return s1.p1 + x;
}

Point getCrossPoint(Segment s1, Segment s2, bool flag = 0) {  // 求两线段交点
    if (!intersect(s1, s2)) {
        if (flag) {
            return getCrossPoint_Line(s1, s2);
        } else {
            return Point(1e18, 1e18);
        }
    }

    Vector base = s2.p2 - s2.p1;
    double d1 = abs(cross(base, s1.p1 - s2.p1));
    double d2 = abs(cross(base, s1.p2 - s2.p1));
    double t = d1 / (d1 + d2);
    Point x = (s1.p2 - s1.p1) * t;
    return s1.p1 + x;
}

pair<Point, Point> getCrossPoint(Line l, Circle C) {  // 求直线与圆交点
    if (get_dis(C.c, l) > C.r) return make_pair(Point(2e18, 2e18), Point(2e18, 2e18));

    Point pr = project(C.c, l);
    Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p2);
    double base = sqrt(C.r * C.r - norm(pr - C.c));
    Vector x = e * base;
    return make_pair(pr + x, pr - x);
}

Polygon andrewScan(Polygon p) {  // 求凸包
    Polygon u, d;
    if (p.size() < 3) return p;
    sort(p.begin(), p.end());

    u.pb(p[0]);
    u.pb(p[1]);

    d.pb(p[p.size() - 1]);
    d.pb(p[p.size() - 2]);

    for (int i = 2; i < p.size(); i++) {
        for (int k = u.size(); k >= 2 && ccw(u[k - 2], u[k - 1], p[i]) != C; k--) u.pop_back();
        u.pb(p[i]);
    }

    for (int i = p.size() - 3; ~i; i--) {
        for (int k = d.size(); k >= 2 && ccw(d[k - 2], d[k - 1], p[i]) != C; k--) d.pop_back();
        d.pb(p[i]);
    }

    reverse(d.begin(), d.end());
    for (int i = u.size() - 2; i; i--) d.pb(u[i]);
    return d;
}

bool cmp_ang(Segment a, Segment b) {  // 极角排序
    return a.ang < b.ang;
}

bool OnLeft(Point p, Line s) {
    return cross(s.p2 - s.p1, p - s.p1) > 0.0;
}

Polygon HPI(vector<Segment> L) {  // 求半平面交
    int n = L.size();
    sort(L.begin(), L.end(), cmp_ang);

    int head, tail;
    vector<Point> p(n);
    vector<Segment> q(n);
    vector<Point> ans;

    q[head = tail = 0] = L[0];
    for (int i = 1; i < n; i++) {
        while (head < tail && !OnLeft(p[tail - 1], L[i])) tail--;
        while (head < tail && !OnLeft(p[head], L[i])) head++;
        q[++tail] = L[i];

        if (equals(cross(q[tail].p2 - q[tail].p1, q[tail - 1].p2 - q[tail - 1].p1), 0.0)) {
            tail--;
            if (OnLeft(L[i].p1, q[tail])) q[tail] = L[i];
        }
        if (head < tail) p[tail - 1] = getCrossPoint(q[tail - 1], q[tail], 1);
    }

    while (head < tail && !OnLeft(p[tail - 1], q[head])) tail--;
    if (head == tail) return ans;
    p[tail] = getCrossPoint(q[tail], q[head], 1);
    for (int i = head; i <= tail; i++) ans.pb(p[i]);
    return ans;
}

double Ploygon_area(Polygon s) {  // 求多边形面积
    int n = s.size();
    double ans = 0.0;
    for (int i = 0; i < n; i++) {
        ans += cross(s[i], s[(i + 1) % n]);
    }
    return ans * 0.5;
}

int main() {
    
    return 0;
}

lixinyan 爱因斯坦

你来做啊

Mikoto

歼十改是吧 @lixinyan

lyhldy CSP-J2二等

有兴趣的话可以考虑一起开发qwq

lyhldy CSP-J2二等

下面这个是我之前的开发版本,你可以康康,借鉴一下

lyhldy CSP-J2二等
lyhldy CSP-J2二等

你可以用switch

lixinyan 爱因斯坦

注:作者才刚学完中级班