⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 work2.txt

📁 c primer 部分习题答案
💻 TXT
字号:

6-12
 

#include <stdio.h>
#define SIZE 8
int main( void )
{
    int twopows[SIZE];
    int i;
    int value = 1;
    
    for (i = 0; i < SIZE; i++)
    {
        twopows[i] = value;
        value *= 2;
    }
    
    i = 0;
    do
    {
        printf("%d ", twopows[i]);
        i++;
    } while (i < SIZE);
    printf("\n");
                    
    return 0;
}
 


6-13
 

#include <stdio.h>
#define SIZE 8
int main(void)
{
    double arr[SIZE];
    double arr_cumul[SIZE];
    int i;
    
    printf("Enter %d numbers:\n", SIZE);
    
    for (i = 0; i < SIZE; i++)
    {
        printf("value #%d: ", i + 1);
        scanf("%lf", &arr[i]);
    /* or scanf("%lf", arr + i);    */
    }
    
    arr_cumul[0] = arr[0];      /* set first element */
    for (i = 1; i < SIZE; i++)
        arr_cumul[i] = arr_cumul[i-1] + arr[i];
    
    for (i = 0; i < SIZE; i++)
        printf("%8g ", arr[i]);
    printf("\n");
    for (i = 0; i < SIZE; i++)
        printf("%8g ", arr_cumul[i]);
    printf("\n");
    
                
    return 0;
}
 
PE 6-15
 

 
#include <stdio.h>
 
#define RATE_SIMP 0.10
#define RATE_COMP 0.05
#define INIT_AMT 100.0
int main( void )
{
    double daphne = INIT_AMT;
    double deidre = INIT_AMT;
    int years = 0;
    
    while (deidre <= daphne)
    {
        daphne += RATE_SIMP * INIT_AMT;
        deidre += RATE_COMP * deidre;
        ++years;
    }
    printf("Investment values after %d years:\n", years);
    printf("Daphne: $%.2f\n", daphne);
    printf("Deidre: $%.2f\n", deidre);
 
    return 0;
}
 

7-1
 

#include <stdio.h>
int main(void)    
{
     char ch;
     int sp_ct = 0;
     int nl_ct = 0;
     int other = 0;
     
     while ((ch = getchar()) != '#')
     {
         if (ch == ' ')
             sp_ct++;
         else if (ch == '\n')
             nl_ct++;
         else
             other++;
     }
     printf("spaces: %d, newlines: %d, others: %d\n", sp_ct, nl_ct, other);
     
    return 0;
}
 
7-3
 

#include <stdio.h>
 
int main(void)    
{
     int n;
     double sumeven = 0.0;
     int ct_even = 0;
     double sumodd = 0.0;
     int ct_odd = 0;
      
     while (scanf("%d", &n) == 1 && n != 0)
     {
         if (n % 2 == 1)
         {
             sumodd += n;
             ++ct_odd;
         }
         else
         {
             sumeven += n;
             ++ct_even;
         }
     }
     printf("Number of evens: %d", ct_even);
     if (ct_even > 0)
         printf("  average: %g", sumeven / ct_even);
     putchar('\n');
         
    printf("Number of odds: %d", ct_odd);
     if (ct_odd > 0)
         printf("  average: %g", sumodd / ct_odd);
     putchar('\n');
     printf("\ndone\n");
     
    return 0;
}
 
PE 7-5
 
/* Programming Exercise 7-5 */
#include <stdio.h>
 
int main(void)    
{
     char ch;
     int ct1 = 0;
     int ct2 = 0;
     
     while ((ch = getchar()) != '#')
     {
         switch(ch)
         {
              case '.'    :  putchar('!');
                             ++ct1;
                             break;
 
              case '!'    :  putchar('!');
                             putchar('!');
                             ++ct2;
                             break;
             default    :    putchar(ch);
         }
     }
     printf("%d replacements of . with !\n", ct1);
     printf("%d replacements of ! with !!\n", ct2);
      
    return 0;
}
 
PE 7-7
 
/* Programming Exercise 7-7 */
#include <stdio.h>
 
#define BASEPAY     10      /* $10 per hour         */
#define BASEHRS     40      /* hours at basepay     */
#define OVERTIME    1.5     /* 1.5 time             */
#define AMT1        300     /* 1st rate tier        */
#define AMT2        150     /* 2st rate tier        */
#define RATE1       0.15    /* rate for 1st tier    */
#define RATE2       0.20    /* rate for 2nd tier    */
#define RATE3       0.25    /* rate for 3rd tier    */
int main(void)    
{
    double hours;
    double gross;
    double net;
    double taxes;
    
    printf("Enter the number of hours worked this week: ");
    scanf("%lf", &hours);
    if (hours <= BASEHRS)
        gross = hours * BASEPAY;
    else
        gross = BASEHRS * BASEPAY + (hours - BASEHRS) * BASEPAY * OVERTIME;
    if (gross <= AMT1)
        taxes = gross * RATE1;
    else if (gross <= AMT1 + AMT2)
        taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
    else
        taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
    net = gross - taxes;
    printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes, net);
      
    return 0;
}
 
PE 7-9
 
/* Programmming Exercise 7-9 */
#include <stdio.h>
#define NO 0
#define YES 1
int main(void)
{
   long num;                      /* value to be checked */
   long div;                      /* potential divisors  */
   long lim;                      /* limit to values     */
   int prime;
 
   printf("Please enter limit to values to be checked; ");
   printf("Enter q to quit.\n");
   while (scanf("%ld", &lim) == 1 && lim > 0)
   {
         for (num = 2; num <= lim; num++)
         {               
          for (div = 2, prime = YES; (div * div) <= num; div++)
             if (num % div == 0)
                prime = NO;           /* number is not prime  */
          if (prime == YES)
             printf("%ld is prime.\n", num);
      }
      printf("Please enter another limit; ");
      printf("Enter q to quit.\n");
   }
   return 0;
}
 
PE 7-11
 
/* pe7-11.c */
/* Programming Exercise 7-11 */
#include <stdio.h>
#include <ctype.h>
int main(void)
{
  const double price_artichokes = 1.25;
  const double price_beets = 0.65;
  const double price_carrots = 0.89;
  const double DISCOUNT_RATE = 0.05;
  
  char ch;
  double lb_artichokes;
  double lb_beets;
  double lb_carrots;
  double lb_total;
  
  double cost_artichokes;
  double cost_beets;
  double cost_carrots;
  double cost_total;
  double final_total;
  double discount;
  double shipping;
 
  printf("Enter a to buy artichokes, b for beets, ");
  printf("c for carrots, q to quit: ");
  while ((ch = getchar()) != 'q' && ch != 'Q')
  {
      if (ch == '\n')
          continue;
      while (getchar() != '\n')
          continue;
       ch = tolower(ch);
      switch (ch)
      {
          case 'a' : printf("Enter pounds of artichokes: ");
                   scanf("%lf", &lb_artichokes);
                   break;
           case 'b' : printf("Enter pounds of beets: ");
                   scanf("%lf", &lb_beets);
                   break;
     
           case 'c' : printf("Enter pounds of carrots: ");
                   scanf("%lf", &lb_carrots);
                   break;
        default  : printf("%c is not a valid choice.\n");
    }
    printf("Enter a to buy artichokes, b for beets, ");
    printf("c for carrots, q to quit: ");
  }
    
  cost_artichokes = price_artichokes * lb_artichokes;
  cost_beets = price_beets * lb_beets;
  cost_carrots = price_carrots * lb_carrots;
  cost_total = cost_artichokes + cost_beets + cost_carrots;
  lb_total = lb_artichokes + lb_beets + lb_carrots;
  if (lb_total <= 0)
      shipping = 0.0;
  else if (lb_total < 5.0)
      shipping = 3.50;
  else if (lb_total < 20)
      shipping = 10.0;
  else
      shipping =  8.0 + 0.1 * lb_total;
  if (cost_total > 100.0)
      discount = DISCOUNT_RATE * cost_total;
  else
    discount = 0.0;
  final_total = cost_total + shipping - discount;
  printf("Your order:\n");
  printf("%.2f lbs of artichokes at $%.2f per pound:$ %.2f\n",
            lb_artichokes, price_artichokes, cost_artichokes);  
  printf("%.2f lbs of beets at $%.2f per pound: $%.2f\n",
            lb_beets, price_beets, cost_beets);  
  printf("%.2f lbs of carrots at $%.2f per pound: $%.2f\n",
            lb_carrots, price_carrots, cost_carrots);
  printf("Total cost of vegetables: $%.2f\n", cost_total);
  if (cost_total > 100)
      printf("Volume discount: $%.2f\n", discount);
  printf("Shipping: $%.2f\n", shipping);
  printf("Total charges: $%.2f\n", final_total); 
 
  return 0;
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -