【问题标题】:how to use enum with decision making switch statements如何将枚举与决策切换语句一起使用
【发布时间】: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


【解决方案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 并且您从各种函数返回浮点数并尝试打印浮点值。

由于我熟悉 ANSI-C 标准,所以我进行了更改。

【讨论】:

  • @yarden 如果您感到满意,请接受答案,不要忘记投票。
【解决方案2】:

当你键入def calcOption 时:

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

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

scanf("%d", &option);

这没有任何意义,因为使用 & 符号时,您给出的是类型的地址,而不是对象的地址。

我感觉你明白这一点,因为你将“calcOption”键入为“object”,然后你创建了“object”对象:

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

如果你像这样切换两行:

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

没错,但你还有其他问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多