The Lecture For Turbo C
The Lecture For Turbo C
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
'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
'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
'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
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
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
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
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
(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
$)-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;
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;
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
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;
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?
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
:DS(5PUT6[;
inT)Tint -int maB0 :maB:2C0000;
%'@ the u'er enter a )o'itive number 'ma&&er than maB 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
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
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
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
Prob&em
[o7 can rand-0 and random-0 return a rea& random numberU 3e can u'e randomiSe-0
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
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