c++ - 为什么这会一直打印圆圈的宽度?
问题描述
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const double PI = 3.141592653589793238463;
enum Figur {DREIECK, RECHTECK, KREIS};
struct Geo {
mutable Figur figur;
mutable double a, b, c;
};
Geo createGeo(Figur f, double a, double b, double c)
{
Geo d;
d.figur = f;
d.a = a;
d.b = b;
d.c = c;
return d;
}
Geo getGeo() {
double a=0.0, b=0.0, c=0.0;
n:
cout << "Please insert which geometrical object you want to create:" << endl;
cout << "0 for triangle" << endl << "1 for rectangle" << endl << "2 for circle" << endl;
Figur f;
int p = 3;
cin >> p;
if (p == 0)
{
f = DREIECK;
cout << "Please insert the length: ";
cin >> a;
cout << endl << "Please insert the width: ";
cin >> b;
{ m:
cout << endl << "Please set the angle >0 and <180°: ";
cin >> c;
if (c <= 0 || c >= 180)
{
cout << endl << "Invalid value! Please try again" << endl;
goto m;
}
}
cout << endl << "Your figure is now going to be created" << endl;
Geo geo = createGeo(f, a, b, c);
return geo;
}
else if (p == 1)
{
f = RECHTECK;
cout << "Please insert the length: ";
cin >> a;
cout << endl << "Please insert the width: ";
cin >> b;
cout << endl << "Your figure is now going to be created" << endl;
Geo geo = createGeo(f, a, b ,0);
return geo;
}
else if (p == 2)
{
f = KREIS;
cout << "Please insert the radius: ";
cin >> a;
cout << endl << "Your figure is now going to be created" << endl;
Geo geo = createGeo(f, a,0,0);
return geo;
}
else
{
cout << "Invalid value. Please try again." << endl;
goto n;
}
}
double flaeche(Geo const &g)
{
double flaeche;
if(g.figur = DREIECK)
{
flaeche = 0.5 * g.a*g.b;
}
else if (g.figur = RECHTECK)
{
flaeche = g.a*g.b;
}
else if(g.figur = KREIS)
{
flaeche = PI * pow(g.a, 2);
}
return flaeche;
}
void putGeo(double a, Geo &g)
{
string typ;
if (g.figur == DREIECK) typ = "triangle";
else if (g.figur == RECHTECK) typ = "rectangle";
else typ = "circle";
cout << "Type of figure: " << typ << endl;
if (g.figur == DREIECK) {
cout << "Length: " << g.a << endl;
cout << "Height: " << g.b << endl;
cout << "Angle: " << g.a << endl;
cout << "Area: " << a << endl;
}
else if (g.figur == RECHTECK) {
cout << "Length: " << g.a << endl;
cout << "Width: " << g.b << endl;
cout << "Area: " << a << endl;
}
else if(g.figur == KREIS){
cout << "Radius: " << g.a << endl;
cout << "Area: " << a << endl;
}
}
int main()
{
Geo g1 = getGeo();
double a = flaeche(g1);
putGeo(a, g1);
return 0;
}
你好,
我希望这个代码示例对你来说很容易理解。我想创建一个程序,用户必须在其中通过控制台创建几何对象。
矩形和三角形现在工作得很好,但是圆形有一点问题。
每当我创建一个圆并调用 putGeo() 方法时,宽度也会被打印出来,并且面积会被计算为零。
有人可能知道为什么会这样吗?
这是我课程中的一个练习,现在我真的不知道了。
该程序完全是德语,但我翻译了输出。因为枚举 DREIECK 是三角形,RECHTECK 是矩形,KREIS 是圆形。
谢谢你的帮助。
输出编辑问题
如果我创建半径为 5 的圆
我想通过 putGeo() 打印
半径:5 区域:78.5398163397
但我实际上得到
半径:5 宽度:0 面积:0
解决方案
看这里:
if(g.figur = DREIECK)
您打算进行测试,但这是一项任务。这会将 的值设置g.figur
为DREIECK
。在此之后,面积将被错误地计算。
当您以这种方式使用赋值时,一个好的编译器会警告您。(如果你禁止或忽略编译器警告,你会给自己制造麻烦。)
正确的形式是
if(g.figur == DREIECK)
推荐阅读
- amazon-web-services - 通过 CDK 中的 ARN 将 eventSource 添加到 Lambda
- angular - 如何在嵌套表单数组中全选和取消全选?
- python - 切换到同一窗口内的弹出窗口
- php - PHP 文档 @example 示例
- c# - ASP.NET MVC - 将动态 ExpandoObject 从控制器传递到视图时出现 RuntimeBinderException
- html - 在所有页面上打印固定的页眉和页脚
- python - ImportError:仅在从 .bat 文件运行时缺少必需的依赖项 ['numpy']
- php - 如何从数据库中选择具有相同查询但表中条件不同的数据?
- javascript - 在Javascript中获取数字之间的数字步骤
- android - 将上下文传递给许多方法是否正常?