[C] DETERMINES THE VALUE OF A COIN COLLECTION →→→→→进入此内容的聊天室

来自 Burly Motmot, 2022-06-20, 写在 C, 查看 69 次.
URL http://www.code666.cn/view/ed99a6fb
  1. /* FILE: coins.c
  2.  * DETERMINES THE VALUE OF A COIN COLLECTION
  3.  * A Variation of the Hanly/Koffman book's example
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. void main ()
  9. {
  10.    // Local data ...
  11.    int pennies;              // input: count of pennies
  12.    int nickels;              // input: count of nickels
  13.    int dimes;                // input: count of dimes
  14.    int quarters;             // input: count of quarters
  15.    int temp, left;           // temporaries for various
  16.                              // computations
  17.  
  18.    // Read in the count of quarters, dimes, nickels and pennies.
  19.    printf("Enter the number of quarters, dimes, nickels, and pennies: ");
  20.    scanf("%d %d %d %d", &quarters, &dimes, &nickels, &pennies);
  21.  
  22.    // Compute the total value in cents.
  23.    left = 25 * quarters + 10 * dimes + 5 * nickels + pennies;
  24.  
  25.    // Find and display the value in dollars
  26.    printf("Your collection is worth\n ");
  27.    temp = left / 100;
  28.    printf("\t%d dollar", temp);
  29.    if (temp==1)
  30.       printf(", ");
  31.    else
  32.       printf("s, ");
  33.    left = left % 100;
  34.  
  35.    // Find and display the value left in quarters
  36.    temp = left / 25;
  37.    printf("%d quarter", temp);
  38.    if (temp==1)
  39.       printf(", ");
  40.    else
  41.       printf("s, ");
  42.    left = left % 25;
  43.  
  44.    // Find and display the value left in dimes
  45.    temp = left / 10;
  46.    printf("%d dime", temp);
  47.    // Here, just for fun, instead of using a conditional statement,
  48.    // I use a conditional expression and string concatenation
  49.    printf ((temp==1) ? ", " : "s, ");
  50.    left = left % 10;
  51.  
  52.    // Find and display the value left in nickels
  53.    temp = left / 5;
  54.    printf("%d nickel", temp);
  55.    if (temp==1)
  56.       printf(", and ");
  57.    else
  58.       printf("s, and ");
  59.    left = left % 5;
  60.  
  61.    // Find and display the value left in pennies
  62.    printf("%d penn", left);
  63.    if (left==1)
  64.       printf("y\n");
  65.    else
  66.       printf("ies\n");
  67. }
  68.  
  69.  

回复 "DETERMINES THE VALUE OF A COIN COLLECTION"

这儿你可以回复上面这条便签

captcha