/*Project 1*/
#include<stdio.h>
int main(void)
{
int ch;
int n = 0;
while ((ch = getchar()) != EOF)
n++;
printf("%d",n);
return 0;
}
/*Project 2*/
#include<stdio.h>
int main(void)
{
char ch;
int n = 0;
while ((ch = getchar()) != EOF)
{
switch(ch)
{
case '\n':
printf("\\n");
printf(" %d,",ch);
break;
case '\t':
printf("\\t");
printf(" %d,",ch);
break;
default:
if (ch < ' ')
{
putchar('^');
putchar(ch + 64);
printf(" %d,",ch);
}
else
{
putchar(ch);
printf(" %d,",ch);
}
break;
}
n++;
if(n == 10)
{
putchar('\n');
n = 0;
}
}
return 0;
}
或者
#include<stdio.h>
int main(void)
{
char ch;
int n = 0;
while ((ch = getchar()) != EOF)
{
if(ch < 32)
{
if(ch == '\n')
printf("\\n %d,",ch);
else if(ch == '\t')
printf("\\t %d,",ch);
else
printf("'^'%c %d,",ch + 64,ch);
}
else
printf("%c %d,",ch,ch);
n++;
if(n == 10)
{
printf("\n");
n = 0;
}
}
return 0;
}
/*Project 3*/
#include<stdio.h>
int main(void)
{
int n = 0;
int m = 0;
char ch;
while((ch = getchar()) != EOF)
{
if (ch >= 'A' && ch <= 'Z')
{
n++;
}
else if (ch >= 'a' && ch <= 'z')
{
m++;
}
}
printf("大寫字母個數(shù):%d\n小寫字母個數(shù):%d",n,m);
return 0;
}
或者
#include<stdio.h>
#include<ctype.h>
int main(void)
{
int n = 0;
int m = 0;
char ch;
while((ch = getchar()) != EOF)
{
if (isupper(ch))
{
n++;
}
else if (islower(ch))
{
m++;
}
}
printf("大寫字母個數(shù):%d\n小寫字母個數(shù):%d",n,m);
return 0;
}
/*Project 4*/
/*假設(shè)字母間均用空格隔開冯勉,標點符號后若有單詞則也加空格*/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
int n = 0; /*字母數(shù)*/
int m = 0; /*單詞數(shù)*/
char ch;
while((ch = getchar()) != EOF)
{
if (isalpha(ch))
{
n++;
}
else if (ch == ' ')
{
m++;
}
}
printf("平均每個字母的單詞數(shù):%d\n",n/m);
return 0;
}
/*Project 5*/
#include<stdio.h>
int main(void)
{
int guess = 50;
int min = 1;
int max = 100;
char response;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan u if it is upper and with an l if it is lower.\n");
printf("Un...is your number %d?\n", guess);
while((response = getchar()) != 'y')
{
if (response == 'u')
{
max = guess;
guess = (min + max) / 2;
printf("Well,then, is it %d?\n",guess);
}
else if (response == 'l')
{
min = guess;
guess = (min + max) / 2;
printf("Well,then, is it %d?\n",guess);
}
else
printf("Sorry,I can't understand only y or u or l.\n");
while(getchar() != '\n')
continue;
}
printf("I knew I could do it!\n");
return 0;
}
/*Project 6*/
#include<stdio.h>
char get_first(void);
int main(void)
{
int ch;
printf("請輸入一個字符:");
ch = get_first();
printf("你輸入的是:%c",ch);
return 0;
}
char get_first(void)
{
int ch;
while((ch = getchar()) == ' ')
;
return ch;
}
/*Project 7*/
#include<stdio.h>
#define OVERTIME 1.5
#define TAX11 300
#define TAX12 0.15
#define TAX21 150
#define TAX22 0.2
#define TAX3 0.25
#define TIME 40
int main(void)
{
float time,pay,tax,RATE;
char num;
printf("********************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("a) $8.75/hr\tb) $9.33/hr\n");
printf("c) $10.00/hr\td) $11.20/hr\n");
printf("q) quit\n");
printf("********************************************************************\n");
scanf("%c",&num);
while((num < 'a' || num > 'd') && num != 'q')
{
printf("請輸入正確的選項杏死!\n");
scanf("%c",&num);
}
if(num != 'q')
{
printf("請輸入一周工作小時數(shù):");
scanf("%f",&time);
while(time < 0 || time > 168)
{
printf("請輸入正確的時間!\n");
scanf("%f",&time);
}
switch(num)
{
case 'a':
RATE = 8.75;
break;
case 'b':
RATE = 9.33;
break;
case 'c':
RATE = 10.00;
break;
case 'd':
RATE = 11.20;
break;
}
if(time <= TIME)
{
pay = time * RATE;
if(pay <= TAX11)
tax = pay * TAX12;
else if(pay <= (TAX11 + TAX21) && pay > TAX11)
tax = TAX11 * TAX12 + (pay - TAX11) * TAX22;
else if(pay > (TAX11 + TAX21))
tax = TAX11 * TAX12 + TAX21 * TAX22 + (pay - TAX11 - TAX21) * TAX3;
}
else if(time > TIME)
{
pay = RATE * OVERTIME * time;
tax = TAX11 * TAX12 + TAX21 * TAX22 + (RATE * TIME - TAX11 - TAX21) * TAX3;
}
printf("工資總額:%.2f$.\n稅金:%.2f$.\n凈收入:%.2f$.\n",pay,tax,pay - tax);
}
else
printf("Done!");
return 0;
}
/*Project 8*/
#include<stdio.h>
int main(void)
{
float first,second;
char choice;
char ch[40];
printf("Enter the operation of you choice:\n");
printf("a) add \tb) subtract\n");
printf("c) multiply\td) divide\n");
printf("q) quit\n");
while(scanf("%c",&choice))
{
while(choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'q')
{
printf("請輸入正確的選項易茬!\n");
scanf("%c",&choice);
}
switch(choice)
{
case 'a':
printf("Enter first number: ");
while(scanf("%f",&first) != 1)
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&second) != 1)
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
printf("%g + %g = %g\n",first,second,first+second); /*%g能自動選擇合適的格式輸出*/
break;
case 's':
printf("Enter first number: ");
while(scanf("%f",&first) != 1)
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&second) != 1)
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
printf("%g - %g = %g\n",first,second,first-second);
break;
case 'm':
printf("Enter first number: ");
while(scanf("%f",&first) != 1)
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
printf("Enter second number: ");
while(scanf("%f",&second) != 1)
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
printf("%g * %g = %g\n",first,second,first*second);
break;
case 'd':
printf("Enter first number: ");
while(scanf("%f",&first) != 1)
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
printf("Enter second number: ");
while((scanf("%f",&second) != 1))
{
scanf("%s",&ch);
printf("%s is not an number.\n",ch);
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
const float EPSINON = 0.000001;
/*由于浮點數(shù)的表示是不精確的茧妒,所以不能直接比較兩個數(shù)是否完全相等萧吠。
一般都是在允許的某個范圍內(nèi)認為某個個浮點數(shù)相等
最好設(shè)置個變量0.000001再比較,直接比較容易出錯*/
while(( second >= -EPSINON ) && ( second <= EPSINON ))
{
printf("Enter a number other than 0: ");
scanf("%f",&second);
}
printf("%g / %g = %g\n",first,second,first/second);
break;
default:
break;
}
while(getchar() == EOF)
break;
if (choice == 'q')
{
printf("Bye!");
break;
}
printf("Enter the operation of you choice:\n");
printf("a) add \tb) subtract\n");
printf("c) multiply\td) divide\n");
printf("q) quit\n");
}
return 0;
}
C Primer Plus第八章課后答案
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門蹋笼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來展姐,“玉大人,你說我怎么就攤上這事剖毯』浚” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵逊谋,是天一觀的道長擂达。 經(jīng)常有香客問我,道長胶滋,這世上最難降的妖魔是什么板鬓? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮究恤,結(jié)果婚禮上俭令,老公的妹妹穿的比我還像新娘。我一直安慰自己部宿,他們只是感情好抄腔,可當我...
- 文/花漫 我一把揭開白布瓢湃。 她就那樣靜靜地躺著,像睡著了一般赫蛇。 火紅的嫁衣襯著肌膚如雪绵患。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼奏寨,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鹰服?” 一聲冷哼從身側(cè)響起病瞳,我...
- 正文 年R本政府宣布讼昆,位于F島的核電站托享,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏浸赫。R本人自食惡果不足惜闰围,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望既峡。 院中可真熱鬧羡榴,春花似錦、人聲如沸运敢。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽者冤。三九已至肤视,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間涉枫,已是汗流浹背邢滑。 一陣腳步聲響...