习题七



1、编写一个程序。该程序读取输入直到遇到#字符,然后报告读取的空格数目、读取的换行符数目以及读取的所有其他字符数目。

#include<stdio.h>

int main(void)

{

    int space=0,newline=0,others=0;

char a;

printf("Please input a string end by #:");

while((a=getchar()) != '#')

    if(a == ' ') space++;

    else if (a == '\n') newline++;

    else others++;

    printf("space: %d,newline: %d,others: %d\n",space,newline,others);

return(0);

}

2、编写一个程序,该程序读取输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCII码。每行打印8个字符,编码对。建议:利用字符计数和模运算符(%)在每8个循环周期时打印一个换行符。

#include<stdio.h>

int main(void)

{

    char a;

int i;

printf("Please input a string end by #:");

for(i = 1; (a = getchar()) != '#'; i++)

{

    printf("%c--%d\t",a,a);

    if(i%8 == 0) printf("\n");

}

printf("\n");

return(0);

}

3、编写一个程序。该程序读取整数,直到输入0。输入终止后,程序应该报告输入的偶数(不包括0)总个数、偶数的平均值,输入的奇数总个数以及奇数的平均值。

#include<stdio.h>

int main(void)

{

    int i_even = 0, sum_even = 0, i_odd = 0, sum_odd = 0, num;

printf("Please input numbers (0 to quit):");

while(1)

{

    scanf("%d",&num);

    if (num == 0) break;

    if (num % 2 == 0) {i_even++; sum_even += num;}

    else {i_odd++; sum_odd += num;}

}

printf("even number's count: %d\n",i_even);

printf("even number's sum  : %d\n",sum_even);

printf("odd  number's count: %d\n",i_odd);

printf("odd  number's sum  : %d\n",sum_odd);

return(0);

}

4、利用if else语句编写程序读取输入,直到#。用一个感叹号代替每个句号,将原有的每个感叹号用两个感叹号代替,最后报告进行了多少次替代。

#include<stdio.h>

int main(void)

{

    int a=0,b=0;

char ch;

printf("Please input a string end by #:");

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

{

    if(ch == '.') {putchar('!'); a++;}

    else if(ch == '!') {putchar('!');putchar('!'); b++;}

    else putchar(ch);

    }

printf("\nthe times of '.' replaced by '!':  %d\n",a);

printf("the times of '!' replaced by '!!': %d\n",b);

return(0);

}

5、switch重做练习3。

#include<stdio.h>

int main(void)

{

int i_even = 0, sum_even = 0, i_odd = 0, sum_odd = 0, num;

printf("Please input numbers (0 to quit):");

while(1)

{

    scanf("%d",&num);

    if (num == 0) break;

    switch(num % 2)

    {

        case 0: i_even++; sum_even += num;break;

        case 1: i_odd++;sum_odd += num;

    }

    }

printf("even number's count: %d\n",i_even);

printf("even number's sum  : %d\n",sum_even);

    printf("odd  number's count: %d\n",i_odd);

printf("odd  number's sum  : %d\n",sum_odd);

return(0);

}

6、编写一个程序读取输入,直到#,并报告序列ei出现的次数。此程序必须要记住前一个字符和当前的字符。用诸如“Receive your eieio award.”的输入测试它。

#include<stdio.h>

int main(void)

{

char former=0,present;

int count=0;

while((present=getchar()) != '#')

{

    if((former == 'e') && (present == 'i')) count++;

    former = present;

    }

printf("ei has appeared %d times\n",count);

return(0);

}

7、编写程序,要求输入一周中的工作小时数,然后打印工资总额、税金以及净工资。作如下假设:

    a.基本工资等级=10.00美元//J,时

    b.加班(超过40小时)=1.5倍的时间

    c.税率    前300美元为15%

    下一个150美元为20%

    余下的为25%

  #define定义常量,不必关心本例是否符合当前的税法。

#include<stdio.h>

#define BASIC 10.00

#define TIME 40  

#define ADD  1.5  

#define LIMIT1 300

#define RATE1 0.15

#define LIMIT2 150

#define RATE2 0.20

#define RATE3 0.25

int main(void)

{

double hours,gross,tax;

printf("input the work hours of a week:");

scanf("%lf",&hours);

if (hours > 40) hours = 40 + (hours - 40) * 1.5;

    gross = hours * BASIC;

printf("gross income:\t\t%lf\n",gross);

if (gross <= LIMIT1) tax = gross * RATE1;

else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;

else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;

printf("tax:\t\t\t%lf\n",tax);

printf("net income:\t\t%lf\n",gross - tax);

return(0);

}

8、修改练习7中的假设a,使程序提供一个选择工资等级的菜单。用switch选择工资等级。程序运行的开头应该像这样:

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

    如果选择l到4.那么程序应该请求输入工作小时数。程序应该一直循环运行,直到输入5。如果输入l到5以外的选项,那么程序应该提醒用户合适的选项是哪些,然后再循环。用#define为各种工资等级和税率定义常量。

    #include<stdio.h>

int get_int(void);

#define TIME 40  

#define ADD  1.5  

#define LIMIT1 300  

#define RATE1 0.15

#define LIMIT2 150  

#define RATE2 0.20

#define RATE3 0.25

int main(void)

    {

    double basic,hours,gross,tax;

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

    printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");

    printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");

    printf("5) quit\n");

    switch( get_int() )

    {

        case 1: basic = 8.75; break;

        case 2: basic = 9.33; break;

        case 3: basic = 10.00; break;

        case 4: basic = 11.20; break;

        default: printf("quit\n"); return(0); //退出程序

    }

    printf("you have select $%.2lf\n",basic);

    printf("input the work hours of a week:");

    scanf("%lf",&hours);

    if (hours > 40) hours = 40 + (hours - 40) * 1.5;

        gross = hours * basic;

    printf("gross income:\t\t%lf\n",gross);

    if (gross <= LIMIT1) tax = gross * RATE1;

    else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;

    else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;

    printf("tax:\t\t\t%lf\n",tax);

    printf("net income:\t\t%lf\n",gross - tax);

    return(0);

}

 

int get_int(void) //得到一个合适的整数,滤除非法数

{

    int num;

    char str[40];

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

   {

       gets(str);

       printf("error! %s is not a number. input again.\n",str);

   }

   while ( getchar() != '\n');

   return num;

}

9、编写一个程序,接受一个整数输入,然后显示所有小于或等于该数的素数。

#include<stdio.h>

int isprime(int);

int main(void)

{

int num,i;

printf("input a positive number:");

scanf("%d",&num);

printf("all the primes <= %d:\n",num);

for(i=2;i<=num;i++)

    if( isprime(i) )

        printf("%d\t",i);

printf("\n");

return(0);

}

 

int isprime(int n)  

{

    int div;

for(div = 2; div * div <= n; div++)

if (n % div == 0)

return 0;

return 1;

}

 

10、 1988年United States Federal Tax Schedule是近期最基本的。它分为4类,每类有两个等级。下面是其摘要;美元数为应征税的收入。

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

┃    种  类        ┃    税  金                                                  ┃

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

┃单身              ┃前17·850美元按15%,超出部分按28%   ┃

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

┃户主              ┃前23,900美元按15%,超出部分按28%┃

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

┃已婚,共有   ┃前29,750美元按15%,超出部分按28% ┃

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

┃已婚,离异   ┃前14,875美元按l5%,超出部分按28%  ┃

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

    例如,有20 000美元应征税收入的单身雇佣劳动者应缴税金0.15×17 850美元+0.28×(20 000美元-17 850美元)。编写一个程序,让用户指定税金种类和应征税收入,然后计算税金。使用循环以便用户可以多次输入。

 

#include<stdio.h>

#define SINGLE 17850

#define HOST 23900

#define MARRIED_SHARE 29750

#define MARRIED_DIVORCE 14875

#define RATE1 0.15

#define RATE2 0.28

    int main(void)

{

    double type,pay,tax;

    char ch;

    while(1)

    {

        printf("Select the type of marriage:\n");

        printf("1)SINGLE\n2)HOST\n3)MARRIED_SHARE\n4)MARRIED_DIVORCE\n5)quit\n");

        while((ch = getchar()) == '\n') continue; //滤掉回车

        switch(ch)

        {

        case '1': type = SINGLE; break;

        case '2': type = HOST; break;

        case '3': type = MARRIED_SHARE; break;

        case '4': type = MARRIED_DIVORCE; break;

        case '5': printf("quit\n"); return(0); //退出程序

        default : printf("input error\n"); continue;

        }

        printf("you have select %c\n",ch);

        printf("input the pay:");

        scanf("%lf",&pay);

        if (pay <= type) tax = pay * RATE1;

        else tax = type * RATE1 + (pay - type) * RATE2;

        printf("wax is %.2lf\n",tax);

        }

}

11、 ABC Mail Order Grocery朝鲜蓟的售价是1.25美元/磅,甜菜的售价是0.65美元/磅,胡萝卜的售价是0.89美元/磅。在添加运输费用之前,他们为100美元的订单提供5%的打折优惠。对5磅或以下的定单收取3.50美元的运输和装卸费用;超过5磅而不足20磅的定单收取1O.OO美元的运输和装卸费用:加磅或以上的运输,在8美元基础上每磅加收0.1美元。编写程序,在循环中使用switch语句,以便对输入a的响应是让用户输入所需的朝鲜蓟磅数,b为甜菜的磅数,c为胡萝卜的磅数,而q允许用户退出订购过程。然后程序计算总费用、折扣和运输费用(如果有运输费的话),以及总数。随后程序应该显示所有的购买信息:每磅的费用、订购的磅数、该订单每种蔬菜的费用、订单的总费用、折扣,如果有的话加上运输费用,以及所有费用的总数。

 

#include <stdio.h>

#include <ctype.h>

#define ARTICHOKE 1.25

#define BEET 0.65

#define CARROT 0.89

#define DISCOUNT_LIMIT 100

#define DISCOUNT_RATE 0.05

#define FREIGHT_FEE1 3.50

#define FREIGHT_LIMIT1 5

#define FREIGHT_FEE2 10.00

#define FREIGHT_LIMIT2 20

#define FREIGHT_FEE3 8

#define FREIGHT_RATE 0.1

 

int main(void)

{

    char ch;

    double artichoke=0,beet=0,carrot=0;  //磅数

    double sum,discount,freight;

    printf("Please select your vegetable: a,b,c,q\n");

    printf("a.artichoke price:$%.2f\n",ARTICHOKE);

    printf("b.beet price:$%.2f\n",BEET);

    printf("c.carrot price:$%.2f\n",CARROT);

    printf("q.end\n");

    printf("(price as dollars per pound)\n");

    While( ( ch = tolower( getchar() )  ) != 'q') //tolower:大写转小写

    {

        switch(ch)

       {

         case 'a': printf("How many pounds of artichokes do you want?");

         scanf("%lf",&artichoke);

         printf("Please select your vegetable: a,b,c,q:");

         continue;

             case 'b': printf("How many pounds of beets do you want?");

         scanf("%lf",&beet);

         printf("Please select your vegetable: a,b,c,q:");

         continue;

         case 'c': printf("How many pounds of carrots do you want?");

         scanf("%lf",&carrot);

         printf("Please select your vegetable: a,b,c,q:");

         continue;

         default: break;

      }

     }

 printf("%10s%10s%10s%10s\n"," ","artichoke","beet","carrot");

 printf("%10s%10.2lf%10.2lf%10.2lf\n","price",ARTICHOKE,BEET,CARROT);

 printf("%10s%10.2lf%10.2lf%10.2lf\n","pound",artichoke,beet,carrot);

 printf("%10s%10.2lf%10.2lf%10.2lf\n","charge",ARTICHOKE * artichoke,BEET * beet,CARROT * carrot);

 sum = ARTICHOKE * artichoke + BEET * beet + CARROT * carrot;

 if (sum > DISCOUNT_LIMIT) discount = sum*DISCOUNT_RATE;

 else discount = 0;

 printf("discount: %.2f\n",discount);

 if (artichoke + beet + carrot <= 5) freight = 3.50;

 else if (artichoke + beet + carrot <20) freight = 10;

 else freight = 8 + (artichoke + beet + carrot) * 0.1;

 printf("freight: %.2f\n",freight);

 sum = sum - discount + freight;

 printf("sum: %.2f\n",sum);

 return 0;

}

发布了47 篇原创文章 · 获赞 4 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章