C Primer Plus (Fifth edition) Chapter seventh programming exercises

Source: Internet
Author: User

Seventh C control statements: branches and jumps


Programming Exercises:


1. Write a program. The program reads the input until the # character is encountered, and then reports the number of spaces read, the number of newline characters read, and the number of all other chars read.

#include <stdio.h>int main (void) {int a=0, b=0, C=0;char ch;while ((ch = getchar ())! = ' # ') {if (ch! = ' && ch! = ' \ n ') c++;else if (ch! = ") b++;elsea++;} printf ("Number of spaces:%3d\n", a);p rintf ("Number of newline:%3d\n", b);p rintf ("Number of other characters:%3d\n", c); return 0;}



2. Write a program that reads input until the # character is encountered. Causes the program to print each input character and its decimal ASCII code. Each line prints 8 character/encoding pairs. Recommendation: Use the character count and modulo operator (%) to print a newline character every 8 cycle cycles.

#include <stdio.h>int main (void) {int i = 0;char Ch;char str[100];p rintf ("Enter the character (enter # end):");d o {ch = getchar (); str[i+ +] = ch;} while (ch! = ' # '); for (i = 0; Str[i]! = ' # '; i++) {if (str[i] = = ' \ n ') printf ("' \\n '/%-3d", str[i]); else if (str[i] = = ' \ t ') printf ("' \\t '/%-3d", Str[i]); elseprintf ("'%c '/%-3d", Str[i], str[i]); if (i% 8 = = 7) putchar (' \ n ');} Putchar (' \ n '); return 0;}



3. Write a program. The program reads an integer until 0 is entered. After the input is terminated, the program should report the total number of inputs (not including 0), the average of the even numbers, the odd number of inputs, and an odd average.

#include <stdio.h>int main (void) {int input;int even = 0, odd = 0;int even_sum = 0, odd_sum = 0;printf ("Please enter an integer (enter 0 end): while (scanf ("%d", &input) = = 1 && 0! = input) {if (input% 2 = = 0) {even++;even_sum + = input;} Else{odd++;odd_sum + = input;}} if (0 = = even) printf ("Total number of even numbers: 0 average: 0\n"), elseprintf ("Total number of even numbers:%d average:%d\n", even, even_sum/even); if (0 = = odd) printf ("Even Total number: 0 Average: 0\n "); elseprintf (" Total number of odd numbers:%d average:%d\n ", odd, odd_sum/odd); return 0;}



4. Write the program with the IF Else statement to read the input until #. Replace each period with an exclamation mark, replace each exclamation mark with two exclamation marks, and finally report how many substitutions are made.

#include <stdio.h>int main (void) {char ch;int a = 0, B = 0;printf ("Please enter string, End with" # ": \ n"); while ((ch = getchar ())! = ' # ' ) {if (ch = = '. ') {Putchar ('! '); a++;} ELSE{IF (ch = = '! ') {Putchar ('! '); Putchar ('! '); b++;} Else{putchar (ch);}}} printf ("\ n");p rintf ("'. ') %d times replaced by '! ' \ n ", a);p rintf (" '! ') %d times replaced by '!! ' \ n ", b);p rintf (" total substitution%d \ n ", A + b); return 0;}



5. Use switch redo Exercise 3 to write a program. The program reads an integer until 0 is entered. After the input is terminated, the program should report the total number of inputs (not including 0), the average of the even numbers, the odd number of inputs, and an odd average.

#include <stdio.h>int main (void) {int input;int even = 0, odd = 0;int even_sum = 0, odd_sum = 0;printf ("Please enter an integer (enter 0 end): while (scanf ("%d", &input) = = 1 && 0! = input) {switch (input% 2) {case 0:even++; Even_sum + = Input;break;case 1:odd++; Odd_sum + = Input;break;}} if (0 = = even) printf ("Total number of even numbers: 0 average: 0\n"), elseprintf ("Total number of even numbers:%d average:%d\n", even, even_sum/even); if (0 = = odd) printf ("Even the total Number: 0 Average: 0\n "); elseprintf (" Total number of odd numbers:%d average:%d\n ", odd, odd_sum/odd); return 0;}



6. Write a program to read the input until #, and report the number of times the sequence ei appears. This program must remember the previous character and the current character. Use your Eieio award such as "Receive." Input to test it.

#include <stdio.h>int main (void) {char ch = ' * ', temp;int i = 0;printf ("Please enter characters until # ends:"); while (temp = ch, ch = getchar ( ), ' # ' = ch) if (' e ' = = temp && ' i ' = = ch) i++;p rintf ("ei occurrences:%d\n", i); return 0;}



7. Write a program that requires you to enter the number of hours of work in a week, and then print payroll, taxes, and net wages. Make the following assumptions:

A Base salary level = 10.00 USD/hour

b Overtime (more than 40 hours) = 1.5 times times the time

C Tax rate before 300 dollars to 15%

The next 150 dollars is 20%

The rest is 25%.

Defining constants with # define doesn't have to be concerned with whether this example conforms to the current tax law.

#include <stdio.h>


#define LIMIT1 300

#define TAX1 0.15

#define LIMIT2 150

#define TAX2 0.2

#define LIMIT3 450

#define TAX3 0.25

#define TIME 40

#define ADD 1.5

#define BASE 10.00


int main (void)

{

float time;

Float total, tax, salary;


printf ("Please enter the number of hours you work in a week:");

scanf ("%f", &time);

if (Time > Time)

Time = (time-time) * ADD + time;


Total = time * BASE;


if (total <= LIMIT1)

Tax = total * TAX1;

else if (total <= LIMIT3)

Tax = LIMIT1 * TAX1 + (total-limit1) * TAX2;

Else

Tax = LIMIT1 * TAX1 + LIMIT2 * TAX2 + (TOTAL-LIMIT3) * TAX3;

salary = Total-tax;



printf ("This week's total wage:%.2f; tax:%.2f; Real wages:%.2f\n",

Total, tax, salary);


return 0;

}


8. Modify the assumption a in Exercise 7 to give the program a menu that selects the payroll level. Use switch to select a salary level. The beginning of the program run should look like this:

Enter the number corresponding to the desired pay rate or action:

1) $8.75/hr 2) $9.33/hr

3) $lO. OO/HR 4) $11.20/hr

5) Quit

If L to 4 is selected. Then the program should request the number of hours to enter the work. The program should run continuously until you enter 5. If you enter an option other than L to 5, the program should remind the user which options are appropriate and then loop. Define constants for various wage levels and tax rates in # #.

#include  <stdio.h> #define  LIMIT1 300#define TAX1 0.15#define LIMIT2  150#define tax2 0.2#define limit3 450#define tax3 0.25#define time 40# define add 1.5#define base1 8.75#define base2 9.33#define base3 10.00# Define base4 11.20void menu (void); Void fun (float base); Int main (void) {char  Ch;menu ();while  (( ch = getchar ())  !=  ' 5 ') {while  (GetChar ()  !=  ' \ n ') continue;switch  (CH) {case  ' 1 ':  fun (BASE1);break;case  ' 2 ':  fun (BASE2); break;case   ' 3 ':  fun (BASE3);break;case  ' 4 ':  fun (BASE4); break;default: printf ("Please enter  the correct option (1 2 3 4 or 5): ");} while  (GetChar ()  !=  ' \ n ') Continue;putchar (' \ n '); menu ();} return 0;} Void menu (void) {printf ("Enter the number correspondIng to ");p rintf ("  the desired pay rate or action :\n ");p rintf (" 1) &NBSP;$8.75&NBSP;/&NBSP;HR&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;2)  $9.33 / hr  \n ");p rintf (" 3)   $lO. oo / hr        4)  $11.20  / hr\n ");p rintf (" 5)  quit\n ");} Void fun (float base) {float time;float total, tax, salary;printf ("Please enter the time of the week to work (hours): "); scanf ("%f ",  &time);if  (time > time) time =  (time - time)  * ADD + TIME;total = time * base;if  (total <=  LIMIT1) tax = total * tax1;else if  (TOTAL&NBSP;&LT;=&NBSP;LIMIT3) tax =  limit1 * tax1 +  (total - limit1)  * tax2;elsetax = limit1  * TAX1 + LIMIT2 * TAX2 +  (TOTAL&NBSP;-&NBSP;LIMIT3) &NBsp;* tax3;salary = total - tax;printf ("This week's total wage:%.2f; tax:%.2f; Real wages:%.2f\n",total,  tax, salary);}



9. Write a program that accepts an integer input and then displays all primes that are less than or equal to the number.

#include <stdio.h>int main (void) {int a;int I, j;printf ("Enter an integer:"), scanf ("%d", &a);p rintf ("1 is the prime number \ n"); for (i = 2; I <= A; i++) {for (j = 2; j*j <= i; j + +) {if (i% j = 0) break;} if (J*j > i) {printf ("%5d is a prime number \ n", i);}} return 0;}



10. The 1988 states Federal Tax schedule is the most basic in recent times. It is divided into 4 categories, each of which has two levels. The following is a summary of the number of dollars as taxable income.

┏━━━━━━┳━━━━━━━━━━━━━━━━━━┓

┃ category ┃ Tax ┃

┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫

┃ single ┃ before 17,850 USD press 15%, out of section press 28%┃

┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫

┃ head of household ┃ before 23,900 USD press 15%, out of the partial press 28%┃

┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫

┃ married total ┃ before 29,750 USD press 15%, out of section press 28%┃

┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫

┃ married divorced ┃ before 14,875 USD press l5%, out of part by 28%┃

┗━━━━━━┻━━━━━━━━━━━━━━━━━━┛

For example, a single wage-earners with 20 000 taxable income should be taxed 0.15x17 $850 +0.28x (20 000 US $17 850 USD).

Write a program that lets the user specify the type of tax and taxable income, and then calculates the tax. Use loops so that users can enter multiple times.

#include  <stdio.h> #define  TAXD 0.15#define TAXU 0.28#define KIND1  17850.00#define kind2 23900.00#define kind3 29750.00#define kind4 14875.00void  menu (void); Void fun (double), int main (void) {Char ch;menu ();while  (ch =  GetChar ())  !=  ' 5 ') {while  (GetChar ()  !=  ' \ n ') continue;switch  (CH) {case  ' 1 ':  fun (KIND1);break;case  ' 2 ':  fun (KIND2);break;case  ' 3 ':  fun (KIND3);break;case  ' 4 ' :  fun (KIND4); break;default: printf ("Please enter the correct option (1&NBSP;2&NBSP;3&NBSP;4&NBSP;OR&NBSP;5):");} Putchar (' \ n '); menu ();while  (GetChar ()  !=  ' \ n ') continue;} return 0;} Void menu (void) {printf ("Please select tax Category: \ n");p rintf ("1) single. 2) head of household. \ n ");p rintf (" 3) married, shared. 4) married, divorced. \ n ");p rintf (" 5) exit. \ n ");} Void fun (double kind) {double income;double tax;printf ("Please enter pre-tax revenue:") scanf ("%lf", & Income);if  (Income <= kind) Tax = income * taxd;elsetax = kind * taxd +  (income -  Kind)  * taxu;printf ("Tax payable:%.2lf\n",  tax);}



The ABC Mail Order Grocery Artichoke is priced at $1.25/lb, the beet is priced at $0.65/lb, and the carrot is priced at $0.89/lb. Before adding shipping charges, they offer a 5% discount on orders of $100. Shipping and handling costs of $3.50 are charged for orders of 5 pounds or less, and orders over 5 lbs. and less than 20 lbs. for shipping and handling costs of 1o.oo USD: 20 pounds or more, plus $8 per pound on a $0.1 basis. Write a program that uses a switch statement in a loop so that the response to input A is to let the user enter the required number of artichoke pounds, B is the number of pounds in the beet, and C is the number of pounds of carrots, and Q allows the user to exit the ordering process. The program then calculates the total costs, discounts, and shipping costs (if any), as well as the total. The program should then display all of the purchase information: The cost per pound, the number of points ordered, the cost of each vegetable in the order, the total cost of the order, the discount, if any, plus the shipping cost, and the total amount of all expenses.

#include  <stdio.h> #define &NBSP;PRICE&NBSP;100#DEFINE&NBSP;DISCOUNT_RATE&NBSP;0.05VOID&NBSP;MENU ( void);d ouble in_lb (void); Void empty (void); Int main (void) {const double price_a =  1.25;const double price_b = 0.65;const double price_c = 0.89; char ch;double lb_a = 0.00;double lb_b = 0.00;double lb_c =  0.00;double lb_total = 0.00;double cost_a = 0.00;double cost_b =  0.00;double cost_c = 0.00;double cost_total = 0.00;double final_total  = 0.00;double discount = 0.00;double shipping = 0.00;menu ();while  (ch  = getchar ())  !=  ' Q ') {empty ();switch  (CH) {case  ' a ':  lb_a += in_lb ();  empty ();break;case  ' B ':  lb_b += in_lb ();  empty ();break;case  ' C ':  lb_c += IN_LB ();  empty (); break;default: printf ("Please enter the correct option (A&NBSP;B&NBSP;C&NBSP;OR&NBSP;Q):");} Menu ();} Total Cost cost_a = price_a * lb_a;cost_b = price_b * lb_b;cost_c =  price_c * lb_c;lb_total = lb_a + lb_b + lb_c;cost_total =  cost_a + cost_b + cost_c;//discount if  (cost_total > price) discount  = cost_total * discount_rate;//transport costs if  (LB_TOTAL&NBSP;&LT;&NBSP;5) shipping = 3.50 ;else if  (lb_total < 20) shipping = 10.00;elseshipping = 8 +   (int) lb_total * 0.10;printf ("\ n Order Details: \ n");p rintf ("Artichoke price $%.2f. %.2f pounds $%.2f\n ",  price_a, lb_a, cost_a);p rintf (" Beet unit price $%.2f. %.2f pounds $%.2f\n ",  price_b, lb_b, cost_b);p rintf (" Carrot price $%.2f. %.2f lb Total Price $%.2f\n ",  price_c, lb_c, cost_c);p rintf (" Total cost of vegetables:%.2lf\n ",  cost_total);p rintf (" Discount:%. 2lf\n ",  discount);printf ("Transport costs:%.2lf\n",  shipping);p rintf ("Costs Payable:%.2lf\n",  cost_total  - discount); return 0;} Void menu (void) {printf ("Please select Category: \ n");p rintf ("a) artichoke b) beet \ n");p rintf ("c) carrot Q) exit \ n"); double in_lb (void) {double a;printf ("Please enter Weight:"); scanf ("%lf",  &a); return a;} Void empty (void) {while  (GetChar ()  !=  ' \ n ');}


This article is from the "30 years old school Programming" blog, please be sure to keep this source http://xiongyi85.blog.51cto.com/8756903/1654905

C Primer Plus (Fifth edition) Chapter seventh programming exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.