Chap 3 ???? - PowerPoint PPT Presentation

About This Presentation
Title:

Chap 3 ????

Description:

Title: chap3 Author: yanhui Last modified by: xinguoliu Created Date: 2/15/1998 12:55:12 PM Document presentation format: (4:3) – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 44
Provided by: yanh2
Category:
Tags: chap | chap3

less

Transcript and Presenter's Notes

Title: Chap 3 ????


1
Chap 3 ????
  • 3.1 ?????????????????
  • 3.2 ?????????????
  • 3.3 ????????

2
3.1 ?????????????????
  • ?3-1 ??10???,??????????????????????
  • 3.1.1 ????
  • 3.1.2 ????
  • 3.1.3 ??????????
  • 3.1.4 ????
  • 3.1.5 else-if ??

3
3.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
????????? ???????
4
3.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'

7
3.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
9
3.1.4 ????
  • (ch gt 'a' ch lt 'z' ) ( ch gt 'A' ch lt
    'Z')
  • ch gt '0' ch lt '9
  • ????????(true/false)
  • ?????

10
3??????
  • ???
  • ???
  • ??? !

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)

15
3.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
  • ??????

16
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
  • if (???1) ??1
  • else if (???2) ??2
  • else if (???n-1) ??n-1
  • else ??n

17
??????
  • ?3-3 ??????

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

19
3.2 ?????????????
  • ?3-4 ?????????????
  • 3.2.1 ????
  • 3.2.2 switch??

20
3.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

21
1 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

22
3.2.2 switch??
  • ?????????,3???
  • 1??switch????????????break??
  • switch(???)
  • case ?????1???1 break
  • case ?????2???2 break
  • ....
  • case ?????n???n break
  • default ???n1 break

23
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
  • 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
26
2??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?
28
3??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
30
3.3 ????????
  • ???????????????????
  • ?????(if ? switch)????
  • 3.3.1 ????????? if ??
  • 3.3.2 ????????? if ???switch??

31
3.3.1 ????????? if ??
  • if (???)
  • ??1
  • else
  • ??2

if (???) ??1
????
32
????????
  • ?3-7 ??1???,????????????

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
????
  • ?3-8 ??1???,??????? ?

?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.
36
3.3.2 ????????? if ???switch??
  • ??????????
  • ????
  • ??? if ??
  • else if ??
  • ??? if - else??
  • switch??

37
??? if ??
  • if (???)
  • ??1
  • else
  • ??2

if ??
if ??
  • else if ??
  • ??? if - else??

38
else 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

40
2???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
41
else ? 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 ??????
  • ?????????????????????????????
  • ????????????????
Write a Comment
User Comments (0)
About PowerShow.com