Title: Chap 3 ????
1Chap 3 ????
- 3.1 ?????????????????
- 3.2 ?????????????
- 3.3 ????????
23.1 ?????????????????
- ?3-1 ??10???,??????????????????????
- 3.1.1 ????
- 3.1.2 ????
- 3.1.3 ??????????
- 3.1.4 ????
- 3.1.5 else-if ??
33.1.1 ????
- include ltstdio.hgt
- int main(void)
- int digit, i, letter, other
- char ch
- digit letter other 0
- printf(Enter 10 characters ")
- for(i 1 i lt 10 i)
- ch getchar() / ?????????,????? ch
/ - if((ch gt 'a' ch lt 'z' ) ( ch gt
'A' ch lt 'Z')) - letter
- else if(ch gt '0' ch lt '9') /
??ch????? / - digit
- else
- other
-
- printf("letterd,digitd,otherd\n",letter,d
igit,other) - return 0
input 10 characters Reold 123? letter5,
digit3, other2
????????? ???????
43.1.2 ????
- char ch
- (ch gt 'a' ch lt 'z' ) ( ch gt 'A' ch lt
'Z') - (ch gt '0' ch lt '9')
- ?????
- ????ch
- ???? 'a'
5????
- 'a' 'z' 'A' 'Z' '0' '9' ' ' '\n'
- ASCII????????????
- ??????????( ASCII ?)
- '0'-'9'
- 'A'-'Z'
- 'a'-'z'
???? 1 ????? '1'
6????
- char ch
- ??????ch,??????????
- ch 'A'
73.1.3 ???????????
- ??????getchar
- ??????
- char ch
- ch getchar( )
- ??????putchar
- ??????
- putchar(????)
char ch ch getchar() putchar (ch) putchar
(?')
?????????
8??scanf?printf??????
- double value1, value2
- char operator
- printf(Type in an expression )
- scanf(lfclf, value1, operator, value2)
- printf(.2f c .2f, value1, operator, value2)
Type in an expression 10.05.61 10.00 5.61
93.1.4 ????
- (ch gt 'a' ch lt 'z' ) ( ch gt 'A' ch lt
'Z') - ch gt '0' ch lt '9
- ????????(true/false)
- ?????
103??????
X Y
X Y
X
Y
X
Y
! X
X
11????????
- ??? ??? ??? !
- (xgt1)(ygt1) (xgt1)(ygt1) (xgt1)
- !(xgt1) ? xlt1
12????????
- ??? ??? ??? !
-
- a b ab ab !a
- ? ? ? ? ?
- ? ? ? ? ?
- ? ? ? ? ?
- ? ? ? ? ?
13?????
- ??????????????????????????
- (ch gt 'a') (ch lt 'z')
- ??ch ?????????
- ?
- ch gt 'a' ch lt 'z'
- (ch gt 'a' ch lt 'z') ( ch gt 'A' ch lt
'Z') - ??ch ???????
14?????
- ?3-2 ?????????C????
- ch ???????
- ch ' ' ch '\n'
- number???
- number 2 0
- year ???,? year ?? 4 ?????? 100 ??,? year ?? 400
?? - (year 4 0 year 100 ! 0) (year
400 0)
153.1.5 else if ??
- if((ch gt 'a' ch lt 'z' )( ch gt 'A' ch
lt 'Z')) - letter
- else if(ch gt '0' ch lt '9')
- digit
- else
- other
- ??????
16else if ??
if((ch gt 'a' ch lt 'z' )( ch gt 'A' ch
lt 'Z')) letter else if(ch gt '0' ch
lt '9') digit else other
- if (???1) ??1
- else if (???2) ??2
-
- else if (???n-1) ??n-1
- else ??n
17??????
18???-??????
- include ltstdio.hgt
- int main(void)
-
- double x, y
- printf("Enter x")
- scanf("lf", x)
- if (x lt 0)
- y 0
-
- else if (x lt 15)
- y 4 x / 3
-
- else
- y 2.5 x - 10.5
-
- printf("f(.2f) .2f\n", x, y)
- return 0
193.2 ?????????????
- ?3-4 ?????????????
- 3.2.1 ????
- 3.2.2 switch??
203.2.1 ????
- ?????????4???,??(crisps)????(popcorn)????(chocolat
e)???(cola),???????3.0?2.5?4.0?3.5????????????,???
??????????,???????5??,????????5??,???????????????
?14,???????????0,??????????,?????0? - 1 Select crisps
- 2 Select popcorn
- 3 Select chocolate
- 4 Select cola
- 0 Exit
211 Select crisps 2 Select popcorn 3 Select
chocolate 4 Select cola 0 Exit Enter choice
1 price 3.0 1 Select crisps 2 Select
popcorn 3 Select chocolate 4 Select cola
0 Exit Enter choice 7 price 0.01 Select
crisps 2 Select popcorn 3 Select
chocolate 4 Select cola 0 Exit Enter choice
0 Thanks
- include ltstdio.hgt
- Int main(void)
- int choice, i double price
- for( i 1 i lt 5 i)
- printf("1 Select crisps \n")
- printf("2 Select popcorn \n")
- printf("3 Select chocolate \n")
- printf("4 Select cola \n")
- printf("0 exit \n")
- printf("Enter choice ")
- scanf("d", choice)
- if(choice 0) break
- switch (choice)
- case 1 price3.0 break
- case 2 price2.5 break
- case 3 price4.0 break
- case 4 price3.5 break
- default price0.0 break
-
223.2.2 switch??
- ?????????,3???
- 1??switch????????????break??
- switch(???)
- case ?????1???1 break
- case ?????2???2 break
- ....
- case ?????n???n break
- default ???n1 break
23switch (choice) case 1 price3.0 break
case 2 price2.5 break case 3
price4.0 break case 4 price3.5 break
default price0.0 break
- switch(???)
- case ?????1???1 break
- case ?????2???2 break
- ....
- case ?????n???n break
- default ???n1 break
?else-if ?????
24???????
- ?3-5 ?????????? ??? ???????????,???????
- ??
- ??3.14.8
- ??7.9
25???
- include ltstdio.hgt
- int main(void)
- char operator double value1, value2
- printf("Type in an expression ")
- scanf("lfclf", value1, operator,
value2) - switch(operator)
- case ''
- printf(".2f\n", value1value2)
- break
- case '-'
- printf(".2f\n", value1-value2)
- break
- case ''
- printf(".2f\n", value1value2)
- break
- case '/'
- printf(".2f\n", value1/value2)
- break
- default
Type in an expression 3.14.8 7.9
262??switch????break
- switch(???)
- case ?????1???1
- case ?????2???2
- ....
- case ?????n???n
- default ???n1
27- switch(???)
- case ?????1???1
- case ?????2???2
- ....
- case ?????n???n
- default ???n1
switch (choice) case 1 price3.0 case
2 price2.5 case 3 price4.0 case 4
price3.5 default price0.0
price?
283??switch?????????break
- ?3-6 ??10???,????????????????????????
?3-1 ??10???,??????????????????????
29- int main(void)
- int blank, digit, i, other char ch
- blank digit other 0
- printf("Enter 10 characters ")
- for(i 1 i lt 10 i)
- ch getchar()
- switch (ch)
- case ' '
- case '\n'
- blank
- break
- case '0' case '1' case '2' case
'3' case '4' - case '5' case '6' case '7' case
'8' case '9' - digit
- break
- default
- other
- break
-
Enter 10 characters Reold 123? blank1, digit3,
other6
303.3 ????????
- ???????????????????
- ?????(if ? switch)????
- 3.3.1 ????????? if ??
- 3.3.2 ????????? if ???switch??
313.3.1 ????????? if ??
if (???) ??1
????
32????????
number 2 0
33???-????????
- include ltstdio.hgt
- int main(void)
- int number
- printf("Enter a number ")
- scanf("d", number)
- if(number 2 0)
- printf("Tne number is even. \n")
-
- else
- printf("Tne number is odd. \n")
-
- return 0
34????
?number lt 0?,number - number ?number gt
0?,number ?
35???-????
- include ltstdio.hgt
- int main(void)
- int number
- printf("Enter a number ")
- scanf("d", number)
- if(number lt 0)
- number -number
-
- printf("The absolute value is d.\n",
number) - return 0
Enter a number 10 The absolute value is 10.
Enter a number -300 The absolute value is 300.
363.3.2 ????????? if ???switch??
- ??????????
- ????
- ??? if ??
- else if ??
- ??? if - else??
- switch??
37??? if ??
if ??
if ??
- else if ??
- ??? if - else??
38else if ??
- if (???1) ??1
- else if(???2) ??2
-
- else if(???n-1) ??n-1
- else ??n
n?????n-1???
39??? if else ??
- if(???1)
- if(???2) ??1
- else ??2
- else
- if(???3) ??3
- else ??4
402???if?????
- if (x lt 1) y x 1
- else if (x lt 2) y x 2
- else y x 3
if (x lt 2) if (x lt 1) y x 1 else
y x 2 else y x 3
41else ? if ???
- if(???1)
- if(???2) ??1
-
- else
- if(???3) ??3
- else ??4
- if(???1)
- if(???2) ??1
- else ??2
- else
- if(???3) ??3
- else ??4
else ???????????? else ???? if ??
- if(???1)
- if(???2) ??1
- else
- if(???3) ??3
- else ??4
42??else ? if ???
- ?3-10 ???? if ??,? else ??1? if ???
- if (x lt 2)
- if (x lt 1) y x 1
- else y x 2
??????????
if (x lt 2) if (x lt 1) y x 1 else y
x 2
if (x lt 2) if (x lt 1) y x 1
else else y x 2
43????
- ????????????????
- switch ???? break ??????
- ?????????????????????????????
- ????????????????