答案为本人自己编写,仅供大家学习参考,如有错误,欢迎大家在评论区留言指正。
ex7.1
// ex_7.1
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
int main(void)
{
int ch;
int n_spaces = 0;
int n_lines = 0;
int n_others = 0;
printf("Enter text to be analyzed(# to terminate): \n");
while ((ch = getchar()) != STOP)
{
if(ch == ' ')
n_spaces++;
else if(ch == '\n')
n_lines++;
else
n_others++;
}
printf("spaces = %d, newlines = %d, others = %d", n_spaces, n_lines, n_others);
return 0;
}
--------------------------------------------------------------------------------------------------------
ex7.2
// ex_7.2
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
int main(void)
{
int ch = 0;
int counter = 0;
printf("Enter text to be analyzed(# to terminate): \n");
while ((ch = getchar()) != STOP)
{
if((counter % 8 == 0) && (counter != 0))
printf("\n");
printf("%c: %d ", ch, ch);
counter++;
}
return 0;
}
程序运行后,最后还会多出 一个换行 和 10。这是因为scanf最后的回车键导致的。暂时没办法解决这个问题,就先放着,看看以后能不能解决。
--------------------------------------------------------------------------------------------------------
ex7.3
// ex_7.3
#include <stdio.h>
#include <ctype.h>
#define STOP '0'
int main(void)
{
int input = 0;
int n_odd = 0;
int n_even = 0;
int sum_odd = 0;
int sum_even = 0;
printf("Enter some integers to be analyzed(0 to terminate): \n");
while ((scanf("%d", &input)) == 1)
{
if (input == 0)
break;
else if (input % 2 == 0)
{
n_even++;
sum_even += input;
}
else
{
n_odd++;
sum_odd += input;
}
}
printf("Number of the even integers : %d\n", n_even);
printf("Average of the even integers: %.2f\n", (float)(sum_even / n_even));
printf("Number of the odd integers : %d\n", n_odd);
printf("Average of the odd integers : %.2f\n", (float)(sum_odd / n_odd));
return 0;
}
--------------------------------------------------------------------------------------------------------
ex7.4
// ex_7.4
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
int main(void)
{
int ch = 0;
int n_period = 0;
int n_exclamation = 0;
printf("Enter text to be analyzed(# to terminate): \n");
while ((ch = getchar()) != STOP)
{
if (ch == '.')
{
n_period++;
putchar('!');
}
else if (ch == '!')
{
n_exclamation++;
putchar('!');
putchar('!');
}
else
putchar(ch);
}
printf("\nNumber of periods: %d\n", n_period);
printf("Number of exclamation marks: %d\n", n_exclamation);
return 0;
}
--------------------------------------------------------------------------------------------------------
ex7.5
// ex_7.5
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
int main(void)
{
int ch = 0;
int n_period = 0;
int n_exclamation = 0;
printf("Enter text to be analyzed(# to terminate): \n");
while ((ch = getchar()) != STOP)
{
switch (ch){
case '.':
n_period++;
putchar('!');
break;
case '!':
n_exclamation++;
putchar('!');
putchar('!');
break;
default:
putchar(ch);
break;
}
}
printf("\nNumber of periods: %d\n", n_period);
printf("Number of exclamation marks: %d\n", n_exclamation);
return 0;
}
--------------------------------------------------------------------------------------------------------
ex7.6
// ex_7.6
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
int main(void)
{
int ch = 0;
int previous = 0;
int n_ei = 0;
printf("Enter text to be analyzed(# to terminate): \n");
while ((ch = getchar()) != STOP)
{
if ((previous == 'e') && (ch == 'i'))
n_ei++;
previous = ch;
}
printf("\"ei\" occurs %d times.\n", n_ei);
return 0;
}
--------------------------------------------------------------------------------------------------------
ex7.7
// ex_7.7
#include <stdio.h>
#define TAX_RATE_1 0.15
#define TAX_RATE_2 0.20
#define TAX_RATE_3 0.25
float payHours(float);
float tax(float);
int main(void)
{
float hours = 0;
float gross_pay = 0;
float taxes = 0;
float net_pay = 0;
printf("Enter the hours you worked in a week: ");
scanf("%f", &hours);
float pay_hours = payHours(hours);
gross_pay = pay_hours * 10;
taxes = tax(gross_pay);
net_pay = gross_pay - taxes;
printf("Gross Pay: $%.2f\n", gross_pay);
printf("Taxes: $%.2f\n", taxes);
printf("Net Pay: $%.2f\n", net_pay);
return 0;
}
float payHours(float h)
{
float hours = 0;
if (h > 40)
hours = (h - 40) * 1.5 + 40;
else
hours = h;
return hours;
}
float tax(float p)
{
float total_tax = 0;
if (p <= 300)
total_tax = p * TAX_RATE_1;
else if (p <= 450)
total_tax = (p - 300) * TAX_RATE_2 + 300 * TAX_RATE_1;
else
total_tax = (p - 450) * TAX_RATE_3 + 150 * TAX_RATE_2 + 300 * TAX_RATE_1;
return total_tax;
}
--------------------------------------------------------------------------------------------------------
ex7.8
// ex_7.7
#include <stdio.h>
#define TAX_RATE_1 0.15
#define TAX_RATE_2 0.20
#define TAX_RATE_3 0.25
float payHours(float);
float tax(float);
int main(void)
{
int ch = 0;
float hours = 0;
float gross_pay = 0;
float taxes = 0;
float net_pay = 0;
float pay_rate = 0;
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("*****************************************************************\n");
while (scanf("%d", &ch)){
switch (ch)
{
case 1:
pay_rate = 8.75;
break;
case 2:
pay_rate = 9.33;
break;
case 3:
pay_rate = 10.00;
break;
case 4:
pay_rate = 11.20;
break;
case 5:
return 0;
default:
printf("Please enter the choice between 1 to 5\n");
continue;
}
printf("Enter the hours you worked in a week: ");
scanf("%f", &hours);
float pay_hours = payHours(hours);
gross_pay = pay_hours * pay_rate;
taxes = tax(gross_pay);
net_pay = gross_pay - taxes;
printf("Gross Pay: $%.2f\n", gross_pay);
printf("Taxes: $%.2f\n", taxes);
printf("Net Pay: $%.2f\n", net_pay);
}
return 0;
}
float payHours(float h)
{
float hours = 0;
if (h > 40)
hours = (h - 40) * 1.5 + 40;
else
hours = h;
return hours;
}
float tax(float p)
{
float total_tax = 0;
if (p <= 300)
total_tax = p * TAX_RATE_1;
else if (p <= 450)
total_tax = (p - 300) * TAX_RATE_2 + 300 * TAX_RATE_1;
else
total_tax = (p - 450) * TAX_RATE_3 + 150 * TAX_RATE_2 + 300 * TAX_RATE_1;
return total_tax;
}
--------------------------------------------------------------------------------------------------------
ex7.9
// ex_7.9
#include <stdio.h>
int isPrime(int);
int main(void)
{
int num = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for (int i = 2; i <= num; i++)
if (isPrime(i))
printf("%d ", i);
printf("\nDone");
return 0;
}
int isPrime(int n)
{
int is_prime = 1;
if (n < 3)
{
if (n==2)
return is_prime;
else
is_prime = 0;
return is_prime;
}
else
for (int i = 2; i < n ; i++)
if ((n % i) == 0)
is_prime = 0;
break;
return is_prime;
}
--------------------------------------------------------------------------------------------------------
ex7.10
// ex_7.10
#include <stdio.h>
#define TAX_RATE_1 0.15
#define TAX_RATE_2 0.28
float tax(float income, int tax_start);
int main(void)
{
int ch = 0;
float income = 0;
int tax_start = 0;
/* Tax Category: 1.Single 2.Head of Household 3.Married, Joint 4.Married, Separate */
printf("*****************************************************************\n");
printf("Enter your tax category:\n");
printf("1) Single 2) Head of Household\n");
printf("3) Married, Joint 4) Married, Separate\n");
printf("5) quit\n");
printf("*****************************************************************\n");
while (scanf("%d", &ch)){
switch (ch)
{
case 1:
tax_start = 17850;
break;
case 2:
tax_start = 23900;
break;
case 3:
tax_start = 29750;
break;
case 4:
tax_start = 14875;
break;
case 5:
return 0;
default:
printf("Please enter the choice between 1 to 5\n");
continue;
}
printf("Enter your income: ");
scanf("%f", &income);
printf("Your income before tax is : $%.2f\n", income);
printf("Your income after tax is : $%.2f\n", income-tax(income, tax_start));
}
return 0;
}
float tax(float income, int tax_start)
{
float tax = 0;
if ((int)income >= tax_start)
tax = TAX_RATE_1 * tax_start + TAX_RATE_2 * (income - tax_start);
return tax;
}
--------------------------------------------------------------------------------------------------------
ex7.11
// ex_7.11
#include <stdio.h>
int main(void)
{
float a_weight = 0;
float b_weight = 0;
float c_weight = 0;
float goods_weight = 0;
float a_cost = 0;
float b_cost = 0;
float c_cost = 0;
float total_weight = 0;
float total_cost = 0;
float shipping_charge = 0;
float grand_total_charge = 0;
int ch = 0;
printf("What item you want to buy:\n");
printf("a) Artichokes $2.05/pound\n");
printf("b) Beets $1.15/pound\n");
printf("c) Carrots $1.09/pound\n");
printf("q) Exit\n");
while ((ch = getchar()) != 'q')
{
if('\n' == ch)
continue;
switch(ch)
{
case 'a':
printf("How many pounds of artichokes you want: ");
scanf("%f", &goods_weight);
a_weight += goods_weight;
printf("%.2f pounds of artichokes are in your chart\n", a_weight);
break;
case 'b':
printf("How many pounds of beets you want: ");
scanf("%f", &goods_weight);
b_weight += goods_weight;
printf("%.2f pounds of beets are in your chart\n", b_weight);
break;
case 'c':
printf("How many pounds of carrots you want: ");
scanf("%f", &goods_weight);
c_weight += goods_weight;
printf("%.2f pounds of carrots are in your chart\n", c_weight);
break;
case 'q':
continue;
default:
printf("Please enter a) b) c) or q)");
break;
}
while (getchar() != '\n')
continue;
}
a_cost = a_weight * 2.05;
b_cost = b_weight * 1.15;
c_cost = c_weight * 1.09;
total_weight = a_weight + b_weight + c_weight;
total_cost = a_cost + b_cost + c_cost;
if (total_cost >= 100)
total_cost *= 0.95;
if (total_weight <= 5)
shipping_charge = 6.50;
else if (total_weight < 20)
shipping_charge = 14.00;
else
shipping_charge = 14 + total_weight * 0.5;
printf("Total cost: %.2f\n", total_cost);
printf("Shipping charge: %.2f\n", shipping_charge);
printf("Final cost: %.2f\n", total_cost + shipping_charge);
return 0;
}