The Lecture for Turbo C
David Van Schumacher BUPT-QM 07B05 rida!" December #$" #007
Section %
STRINGS FUNCTIONS
undamenta& function'
(n)ut
'canf *etchar *et'
+ut)ut
)rintf )utchar )ut'
,rammar
'canf-.in)ut format/"&variety name01 *et'-variet! name01 variet! name 2 *etchar-01
3hat4' 'trin* in turbo C
(t4' 5+T a 'trin* an!more6 (t i' 'eemed a' a 'erie' of character'" 7hich i' a character arra!6 The number of the character arra! i' 8u't the 'ame a' the number of &etter' of the 7ord or 'entence )&u' one6 The one more e&ement i' .90/ 7hich i' )rinted a' .9n/ b! *et' and )rinted a' ./-5ULL0 b! other 7ord'6
Librar! unction' :'trin*6h;
'trcat-'trin*<"'trin*#0
%))end' 'trin*# to the end of 'trin*< U'a*e= Create the name of data fi&e6
'tr&en-'trin*0
>eturn' the &en*th of 'trin*6 Doe'n4t inc&ude the .90/????????? U'a*e= Determine 7hether a in)ut data i' va&id for !our )ro*ram6
Librar! unction' :'trin*6h;
'trc)!-'trin*<"'trin*#0
Co)ie' 'trin*# to 'trin*<" inc&udin* the .90/
'trnc)!-'trin*<"'trin*#"n0
Co)ie' at mo't n character' of 'trin*# to 'trin*<6 (f 'trin*# ha' fe7er than n character'" it )ad' 'trin*< 7ith .90/'6 U'a*e= U'ed for Create a 'hort fi&e name if a 'trin* i' too &on*6
Librar! unction' :'trin*6h;
'trcm)-'trin*<"'trin*#0
Com)are' 'trin*< to 'trin*#6 >eturn' a ne*ative inte*er if 'trin*<:'trin*#6 0 if 'trin*<22'trin*#6 )o'itive inte*er if 'trin*<;'trin*#6
'trncm)-'trin*<"'trin*#"n0
Com)are' the fir't n &etter' of 'trin*< and 'trin*# return the va&ue a' function .'trcm)/6
U'a*e= 3e can u'e it for com)arin* if !our )ro*ram 7or@ )ro)er&! 7hen !ou ca&& 'ome data fi&e6
Librar! unction' :ct!)e6h;
i'a&)ha -determine 7hether i' it a &etter0 i'u))er -determine 7hether i' it u))erca'e0 i'&o7er -determine 7hether i' it &o7erca'e0 i'di*it -determine 7hether i' it a number0 i')unct -determine 7hether i' it a )unctuation0 tou))er -chan*e &o7erca'e to u))erca'e0
U'a*e
To ma@e !our name of data fi&e more c&ear&!6 %nd 7e can u'e it for determine 7hether a in)ut in va&id6
Librar! unction' :'td&ib6h;
atoi
Convert' an %SC(( 'trin* to an inte*er6 Conver'ion 'to)' at the fir't noninte*er character6
atof
Convert' an %SC(( 'trin* to a doub&e)reci'ion number6 Conver'ion 'to)' at the fir't character that cannot be inter)reted a' a doub&e6
itoa
Convert' an inte*er to an %SC(( 'trin*6 ormat= itoa-number"'trin*"n0
Section B
DEALING WITH STRINGS
ABam)&e
3rite a )ro*ram that )rom)t' the u'er to t!)e in four character 'trin*'" )&ace' the'e in an arra! of 'trin*'" and then )rint' out= -e6*6 ( am Peter Pan0 #6The four 'trin*' in rever'e order6 -e6*6 Pan Peter am (0 C6The four 'trin*' in the ori*ina& order" but 7ith each 'trin* bac@7ard'6 -e6*6 ( ma reteP naP0 D6The four 'trin*' in rever'e order 7ith
7a! E
''canf P&ea'e 'ee )a*e C7# of the boo@ C6Dec&are a character arra! D6,rammar= ''canf-character arra! name" .format/" varietie' to 'tore the inte*er of the character0
So&ution E
char x[40],a[4][10]; in i,!,"[4]; #rin $%&'n'n()*a+* in#, $-,r .-r/+0&1; 2* +%x1; ++can$%x,&3+ 3+ 3+ 3+&, a[0],a[1],a[4],a[5]1; $-r%i65;i781;i881 #rin $%&3+ &,a[i]1;
So&ution E
$-r%i60;i:4;i;;1 "[i]6+ r)*n%a[i]181; $-r%i60;i:4;i;;1 < $-r%!6"[i];!760;!881 #rin $%&3c&,a[i][!]1;#rin $%& &1; = #rin $%&'n&1; $-r%i65;i760;i881 < $-r%!6"[i];!760;!881 #rin $%&3c&,a[i][!]1;#rin $%& &1; =
3%F G
Save a' a 'trin*6 #6U'e ,et' to receive the in)ut and chec@ 7hether i' it va&id for !our )ro*ram6 C6(f that i' va&id" do the neBt 'te)6
So&ution G
int i"81 char 'trHDIH<<I1 )rintf-JP&ea'e in'ert four 'trin*' =9nJ01 for-i201i:D1iKK0 'canf-JL'J"M'trHiI01 )rintf-JThe four 'trin*' !ou have 8u't in)ut are=9nJ01 for-i201i:D1iKK0 )rintf-J L'J"'trHiI01 )rintf-J9nJ01
So&ution G
for-i2C1i;201i--0 )rintf-J L'J"'trHiI01 )rintf-J9nJ01 for-i201i:D1iKK0 N for-82O18;2018--0 N
for-i2C1i;201i--0 N for-82O18;2018--0 N
)rintf-JLcJ"'trHiIH8I0 1 P )rintf-JLcJ"'trHiIH8I P 01 )rintf-J9nJ01 P return 01
3a! Q
U'e the 'entence 7hi&e--c 2 *etchar-00 && .condition'/0
So&ution Q
R 3e omit it here6 Becau'e it i'n4t *ood at dea&in* 7ith 'trin*'
SummariSe
U'e the method of coordinate can dea& 7ith the 'trin*' character b! character6
Section C
IN(UT >ALIDATION
ABerci'e
3rite a C )ro*ram that read' 'evera& )o'itive number' and u'e' the function roundTtoTneare't to round each of the'e number' to the neare't inte*er6 The )ro*ram 'hou&d )rint both the ori*ina& number and the rounded number6
%n'7er
int main-0 N f&oat a1 void roundTtoTneare't-f&oat01 )rintf-JP&ea'e in)ut a rea& number=J01 'canf-JLfJ"Ma01 roundTtoTneare't-a01 return 01 P
%n'7er
void roundTtoTneare't-f&oat a0 N int S1 f&oat B1 S2a1B2a-S1 if-B;20650 SKK1 )rintf-J9nthe ori*ina& number i'=LfJ"a01 )rintf-J9nthe rounded number i'=LdJ"S01 P
Que'tion
3hat 7i&& ha))en if ( enter .%B6CD/UUU
How could that happen??????
(n)ut Va&idation
Becau'e the in)ut i' inva&id to the )ro*ram6 % inva&id va&ue can cau'e 'o dama*in* error' to !our )ro*ram6 So 7e 'hou&d found the inva&id in)ut out6
So&ution 7ith 3a! E
char a))&eH$I1 f&oat number1 )rintf-.)&ea'e enter a number=/01 *et'-a))&e01 ''canf-a))&e".Lf/"Mnumber01
So&ution 7ith 3a! G
in i+$)-a %char ?c,in n,@"*r1 < in #/#61,i,/*c; i$ %?c A6 B8B CC ?c A6 B;B CC %?c : B0B DD ?c 7BEB11 r* ,rn 0; $-r %i61;i:n,@"*r;i;;1 i$ %?%c;i1 66 BFB1 <?%c;i16B0B;/*c 6 i;"r*aG;= $-r %i61;i:n,@"*r;i;;1 < #/# 6 #/# ? i+/i2i %?%c;i11; i$ %#/#710001 #/#61; = ?%c;/*c1 6 BFB; i$ %#/#6601 /*c60; *)+* /*c61; r* ,rn /*c; =
The Strin* in function'
3e can4t )a'' a 'trin* into a function" becau'e it i' an arra!6 3e can on&! te&& the function 7here the addre'' of the fir't &etter6 3e )a'' the addre'' to the &etter" and from that" 7e *et a&& addre''e'6 Then" 7e can chan*e their va&ue6
$)-a 2* +c-r*%H-i/1 < Can be char c,i60,n60,n,@"*r[E]; re)&aced $)-a +c-r*; b! the .hi)*%%c62* char%11A6B'nB1 function i'di*it < i$ %cA6.CC%c:I0JDDc7JEJ11 <#rin $ %&Warnin20 #)*a+* *n *r a n,@"*rAAA'n&1; r* ,rn 81F0;= i$ %c66IFJ1 i61; i$ %i661CCc66IFJ1 <#rin $%&Warnin20 -- @,ch /*ci@a) #-in A'n&1;r* ,rn 81F0;= i$ %n76K1 <#rin $%&Warnin20.* -n)L acc*# M /i2i +A'n&1;r* ,rn 81F0;= What is it used for? n,@"*r[n;;]6c; = +c-r* 6 a -$%n,@"*r1;
So&ution 7ith 3a!
SummariSe= ''canf
A/Han a2* Can 'ave number' and 'trin*' at the 'ame time6 Can ea'! turn ever! di*it' to a va&ue6 Di+a/Han a2* Can not find inva&id va&ue and *ive the u'er a 7arnin*6
ABam)&e
char x[40],a[4][10]; in i,!,"[4]; #rin $%&'n'n()*a+* in#, $-,r .-r/+0&1; 2* +%x1; ++can$%x,&3+ 3+ 3+ 3+&, a[0],a[1],a[4],a[5]1; $-r%i65;i781;i881 #rin $%&3+ &,a[i]1;
SummariSe= Savin* a' 'trin*'
A/Han a2*+ Can chec@ inva&id in)ut' and *ive the u'er a 7arnin*6 Can correct 'ome inva&id in)ut' in 'ome ca'e'6 Di+a/Han a2*+ if the &en*th of the in)ut i' not fiBed" it 7i&& be ver! hard to contro&6 (t can be ver! hard to turn the 'trin*' to number6
int main-0 N After we make sure char VH<00I1 int i"@2<1 the input is valid. We 7hi&e -@?200 can use sscanf deal N with the strings! @201 )rintf-J)&ea'e t!)e in !our time-m=''6@@@0=J01 *et'-V01 i2'tr&en-V01 if -VH<I?2W=WXXVHDI?2Y640 N@2<1 P e&'e N@H<I201@HDI201P if -i?2$0 @2<1 for -i201i:$1iKK0 if -VHiI:Y04MMVHiI;YZ40 @2<1 if -@22<0 )rintf -.error? P&ea'e enter time a' m=''6@@@/01 P
ABam)&e
SummariSe=U'e 7hi&e--c 2 *etchar-00 && .condition'/0
A/Han a2*+ Can chec@ a&& @ind' of 7ron* in)ut6 %nd *ive re')on'ib&e 7arnin*'? Di+a/Han a2*+ Can be too com)&eB and 7a'te too much time? Stron*&! recommend that not to u'e thi' method 7hi&e eBamination6 Stron*&! recommend that u'e it 7hen !ou de'i*n a )ro*ram6
ABam)&e
,etScore<= )rintf -JThe 'core for 9Jea'! to read9J=J01 'coreAa'! 2 *et'core-01 if -'coreAa'!;<60 XX'coreAa'!:0600 N )rintf-J5ote=the 'core 'hou&d be in H0"<I?9nJ01 *oto ,etScore<1 P )rintf -Jthe 'core i' LC6<f9nJ"'coreAa'!01 iKK1
*oto
The command Y*oto4 can be u'ed ever!7here in !our )ro*ram6 ,rammar= *oto Linename1 Fou 'hou&d name one &ine in !our )ro*ram in !our )ro*ram a' .*et'core<=/
5ote
Mo't )ro*rammer don4t &i@e Y*oto4 becau'e it can cau'e confu'ion'6 3e u'e 'ome dead re)etition in'tead6 Such a' .7hi&e-<0/
$)-a 2* +c-r*%H-i/1 < char c,i60,n60,n,@"*r[E]; $)-a +c-r*; .hi)*%%c62* char%11A6B'nB1 < i$ %cA6.CC%c:I0JDDc7JEJ11 <#rin $ %&Warnin20 #)*a+* *n *r a n,@"*rAAA'n&1; r* ,rn 81F0;= i$ %c66IFJ1 i61; i$ %i661CCc66IFJ1 <#rin $%&Warnin20 -- @,ch /*ci@a) #-in A'n&1;r* ,rn 81F0;= i$ %n76K1 <#rin $%&Warnin20.* -n)L acc*# M /i2i +A'n&1;r* ,rn 81F0;= n,@"*r[n;;]6c; = 2* char%1;
[o7 to 'et &imitU
Sometime'" 7e 7ant to 'et a &imit of &en*th for our u'er'6 (f the u'er in)ut 'omethin* too &on*" 7e have to i*nore the &a't &etter'6
Method
7hi&e--c2*etchar-00MM-iKK0:maB0 N\\P if -n;2maB0 N 7hi&e -<0 N c2*etchar-01 if -c22Y9n40 brea@1 P P
%nother )rob&em
(f one 7ant to find the rounded number for 'evera& time'" 7hat 'hou&d he doU +bviou'&!" he can on&! run the )ro*ram for &ot' of time'6 3hat 7ou&d he thin@U [o7 foo&i'h the )ro*rammer are?
3e chan*e the )ro*ram
int 5um" o@2<1 f&oat num1 doN num 2 *et'core-01 if -num:<0 N )rintf-.no ne*ative )&ea'e? 9n/01 continue1 P 5um 2 roundTtoTneare't-num01 o@ 2 Continue-01P7hi&e-o@22001
int Continue-01
do N char c1 int o@2C1 )rintf-.do !ou 7ant to continueU-F]50=/01 c 2 *etchar-01 if -c?2Y9n40 *etchar-01 if -c22Y9n4 XX c22Y!4 XX c22YF40 o@ 2 01 if -c22Yn4XXc22Y540 o@ 2<1 P 7hi&e -o@22C01 return o@1
continue
Sto) the re)utation and re-'tart it from the be*innin* but a&& varietie' 7i&& @ee) the current va&ue6
B! the 7a!
3hen 7e u'e the method ((( 7e a&7a!' need the %SC(( code for 'ome character'6 3hat 'hou&d 7e do 7ith thatU (n fact 7e can u'e Turbo C and a'@ the com)uter to 'ho7 !ou the %SC(( number'6
Pro*ram
for -i201i:2#551iKK10 )rintf-.Ld---Lc/" i"-char0 i01 5ote the ran*e of %SC(( code i' from -<#7 to <#$
(n)ut Va&idation
Thi' @ind of )roce''e' are u'ed for avoidin* mi'-in)ut 7hich cau'ed b! Yenter4 ha'n4t been *ot b! !our )ro*ram6 Moreover" !ou can de'i*n that if an u'er tr! too man! time' he can choo'e to eBit6 -[int= u'e the eBit-0 in :'td&ib6h;0
5e7 Fear ,ift'
( have made 'evera& in)ut va&idation 'ub-)ro*ram in the &ibrar! : DS(5PUT6h;6 ( ho)e !ou can en8o! it? There are 'iB function' in m! &ibrar!6
:DS(5PUT6[;
inT)Tint -int maB0 :maB:2C0000;
%'@ the u'er enter a )o'itive number 'ma&&er than maB va&ue6
inTnTint -int min0 :min;2-C0000;
%'@ the u'er enter a ne*ative number that &ar*er than min va&ue6
inTint -int min" int maB0
%'@ the u'er enter a number that &ar*er than min va&ue and 'ma&&er than min va&ue6
:DS(5PUT6[;
void *oon-0 %'@ the u'er if he4d &i@e to u'e the )ro*ram a*ain6 inTf&oat -int min" int maB0 %'@ the u'er enter a rea& number that &ar*er than min va&ue and 'ma&&er than min va&ue6 int i'f&oat-char ^c"int number0 ^c i' the head addre'' of a 'trin*6 5umber i' the &en*th of the 'trin* Determine 7hether a 'trin* can be turned to rea& number6 int i''trin*-char ^'tr0 Determine 7hether i' a 'trin* a 'entence6
Section D
FIND HEL(
5eed he&)U
Don4t *o to the teacher or _han*fan or me6 Tr! to thin@ about it b! !our'e&f6 (f !ou need more he&) )&ea'e tr! to u'e the he&) in turbo C6
Let me 'ho7 'ome eBam)&e'
Stud! time function'6
Let me 'ho7 'ome eBam)&e'
Stud! time function'6
or the function !delay"
#he function !delay" ask the program have a pause $efore ne%t command &ms'
A constant
Time function'
(n fact" the faction c&oc@-0 doe'n4t *ive the )ro*ram a rea& time6 3hat the function *ive to the )ro*ram can be 'eemed a' a .time number/6 The con'tant CLVTTCV can turn the difference of .time number/ to rea& time6 The CLVTTCV e`ua&' to <$6#00
Con'tant
Let me 'ho7 'ome eBam)&e'
Stud! random function'
Let me 'ho7 'ome eBam)&e'
Stud! random function'
itted m o e $ e c an c n e t n e h is s t (an $e placed $y o s o d u o y ,f time&)*++'
Let me 'ho7 'ome eBam)&e'
Stud! random function'
rand
The random function rand can on&! offer 'ome fiBed random number6 (f !ou run the 'ame )ro*ram for 'evera& time'" the random number' offered b! rand-0 7i&& a&7a!' be the 'ame number'6
ABam)&e
ainc&ude:'tdio6h; ainc&ude:'td&ib6h; int main-0 N int i1 for-i2<1i:2<01iKK0 )rintf -Jnumber Ld=Ld9nJ"i"rand-001 )rintf -J9nJ01 return 01 P
+ut)ut
.ust the same
Let me 'ho7 'ome eBam)&e'
Stud! random function'
random
The random function rand can on&! offer 'ome fiBed random number6 (f !ou run the 'ame )ro*ram for 'evera& time'" the random number' offered b! rand-0 7i&& a&7a!' be the 'ame number'6
ABam)&e
ainc&ude:'tdio6h; ainc&ude:'td&ib6h; int main-0 N int i1 for-i2<1i:2<01iKK0 )rintf -Jnumber Ld=Ld9nJ"i"random-<0000001 )rintf -J9nJ01 return 01 P
+ut)ut
.ust the same
Prob&em
[o7 can rand-0 and random-0 return a rea& random numberU 3e can u'e randomiSe-0
Let me 'ho7 'ome eBam)&e'
Stud! random function'
ABam)&e E
ainc&ude:'tdio6h; ainc&ude:'td&ib6h; int main-0 N int i1 randomiSe-01 for-i2<1i:2<01iKK0 )rintf -Jnumber Ld=Ld9nJ"i"rand-001 )rintf -J9nJ01 return 01 P
+ut)ut
ABam)&e G
ainc&ude:'tdio6h; ainc&ude:'td&ib6h; int main-0 N int i1 randomiSe-01 for-i2<1i:2<01iKK0 )rintf -Jnumber Ld=Ld9nJ"i"random-<0000001 )rintf -J9nJ01 return 01 P
+ut)ut
More eBam)&e'
,ame TT Source code ,ame TT The'e )ro*ram' u'e both time function' and random function'6
Section A
DENUG OOUR (ROGRAP
method
ind the va&ue of !our variet! that mi*ht cau'e bu*'6 (f a&& the va&ue i' ri*ht" chec@ the fo&&o7in*=
u'e Y24 )ro)er&! or not chec@ the variet! of &eft 'ide and ri*ht 'ide of Y24 i' in the ri*ht order6
Section
WHITE (ROGRAP
5ote
chec@ NP -0 u'e ')ace )ro)er&! made the 'tructure of !our )ro*ram more c&ear&!6 [o7 to name !our variet!
The And
Than@ !ou ver! much Connect 7ith me at 'hen*chen&ibhotmai&6com