如何在决策制定开关语句中使用枚举

时间:2016-12-17 16:51:26

标签: c enums switch-statement

我正在使用一个小型计算器,我不知道如何使用开关/外壳的枚举,所以我需要不要使这个工作 我只需要扫描一个数字,然后用enum查看他想要的内容 不要参考评论

Error: $q is not defined

2 个答案:

答案 0 :(得分:1)

当你输入defde calcOption时:

typedef enum calcOption
{
distance=1,
hypotenuse=2,
areaC=3,
areaR=4,
areaS=5
}option;

最后一个大括号之后的选项是一个类型,你说“让选项与calcOption具有相同的含义。它不是一个对象。所以稍后在main()中执行:

scanf("%d", &option);

它没有意义,因为使用&符号,您提供的是类型的地址,而不是对象。

我觉得你明白这一点,因为你把“calcOption”命名为“对象”,然后你创建了“对象”对象:

calcOptions options;
// Is the same as:
option option
// Though very confusing as you've got the same name.

如果你这样切换两行:

calcOption option;
scanf("%d", &option);

那是对的,但你还有其他问题。

答案 1 :(得分:1)

更正了枚举部分和无限循环

#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);
enum calcOption
{
distance=1,
hypotenuse=2,
areaC=3,
areaR=4,
areaS=5
};


int main(void)
{
 int i=0;
float finish=0, y=0;
printf("Welcome to my calculator!\n");
while(i<1 || i>5 )
{
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",&i);
//calcOption option;
//option=calcOption(i);
switch(i)
{
    case distance: disBePoints();
                   printf("the distance is %f",finish);
                   break;

    case hypotenuse: hypotri();
                     printf("the hypotenuse is %f",finish);
                     break;
    case areaC: for(y=1;y>2;y++)
                    circle(y);
                if(y=1)
                {
                    printf("the perimeter is %f",finish);
                }
                else
                {
                    printf("the area is %f",finish);
                }
                break;
    case areaR:
    {
        square();
        printf("the hypotenuse is %f",finish);
        break;
    }
    case areaS:
    {
        square();
        printf("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;
#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);
enum calcOption
{
distance=1,
hypotenuse=2,
areaC=3,
areaR=4,
areaS=5
};


int main(void)
{
 int i=0;
float finish=0, y=0;
printf("Welcome to my calculator!\n");
while(i<1 || i>5 )
{
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",&i);
//calcOption option;
//option=calcOption(i);
switch(i)
{
    case distance: disBePoints();
                   printf("the distance is %f",finish);
                   break;

    case hypotenuse: hypotri();
                     printf("the hypotenuse is %f",finish);
                     break;
    case areaC: for(y=1;y>2;y++)
                    circle(y);
                if(y=1)
                {
                    printf("the perimeter is %f",finish);
                }
                else
                {
                    printf("the area is %f",finish);
                }
                break;
    case areaR:
    {
        square();
        printf("the hypotenuse is %f",finish);
        break;
    }
    case areaS:
    {
        square();
        printf("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;
}

我也知道为什么你的各种函数返回类型是int,你从各种函数返回float并尝试打印浮点值。

我已经做了更改,因为我熟悉ANSI-C标准。