为什么这一直打印圆的宽度?
Why does this print the width all the time for the circle?
#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)
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- 为什么这一直打印圆的宽度?
- 我试图打印强连接的组件,但我一直得到内存错误