【发布时间】:2016-12-17 16:51:25
【问题描述】:
我正在开发一个小计算器,但我不知道如何将 enum 与开关/机箱一起使用,所以我需要什么不要让它工作 我只需要扫描一个数字,然后用枚举看看他想要什么 不要指cmets
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Pi 3.141592
int hypotri(void);
int disBePoints(void);
int circle(int y);
int square(void);
typedef enum calcOption
{
distance=1,
hypotenuse=2,
areaC=3,
areaR=4,
areaS=5
}option;
int main(void)
{
float finish=0, y=0,i=0;
printf("Welcome to my calculator!\n");
while(i!=6)
{
printf("Welcome to my calculator!\nchoose option:\n1 - Calc distance between 2 points\n2 - Calc hypotenuse of triangle\n3 - Calc area and perimeter of circle\n4 - Calc area of rectangle\n5 - Calc area of square\n6 - Exit");
scanf("%d",&option);
calcOption option;
option=calcOPtion(i);
switch(option)
{
case distance:
{
disBePoints();
("the distance is %f",finish);
break;
}
case hypotenuse:
{
hypotri();
("the hypotenuse is %f",finish);
break;
}
case areaC:
{
for(y=1;y>2;y++)
{
circle(y);
if(y=1)
{
("the perimeter is %f",finish);
}
else
{
("the area is %f",finish);
}
break;
}
}
case areaR:
{
square();
("the hypotenuse is %f",finish);
break;
}
case areaS:
{
square();
("the hypotenuse is %f",finish);
break;
}
default:
{
printf("dont mess with me enter number between 1-6");
}
}
}
system("pause");
return 0;
}
/** why= to see what is the distance between any points\n
input=none
output=none
**/
int disBePoints(void)
{
float xOne=0,xTwo=0,yOne=0,yTwo=0, finish=0;
printf("Enter point 1 coordinates:");
scanf("%f%f",&xOne,&yOne);
printf("\nEnter point 2 coordinates:");
scanf("%f%f",&xTwo,&yTwo);
finish=sqrt(pow(xTwo-xOne,2)+pow(yTwo-yOne,2));
return finish;
}
/** why= to see what is the hypotenuse of the triange
input=none
output=none
**/
int hypotri(void)
{
float x=0,y=0,finish;
printf("Enter 2 sides of the triangle:");
scanf("%f%f",&x,&y);
finish=sqrt(pow(x,2)+pow(y,2));
return finish;
}
/** why= to see what is primeter and the area of the circle
input=none
output=none
**/
int circle(int y)
{
float radius=0,finish=0;
printf("Enter circle radius:");
scanf("%f",&radius);
if(y=1)
{
finish=radius*2*Pi;
}
else
{
finish=pow(radius,2)*Pi;
}
return finish;
}
/** why= to see what is the area of the rectangle or the square
input=none
output=none
**/
int square(void)
{
float yside=0,xside=0,finish=0;
printf("Enter lentgh and width:");
scanf("%f%f",&xside,&yside);
finish=yside*xside;
return finish;
}
【问题讨论】:
-
您将
option定义为类型名称,它用作类型而不是变量。 -
读完minimal reproducible example的原因后介意问一个问题吗?
-
您应该更好地解释问题所在。什么不工作?
-
您的编译器在看到
calcOption option;时不会发出任何诊断信息?您正在将代码编译为c++。切换到c,您会收到有用的警告。 -
开始正确缩进你的代码。
标签: c enums switch-statement