1. 程式人生 > >C primer Plus作業第七章

C primer Plus作業第七章

收入 ring fin done 退出 ont 11.2 eve fault

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/************************************/

/* practice 1 第七章作業 */

/***********************************/

void p7_1(void)

{

char ch;

int n_space = 0;

int n_break = 0;

int n_number = 0;

printf("輸入一段字符串:");

while((ch=getchar()) != ‘#‘)

{

if(ch == ‘\n‘)

{

n_break++;

}

else if(ch == ‘ ‘)

{

n_space++;

}

else

{

n_number++;

}

}

printf("一共有空格:%d個\n一共有換行符:%d個\n共有字符:%d個",n_space,n_break,n_number);

}

/************************************/

/* practice 2 第七章作業 */

/***********************************/

void p7_2(void)

{

char ch;

printf("輸入一段字符串:");

int count=0;

while((ch=getchar()) != ‘#‘)

{

count++;

printf("%c:%d\t",ch,ch);

if(count%8==0)

{

putchar(‘\n‘); //printf("\n")相同

}

}

}

/************************************/

/* practice 3 第七章作業 */

/***********************************/

void p7_3(void)

{

int input;

int even=0;

double sum_even=0;

int odd=0;

double sum_odd=0;

printf("請輸入整數:");

while(scanf("%d",&input)==1 && input !=0)

{

if(input %2 ==0)

{

even++;

sum_even += input;

}

else

{

odd++;

sum_odd += input;

}

printf("請輸入整數:");

}

printf("輸入偶數的個數是:%d,平均值是:%.2lf \n輸入奇數的個數是:%d,平均值是:%.2lf",even,sum_even/even,odd,sum_odd/odd);

}

/************************************/

/* practice 4 第七章作業 */

/***********************************/

void p7_4(void)

{

char ch;

int count1=0,count2=0;

while((ch=getchar()) != ‘#‘)

{

if(ch==‘.‘)

{

count1++;

putchar(‘!‘);

}

else if(ch == ‘!‘)

{

count2++;

putchar(‘!‘);

putchar(‘!‘);

}

else

{

putchar(ch);

}

}

printf("用感嘆號替換句號替換了%d次\n",count1);

printf("用兩個感嘆號替換原來感嘆號替換了%d次",count2);

}

/************************************/

/* practice 5 第七章作業 */

/***********************************/

void p7_5(void)

{

char ch;

int count1=0,count2=0;

while((ch=getchar()) != ‘#‘)

{

switch(ch)

{

case ‘.‘:

putchar(‘!‘);

count1++;

break;

case ‘!‘:

putchar(‘!‘);

putchar(‘!‘);

count2++;

break;

default:

putchar(ch);

}

}

printf("用感嘆號替換句號替換了%d次\n",count1);

printf("用兩個感嘆號替換原來感嘆號替換了%d次",count2);

}

/************************************/

/* practice 6 第七章作業 */

/***********************************/

void p7_6(void)

{

char ch,ch_pre;

int count=0;

while((ch=getchar())!=‘#‘)

{

if(ch == ‘i‘) //新的一輪輸入字符,若ch==‘i‘

{

if(ch_pre == ‘e‘) //而且上一輪輸入ch_pre==‘e‘的話

{

count++; //計數+1

}

}

ch_pre =ch; //把當前輸入字符作為上一次輸入字符

}

printf("ei出現的次數是%d次",count);

}

/************************************/

/* practice 7 第七章作業 */

/***********************************/

#define SALARY1 10.0

#define RATE1 0.15

#define RATE2 0.2

#define RATE3 0.25

void p7_7(void)

{

int hour;

double total; //工資總額

double tax; //稅金

double salary; //凈收入

printf("a.基本工作 = 10.00美元/小時\n");

printf("b.加班(超過40小時) = 1.5倍時間\n");

printf("c.稅率:\t前300美元為15%%\n\t續150美元為20%%\n\t余下的為25%%\n");

printf("請輸入你一周工作的小時數:");

scanf("%d",&hour);

if(hour>40)

{

total = (hour*1.5) * SALARY1;

}

else

{

total = hour * SALARY1;

}

if(total<300)

{

tax = total * RATE1;

}

else if(total>300 && total<=450)

{

tax = 300*RATE1 + (total-300)*RATE2;

}

else

{

tax = 300*RATE1 + 150*RATE2 + (total - 450)*RATE3;

}

salary = total - tax;

printf("你這周的的工資總額:%.2lf,稅金:%.2lf,凈收入:%.2lf",total,tax,salary);

}

/************************************/

/* practice 8 第七章作業 */

/***********************************/

void p7_8(void)

{

int hour;

double total; //工資總額

double tax; //稅金

double income; //凈收入

int choice; //輸入選擇項

double salary;

printf("*****************************************************************\n");

printf("Enter the number corresponding to the desired pay rate or action:\n");

printf("1)%-10s\t2)%-10s\n3)%-10s\t4)%-10s\n5)%-10s\n","$8.75/hr","$9.33/hr","$10.00/hr","$11.20/hr","quit");

printf("*****************************************************************\n");

printf("Enter the number corresponding to the desired pay rate or action:\n");

while((scanf("%d",&choice)==1) &&choice !=5)

{

switch(choice)

{

case 1:

salary = 8.75;

break;

case 2:

salary = 9.33;

break;

case 3:

salary = 10.00;

break;

case 4:

salary = 11.20;

break;

default:

printf("請輸入1~5以內的數字!\n");

break;

}

printf("請輸入你一周工作的小時數:");

scanf("%d",&hour);

if(hour>40)

{

total = (hour*1.5) * salary;

}

else

{

total = hour * salary;

}

if(total<300)

{

tax = total * RATE1;

}

else if(total>300 && total<=450)

{

tax = 300*RATE1 + (total-300)*RATE2;

}

else

{

tax = 300*RATE1 + 150*RATE2 + (total - 450)*RATE3;

}

income = total - tax;

printf("你這周的的工資總額:%.2lf,稅金:%.2lf,凈收入:%.2lf\n",total,tax,income);

printf("Enter the number corresponding to the desired pay rate or action:\n");

}

printf("Done");

}

/************************************/

/* practice 10 第七章作業 */

/***********************************/

void p7_10(void)

{

int choice = 0;

double income = 0;

double threshold = 0;

double tax = 0;

while (1)

{

printf("請選擇你的類型:\n");

printf("1) 單身\n2) 戶主\n3) 已婚,共有\n4) 已婚,離異\n請輸入你的選擇:");

scanf("%d", &choice);

switch (choice)

{

case 1:

threshold = 17850.0;

break;

case 2:

threshold = 23900.0;

break;

case 3:

threshold = 29750.0;

break;

case 4:

threshold = 14875.0;

break;

default:

printf("輸入有誤,請輸入1-4的數字!");

continue;

}

printf("請輸入你的工資:");

scanf("%lf", &income);

if (income < threshold)

{

tax = income * 0.15;

}

else

{

tax = threshold * 0.15 + (income - threshold) * 0.28;

}

printf("類別:%d, 凈收入:%.2lf, 稅金:%.2lf\n", choice, income, tax);

}

}

/************************************/

/* practice 11 第七章作業 */

/***********************************/

#define PRICE1 2.05 //洋薊的價錢

#define PRICE2 1.15 //甜菜的價錢

#define PRICE3 1.09 //胡蘿蔔的價錢

void p7_11(void)

{

double p_artichoke=0; //洋薊的磅數

double p_beet=0; //甜菜的磅數

double p_carrot=0; //胡蘿蔔的磅數

double sum_artichoke=0; //洋薊的總數

double sum_beet=0; //甜菜的總數

double sum_carrot=0; //胡蘿蔔的總數

char choice=0;

double p_total=0; //訂購的重量(單位磅)

double totalCost=0; //總費用

double discount=1; //折扣價

double packingCost=0; //運費及包裝費

double cost=0;

printf("《訂貨商店》-共有以下三種食材:\n");

printf("a)%-6sb)%-6sc)%-6s\n","洋薊","甜菜","胡蘿蔔");

while(choice!=‘q‘)

{

printf("請輸入你要訂購的的食材(按q退出):");

fflush(stdin);

choice=getchar();

fflush(stdin);

switch(choice)

{

case ‘a‘:

printf("請輸入洋薊的磅數:");

scanf("%lf",&p_artichoke);

sum_artichoke += p_artichoke;

break;

case ‘b‘:

printf("請輸入甜菜的磅數:");

scanf("%lf",&p_beet);

sum_beet += p_beet;

break;

case ‘c‘:

printf("請輸入胡蘿蔔的磅數:");

scanf("%lf",&p_carrot);

sum_carrot += p_carrot;

break;

case ‘q‘:

continue;

default:

printf("輸入有誤,請重新輸入!\n");

break;

}

}

p_total = p_artichoke + p_beet + p_carrot; //訂購的重量(單位磅)

if(p_total<5)

{

packingCost = 6.5;

}

else if(p_total>5 && p_total<=20)

{

packingCost = 14;

}

else

{

packingCost = 14+(p_total-20)*0.5;

}

totalCost = sum_artichoke*PRICE1 + sum_beet*PRICE2 + sum_carrot*PRICE3;

if(totalCost>100)

{

discount = 0.95; //在添加運費之前100美元訂單有5%的打折優惠

}

if(totalCost==0)

{

cost=0;

packingCost=0;

discount=0;

}

else

{

cost = totalCost*discount+packingCost;

}

printf("%-10s%-10s%-24s%-12s\n","商品名稱","物品售價","訂購的重量(單位:磅)","訂購蔬菜費用");

printf("%-10s$%-10.2lf%-24.2lf$%-12.2lf\n","洋薊",PRICE1,p_artichoke,p_artichoke*PRICE1);

printf("%-10s$%-10.2lf%-24.2lf$%-12.2lf\n","甜菜",PRICE2,p_beet,p_beet*PRICE2);

printf("%-10s$%-10.2lf%-24.2lf$%-12.2lf\n","胡蘿蔔",PRICE3,p_carrot,p_carrot*PRICE3);

printf("%-12s%-6s%-14s%-12s\n","訂單總費用","折扣","運費和包裝費","費用總額");

printf("$%-12.2lf%-6.2lf$%-14.2lf$%-12lf",totalCost,discount,packingCost,cost);

}

int main()

{

p7_11();

return 0;

}

C primer Plus作業第七章