2014-06-17 26 views
-2
// 2.cpp : Sample Program with a menu 
//Write a program that will use a menu 



#include "stdafx.h" 
#include<stdio.h> 

#define Pi 3.14159 

int main(void) 
{ 
int digit1; 
int choice; 
int a, b, c; //user input for choice 3 & 4 
int a1, a2;// User input for Choice 1 
int divi; 
int divisor; 

/*Menu*/ 

printf("***** MENU *****\n"); 
printf(" 1 - Greater Than, Less Than, Or Equal?\n"); 
printf("\t In this selection, you will enter two integers. \n"); 
printf("\t The program will return'Greater Than' if the first\n"); 
printf("\t integer entered is less than the second,'Less than'\n"); 
printf("\t if the first integer is greater than the second, or\n"); 
printf("\t'Equal' if the two integers entered are equal\n"); 
printf(" 2 - Evenly Divisible\n"); 
printf("\t In this slection, you will enter two integers. \n"); 
printf("\t The program will test if the the first integer\n"); 
printf("\t is evenly divisible by the second. The program\n"); 
printf("\t will then return its result and display\n"); 
printf("\t the quotient rounded to the nearest thousandth\n"); 

printf(" 3 - Calculations with 2 integers\n"); 
printf(" 4 - Calculations with 3 integers\n"); 
printf(" 5 - Calculations with circles\n"); 
printf(" 6 - Quit\n"); 

printf("Please enter your choice: "); 
scanf("%i",&choice); 

printf("\n\n"); 

switch(choice) 
{ 
    case 1:  //Greater than, less than, or equal 

    printf("Please Enter two integers: \n"); 
    scanf("%i %i", &a1, &a2); 
    if (a1<a2) 
    printf("Greater Than\n"); 
    else if (a1>a2) 
    printf("Less Than\n"); 
    else if (a1=a2) 
    printf("Equal\n"); 

    break; 

    case 2: //Equally Divisible 

我需要這部分代碼的幫助。爲商取0.000。爲什麼?這些情況如何使它不能得到整數?我試圖用大括號將整數本地化。我究竟做錯了什麼?無法完成除法運算。商數是0.0。有沒有一種方法可以將int更改爲float?

printf("Please Enter two integers: \n"); 
    { 
     int divi; 
     int divisor; 

     scanf("%i %i", &divi, &divisor); 
     float modQuotient = divi%divisor; 

     if (modQuotient!=0) 
      { 
      printf("%i is not evenly divisible by %i\n", divi,  divisor); 
      printf("The quotient is %.3f\n", divi/divisor); 
      } 
     else 
      { 
      printf("%i is evenly divisible by %i\n", divi, divisor); 
      printf("The quotient is %.3f\n", divi/divisor); 
      } 


    } 



    break; 

    /*case 3:  /*Calculations with 2 integers*/ 


    case 4: /*Calculations with 3 integers*/ 
     printf(" You have selected to do some calculations with 2  

integers\n"); 
     printf("Please enter your 3 integers: "); 
     scanf("%i%i%i",&a, &b, &c); 

     printf("The average of %i, %i and %i is %g\n\n",a, b, c, (a+b+c)/3.0); 
     break; 

    case 5: /*Calculations with circles*/ 
     float radius; 

     printf("Please enter the radius of a circle: "); 
     scanf ("%g", &radius); 
printf("\nThe diameter of a circle with a radius of %g units is %.2f units\n\n",  

radius, radius*2); 
     printf("The circumference is of the circle with a radius of %g 

units is %.2f units\n\n", radius, Pi*radius*2); 
     printf("The area of a circle with a radius of %g units is %.2f 

square units\n\n", radius, Pi*radius*radius); 
     break; 

    case 6: /*Quit*/ 
     printf("Thank you. Good bye\n\n"); 
     break; 

     default: /*Invalid Entry*/ 
     printf("Invalid Entry...\n"); 
     break; 
} 

printf("The program will now end. Have a great day!\n"); 


return 0; 
} 
+0

http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c –

+1

不要使用那個不準確的'Pi'常數。改爲使用'math.h'中的'M_PI'。 – HolyBlackCat

+1

@HolyBlackCat確信C沒有在'math.h'或其他地方指定'M_PI' _must_。雖然C的許多版本都有高精度的宏。同意OP應該使用更精確的值。見http://www.piday.org/million/ – chux

回答

3

兩個int的模數是另一個int。此外,除以零會導致未定義的行爲。所以,你應該寫:

if (divisor == 0) 
    exit(EXIT_FAILURE);  // or some other error handling 

int modQuotient = divi % divisor; 

,記得使用%i%d如果你是printf「荷蘭國際集團modQuotient。

在行:

printf("The quotient is %.3f\n", divi/divisor); 

有一個問題。 dividivisor都是int,因此二進制運算的結果也是int。使用printf它不會對參數進行任何轉換來匹配格式說明符 - 您必須親自手動確保匹配。因此,通過使用%f嘗試打印int,此代碼會導致未定義的行爲。

如果你想做一個浮點除法,那麼你必須至少讓其中一個操作數爲浮點,例如, (爲了清晰起見,分成兩行):

float quot = (float)divi/divisor; 
printf("The quotient is %.3f\n", quot); 

NB。你必須在這段代碼中的邏輯錯誤:

else if (a1=a2) 
    printf("Equal\n"); 

=的操作裝置分配。此行更改爲a2等於a1。比較運營商的平等是==

-1
printf("The quotient is %.3f\n", divi/divisor); 

在你說的是程序的格式輸出爲float精密出來到小數點後三位上面的代碼。因此,當發生分裂時,如果答案爲0,則它​​將輸出爲0.000。

相關問題