2016-12-17 43 views
-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); 
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; 
} 
+0

您將'option'定義爲*類型名稱*,它用作類型而不是變量。 –

+2

在閱讀什麼使[mcve]之後提出一個問題? – sjsam

+1

你應該更好地解釋一下問題所在。什麼不工作? – Zebrafish

回答

0

當你的typedef calcOption喜歡:

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

的最後一個大括號後的選項是一個類型,你說「讓選項與calcOption具有相同的含義。這不是一個目標克拉。因此,在主後(),當你這樣做:

scanf("%d", &option); 

它不會因爲與符號你給一個類型,而不是一個對象的地址是有意義的。

我有一種感覺,你明白這一點,因爲你typedeffed「calcOption」作爲「對象」,然後就創造了「對象」對象:

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

如果切換左右兩行是這樣的:

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

這將是正確的,但你有其他問題。

0

更正枚舉部分和無限循環

#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標準,所以我做了更改。

+0

@yarden如果你覺得滿意然後接受答案,不要忘記投票。 – NeoR