復(fù)習(xí)題
1、確定哪個(gè)表達(dá)式為true,哪個(gè)為false。
a.false
b.true
c.false
2、構(gòu)造一個(gè)表達(dá)式來表示下列條件:
a.number等于或大于90,但是小于100
b.ch不是字符q也不是字符k
c.number界于1到9之間(包括1和9),但是不等于5
d.number不在1到9之間
答:
a.number >= 90 && number < 100
b.ch != 'q' && ch != 'k'
c.number >= 1 && number <= 9 && number != 5
d.number < 1 || number > 9
3、下面程序中的關(guān)系表達(dá)式過于復(fù)雜,并有些錯(cuò)誤,請(qǐng)簡化并改正它。
第5行:應(yīng)該是scanf("%d %d", &weight, &height);。在scanf()中不要忘記使用&運(yùn)算符。這一行前面也應(yīng)該有提示輸入的語句。但第6行已經(jīng)保證height>64,因此,不需要任何測(cè)試,并且
else if應(yīng)該是else。
第9行:它的意思是(height<72&&height>64)。但是表達(dá)式的第一部分是不必要的,因?yàn)榧热怀绦蛞呀?jīng)到達(dá)了這個(gè)else if,那么height必然小于72。因此一個(gè)簡單的(height>64)就可以了。
第11行:條件冗余;第二個(gè)表達(dá)式(weight不是小于或等于300的)與第一個(gè)子表達(dá)式意義相同。所需要的只是一個(gè)簡單的(weight>300)。但是這里還有更多的問題。第11行屬于一個(gè)不正確的if!很明顯,這個(gè)else是與第6行中的if相匹配的。但是根據(jù)if的“最就近原則”,它會(huì)與第7行的if相匹配。(因此會(huì)在weight小于100并且height小于或等于64時(shí)到達(dá)第11行。這就使得在到達(dá)該語句時(shí)weight不可能超過300。)
第7到10行:應(yīng)該用花括號(hào)括起來。這樣第11行就會(huì)是第6行而不是第7行的可選情況。而如果到第9行的else if由一個(gè)簡單的else替代了,就不再需要花括號(hào)了。
第13行:應(yīng)該簡化為if(height<48)。其實(shí)這一行完全可以忽略,因?yàn)榈?2行已經(jīng)作了這種測(cè)試。
第15行:這個(gè)else與第13行的if相匹配。把第13行和第14行括在花括號(hào)中可以強(qiáng)制使這個(gè)else與第6行的if相匹配?;蛘撸凑战ㄗh,簡單地刪除第13行。
下面是一個(gè)正確的版本:
a.5 > 2
b.3 + 4 > 2 && 3 < 2
c.x >= y || y > x
d.d = 5 + (6 > 2)
e.'X' > 'T' ? 10 : 5
f. x > y ? y > x : x > y
答:
a.1
b.0
c.1(如果第一個(gè)表達(dá)式為假則第二個(gè)為真,反之亦然;只需要一個(gè)為真的表達(dá)式,結(jié)果就為真。)
d.6
e.10
f.0
5、下列程序?qū)⒋蛴〕鍪裁矗?br />
hatcatOh no!
catOh no!
7、下列程序有什么錯(cuò)誤?
第5行到第7行的注釋應(yīng)該以*/結(jié)尾,或者用//來代替/*。表達(dá)式'a' <= ch >= 'z'應(yīng)該被寫成這樣:ch >= 'a' && ch <= 'z'?;蛘哂靡环N更簡單也更通用的方法:包含ctype.h文件并使用islower()。順便提一下,'a' <= ch >= 'z'在C中是合法的,只是不具有正確的意義。因?yàn)殛P(guān)系運(yùn)算符是從左到右結(jié)合的,所以這個(gè)表達(dá)式被解釋為('a' <= ch) >= 'z'。圓括號(hào)中表達(dá)式的值為1或0(真或假),然后檢查這個(gè)值來看它是否大于或等于'z'的數(shù)值編碼。0和1都不能滿足這個(gè)條件,所以整個(gè)表達(dá)式的值總是為0(假)。在第二個(gè)判斷表達(dá)式中,在第二個(gè)表達(dá)式中,||應(yīng)該為&&,盡管!(ch < 'A')是合法的,而且意義也正確,但ch >= 'A'更為簡單。'Z'后面需要有兩個(gè)結(jié)束圓括號(hào),而不是一個(gè)。再一次更簡單的方法是使用isuper()。應(yīng)該在oc++;語句前面放置一個(gè)else,否則。每輸入一個(gè)字符,它都會(huì)加1。在printf()調(diào)用中的控制表達(dá)式應(yīng)該用雙引號(hào)引起來。
下面是一個(gè)正確的版本:
無休止地打印同一行:
You are 65. Here is your gold watch.
9、當(dāng)給定下述輸入時(shí),下列程序?qū)⒋蛴〕鍪裁矗?br />
q
Step 1
Step 2
Step 3 (當(dāng)ch == 'q'時(shí),laststep: printf("Step 3\n");語句也要打印出來)
c
Step 1
g
Step 1
Step 3 (當(dāng)ch == 'g'時(shí),通過goto語句跳轉(zhuǎn)到laststep: printf("Step 3\n");,前面的語句
b
Step 1
Done!
10、重寫題目9的程序,以使它表現(xiàn)相同的行為但不使用continue或goto。
1、
1、確定哪個(gè)表達(dá)式為true,哪個(gè)為false。
a.100 > 3 && 'a' > 'c'
b.100 > 3 || 'a' > 'c'
c.!(100>3)
答:b.100 > 3 || 'a' > 'c'
c.!(100>3)
a.false
b.true
c.false
2、構(gòu)造一個(gè)表達(dá)式來表示下列條件:
a.number等于或大于90,但是小于100
b.ch不是字符q也不是字符k
c.number界于1到9之間(包括1和9),但是不等于5
d.number不在1到9之間
答:
a.number >= 90 && number < 100
b.ch != 'q' && ch != 'k'
c.number >= 1 && number <= 9 && number != 5
d.number < 1 || number > 9
3、下面程序中的關(guān)系表達(dá)式過于復(fù)雜,并有些錯(cuò)誤,請(qǐng)簡化并改正它。
#include <stdio.h>
1 int main(void)
2 {
3 int weight, height; /* weight以磅為單位,height以英寸為單位 */
4
5 scanf("%d, weight, height);
6 if(weight < 100 && height > 64)
7 if(height >= 72)
8 printf("You are very tall for your weight.\n");
9 else if(height < 72 && > 64)
10 printf("You are tall for your weight.\n");
11 else if(weight > 300 && !(weight <= 300)
12 && height < 48)
13 if(!(height >= 48))
14 printf(" You are quite short for your weight.\n");
15 else
16 printf("Your weight is ideal.\n");
17
18 return 0;
19 }
答:1 int main(void)
2 {
3 int weight, height; /* weight以磅為單位,height以英寸為單位 */
4
5 scanf("%d, weight, height);
6 if(weight < 100 && height > 64)
7 if(height >= 72)
8 printf("You are very tall for your weight.\n");
9 else if(height < 72 && > 64)
10 printf("You are tall for your weight.\n");
11 else if(weight > 300 && !(weight <= 300)
12 && height < 48)
13 if(!(height >= 48))
14 printf(" You are quite short for your weight.\n");
15 else
16 printf("Your weight is ideal.\n");
17
18 return 0;
19 }
第5行:應(yīng)該是scanf("%d %d", &weight, &height);。在scanf()中不要忘記使用&運(yùn)算符。這一行前面也應(yīng)該有提示輸入的語句。但第6行已經(jīng)保證height>64,因此,不需要任何測(cè)試,并且
else if應(yīng)該是else。
第9行:它的意思是(height<72&&height>64)。但是表達(dá)式的第一部分是不必要的,因?yàn)榧热怀绦蛞呀?jīng)到達(dá)了這個(gè)else if,那么height必然小于72。因此一個(gè)簡單的(height>64)就可以了。
第11行:條件冗余;第二個(gè)表達(dá)式(weight不是小于或等于300的)與第一個(gè)子表達(dá)式意義相同。所需要的只是一個(gè)簡單的(weight>300)。但是這里還有更多的問題。第11行屬于一個(gè)不正確的if!很明顯,這個(gè)else是與第6行中的if相匹配的。但是根據(jù)if的“最就近原則”,它會(huì)與第7行的if相匹配。(因此會(huì)在weight小于100并且height小于或等于64時(shí)到達(dá)第11行。這就使得在到達(dá)該語句時(shí)weight不可能超過300。)
第7到10行:應(yīng)該用花括號(hào)括起來。這樣第11行就會(huì)是第6行而不是第7行的可選情況。而如果到第9行的else if由一個(gè)簡單的else替代了,就不再需要花括號(hào)了。
第13行:應(yīng)該簡化為if(height<48)。其實(shí)這一行完全可以忽略,因?yàn)榈?2行已經(jīng)作了這種測(cè)試。
第15行:這個(gè)else與第13行的if相匹配。把第13行和第14行括在花括號(hào)中可以強(qiáng)制使這個(gè)else與第6行的if相匹配?;蛘撸凑战ㄗh,簡單地刪除第13行。
下面是一個(gè)正確的版本:
#include <stdio.h>
int main(void)
{
int weight, height; /* weight以磅為單位,height以英寸為單位 */
printf("Enter your weight in pounds and ");
printf("your height in inches.\n");
scanf("%d %d", &weight, &height);
if(weight < 100 && height > 64)
if(height >= 72)
printf("You are very tall for your weight.\n");
else
printf("You are tall for your weight.\n");
else if(weight > 300 && height < 48)
printf(" You are quite short for your weight.\n");
else
printf("Your weight is ideal.\n");
return 0;
}
4、下列每個(gè)表達(dá)式的數(shù)值是多少?int main(void)
{
int weight, height; /* weight以磅為單位,height以英寸為單位 */
printf("Enter your weight in pounds and ");
printf("your height in inches.\n");
scanf("%d %d", &weight, &height);
if(weight < 100 && height > 64)
if(height >= 72)
printf("You are very tall for your weight.\n");
else
printf("You are tall for your weight.\n");
else if(weight > 300 && height < 48)
printf(" You are quite short for your weight.\n");
else
printf("Your weight is ideal.\n");
return 0;
}
a.5 > 2
b.3 + 4 > 2 && 3 < 2
c.x >= y || y > x
d.d = 5 + (6 > 2)
e.'X' > 'T' ? 10 : 5
f. x > y ? y > x : x > y
答:
a.1
b.0
c.1(如果第一個(gè)表達(dá)式為假則第二個(gè)為真,反之亦然;只需要一個(gè)為真的表達(dá)式,結(jié)果就為真。)
d.6
e.10
f.0
5、下列程序?qū)⒋蛴〕鍪裁矗?br />
#include <stdio.h>
int main(void)
{
int num;
for(num = 1; num <= 11; num++)
{
if(num % 3 == 0)
putchar('$');
else
putchar('*');
putchar('#');
putchar('%');
}
putchar('\n');
return 0;
}
答:int main(void)
{
int num;
for(num = 1; num <= 11; num++)
{
if(num % 3 == 0)
putchar('$');
else
putchar('*');
putchar('#');
putchar('%');
}
putchar('\n');
return 0;
}
*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
6、下列程序?qū)⒋蛴〕鍪裁矗?/div>
fathatcatOh no!#include <stdio.h>
int main(void)
{
int i = 0;
while(i < 3)
{
switch(i++)
{
case 0: printf("fat");
case 1: printf("hat");
case 2: printf("cat");
default: printf("Oh no!");
}
putchar('\n');
}
return 0;
}
答:int main(void)
{
int i = 0;
while(i < 3)
{
switch(i++)
{
case 0: printf("fat");
case 1: printf("hat");
case 2: printf("cat");
default: printf("Oh no!");
}
putchar('\n');
}
return 0;
}
hatcatOh no!
catOh no!
7、下列程序有什么錯(cuò)誤?
1 #include <stdio.h>
2 int main(void)
3 {
4 char ch;
5 int lc = 0; /*統(tǒng)計(jì)小寫字符
6 int uc = 0; /*統(tǒng)計(jì)大寫字符
7 int oc = 0; /*統(tǒng)計(jì)其他字符
8
9 while((ch = getchar()) != '#')
10 {
11 if('a' <= ch >= 'z')
12 lc++;
13 else if(!(ch < 'A') || !(ch > 'Z')
14 uc++;
15 oc++;
16 }
17 printf("%d lowercase, %d uppercase, %d other, lc, uc, oc");
18 return 0;
19 }
答:2 int main(void)
3 {
4 char ch;
5 int lc = 0; /*統(tǒng)計(jì)小寫字符
6 int uc = 0; /*統(tǒng)計(jì)大寫字符
7 int oc = 0; /*統(tǒng)計(jì)其他字符
8
9 while((ch = getchar()) != '#')
10 {
11 if('a' <= ch >= 'z')
12 lc++;
13 else if(!(ch < 'A') || !(ch > 'Z')
14 uc++;
15 oc++;
16 }
17 printf("%d lowercase, %d uppercase, %d other, lc, uc, oc");
18 return 0;
19 }
第5行到第7行的注釋應(yīng)該以*/結(jié)尾,或者用//來代替/*。表達(dá)式'a' <= ch >= 'z'應(yīng)該被寫成這樣:ch >= 'a' && ch <= 'z'?;蛘哂靡环N更簡單也更通用的方法:包含ctype.h文件并使用islower()。順便提一下,'a' <= ch >= 'z'在C中是合法的,只是不具有正確的意義。因?yàn)殛P(guān)系運(yùn)算符是從左到右結(jié)合的,所以這個(gè)表達(dá)式被解釋為('a' <= ch) >= 'z'。圓括號(hào)中表達(dá)式的值為1或0(真或假),然后檢查這個(gè)值來看它是否大于或等于'z'的數(shù)值編碼。0和1都不能滿足這個(gè)條件,所以整個(gè)表達(dá)式的值總是為0(假)。在第二個(gè)判斷表達(dá)式中,在第二個(gè)表達(dá)式中,||應(yīng)該為&&,盡管!(ch < 'A')是合法的,而且意義也正確,但ch >= 'A'更為簡單。'Z'后面需要有兩個(gè)結(jié)束圓括號(hào),而不是一個(gè)。再一次更簡單的方法是使用isuper()。應(yīng)該在oc++;語句前面放置一個(gè)else,否則。每輸入一個(gè)字符,它都會(huì)加1。在printf()調(diào)用中的控制表達(dá)式應(yīng)該用雙引號(hào)引起來。
下面是一個(gè)正確的版本:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int lc = 0; //統(tǒng)計(jì)小寫字符
int uc = 0; //統(tǒng)計(jì)大寫字符
int oc = 0; //統(tǒng)計(jì)其他字符
while((ch = getchar()) != '#')
{
if(islower(ch))
lc++;
else if(isupper(ch))
uc++;
else
oc++;
}
printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);
return 0;
}
8、下列程序?qū)⒋蛴〕鍪裁矗?br />#include <ctype.h>
int main(void)
{
char ch;
int lc = 0; //統(tǒng)計(jì)小寫字符
int uc = 0; //統(tǒng)計(jì)大寫字符
int oc = 0; //統(tǒng)計(jì)其他字符
while((ch = getchar()) != '#')
{
if(islower(ch))
lc++;
else if(isupper(ch))
uc++;
else
oc++;
}
printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);
return 0;
}
/* retire.c*/
#include <stdio.h>
int main(void)
{
int age = 20;
while(age++ <= 65)
{
if((age % 20) == 0) /* age能被20整除嗎?*/
printf("You are %d. Here is a raise.\n", age);
if(age = 65)
printf("You are %d. Here is your gold watch.\n", age);
}
return 0;
}
答:#include <stdio.h>
int main(void)
{
int age = 20;
while(age++ <= 65)
{
if((age % 20) == 0) /* age能被20整除嗎?*/
printf("You are %d. Here is a raise.\n", age);
if(age = 65)
printf("You are %d. Here is your gold watch.\n", age);
}
return 0;
}
無休止地打印同一行:
You are 65. Here is your gold watch.
9、當(dāng)給定下述輸入時(shí),下列程序?qū)⒋蛴〕鍪裁矗?br />
q
c
g
b
#include <stdio.h>
int main(void)
{
char ch;
while((ch = getchar()) != '#')
{
if(ch == '\n')
continue;
printf("Step 1\n");
if(ch == 'c')
continue;
else if(ch == 'b')
break;
else if(ch == 'g')
goto laststep;
laststep: printf("Step 3\n");
}
printf("Done!\n");
return 0;
}
答:c
g
b
#include <stdio.h>
int main(void)
{
char ch;
while((ch = getchar()) != '#')
{
if(ch == '\n')
continue;
printf("Step 1\n");
if(ch == 'c')
continue;
else if(ch == 'b')
break;
else if(ch == 'g')
goto laststep;
printf("Step 2\n")
;laststep: printf("Step 3\n");
}
printf("Done!\n");
return 0;
}
q
Step 1
Step 2
Step 3 (當(dāng)ch == 'q'時(shí),laststep: printf("Step 3\n");語句也要打印出來)
c
Step 1
g
Step 1
Step 3 (當(dāng)ch == 'g'時(shí),通過goto語句跳轉(zhuǎn)到laststep: printf("Step 3\n");,前面的語句
printf("Step 2\n")
;就不要打印出來了)b
Step 1
Done!
10、重寫題目9的程序,以使它表現(xiàn)相同的行為但不使用continue或goto。
#include <stdio.h>
int main(void)
{
char ch;
while((ch = getchar()) != '#')
{
if(ch != '\n')
{
printf("Step 1\n");
if(ch != 'c')
{
if(ch == 'b')
break;
if(ch != 'g')
printf("Step 2\n");
printf("Step 3\n");
}
}
}
printf("Done!\n");
return 0;
}
編程練習(xí)int main(void)
{
char ch;
while((ch = getchar()) != '#')
{
if(ch != '\n')
{
printf("Step 1\n");
if(ch != 'c')
{
if(ch == 'b')
break;
if(ch != 'g')
printf("Step 2\n");
printf("Step 3\n");
}
}
}
printf("Done!\n");
return 0;
}
1、
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int s_ct = 0; //統(tǒng)計(jì)空格數(shù)
int l_ct = 0; //統(tǒng)計(jì)換行數(shù)
int o_ct = 0; //統(tǒng)計(jì)除空格和換行符之外的其他字符
char ch;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
if(ch == ' ')
s_ct++;
if(ch == '\n')
l_ct++;
if(ch != ' ' && ch != '\n')
o_ct++;
}
printf("The number of spaces,lines,others: %d, %d, %d", s_ct, l_ct, o_ct);
return 0;
}
2、#include <ctype.h>
int main(void)
{
int s_ct = 0; //統(tǒng)計(jì)空格數(shù)
int l_ct = 0; //統(tǒng)計(jì)換行數(shù)
int o_ct = 0; //統(tǒng)計(jì)除空格和換行符之外的其他字符
char ch;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
if(ch == ' ')
s_ct++;
if(ch == '\n')
l_ct++;
if(ch != ' ' && ch != '\n')
o_ct++;
}
printf("The number of spaces,lines,others: %d, %d, %d", s_ct, l_ct, o_ct);
return 0;
}
#include <stdio.h>
int main(void)
{
char ch;
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#') // 當(dāng)讀取\n時(shí),又是一種什么情況,比如說要從幾行來鍵入???
{
count++;
printf("%c/%d ", ch, ch);
if(count % 8 == 0)
printf("\n");
}
//printf("Each character and its ASCII code value:");
return 0;
}
3、int main(void)
{
char ch;
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#') // 當(dāng)讀取\n時(shí),又是一種什么情況,比如說要從幾行來鍵入???
{
count++;
printf("%c/%d ", ch, ch);
if(count % 8 == 0)
printf("\n");
}
//printf("Each character and its ASCII code value:");
return 0;
}
#include <stdio.h>
int main(void)
{
int even_count = 0;
float even_sum = 0;
int odd_count = 0;
float odd_sum = 0;
int number;
printf("Please enter text to be analyzed(# to terminate): \n");
while(scanf("%d", &number) == 1)
{
if(number == 0)
break;
if(number % 2 == 0)
{
even_count++;
even_sum += number;
}
else
{
odd_count++;
odd_sum += number;
}
}
printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
return 0;
}
4、int main(void)
{
int even_count = 0;
float even_sum = 0;
int odd_count = 0;
float odd_sum = 0;
int number;
printf("Please enter text to be analyzed(# to terminate): \n");
while(scanf("%d", &number) == 1)
{
if(number == 0)
break;
if(number % 2 == 0)
{
even_count++;
even_sum += number;
}
else
{
odd_count++;
odd_sum += number;
}
}
printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
return 0;
}
#include <stdio.h>
int main(void)
{
char ch;
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
if(ch == '!')
{
count++;
printf("!!");
}
else if(ch == '.')
{
count++;
putchar('!');
}
else
putchar(ch);
}
printf("\nNumber of alternatives: %d", count);
return 0;
}
5、int main(void)
{
char ch;
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
if(ch == '!')
{
count++;
printf("!!");
}
else if(ch == '.')
{
count++;
putchar('!');
}
else
putchar(ch);
}
printf("\nNumber of alternatives: %d", count);
return 0;
}
#include <stdio.h>
int main(void)
{
int even_count = 0;
float even_sum = 0;
int odd_count = 0;
float odd_sum = 0;
int number;
printf("Please enter text to be analyzed(# to terminate): \n");
while(scanf("%d", &number) == 1)
{
// 此判斷如何才能弄到switch里去呢???
if(number == 0)
break;
switch(number % 2)
{
case 0: even_count++;
even_sum += number;
break;
case 1: odd_count++;
odd_sum += number;
break;
}
}
printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
return 0;
}
6、int main(void)
{
int even_count = 0;
float even_sum = 0;
int odd_count = 0;
float odd_sum = 0;
int number;
printf("Please enter text to be analyzed(# to terminate): \n");
while(scanf("%d", &number) == 1)
{
// 此判斷如何才能弄到switch里去呢???
if(number == 0)
break;
switch(number % 2)
{
case 0: even_count++;
even_sum += number;
break;
case 1: odd_count++;
odd_sum += number;
break;
}
}
printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
char prev = 0; // 前一個(gè)字符
char current = 0, ch; // 當(dāng)前字符
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
prev = current; // 每次都要更新前一個(gè)字符,我怎么就沒想到呢???
current = ch;
if(prev == 'e' && current == 'i')
count++;
}
printf("The number of ei: %d\n", count);
return 0;
}
7、#include <stdbool.h>
int main(void)
{
char prev = 0; // 前一個(gè)字符
char current = 0, ch; // 當(dāng)前字符
int count = 0;
printf("Please enter text to be analyzed(# to terminate): \n");
while((ch = getchar()) != '#')
{
prev = current; // 每次都要更新前一個(gè)字符,我怎么就沒想到呢???
current = ch;
if(prev == 'e' && current == 'i')
count++;
}
printf("The number of ei: %d\n", count);
return 0;
}
#include <stdio.h>
#define BASE_PAY 10.00
#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
int hour;
double total, tax, net_pay;
printf("Please enter the hour used.\n");
scanf("%d", &hour);
if(hour <= WORK_OVERTIME)
{
total = hour * BASE_PAY;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = BASE_PAY * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * BASE_PAY;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
return 0;
}
8、#define BASE_PAY 10.00
#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
int hour;
double total, tax, net_pay;
printf("Please enter the hour used.\n");
scanf("%d", &hour);
if(hour <= WORK_OVERTIME)
{
total = hour * BASE_PAY;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = BASE_PAY * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * BASE_PAY;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
return 0;
}
#include <stdio.h>
#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
int hour, choise;
double total, tax, net_pay;
double base_pay; // 基本工資等級(jí)不能用#define來定義了,因?yàn)樗S著程序而改變了,書上真是胡說八道
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t2) $9.33/hr\n");
printf("3) $10.00/hr\t4) $11.20/hr\n");
printf("5) quit\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while(scanf("%d", &choise) == 1)
{
if(choise == 5)
break;
else
switch(choise)
{
case 1:
base_pay = 8.15;
break; // break只是導(dǎo)致程序脫離switch語句,跳到switch之后的下一條語句?。?!
case 2:
base_pay = 9.33;
break;
case 3:
base_pay = 10.00;
break;
case 4:
base_pay = 11.20;
break;
default:
printf("default choices to the user: 1 2 3 4 5\n");
printf("Please enter your choise: ");
continue; // continue導(dǎo)致程序跳過該循環(huán)的其余部分,其中包括switch的其余部分!!!
}
printf("Please enter the hour used: ");
scanf("%d", &hour);
if(hour <= WORK_OVERTIME)
{
total = hour * base_pay;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
printf("Please enter your choise: ");
}
return 0;
}
9、(接受一個(gè)整數(shù)輸入,然后顯示所有大于或等于該數(shù)的素?cái)?shù),我覺得這是第7章編程練習(xí)里最難的了?。?!借鑒創(chuàng)十三的做法終于做出來了!??!)#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
int hour, choise;
double total, tax, net_pay;
double base_pay; // 基本工資等級(jí)不能用#define來定義了,因?yàn)樗S著程序而改變了,書上真是胡說八道
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t2) $9.33/hr\n");
printf("3) $10.00/hr\t4) $11.20/hr\n");
printf("5) quit\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while(scanf("%d", &choise) == 1)
{
if(choise == 5)
break;
else
switch(choise)
{
case 1:
base_pay = 8.15;
break; // break只是導(dǎo)致程序脫離switch語句,跳到switch之后的下一條語句?。?!
case 2:
base_pay = 9.33;
break;
case 3:
base_pay = 10.00;
break;
case 4:
base_pay = 11.20;
break;
default:
printf("default choices to the user: 1 2 3 4 5\n");
printf("Please enter your choise: ");
continue; // continue導(dǎo)致程序跳過該循環(huán)的其余部分,其中包括switch的其余部分!!!
}
printf("Please enter the hour used: ");
scanf("%d", &hour);
if(hour <= WORK_OVERTIME)
{
total = hour * base_pay;
if (total <= BREAK1)
{
tax = total * RATE1;
net_pay = total - tax;
}
else
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
}
else
{
total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
if(total <= BREAK2)
{
tax = BASE1 + (total - BREAK1) * RATE2;
net_pay = total - tax;
}
else
{
tax = BASE2 + (total - BREAK2) * RATE3;
net_pay = total - tax;
}
}
printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
printf("Please enter your choise: ");
}
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool isPrime(int a); // 判斷一個(gè)數(shù)a是否為素?cái)?shù)
void printPrime(int a); // 輸出所有小于或等于a的素?cái)?shù)
int main(void)
{
int num;
printf("Please input a number: ");
scanf("%d", &num);
printPrime(num);
return 0;
}
bool isPrime(int a)
{
int i;
int j = sqrt(a);
for(i = 2; i <= j; i++)
{
if(a % i == 0)
return false;
}
return true;
}
void printPrime(int a)
{
if(a == 1)
printf("1是一個(gè)無效的輸入數(shù)?。?!");
else
while(a != 1)
{
if(isPrime(a))
printf("%d ", a);
a--;
}
}
10、(與練習(xí)8類似)#include <stdbool.h>
#include <math.h>
bool isPrime(int a); // 判斷一個(gè)數(shù)a是否為素?cái)?shù)
void printPrime(int a); // 輸出所有小于或等于a的素?cái)?shù)
int main(void)
{
int num;
printf("Please input a number: ");
scanf("%d", &num);
printPrime(num);
return 0;
}
bool isPrime(int a)
{
int i;
int j = sqrt(a);
for(i = 2; i <= j; i++)
{
if(a % i == 0)
return false;
}
return true;
}
void printPrime(int a)
{
if(a == 1)
printf("1是一個(gè)無效的輸入數(shù)?。?!");
else
while(a != 1)
{
if(isPrime(a))
printf("%d ", a);
a--;
}
}
#include <stdio.h>
#define RATE1 0.15
#define RATE2 0.28
int main(void)
{
int choise, break_point;
double income, tax;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired tax type or action:\n");
printf("1) 單身\t2) 戶主\n");
printf("3) 已婚,共有\(zhòng)t4) 已婚,離異\n");
printf("5) 退出\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while(scanf("%d", &choise) == 1)
{
if(choise == 5)
break;
else
switch(choise)
{
case 1:
break_point = 17850;
break;
case 2:
break_point = 23900;
break;
case 3:
break_point = 29750;
break;
case 4:
break_point = 14875;
break;
default:
printf("default choices to the user: 1 2 3 4 5\n");
printf("Please enter your choise: ");
continue;
}
printf("Please enter your income: ");
scanf("%lf", &income);
if(income <= break_point)
tax = income * RATE1;
else
tax = break_point * RATE1 + (income - break_point) * RATE2;
printf("your tax is %.2f\n", tax);
printf("Please enter your choise: ");
}
return 0;
}
11、#define RATE1 0.15
#define RATE2 0.28
int main(void)
{
int choise, break_point;
double income, tax;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired tax type or action:\n");
printf("1) 單身\t2) 戶主\n");
printf("3) 已婚,共有\(zhòng)t4) 已婚,離異\n");
printf("5) 退出\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while(scanf("%d", &choise) == 1)
{
if(choise == 5)
break;
else
switch(choise)
{
case 1:
break_point = 17850;
break;
case 2:
break_point = 23900;
break;
case 3:
break_point = 29750;
break;
case 4:
break_point = 14875;
break;
default:
printf("default choices to the user: 1 2 3 4 5\n");
printf("Please enter your choise: ");
continue;
}
printf("Please enter your income: ");
scanf("%lf", &income);
if(income <= break_point)
tax = income * RATE1;
else
tax = break_point * RATE1 + (income - break_point) * RATE2;
printf("your tax is %.2f\n", tax);
printf("Please enter your choise: ");
}
return 0;
}
#include <stdio.h>
#define SALE1 1.25
#define SALE2 0.65
#define SALE3 0.89
#define DISCOUNT_MONEY 100
#define DISCOUNT 0.05
#define WEIGHT1 5
#define WEIGHT2 20
#define TRANSPORTATION_COST1 3.50
#define TRANSPORTATION_COST2 10.00
#define BASE 8
#define COST3 0.1
int main(void)
{
int a = 0, b = 0, c = 0;
int total_pounds;
char choise;
double total, discount, transportation_cost;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired action:\n");
printf("a) 輸入所需朝鮮薊的磅數(shù)\n");
printf("b) 輸入所需甜菜的磅數(shù)\n");
printf("c) 輸入所需胡蘿卜的磅數(shù)\n");
printf("q) 退出訂購過程\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while((choise = getchar()) != 'q')
{
switch(choise)
{
case 'a':
printf("輸入所需朝鮮薊的磅數(shù): ");
scanf("%d", &a);
break;
case 'b':
printf("輸入所需甜菜的磅數(shù): ");
scanf("%d", &b);
break;
case 'c':
printf("輸入所需胡蘿卜的磅數(shù): ");
scanf("%d", &c);
break;
default:
printf("輸入有誤,您有合適的選項(xiàng):a b c q\n");
break;
}
while(getchar() != '\n')
continue;
printf("Please enter your choise: ");
}
total = SALE1 * a + SALE2 * b + SALE1 * c;
total_pounds = a + b + c;
if(total < DISCOUNT_MONEY)
discount = 0;
else
discount = total * DISCOUNT;
if(total_pounds <= WEIGHT1)
transportation_cost = TRANSPORTATION_COST1;
else if(total_pounds > 5 && total_pounds < 20)
transportation_cost = TRANSPORTATION_COST2;
else
transportation_cost = BASE + COST3 * total_pounds;
printf("所需朝鮮薊的磅數(shù): %d\n", a);
printf("所需甜菜的磅數(shù): %d\n", b);
printf("所需胡蘿卜的磅數(shù): %d\n", c);
printf("訂購的總磅數(shù):%d\n", total_pounds);
printf("訂購的朝鮮薊蔬菜的費(fèi)用:%.2f\n", a * SALE1);
printf("訂購的甜菜蔬菜的費(fèi)用:%.2f\n", b * SALE2);
printf("訂購的胡蘿卜蔬菜的費(fèi)用:%.2f\n", c * SALE3);
printf("訂單的運(yùn)輸和裝卸費(fèi)用:%.2f\n", transportation_cost);
printf("訂單的折扣:%.2f\n", discount);
printf("訂單的總費(fèi)用(包括運(yùn)輸費(fèi)用,而且還要減去折扣):%.2f\n", total - discount + transportation_cost);
return 0;
}
#define SALE1 1.25
#define SALE2 0.65
#define SALE3 0.89
#define DISCOUNT_MONEY 100
#define DISCOUNT 0.05
#define WEIGHT1 5
#define WEIGHT2 20
#define TRANSPORTATION_COST1 3.50
#define TRANSPORTATION_COST2 10.00
#define BASE 8
#define COST3 0.1
int main(void)
{
int a = 0, b = 0, c = 0;
int total_pounds;
char choise;
double total, discount, transportation_cost;
printf("*****************************************************************\n");
printf("Enter number corresponding to the desired action:\n");
printf("a) 輸入所需朝鮮薊的磅數(shù)\n");
printf("b) 輸入所需甜菜的磅數(shù)\n");
printf("c) 輸入所需胡蘿卜的磅數(shù)\n");
printf("q) 退出訂購過程\n");
printf("*****************************************************************\n");
printf("Please enter your choise: ");
while((choise = getchar()) != 'q')
{
switch(choise)
{
case 'a':
printf("輸入所需朝鮮薊的磅數(shù): ");
scanf("%d", &a);
break;
case 'b':
printf("輸入所需甜菜的磅數(shù): ");
scanf("%d", &b);
break;
case 'c':
printf("輸入所需胡蘿卜的磅數(shù): ");
scanf("%d", &c);
break;
default:
printf("輸入有誤,您有合適的選項(xiàng):a b c q\n");
break;
}
while(getchar() != '\n')
continue;
printf("Please enter your choise: ");
}
total = SALE1 * a + SALE2 * b + SALE1 * c;
total_pounds = a + b + c;
if(total < DISCOUNT_MONEY)
discount = 0;
else
discount = total * DISCOUNT;
if(total_pounds <= WEIGHT1)
transportation_cost = TRANSPORTATION_COST1;
else if(total_pounds > 5 && total_pounds < 20)
transportation_cost = TRANSPORTATION_COST2;
else
transportation_cost = BASE + COST3 * total_pounds;
printf("所需朝鮮薊的磅數(shù): %d\n", a);
printf("所需甜菜的磅數(shù): %d\n", b);
printf("所需胡蘿卜的磅數(shù): %d\n", c);
printf("訂購的總磅數(shù):%d\n", total_pounds);
printf("訂購的朝鮮薊蔬菜的費(fèi)用:%.2f\n", a * SALE1);
printf("訂購的甜菜蔬菜的費(fèi)用:%.2f\n", b * SALE2);
printf("訂購的胡蘿卜蔬菜的費(fèi)用:%.2f\n", c * SALE3);
printf("訂單的運(yùn)輸和裝卸費(fèi)用:%.2f\n", transportation_cost);
printf("訂單的折扣:%.2f\n", discount);
printf("訂單的總費(fèi)用(包括運(yùn)輸費(fèi)用,而且還要減去折扣):%.2f\n", total - discount + transportation_cost);
return 0;
}
只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。 | ||
![]() |
||
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
|
||
相關(guān)文章:
|
||