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;
}