Introducere in programare
&$3,72/8/
Introducere in programare
,QWURGXFHUH &DOFXODWRDUHOH HOHFWURQLFH DX DSUXW vQ MXUXO DQXOXL 7RWXúL XQHOH PDúLQL mecanice de calculat au fost propuse anterior: -
3DVFDO±UHDOL]DQXPDLDGXQDUHúLVFGHUH /HLEQLW]±UHDOL]DúLvQPXO LUH %DEEDJH±PDúLQDQDOLWLF
&KLDU úL vQ DFHVWH DSDUDWH SULPLWLYH XQHOH GLQ FRPSRQHQWHOH XQXL DUKLWHFWXUL PRGHUQH SRW IL JVLWH 'H H[HPSOX XQ UHJLVWUX WHPSRUDU GH PHPRULH FDUH PHQ LQH variabilele aflate în curs de procesare. Acest registru este cunoscut sub denumirea de UHJLVWUX DFXPXODWRU &DOFXODWRDUHOH HOHFWURQLFH DX IRVW LQL LDO SURSXVH VXE IRUPD XQXL DSDUDW DEVWUDFW FDUH SRDWH UHDOL]D VDX VLPXOD RULFH WLS GH PDúLQ PHFDQLF )RUPD WHRUHWLFDIRVWLQWURGXVGHAlan Turing pe la mijlocul anului 1930. &D úL vQ FD]XO PXOWRU DOWH WHKQRORJLL GH]YROWDUHD SUDFWLF D XQHL PDúLQL FDUH V respecte acest model, a fost LQIOHQ DW GH DO GRLOHD U]ERL PRQGLDO $FHVW OXFUX D IRVW posibil deoarece se dorea calcularea traiectoriilor de rachete (&ROORVXV úL Eniac) sau GHFRGDUHDFRGXOXL(QLJPDIRORVLWGH*HUPDQLDSHQWUXSURWHF LDPHVDMHORU Primul program stocat electronic a fost folosit în 1948. În primii ani de dezvoltare FRPSXWHUHOHúLLPSDFWXOORUXOWHULRUvQYLD DXPDQDXIRVWPXOWVXEHYDOXDWH(GH QRWRULHWDWHGHFODUD LDOXLBill *DWHVIRQGDWRUXO0LFURVRIWFXPFQXYHGHFXPDUSXWHDIL YUHRGDWQHYRLHGHPDLPXOWGHKo RAM. ÌQ DQLL ¶ D DSUXW SDVWLOD GH VLOLFLX úL WHKQRORJLD 9/6, Very large Scale ,QWHJUDWLRQ FD WHKQLF GH SURGXF LH IDSW FH DGXV OD FRPSXWHUH LHIWLQH úL UDSLGH $FHVW OXFUX D FRQGXV OD FUHúWHUHD úL GLYHUVLILFDUHD UDSLG D FDWHJRULLORU GH XWLOL]DWRUL FDUH úL SXWHDXSHUPLWHVOHDFKL]L LRQH]H 3HULRDGD FDUH HVWH FDUDFWHUL]DW GH SULQ DSDUL LD microsistemelor pe 8 EL L FDUH GHúL HUDX VFXPSH SHUPLWHDX DFFHVXO VHSDUDW GH PDLQIUDPH OD SRVLELOLW LOH VLVWHPXOXLGHFDOFXO ÌQDFHDVW SHULRDG HVWH GHPQ GH UHPDUFDW DSDUL LD vQ D OXL ,FDUHSRDWHILFRQVLGHUDWXQSXQFWGHUHIHULQ vQGRPHQLX'LQDFHVWPRPHQWVRIWXO DvQFHSXWVILHGH]YROWDWQXQXPDLSHQWUXFDOFXOHH[WUHPGHFRPSOH[HFLúLSHQWUXMRFXUL HGLWRDUHGHWH[WHSURJUDPHGHED]HGHGDWHúLDOWHIDFLOLW LFHUXWHGHSLD 7RW vQ DFHDVW SHULRDG IRU DW úL GH UXSHUHD GH PDLQIUDPH D SRUQLW úL GH]YROWDUHDVLVWHPHORUGHRSHUDUHORFDOH&30úLDSRL'26 6LVWHPXO GH RSHUDUH 06'26 D IRVW UHDOL]DW SULQ DGDSWDUHD OD QRLOH LQVWUXF LXQL PDúLQ D XQXL PLFURNHUQHO GH 81,; OD FDUH VDX DGXJDW IXQF LLOH QHFHVDUH FRQWUROXOXL SHULIHULFHORUFDUHvQFHSXVHUVILHDWDúDWHFDOFXODWRDUHORUSHUVRQDOH 'LQ SkQ vQ GHúL vQF IRDUWH VFXPSH DX vQFHSXW V DSDU SH SLD DUKLWHFWXULGLQFHvQFHPDLSXWHUQLFHSHEL LLDUDULDORUGHIRORVLUHVDH[WLQVIRDUWH PXOWúLvQDOWHGRPHQLL 1XHVWHGHQHJOLMDWQLFLLPSDFWXOSURGXVGHLQWHUIH HOHYL]XDOHúLGHFRQWURDOHOHSULQ]RQH active ale ecranului. Aceste controale care sunt activate din PRXVH sunt caracteristice sistemului de operare WINDOWS.
1
Introducere in programare
7RW vQ DFHDVW SHULRDG GLQ UD LXQL HFRQRPLFH úL SUDFWLFH VD SURGXV GLVSDUL LD PLFURFDOFXODWRDUHORUDFHVWHDILLQGPXOWGHSúLWHGHFHULQ HOHSLH LL 'LQ VH SRDWH VSXQH F VD LQWUDW vQWUR QRX HU vQ DFHVW GRPHQLX SULQ LHIWLQLUHDGUDPDWLFDKDUGZDUHXOXLúLDSDUL LDUH HOHORUGHFDOFXODWRDUH 2GDWFXLHIWLQLrea arhitecturilor de calcul s-a putut GLYHVULILFDúLPDLPXOWDULDGH XWLOL]DUHDORU$FHVWOXFUXDDYXWFDUH]XOWDWRFUHúWHUHDSUH XOXLVRIWXOXL &RQFHSWXOGHUH HDSHUPLWHDWUDQVIHUXULUDSLGHGHGDWHDFURUFDQWLWDWHDvQFHSXW VILHGLQFHvQFHPDLPDUH ,QL LDO PDMRULWDWHD UH HOHORU GH FDOFXODWRDUH HUDX vQFKLVH vQ VHQVXO F QX FRPXQLFDX vQWUH HOH $SRL vQ XOWLPD SHULRDG WHUPHQXO GH VLVWHP GHVFKLV FDSW GLQ SXQFWGHYHGHUHSUDFWLFRUVSkQGLUHGLQFHvQFHPDLPDUHODQLYHOJOREDO'LQDFHVWSXQFW GHYHGHUHUH HOHGHWLSArpanet , Internet úDPGVXQWUHSUH]HQWDWLYH ÌQXOWLPLLDQLvQVXúLFRQFHSWXOGHWUDQVPLVLHGHLQIRUPD LHLQWHUXPDQWLQGHVVH VFKLPEH7HOHYL]LXQHDGLJLWDOHVWHGHMDLPSOHPHQWDWLDUSUH XOSHULIHULFHORUGHGLFDWHD VF]XW H[WUDRUGLQDU DVWIHO vQFkW VH SUHFRQL]HD] GH]YROWDUHD FDOFXODWRUXOXL DVWIHO vQFkW HO VQXPDLILHVWULFWRULHQWDWVSUHDSOLFD LLGHGLFDWHFLVILHROHJWXUGLQDPLFGHRULFHWLS (audio, video, teleworking) a individului cu societatea. 9RPSUH]HQWDFkWHYDGLQDSOLFD LLOHPDLFXQRVFXWHDOHFDOFXODWRDUHORUvQGLYHUVHGRPHQLL -
-
,QGXVWULHFRQWUROúLDXWRPDWL]DUHÆ microcontrolere, CAM (Computer Aided Manufactory); Economie: gestiune, transferul banilor, "banii electronici" . 0HGLFLQGLDJQR]DXWRPDWFHUFHWDUHDHOHFWURQLFODGLYHUVHQLYHOHDOH RUJDQLVPXOXLXPDQDQDOL]DFRGXOXLJHQHWLFGLDJQR]DXWRPDWÆUH HOHQHXUDOH VWD LLJUDILFHVLVWHPHH[SHUW 3LFWXU QRL UDPXUL DOH SLFWXULL IRORVLQG FRPSXWHUXO UHVWDXUDUH Æ KRORJUDILH UH HOH neurale, procesoare analogice. 0X]LF úL WHOHYL]LXQH SUHOXFUUL GH LPDJLQL úL VXQHWH SUDFWLF GH RULFH QDWXU Æ SURFHVRDUHDQDORJLFHVWD LLJUDILFHDUKLWHFWXUL00; Proiectare : programele de tip CAD (Computer Aided Design)
Se mai pot da exemple în mulWHGRPHQLLvQVHVWHFHUWF OD RUD DFWXDO YLD D úL VRFLHWDWHDXPDQQXPDLSRWILFRQFHSXWHIUFDOFXODWRU
2
Introducere in programare
3URJUDPDUHDúLOLPEDMHGHSURJUDPDUH
Prin SURJUDPDUHVHvQ HOHJHvQPRGJHQHULFWUDQVSXQHUHDXQRURSHUD LLUHSHWLWLYH DVXSUD XQXL VHW GH GDWH vQWUXQ OLPEDM LQWHOLJLELO GH FWUH XQ VLVWHP GH FDOFXO FDUH XUPHD]XOWHULRUVOHH[HFXWH$FHVWOXFUXHVWHUHDOL]DWvQGRXHWDSH -
HWDS vQ FDUH HVWH LPSOLFDW RPXO úL DQXPH FHD GH WUHFHUH GH OD SUREOHPD UHDO OD transpunerea într-un limbaj de programare. R D GRXD HWDS DXWRPDW FDUH WUDQVSXQH FRGXOVXUV vQúLUXLUHD GH LQVWUXF LXQL specifice limbajului respectiv într-un FRGGLUHFWH[HFXWDELOinteligibil sistemului GHFDOFXO OXFUXGHFDUHVHRFXSSURJUDPHVSHFLDOL]DWHQXPLWHFRPSLODWRDUH.
5ROXOSURJUDPULL HVWH FD ILH GDW R DQXPLW RSHUD LXQH VDX VXLW GH RSHUD LXQL UHSHWLWLYH FDUH VH DSOLF DVXSUD XQRU VHWXUL GH GDWH PHUHX GLIHULWH V ILH VFULV XQ SURJUDP FDUH V FHDU VHWXO GH GDWH GHLQWUDUHFHOH FDUH WUHEXLH V ILH SUHOXFUDWH V H[HFXWH DVXSUD ORU VXLWD VWDQGDUG GH RSHUD LXQL úL V OLYUH]H GDWHOHGHLHúLUH DGLF rezultatele). ÌQ DFHVWH FRQGL LL SURJUDPXO WUHEXLH V ILH FRPSXV GLQ PDL PXOWH LQVWUXF LXQL ÌQ primul rând cele care UH]HUYGHFODUR]RQGHPHPRULHFDILLQGVSHFLDOGHGLFDWQXPDL SHQWUXDQXPLWHGDWH R]RQGHPHPRULHvQFDUHVHYRUFLWLGDWHOHGHLQWUDUHR]RQGH PHPRULHvQFDUHVHYRUSVWUDGDWHOHLQWHUPHGLDUH(cele care apar în decursul calculelor GDUGHFDUHXWLOL]DWRUXOQXDUHQHYRLH úLGHDVHPHQLSHFHOHGHLHúLUH3URJUDPXOWUHEXLH VFRQ LQLQVWUXF LXQLFDUHVFLWHDVFGHODXWLOL]DWRUGDWHOHGHLQWUDUHXUPDWHvQVIkUúLW GH LQVWUXF LXQLOH GH FDOFXO SURSULX ]LV FD vQ ILQDO V FRQ LQ LQVWUXF LXQLOH GH DILúDUH D UH]XOWDWHORUGDWHOHGHLHúLUH $UKLWHFWXUDXQXLVLVWHPGHFDOFXO Ca REVHUYD LH de ED]: un sistem de calcul este pur úL simplu versiunea mult GH]YROWDW a unui calculator de buzunar úL deci nu trebuie V ne DúWHSWP de la el ca vreo GDW V IDF altceva decât l-a pus cineva, un programator prin intermediul unui program, V IDF. “,QWHOLJHQ D´DSDUHQWa unui sistem de calcul provine din cantitatea de LQWHOLJHQ XPDQLQYHVWLWîn respectivul program aflat în H[HFX LH Hilar, am putea spune F omul vQFHDUF V ³IDF pe cineva GXS chipul úL DVHPQDUHD lui” dar rezultatul este o copie SDOLG FDUH altfel este extrem de XWLO Din aceste lucruri GHULY vestitul principiu de aur al primilor programatori GIGO (Garbage In Garbage Out), care într-o traducere SUR]DLF vQVHDPQ: “gunoi bagi gunoi VFR L” úL care are meritul, în DIDU de cel umoristic, de a atrage DWHQ LD oricui care se va apuca de programare F din nefericire calculatorul face numai ce i se LQGLF prin diverse metode V IDF. De aici UH]XOW R SULP DWHQ LRQDUH YDODELO aprope tot timpul în cazul GHSDQULL(H[HFX LHLSDVFXSDVîn scopul JVLULLeventualelor defecte în IXQF LRQDUH unui SURJUDP &$87 Ì17Æ, (52$5($ 7$ '( 352*5$0$725 ù, $32, ' 9,1$ PE CALCULATOR. Desigur H[LVWun procent cam de 5% din VLWXD LLîn care un program scris corect este executat JUHúLW lucru care apare ca rezultat al LQWHUIHUHQ HL unor programe neautorizate, cum ar fi YLUXúLL FX programul aflat în H[HFX LH În cazul în care nu este
3
Introducere in programare
valabil acest lucru problema este mai FRPSOH[putând fi implicate GHIHF LXQLaleatorii ale SU LL KDUGZDUH Oricum ar fi aceste lucruri LQWU în sfera de DF LXQH a unui inginer de sistem, úLdeci nu sunt luate aprioric în FRQVLGHUD LHfiind de obicei rare. Pentru a putea vQ HOHJHarhitectura vom FXWDanumite analogii între un om úLun sistem de calcul. 6 DQDOL]P ce se vQWkPSO FX un om în momentul în care vede o formulare de genul : 2*2=… . 'HúLni se pare ceva evident, cu WR LLDPvQY DWpe de rost tabla vQPXO LULL V vQFHUFPacum o descompunere a SDúLORUFDUHsunt H[HFXWD L În primul rând PHPRUPîn memoria WHPSRUDUsau de VFXUWGXUDW RSHUDQ]LLdatele asupra FURUDvom executa o RSHUD LXQH apoi UHDOL]PRSHUD LXQHDGHvQPXO LUHrezultând 4, care este SkQODcomunicarea lui YHUEDOsau prin scris tot în memoria WHPSRUDU6H REVHUYFoperanzii 2,2 úLRSHUD LXQHD DXfost preluate de pe un suport extern si anume cel scris, rezultatul putând fi UHWXUQDWtrimis FWUHHPL WRUXOunei cereri de lucru GXS cum am spus tot pe DFHOúLsistem extern de stocare. $FHVWH[HPSOXQXDIRVWGDWIUXQVFRSúLDQXPHUROXOOXLHVWHSULQWUHDOWHOHGHD Y RELúQXL FX QLYHOXO H[WUHP GH VF]XW OD FDUH VH IDFH DQDOL]D úL GHVFRPSXQHUHD XQHL SUREOHPHUHDOHvQPRPHQWXOvQFDUHGRUHVFVRWUDQVSXQvQWUXQOLPEDMGHSURJUDPDUH ùi acum VDQDOL]PDUKLWHFWXUDEORF(structura IXQF LRQDO Dunui sistem de calcul. 'XS FXP VH REVHUY GLQ ILJ într-un sistem de calcul H[LVW - un dispozitiv de stocare în PDV rolul caietului pe care scrim /citim în cazul nostru. Acest dispozitiv este materializat prin Hard Disk (HDD) FUXLD i se mai 0HPRULH 'LVSR]LWLY spune úLsuport fix de date úL)ORSS\'LVN 7HPSRUDUm ,QWUDUH (FDD) FUXLD i se mai spune úL suport 5$0 ,H LUH mobil de date . )LJ$UKLWHFWXUDXQXL - o memorie WHPRUDU 5$0 5DQGRP VLVWHPGHFDOFXO Acces Memory) care MRDF H[DFW rolul memoriei de VFXUWGXUDWumane. - dispozitive de Intrare sau de ,HúLUH prin care sistemul SULPHúWH sau UHWXUQHD] GDWH În cazul unui sistem de calcul acestea sunt tastatura úL PRXVH FD dispozitive de intrare úL monitorul sau imprimanta ca dispozitive de LHúLUH A mai Umas de OPXULWproblema microprocesorului care este “inima” unui sistem de calcul úL DUH rolul de a coordona întregul sistem. Desigur F DFHDVW coordonare se UHDOL]HD]WRWprin intermediul unor programe care H[LVWîn ceea cea am putea numi, prin analogie cu omul, partea de reflexe FRQGL LRQDWH aceste programe se JVHVF într-o memorie ROM (Read Only Memory) deci nu pot fi decât citite. ' LV S R ] LW LY G H
0 LF U R S U R F H V R U
VWR FDUH
+ ' ' ) ' '
ú
)XQF LRQDUHDXQXLVLVWHPGHFDOFXO Sistemul de calcul GXS FXP VH REVHUY FRPSRUW destul similitudini cu omul la nivel de DUKLWHFWXUúLdeasemeni, GXSFXPvom discuta, úLODnivel de IXQF LRQDUH 6vedem cum se UHDOL]HD]ELQHvQ HOHVODnivel simplificat, H[HFX LDunei formule de genul 2*2=… .
4
Introducere in programare
Se FLWHúWH GH pe suportul de stocare programul care FRQ LQH cererea QRDVWU VH vQFDUF în memoria WHPSRUDU GH acest lucru se RFXS VLVWHPXOGHRSHUDUHprogramul specializat care se RFXS GH traducerea unor LQVWUXF LXQL VLPSOH úL LQWXLWLYH DOH utilizatorului în comenzi directe FWUH partea de H[HFX LH D sistemului , în urma cererii noastre explicite de lansare a programului) úL VH începe H[HFX LD lui de FWUH procesor H[HFX LHFDUHXUPHD]aproximativ sistemul uman de gândire. Pe scurt modul de H[HFX LH la nivelul procesorului poate fi descris ca : • FLWHúWHdatele utile (din RAM în aceast caz) (2,2) • FLWHúWHRSHUD LDFDUHtrebuie H[HFXWDWasupra datelor suspomenite(tot din RAM)(*) • H[HFXWRSHUD LD • returneazrezultatul (înnapoi în RAM)(4). La nivelul sistemului de calcul modul de H[HFX LHDUDWFDmai jos: • FLWHúWHprogramul de executat (de pe sistemul de stocare fix/mobil) úL • vQFDUFOîn RAM • H[HFXWprogramul (din RAM) • la terminarea programului DúWHDSWnoua cerere de H[HFX LH
0HPRULD7HPSRUDUDXQXLVLVWHPGHFDOFXO 'XSFXPDPpomenit în primul capitol, una din componentele de ED]DOHunui sistem de calcul este memoria GLQDPLF5$0sau memoria WHPSRUDU $FHDVWmemorie a fost FRQFHSXW LQkQGcont de faptul Fsistemul de QXPHUD LH folosit în interiorul unui calculator este ED] 'H aici UH]XOW F orice fel de GDW sau LQVWUXF LXQHefectiv SURFHVDWsau H[HFXWDWODnivelul PDúLQLLtrebuie Vfie SDVWUDWVXE forma ei ELQDU deci FRUHVSXQ]WRDUH bazei 2). Întrucât în baza 2 nu H[LVW decât dou numere 0 úL UH]XOW F vom avea UHSUH]HQWUL VXE IRUPD unor úLUXUL GH úL GH /D nivelul PDúLQLL materializarea HIHFWLY D respectivelor numere este UHDOL]DW GH obicei, prin DWDúDUHD ORJLF D lui “1” la o tensiune de 5 YRO L iar lui “0” lipsei acestei tensiuni. 7RWXúLindiferent de nivelul tehnologic este absolut nepractic ca VFRQVLGHUPIL[9sau fix 0 V ca valori de FRPSDUD LHpentru Facest lucru ar fi dus la QLúWHechipamente extrem de scumpe úLGHlente. Din aceasta FDX]VHFRQVLGHUDILsau 1 logic în jurul valorilor sus PHQ LRQDWH FX ±∆V. De aici a rezultat posibilitatea folosirii unui condensator ca element de memorare. Acest lucru este posibil deoarece în cazul unei UHvQFUFUL periodice a acestuia se poate RE LHQH FD tensiunea la bornele lui V varieze în limitele necesare QRX GDF fac suficient de repede DFHDVW UHvQFUFDUH În aceste FRQGL LL foarte simplist putem vedea o memorie GLQDPLF ca o matrice LPHQV de condensatoare care pot fi vQFUFDWHsau GHVFUFDWHîn cursul RSHUD LXQLORUGHscriere/citire. Aici mai apare o SUREOHPúLanume QXPUXOGHEL LFDUHcompun datele utile de calcul (ELWunitate LQIRUPD LRQDO FRUHVSXQ]WRDUH H[LVWHQ HL sau a unei tensiuni la intrarea unui dispozitiv digital) care pot fi SURFHVD LODun moment dat de un procesor. Întrucât memoria RAM este cea care IXUQL]HD] respectivele date era logic ca ea V fie RUJDQL]DW în diviziuni de o PULPH HJDO cu cea a QXPUXOXL de EL L sus PHQ LRQD L. Aceste JUXSUL se numesc ORFD LLGHPHPRULHúL pot fi considerate ca minima unitate care poate fi VFULV sau FLWLW dintr-o memorie. O GDW OPXULW acest aspect devine clar F pentru a P putea referi la una sau alta din ORFD LL trebuie V existe o PHWRG GH individualizare VWULFW6DDOHVcea mai VLPSOFXSXWLQ úLanume numerotarea lor QXPUXOORFD LHLnumindu-se DGUHVDORFD LHL. 5
Introducere in programare
1R LXQHDGHDOJRULWP
ÌQSURFHVXOGHUH]ROYDUHDXQHLSUREOHPHIRORVLQGFDOFXODWRUXOH[LVWGRXID]H -
GHILQLUHDúLDQDOL]DSUREOHPHL SURLHFWDUHDúLLPSOHPHQWDUHDXQXLDOJRULWPFDUHUH]ROYSUREOHPD
'HILQLUHDúLDQDOL]DSUREOHPHLSRDWHILODUkQGXOHLGHVFRPSXVvQ - specificarea datelor de intrare; - VSHFLILFDUHDGDWHORUGHLHúLUH 6SHFLILFDUHDGDWHORUGHLQWUDUHFRQVWvQ 1. 2. 3. 4. 5.
Ce date vor fi primite la intrare; Care este formatul (forma lor de reprezentare) a datelor de intrare; Care sunt valorile permise sau nepermise pentru datele de intrare; ([LVWXQHOHUHVWULF LLDOWHOHGHFkWOD SULYLQGYDORULOHGHLQWUDUH &kWH YDORUL YRU IL OD LQWUDUH VDX GDF QX VH SRDWH VSHFLILFD XQ QXPU IL[ GH YDORUL FXPVHYDúWLFkQGVDXWHUPLQDWGHLQWURGXVGDWHOHGHLQWUDUH
6SHFLILFDUHDGDWHORUGHLHúLUHWUHEXLHV LQFRQWGHXUPWRDUHOHDVSHFWH 1. &DUHGLQYDORULOHUH]XOWDWHvQFXUVXODSOLFULLDOJRULWPXOXLGHFDOFXODVXSUDGDWHORUGH LQWUDUHYRUILDILúDWHQHFHVDUHXWLOL]DWRUXOXL ÌQDFHVWSDVVHIDFHGLIHUHQ LHUHDFODU vQWUHGDWHLQWHUPHGLDUHúLGDWHGHLHúLUH 2. &DUHYDILIRUPDWXOGDWHORUGHLHúLUHGHH[HPSOXXQQXPUUHDOSRDWHILDILúDWFXWUHL VDXFXFLQFL]HFLPDOHVDXXQWH[WSRDWHILDILúDWLQWHJUDOVDXSDU LDO 3. 6XQW VDX QX QHFHVDUH H[SOLFD LL VXSOLPHQWDUH SHQWUX XWLOL]DWRU vQ DIDUD GDWHORU GH LHúLUH 4. &DUHHVWHQXPUXOGHGDWHGHLHúLUHFDUHWUHEXLHWUDQVPLVHFWUHLHúLUH 2GHILQL LHDQR LXQLLGHDOJRULWPSRDWHILvQOQ XLUHDGHSDúLVLPSOLRSHUD LLGLVWLQFWH FDUH GHVFULX PRGXO GH SUHOXFUDUH D XQRU GDWH GH LQWUDUH vQ VFRSXO UH]ROYULL XQHL SUREOHPH 'HFL $/*257,08/ (67( $&($ 3$57( '( &$/&8/ &2081 Ì1 &$=8/5(=2/95,,81(,352%/(0('(&$/&8/ 8QH[HPSOXVLPSOXGHDOJRULWPDUILVXLWDGHRSHUD LLPDWHPDWLFHIFXWvQUH]ROYDUHD XQHL HFXD LL PDWHPDWLFH GH JUDGXO ,, D;E;F FRHILFLHQ LL D E F VH VFKLPE GDU modul de procesare a valorilor lor, nu. 3URSULHW LOHXQXLDOJRULWPVXQW 1. (VWHFRPSXVGLQLQVWUXF LXQLVLPSOHúLFODUH 2. 2SHUD LXQLOHVSHFLILFDWHGHLQVWUXF LXQLVHH[HFXWvQWURDQXPLWVHFYHQ 3. 6ROX LDWUHEXLHRE LQXWvQWUXQQXPUILQLWGHSDúL 'LQ FHOH VSXVH PDL VXV UH]XOW F 81 $/*25,70 (67( ,1'(3(1'(17 '( 7,38/ '( /,0%$- Ì1 &$5( (67( 75$16386 6$8 '( 7,38/ '( 0$ù,1 3( CARE ESTE EXECUTAT. 6
Introducere in programare
2EVHUYD LHÌQDFHDVWFDUWHVHYRUGLVFXWDQXPDL$/*25,70,6(&9(1,$/,úL UHVSHFWLY352*5$0$5($6(&9(1,$/ 8Q DOJRULWP VHFYHQ LDO HVWH DFHO DOJRULWP FUHDW SHQWUX H[HFX LD SH XQ FDOFXODWRU VHFYHQ LDO'HQXPLUHDGHVHFYHQ LDOSURYLQHGLQIDSWXOFDFHVWDQXSRDWHH[HFXWDGHFkW RVLQJXURSHUD LHODXQPRPHQWGDW 5HSUH]HQWDUHDDOJRULWPLORU
ÌQ JHQHUDO XQ DOJRULWP SRDWH IL FRQVLGHUDW FD R GHVFULHUH D SUHOXFUULL HIHFWXDWH asupra unui flux de date, prelucrare care are loc cu un scop bine determinat. Ca de obicei, vQ FD]XO GH]YROWULL XQHL QRL WHRULL VD FXWDW DWkW R FDOH GH VWDQGDUGL]DUH D PRGXOXL GH GHVFULHUH D XQXL DOJRULWP FkW úL R FDOH GH DO IDFH LQGHSHQGHQW GH XQ OLPEDM GH SURJUDPDUHDQXPHUH]XOWkQGGRXPHWRGHFODVLFH -
metoda schemei logice (diagramei de flux (flowchart)); metoda pseudocodului.
0HWRGDVFKHPHLORJLFH ÌQFDGUXODFHVWHLPHWRGHVHIRORVHúWHXQVHWGHVLPEROXULSUH]HQWDWvQILJXUD SHQWUXGHVFULHUHDSDúLORUFHWUHEXLHH[HFXWD LSHQWUXFDSURJUDPXOUH]XOWDWVQHUH]ROYHR DQXPLWSUREOHP 'HúLDIRVWH[WUHPGHIRORVLWSkQQXGHPXOWDFHDVWPHWRGSDUHDSLHUGHWHUHQ vQ ID D UHSUH]HQWULL GH WLS SVHXGRFRG SRDWH úL GDWRULW WLPSXOXL VXSOLPHQWDU SLHUGXW GH utilizator cu executarea simbolurilor grafice. 6 DQDOL]P XQ SURJUDP GH FDOFXO D PHGLHL SHQWUX WUHL QRWH úL V YHGHP FXP DU DSUHDGHVFULVSULQDFHDVWPHWRG 'XSFXPVHREVHUYvQILJXUDVHGHVFULXHIHFWLYSDVFXSDVWRDWHRSHUD LXQLOH care trebuiesc urmate. ÌQ ILJXUD VD VSHFLILFDW úL vQFHSXWXO SURJUDPXOXL IDSW FDUH SDUH LQXWLO GDU vQ FD]XOGHVFULHULLXQXLSURJUDPIRDUWHPDUHVHSUHDSRDWHFDVFKHPDORJLFVVHvQWLQGSH ]HFLGHSDJLQLFD]vQFDUHGHYLQHHYLGHQWQHFHVLWDWHDGHFODUULLvQFHSXWXOXLúLVIkUúLWXOXL de program. ÌQXUPWRUXOSDVVHGHFODUYDULDELOHOH2YDULDELOHVWHXQLGHQWLILFDWRUSURSULXDO SURJUDPDWRUXOXL FDUH vQ FD]XO RSHUD LXQLL GH GHFODUDUH VH DWDúHD] OD JHQHUDUHD SURJUDPXOXL XQHL ]RQH GH PHPRULH D FUHL GLPHQVLXQH HVWH vQ GLUHFW FRQFRUGDQ FX WLSXOGHGDWVWRFDWGHDFHDVWD Se trece apoi la citirea datelor de intrare. În acest caz este vorba de trei note ceea FH SUHVXSXQH F YDORULOH GH LQWUDUH SRW DYHD úL FX ]HFLPDOH 0DL WkU]LX VH YD GLVFXWD GHVSUHDOHJHUHDWLSXOXLGHYDULDELOIXQF LHGHVSHFLILFD LLOHXWLOL]DWRUXOXL 6HH[HFXWFDOFXOXOSURSULX]LVGXSFDUHVHDILúHD]UH]XOWDWXOFX]HFLPDOH$ PDL UPDV GRDU PDUFDUHD VIkUúLWXO GH SURJUDP &KLDU GDF PHWRGD DUH R LPSUHVLH ³DUWLVWLF´VSRULWFkWHRGDWVHSLHUGHSUHDPXOWWLPSúLKkUWLHvQFD]XOIRORVLULLHLúLGH DFHHDGLQFHvQFHPDLPXO LSUHIHUFDPHWRGGHGHVFULHUHpseudocodul.
7
Introducere in programare 6LPERO GH WHUPLQDUH VDX vQFHSHUH SURJUDP &XWLH GH FDOFXO &XWLH GH FRPHQWDULX &XWLH GH ,QWUDUH VDX ,HúLUH 'DWH
)LU GH H[HFX LH D SURJUDPXOXL XQXL
)LJ 6LPEROXUL VWDQGDUG SHQWUX
)LJ 6FKHPD ORJLF
PHWRGD VFKHPHL ORJLFH
SURJUDP GH FDOFXO D PHGLHL SHQWUX WUHL QRWH
0HWRGDSVHXGRFRGXOXL )D GHVFKHPDORJLFDUHGH]DYDQWDMXOFQXvQWRWGHDXQDUHGDEVROXWWR LSDúLLGH FDOFXO 7RWXúL HVWH PDL FRPRG GH IRORVLW vQ FD]XO vQ FDUH VH GHVFULX DOJRULWPL SHQWUX OLPEDMHOHGHSURJUDPDUHGHQLYHOPHGLXúLvQDOW 6FKHPD ORJLF HVWH FHD PDL LQGLFDW FD PHWRG GH GHVFULHUH vQ VSHFLDO vQ FD]XO LPSOHPHQWULLXQXLDOJRULWPIRORVLQGOLPEDMGHDVDPEODUHXQGHGHVFULHUHDDPQXQ LWD ILHFUXL SDV HVWH QHFHVDU SHQWUX SXWHD PLQLPL]D úDQVHOH DSDUL LHL XQRU HURUL GH implementare. DescrierHDHIHFWLYDSDúLORUFHWUHEXLHHIHFWXD LIRORVLQGFXYLQWHOHOLPELLURPkQH vPSUHXQ FX FXYLQWH FKHLH DOH OLPEDMXOXL GH SURJUDPDUH vQ FDUH VH YD LPSOHPHQWD DOJRULWPXO VH QXPHúWH SVHXGRFRG 6H REVHUY F vQ DFHVW FD] FULWHULXO LQGHSHQGHQ HL WRWDOHDGHVFULHULLID GHOLPEDMXOGHLPSOHPHQWDUHQXHVWHvQWUXWRWXOUHVSHFWDW 3HQWUX FRPSDUD LH YRP GHVFULH PDL MRV vQ SVHXGRFRG SUREOHPD SUH]HQWDW vQ figura 1.2. YDULDELOHPDQQQ ^ FLWHúWHQQQ PD QQQ DILúHD]PDFX]HFLPDOH `
8
CAPITOLUL 2
Introducere in programare
2. Limbajul C Limbajele de programare de nivel mediu au fost serios dezvoltate pe la mijlocul anilor ¶ ,GHLOH GH ED] IRORVLWH SHQWUX SURLHFWDUHD ORU GHULY GLQ IRUPXOHOH IXQF LRQDOH λ&KXUFKFHIXVHVHUSURSXVHSULQDQLL¶/DRUDDFWXDOVHHVWLPHD]FH[LVWSHVWH GH OLPEDMH GH SURJUDPDUH GLIHUHQ HOH vQWUH HOH ILLQG OHJDWH vQ SULQFLSDO GH VWLOXO GH programare. Limbajul FORTRAN (FORmula TRANslator) a fost unul dintre primele limbaje GH SURJUDPDUH (O HVWH IRORVLW úL DFXP vQ LQGXVWULH VDX FHUFHWDUH SHQWUX D LPSOHPHQWD DSOLFD LLVSHFLILFHFXYROXPHPDULGHFDOFXOHPDWHPDWLFHFRPSOH[H 3H OkQJ OLPEDMH GH SURJUDPDUH SHQWUX PDúLQL VHFYHQ LDOH VDX GH]YROWDW úL OLPEDMHFDUHVXSRUWSURFHVDUHSDUDOHO'LQQHIHULFLUHPDMRULWDWHDQXVXQWSRUWDELOHILLQG RULHQWDWH VWULFW SH DUKLWHFWXUD PDúLQLL SHQWUX FDUH D IRVW GH]YROWDW OLPEDMXO 'H DELD vQ XOWLPLL]HFHDQLGH]YROWDUHDUDSLGDUH HOHORUGHFDOFXODWRDUHDFRQGXVúLODRvQFHUFDUHGH standardizare a unor limbaje ce pot crea cod executabil simultan pe mai multe sisteme de calcul. $YDQWDMHOHSURJUDPULLODQLYHOinalt sunt: − ascund GLIHUHQ HOHGHDUKLWHFWXULQWHUQ − sunt XúRUGHXUPULWúLPHQ LQXWmodificare úi depanare XúRDU − sunt portabile . De exemplu un cod scris în C poate fi executat úi sub UNIX úLVXE DOS − nu au cel mai eficient FRGvQFRPSDUD LHFXFHOVFULVLQ$60GLUHFW De obicei sistemele de operare erau dezvoltate in limbaj de ansamblare pentru a fi mai rapide . Limbajul C, dezvoltat in 1972 este Dennis M.Retchie la Laboratoarele AT&T Bell, este primul limbaj pentru crearea sisteme de operare. Numele limbajului provine din faptul c este rezultatul îPEXQW LULLlimbajului B, folosit în scrierea UNIX pentru DEC PDP7. Prima GRFXPHQWD LH despre acest limbaj a fost “The C Programing Language”, scrisGH5HWFKLHúLBrian Kernighan în 1977. Înaintea ei exista doar “The C Reference Manual”, scris de Retchie. 2 FDUDFWHULVWLF LPSRUWDQW D DFHVWXL OLPEDM HVWH IDSWXO F SRDWHILFRQVLGHUDWVLPXOWDQúLXQOLPEDMGHQLYHOPHGLXúLXQOLPEDMGHQLYHOVF]XW 2 GDW FX UVSkQGLUHD OXL SULQWUH SURJUDPDWRULL SURIHVLRQLúWL VD WUHFXW úL OD DGDSWDUHDFRPSLODWRUXOXLSHQWUXVLVWHPXOGHRSHUDUH06'26'H DVHPHQHDD DSUXW úL DOWSUREOHPúLDQXPHIDSWXOFVWDQGDUGXO.&R nu mai era satisfFWRU'HDVHPHQHD GDWRULWYDULDQWHORUGHFRPSLODUHSHQWUX3&-uri C începuse VúLpiardGLQportabilitate. În acel moment Institutul 1D LRQDOpentru Standarde al Americii (ANSI), a creat un subcomitet pt. a defini o versiune RILFLDOSWlimbajul C, rezultând astfel ANSI C. /LPEDMXO & úL YHUVLXQLOH VDOH 223 Object Oriented 3URJUDPPLQJ & úL PDL QRXO 9LVXDO & VXQW SULQWUH FHOH PDL IRORVLWH OLPEDMH GH SURJUDPDUH OD RUD DFWXDO $ IRVW SURLHFWDW SRUQLQG GH OD GRX OLPEDMH GH]YROWDWH VSUH VIkUúLWXO DQLORU ¶ %&3/ úL prima sa versiune B. 8Q OLPEDM GH QLYHO VF]XW HVWH RULHQWDW SH DUKLWHFWXU úL SRDWH IL GHILQLW FD DYkQG IDFLOLWDWHDGHDVFULHLQVWUXF LXQLFHVHUHIHUGLUHFWODHOHPHQWHDOHUHVSHFWLYHLDUKLWHFWXUL interne (ca în cazul limbajului de asamblare).
9
Introducere in programare
8Q OLPEDM GH QLYHO vQDOW VDX PHGLX HVWH RULHQWDW SH SUREOHP SHUPL kQG SURJUDPDWRUXOXL VúL VWUXFWXUH]H SURJUDPXO FkW PDL DSURSLDW SRVLELO GH SUREOHPD FH WUHEXLHLPSOHPHQWDW$FHVWOXFUXVFDGHúDQVHOHXQHLHURULGHSURLHFWDUHODQLYHOORJLFDO DSOLFD LHL&KLDUGDFSURJUDPXOYDILPDLLQHILFLHQWGLQSXQFWGHYHGHUHDOYLWH]HLúLDO RFXSULL UHVXUVHORU SULQ FRPSDUD LH FX XQ SURJUDP VFULV vQ OLPEDM GH DVDPEODUH HO DUH DYDQWDMXOFSRDWHILVFULVUHSHGHúLH[WLQVFXXúXULQ Limbajul C SHUPLWH IRORVLUHD DPEHORU WHKQLFL SURJUDPDUH VWUXFWXUDW úL DFFHV GLUHFWODPDúLQIDSWFDUHOIDFHVILHIRDUWHIOH[LELO 8OWLPXOúLSRDWHFHOPDLLPSRUWDQWPRWLYSHQWUXvQY DUHDOLPEDMXOXL&HVWHIDSWXO FSHUPLWHWUHFHUHDFXXúXULQ OD&VDX-DYD 'HúLlimbajul C are o libertate foarte mare in scrierea codului, el acceptând multe forme de scriere care în alte limbaje nu sunt premise, acest lucru ascunde vQVúLpericole, în special pentru programatorii IUH[SHULHQ 6WUXFWXUDJHQHULFDXQXLSURJUDPVFULVvQ& 'HODSULPHOHSURJUDPHVFULVHHVWHELQHVVH LQFRQWGHRVHULHGHUHJXOLSULYLQG RUJDQL]DUHDFRGXOXLVXUV6WUXFWXUDJHQHUDODXQXLFRGYDIL Apeluri ale directivelor speciale de compilare; 'HFODUD LLGHELEOLRWHFLIRORVLWH 'HFODUD LLIXQF LLLQOLQH 'HFODUD LLDOHSURWRWLSXULORUIXQF LLORUSHUVRQDOH 'HFODUD LLYDULDELOHJOREDOH Corpul programului principal { GHFODUD LLYDULDELOH DSHOXULGHIXQF LL } &RUSXULOHIXQF LLORUGH]YROWDWHGHSURJUDPDWRU ([LVW FkWHYD REVHUYD LL VXSOLPHQWDUH SULYLQG WHKQRUHGDFWDUHD FRGXOXL VXUV GH FDUHDUILELQHVVH LQVHDPD 1. 6QXVHVFULHLQVWUXF LXQLOHXQDGXSDOWDFLXQDVXEDOWD 2. 7RDWHLQVWUXF LXQLOHFDUHIRUPHD]XQEORFSHQWUXRDOWLQVWUXF LXQHVILHGHSODVDWH ID GHvQFHSXWXOUHVSHFWLYHLLQVWUXF LXQL (WDSHOHJHQHUULLXQXLSURJUDPH[HFXWDELOSRUQLQGGHODRSUREOHPUHDO 8QD GLQ JUHúHOLOH vQFHSWRULORU vQ SURJUDPDUH FRQVW vQ IDSWXO F SXQ SUHD PXOW DFFHQW SH LQWURGXFHUHD XQRU SURJUDPH JDWD VFULVH IU FD vQ SUHDODELO V VWXGLH]H SUREOHPDGHSRUQLUHSUHFXPúLDOJRULWPXOJHQHUDWvQXUPDDQDOL]HLDFHVWHLSUREOHPH ÌQPRPHQWXOJkQGLULLXQXLSURJUDPSHOkQJDOWHFRQVLGHUHQWHWUHEXLH LQXWFRQWúLGH IDSWXOFHVWHRPDUHGLIHUHQ vQWUHSURJUDPHOHbune si cele care merg. Un program bun nu numai cPHUJHGDUHVWHúLXúRUGHvQ HOHVúLPHQ LQXW0XO LGLQWUHvQFHSWRULLvQ
10
Introducere in programare
programarea C-XOXLDXWHQGLQ DGHD seOLPLWDODSURJUDPHFDUHVPHDUJúLDFHVWIDSW conduce la GHSULQGHULQHVQWRDVH.
'H DFHHD PDL MRV YRP SUH]HQWD SDúLL QHFHVDUL D IL XUPD L vQ HODERUDUHD XQHL DSOLFD LL SRUQLQGGHODRSUREOHPUHDO 1. ÌQ HOHJHUHDSUREOHPHL 2. 5HDOL]DUHDPRGHOXOXLPDWHPDWLFGDFHVWHFD]XO 3. 9HULILFDUHDPRGHOXOXLPDWHPDWLFFXXQVHWGHGDWHUHDOVDXFDOFXODWHPDQXDO'DFVH REVHUY QHFRQFRUGDQ H VH YD UHOXD YHULILFDUHD SXQFWHORU DQWHULRDUH GDF QX VH FRQWLQX 4. ,GHQWLILFDUHD GDWHORU GH LQWUDUH D GDWHORU GH LHúLUH SUHFXP úL D YDULDELOHORU intermediare. 5. $OHJHUHDWLSXULORUGHGDWHIRORVLWHSHQWUXUH LQHUHDvQFXUVXOSURFHVULLDUHVSHFWLYHORU date. 6. Specificarea modului de LQWHUID DUHFXXWLOL]DWRUXOODQLYHOGHLQWUDUHVDXLHúLUH 7. Generarea schemei logice sau a pseudocodului ce descrie algoritmul propus pentru rezolvarea problemei. 8. Verificarea modelului creat la punctul 7 cu un set date reale sau calculate manual. În caz de QHFRQFRUGDQ HVHUHLDYHULILFDUHDSXQFWHORUDQWHULRDUHGDFQXVHFRQWLQX 9. 6FULHUHDFRGXOXLVXUVWUDQVSXQHUHDvQWUXQOLPEDMGHSURJUDPDUHRDUHFDUH 10.(OLPLQDUHDHURULORUGHVLQWD[ 3HQWUXDH[HPSOLILFDSDúLLPHQ LRQD LDQWHULRUYRPOXDXQH[HPSOXH[WUHPGHVLPSOX “6VHUHDOL]H]Hun program care UH]RYRHFXD LHRDUHFDUHGHJUDGXO,,” Pasul 1 În acest caz este simplu, e clar Fne referim la o HFXD LHGHfoma ax+bx+c=0; Pasul 2 Modelul matematic este cel studiat în liceu: Fie ∆ = b − ⋅ a ⋅ c atunci :
−E ± E − ⋅D⋅F . ⋅D −E ± L⋅ −E + ⋅D⋅F . 'DF∆<0 atunci: [ = ⋅D E 'DF∆=0 atunci: [ = − . ⋅D Pasul 3 În acest caz poate fi evitat modelul matematic este prea simplu. 'DF∆>0 atunci: [
=
Pasul 4 6 YHGHP GHFL FDUH DU IL GDWHOH FH WUHEXLHVF IXUQL]DWH GH XWLOL]DWRU $YkQG vQ YHGHUHFVHGRUHúWHUH]ROYDUHDXQHLHFXD LLJHQHULFHGHRUGLQXO,,HVWHFODUF - a,b,c date de intrare
11
Introducere in programare
- x GDWHGHLHúLUH - ∆GDWLQWHUPHGLDU Analizând modHOXO PDWHPDWLF REVHUYP F D WUHEXLW V IRORVLP XQ VLPERO VXSOLPHQWDU IXQF LH GH FDUH VH IDFH vQWUHDJD GLVFX LH $FHVW VLPERO QX HVWH QLFL GDW GH XWLOL]DWRUúLQLFLQXLQWHUHVHD]SHQWUXUH]XOWDWXOILQDOGHFL∆HVWHRGDWLQWHUPHGLDUGH calcul.
Pasul 5 $FXP WUHEXLH V VSHFLILFP WLSXO GH GDW IRORVLW SHQWUX UHSUH]HQWDUHD LQWHUQ vQ PHPRULDFDOFXODWRUXOXL DYDULDELOHORU'HFLDYHPFkWHYDGDWHVXSOLPHQWDUHODGLVSR]L LH pentru a vedea tipul de reprezentare care trebuie ales: - VSHFLILFD LLOHXWLOL]DWRUXOXL - modelul matematic - H[SHULHQ DvQSURJUDPDUH ÌQ DFHVW FD] PRGHOXO PDWHPDWLF QH VSXQH F DEF VXQW QXPHUH UHDOH )U R VSHFLILFDUHFODUDXWLOL]DWRUXOXLFDUHVvQGLFHFHOHDSDU LQDOWXLLQWHUYDOHVWHREOLJDWRULX FDVOXPvQFRQVLGHUDUHPRGHOXOFHOPDLH[WLQVGHFLFHOPDLDGDSWDELO'HVLJXU FQX YRP DOHJH FHO PDL FXSULQ]WRU WLS GH GDWH SXV OD GLVSR]L LH GH FWUH FRPSLODWRU vQ OLSVD XQRUVSHFLILFD LLH[WHUQH'HFLa,b,c vor fi de tipul float. 9DULDELOD LQWHUPHGLDU ∆ HVWH UH]XOWDW DO DSOLFULL XQHL IXQF LL OLQHDUH DVXSUD XQRU YDULDELOHGHWLSUHDOGHFLúLGHOWDYDWUHEXLVILHfloat. 'DWHOH GH LHúLUH [ GXS FXP VH REVHUY VXQW UH]XOWDWXO XQHL vQSU LUL úL GHFL REOLJDWRULXVXQWUHDOHLQGLIHUHQWGHWLSXOGHGDWDOHVSHQWUXGDWHOHGHLQWUDUH
Pasul 6 ÌQOLSVDXQRUVSHFLILFD LLGHODXWLOL]DWRUYRPDOHJHSHQWUXFLWLUHDGDWHORUFHDPDL VLPSOPHWRGFLWLUHDGLUHFWGXSFXPDPIFXWúLSkQDFXP3HQWUXDILúDUHvQVDYHP GRXPHWRGHDILúDUHDGHWLSXO5H[ úL,P[ VDXGLUHFWUH]XOWDWXOFDvQFDOFXOXOPDQXDO 9RPDOHJHFHDGHDGRXDPHWRG Pasul 7 3VHXGRFRGXOYDDUWDFDPDLMRV FLWHúWHD FLWHúWHE FLWHúWHF FDOFXOHD]GHOWDGXSIRUPXOGHOWD E E D F GDFGHOWD>=0 atunci { x=(-b+sqrt(delta))/(2*a); x=(-b-sqrt(delta))/(2*a); afiDILúHD][[ } altfel { delta=-delta; delta=sqrt(delta)/(2*a)
12
Introducere in programare
afiúHD]x1 de forma re+i*im afiúHD]x2 de forma re-i*im }
CâWHYDREVHUYD LLUHODWLYHODFRQVWUXF ia pseudocodului - VH REVHUY F QX DP WUDWDW VHSDUDW VLWXD LD GHOWD SHQWUX F ]HUR OD DGXQDUH QX PRGLILFUH]XOWDWXO - vQDGRXDSDUWHDPIRORVLWXQDUWLILFLXGHFDOFXOvQVHQVXOFDPSVWUDWWRWvQYDULDELOD GHOWD UH]XOWDWH SDU LDOH GH FDOFXO $OWIHO DU IL WUHEXLW V LQWURGXFHP YDULDELOH VXSOLPHQWDUHSHQWUXSVWUDUHDS LLvQWUHJLúLDSU LLUHDOH&RGXODUILIRVWPDLFODUGDU vQDFHODúLWLPSDPILIRORVLWPDLPXOWPHPRULH'DFvQFD]XODFHVWXLSURJUDPFkWHYD YDULDELOH VXSOLPHQWDUH QX QH GHUDQMHD] vQ FD]XO XQRU SURJUDPH IRDUWH PDUL DFHVW OXFUX SRDWH ULGLFD JUHXW L FH QX SRW IL HOLPLQDWH GHFkW SULQ WUHFHUHD OD DORFDUHD GLQDPLFDPHPRULHL$FHDVWPHWRGYDILSUH]HQWDWXOWHULRU - VH REVHUY F DP VFULV PXOW FRG VXSOLPHQWDU FDUH GLQ SXQFW GH YHGHUH DO FDOFXOXOXL brut nu ne trebuie, dar din punct de vedere al modului în care îi prezint rezultatul uni XWLOL]DWRUHVWHHVHQ LDOSHQWUXFHVWHH[DFWvQIRUPDPDWHPDWLFFXQRVFXW Pasul 8 Va fi evitat pentru acest exemplu deoarece este prea simplu. Pasul 9 Codul în cazul limbajului C va DUDWDFDPDLMRV #include <stdio.h> * #inlude
// aici eroare de sintaxGHWHFWDELOGHinterpretor void main(void) { float a,b,c,delta,x1,x2;
**
***
clrscr() printf(“Pentru calculul ecuatiei a*x*x+b*x*c=0 dati coeficientii”); printf(“\na=”); scanf(“%f”,a); //aici eroare de VLQWD[QHGHWHFWDELOGHinterpretor printf(“\nb=”); scanf(“%f”,&b); printf(“\nb=”); scanf(“%f”,&b); delta=b*b+4*a*c; // aici eroare de calcul if( delta >=0) { x=(-b+sqrt(delta))/(2*a); x=(-b-sqrt(delta))/(2*a); printf(“\nx1=%f”,x1) printf(“\nx2=%f”,x2) }
13
else {
Introducere in programare
delta=-delta; delta=sqrt(delta)/(2*a) printf(“x1=%f+i*%f”,-b/(2*a),delta); printf(“x2=%f-i*%f”,-b/(2*a),delta); } } Pasul 10 'XSFXPVHREVHUY DPLQWURGXVLQWHQ LRQDWWUHLWLSXULGHJUHúHOL 3ULPD PDUFDW FX R VWHDHVWH JUHúHDO GH VLQWD[ FH YD IL GHWHFWDW GH FRPSLODWRU úL GHFL R SXWHP FRUHFWD vQ DFHDVWID] Pasul 11 ÌQ FDGUXO UXOULL SURJUDPXOXL UH]XOWDW VH REVHUY F DFHVWD QX IXQF LRQHD] FRUHFW /D H[HFX LDSDVFXSDVVHYDREVHUYDFvQYDULDELODDQXVHGHSXQHFHHDFHVHFLWHúWHGHOD WDVWDWXU (VWH R JUHúHDO GH ORJLF GH SURJUDPDUH SHQWUX F scanf DúWHDSW XQ pointer pentru a returna corect rezultatul, iar a nu este declarat SRLQWHU WUHEXLH WUDQVPLV DGUHVD OXLD$FHDVWHURDUHQXSRDWHILGHWHFWDWGHinterpretor. 'HDVHPHQHD FX WUHL VWHOH DP PDUFDW R JUHúHDO GH WUDQVSXQHUH D DOJRULWPXOXL R JUHúHDOVLPSOGHWUDQVFULHUHD XQHL IRUPXOH GH FDOFXO 'HDELD GXS HOLPLQDUHD DFHVWRU erori se poate continua testarea programului. (VWHELQHFDVH[LVWHXQ comentariu la vQFHSXWXORULFUHLVXUVHL&DYkQGXUPWRUXO FRQ LQXW /*----autor: GDWDFUHULL/modificrii ------copyright */
14
CAPITOLUL 3
Introducere in programare
1R LXQHDGHYDULDELOúLWLSXULGHYDULDELOH 2YDULDELO HVWHXQLGHQWLILFDWRUQXPH GDWGXSGRULQ GHFWUHXWLOL]DWRUXQHL]RQH din memorie cu care se va lucra (stoca sau modifica datele utile programului) pe SDUFXUVXOVFULHULLFRGXOXLVXUV ÌQ OLPEDMXOXL & SRW IDFH DVHPHQHD UHIHULUL GLUHFWH OD R ]RQ GH PHPRULH SULQ intermediul SRLQWHULORU DYkQG WRWRGDW DYDQWDMXO VLQWD[HL IDFLOH D XQXL OLPEDM GH QLYHO mediu. 3URJUDPXO VDX FRGXO VXUV HVWH WUDGXV SULQ LQWHUPHGLXO FRPSLODWRUXOXL & vQWUXQ program executabil. În cadrul acestui proces se face identificarea dintre numele variabilei úL XQ DQXPLW QXPU GH ORFD LL GH PHPRULH 7RW vQ VDUFLQD FRPSLODWRUXOXL UPkQH úL JHQHUDUHD GH LQVWUXF LXQL FDUH V VH RFXSH GH VFULHUHD VDX FLWLUHD vQ EORF D PDL PXOWRU ORFD LLGHRGDW2YDULDELOSRDWHRFXSDPDLPXOWGHRORFD LHGHPHPRULH Caracteristicile variabilelor sunt : 1. )LHFDUHYDULDELODUHXQQXPH 2. )LHFDUHYDULDELODSDU LQHXQXLWLSGHGDW 3. )LHFDUH YDULDELO UH LQH R YDORDUH VDX XQ VHW GH YDORUL XWLOH SHQWUX SURJUDP $FHVWH valori pot fi: - GHSXVHvQPHPRULHSULQLQL LDOL]DUHvQSURJUDP - SRWILFLWLWHGHODWDVWDWXUVDXGHODDOWGLVSR]LWLYGHLQWUDUH /HJDWGHQXPHOHXQHLYDULDELOHVHSRWIDFHFkWHYDREVHUYD LL 1. 1XPHOH XQHL YDULDELOH VH DOHJH GH RELFHL DVWIHO vQFkW V GHVFULH FH vQVHDPQ SHQWUX programator, din punct de vedere al SVHXGRFRGXOXL vQWUR IRUP FDUH SRDWH IL SUHVFXUWDW VDX QX GXS SUHIHULQ 'H H[HPSOX vQ FD]XO SUREOHPHL GLQ ILJXUD QXPHOH XQHL YDULDELOH FH UH LQH R QRW SXWHD IL QRWDB VDX Q VDX 1RWDB &D REVHUYD LH OLPEDMXO & HVWH ³FDVH VHQ]LWLY´ DGLF IDFH GLVWLQF LH vQWUH OLWHUHOH PLFL úL PDUL IRORVLWH vQ VFULHUHD FRGXOXL VXUV GHFL 18 6( &216,'(5 $ ),,1' $&(/$ù, 180( '( 9$5,$%,/ Ì1 &$=8/ Ì1 &$5( (67( 6&5,6 &8 LITERE MARI SAU CU LITERE MICI. Mai concret nota_1 ≠ Nota_1≠NOTA_1. 2. &kQGVHGRUHúWHFDXQQXPHGHYDULDELOVILHFRPSXVGLQGRXFXYLQWH186(9$ /6$ 63$,8 Ì175( (/( FFL YRU IL FRQVLGHUDWH GRX FXYLQWH VHSDUDWH HOH WUHEXLHVFXQLWHFXOLQLHLQIHULRDUunderscore). 3. 1XPHOHGHYDULDELOHVXIHUúLGHXUPWRDUHOHUHVWULF LL − QXSRWvQFHSHFXXQQXPUM − nu pot începe cu $ ( $ abc ) − nu pot fi cuvinte rezervate ( int ) − nu pot FRQ LQHcaractere speciale ( bab*#*@l ) excep LHIFQG“underscore” ex. bad_babe
15
Introducere in programare
,QL LDOL]DUHDYDULDELOHORU
ÌQOLSVDXQHLLQL LDOL]ULH[SOLFLWHRYDULDELOORFDOYDFRQ LQHRYDORDUHRDUHFDUHDOHDWRDUH QXPLWvQPRGFXUHQW ³JDUEDJH´ $FHDVW YDORDUH VH JVHD vQ PHPRULD DORFDW UHVSHFWLYHL YDULDELOHODODQVDUHDvQH[HFX LDDSURJUDPXOXL ,QL LDOL]DWRULLH[SOLFL LVHSODVHD]GXSGHFODUDWRUXOODFDUHVHUHIHUVHSDUD LGH acesta prin semnul egal (de exemplu nr1=234). H H OYDOXH
UYDOXH
'DFRELHFWXOLQL LDOL]DWHVWHXQDJUHJDWXQFRQJORPHUDWGHGDWHUHFXQRVFXWVXEXQ QXPHFRPXQ SHQWUXLQL LDOL]DUHVHIRORVHúWHROLVWGHH[SUHVLLvQFKLVvQDFRODGHXQGHOD ILHFDUHFRPSRQHQWFRUHVSXQGHFkWHRH[SUHVLH 7UHEXLHUHVSHFWDWHXUPWRDUHOHUHJXOL 1. QXPUXO GH LQL LDOL]DWRUL QX SRDWH IL PDL PDUH GHFkW QXPUXO GH FRPSRQHQWH GH LQL LDOL]DW 2. QXVHSRWLQL LDOL]DGHFkWRELHFWHVDXWDEORXULFXGLPHQVLXQHGHILQLWDGLFVHFXQRDúWH QXPUXOPD[LPGHYDORULGHLQWUDUHFHSRWILGDWHGHXWLOL]DWRUSHQWUXDILSURFHVDWH 3. H[SUHVLLOHIRORVLWHSHQWUXLQL LDOL]DUHWUHEXLHVILHFRQVWDQWHvQXUPWRDUHOHVLWXD LL ODLQL LDOL]DUHDXQXLRELHFWGHWLSVWDWLFvQ&QHVHPDLDSOLF ODLQL LDOL]DUHDXQXLWDEORXXQHLVWUXFWXUL sau unei uniuni. 4. QX VH SRW LQL LDOL]D LGHQWLILFDWRUL FX YDODELOLWDWH vQ LQWHULRUXO XQXL EORF GDU FX OHJDUH H[WHUQVDXLQWHUQ 5. GDF R OLVW vQFKLV vQWUH DFRODGH DUH PDL SX LQH HOHPHQWH GHFkW FRPSRQHQWH DUH DJUHJDWXOLQL LDOL]DWUHVWXOFRPSRQHQWHORUVHYRULQL LDOL]DDXWRPDW 2ELHFWHOHGHWLSVFDODURVHULHGHGDWHGHDFHODúLWLSDFURUGHILQL LHSRDWHILGDWFD R vQúLUXLUH GH GDWH FH SRW IL LGHQWLILFDWH SULQ SR]L LD ORU vQWUR vQúLUXLUH VH LQL LDOL]HD] IRORVLQGRVLQJXUH[SUHVLHFDUHSRDWHILHYHQWXDOvQFKLVvQDFRODGH8QLXQLOHJUXSULGH PDL PXO L EL L GH GLPHQVLXQH GLYL]LELO FX GRL VH LQL LDOL]HD] FX R VLQJXU H[SUHVLH corespunzând ca tip primului membru al uniunii. 6WUXFWXULOHJUXSULGHGLYHUVHGDWHGHWLSXULGLIHULWHVXEXQQXPHFRPXQ úLXQLXQLOH VHSRWLQL LDOL]DFXRH[SUHVLHGHWLSVWUXFWXUVDXXQLXQHFRUHVSXQ]WRDUH9DORDUHDLQL LDOD structurii sau uniunii va fi valoarea obiectului citat. 3UH]HQ DXQHLLQL LDOL]ULvQWURGHFODUD LHLQGLFRGHFODUD LHGHILQLWRULHFHVSHFLILF FXFHYDILXPSOXWUHVSHFWLYXOVSD LXGHPHPRULH 0DLMRVHVWHSUH]HQWDWXQH[HPSOX int zile [7] = { 1, 1, 1, 1, 1, 1, 1 } ; char name1 [] = { "Costica" } ; char name2 [] = "Dragan" ; struct datep { int i ; // nr. ani char str [21] ; //nume double d ; //salar } vas = { 28, "Vasile", 1500000.0 } ;
16
Introducere in programare
$FHHDúL YDULDELO QX VH SRDWH LQL LDOL]D FX GRX YDORUL GHFkW vQ FD]XO vQ FDUH VH UHQXQ ODYDORDUHDDQWHULRDUFXFDUHDIRVWLQL LDOL]DW 1XSRWH[LVWDGRXGHFODUD LLGHILQLWRULLFDUHvLVSHFLILFFRQ LQXWXO vQWU-un program. 3.2.Tipuri de variabile 8QD GLQ FHOH PDL GLILFLOH SUREOHPH FDUH DSDU vQ FDGUXO vQY ULL XQXL OLPEDM GH SURJUDPDUHHVWHGHVFULHUHDXQHLVLWXD LLGLQOXPHDUHDOIRORVLQGHOHPHQWHOHUHVSHFWLYXOXL limbaj. ÌQ DIDU GH SURSXQHUHD XQXL DOJRULWP SHQWUX D UH]ROYD R SUREOHP UHDO SURJUDPDWRUXO PDL HVWH FRQIUXQWDW úL FX XQ DOW DVSHFW GLILFLO DOHJHUHD WLSXOXL GH GDW SHQWUXRYDULDELOWUHEXLHIFXWvQFRQFRUGDQ FXYDORULOHFDUHOHSRDWHOXDDFHDVWD 'H DFHHD SULPXO SDV HVWH V YHGHP FH WLSXUL IXQGDPHQWDOH GH GDWH QH SXQH OD GLVSR]L LHOLPEDMXO& SUHFXP úL JDPD GH YDORUL vQ FDUH HYROXHD] DFHVWHD 2 GDW úWLXWH DFHVWHOXFUXULSXWHPvQFHSHVYHGHPFDUHHVWHOHJWXUDFXH[HPSOHOHGLQYLD DUHDO 'LQDQDOL]DLQIRUPD LHLFDUHFLUFXOFXUHQWvQWUHRDPHQLUH]XOWFH[LVWGLQSXQFW GHYHGHUHDOSURFHVULLGRXWLSXULGHLQIRUPD LH - LQIRUPD LH RE LQXW SULQ PVXUWRUL DVXSUD IHQRPHQHORU UHDOH FDUH VXQW H[DFWH úL DVXSUDFURUDVHIDFSUHOXFUULPDWHPDWLFHGHRELFHL - LQIRUPD LH VXE IRUP FRQGHQVDW FDUH GH RELFHL HVWH UHFRPSXV DVRFLDWLY vQ PLQWHD XPDQFXPDUILOLPEDMXOVLLQIRUPD LDVFULV 'HDLFLUH]XOWFvQWUXQOLPEDMGHSURJUDPDUHDYHPQHYRLHGHGRXWLSXULGHYDULDELOH FDUHVSRDWILIRORVLWHSHQWUX -
stocarea unor valori numerice. VWRFDUHDXQRUFDUDFWHUHLQIRUPD LHVFULV
&RQIRUP DFHVWRU QHFHVLW L OLPEDMXO & D IRVW SUHY]XW FX XUPWRDUHOH WLSXUL fundamentale de date: -
intVWRFKHD]QXPDLQXPHUHvQWUHJL float, doubleSHQWUXQXPHUH]HFLPDOHFDOFXOvQYLUJXOPRELO charSHQWUXDVWRFDFRGXOXQXLFDUDFWHUVDXXQQXPUvQWUHJUHSUH]HQWDWSHPD[LPXP RSWEL LLQFOX]kQGúLVHPQXO 2EVHUYD LHchar[QU@YDUH LQHXQúLUGHnr caractere sau întregi. 8QDGLQWUHXWLOL]ULOHúLUXULORUGHFDUDFWHUHDUILPHVDMHOHGHGLDORJFXXWLOL]DWRUXOXQXL SURJUDPVDXPHVDMHOHGH HURDUH ÌQ DFHVWFD]QXQHLQWHUHVHD] FRQ LQXWXO GLQ SXQFW GH vedere al valorii lor numerice ca atare. )XQF LH GH QHFHVLW LOH SURJUDPDWRUXOXL SXWHP VSXQH F H[LVW GRX FODVH GH WLS GH date care pot fi folosite în cazul unui limbaj de nivel mediu sau înalt: 1. tipuri de date predefinite tipuri de date definite de utilizator.
17
CAPITOLUL 4
Introducere in programare
4. Tipuri de date predefinite 4.1. Date numerice /LPEDMXO & HVWH SUHY]XW FX XQ VHW GH GDWH predefinite. Aceste tipuri de date sunt FRQFUHWL]DWH SULQ IRORVLUHD XQXL QXPU GH ORFD LL GH PHPRULH SHQWUX VWRFDUHD FRQIRUP XQXLDOJRULWPELQHGHILQLWIXQF LHGHWLSXOGHGDWDXQXLWLSGHGDWHH[LVWHQWHÌQFD]XO tipului de date FKDU GHúL D IRVW LQWURGXV SHQWUX VWRFDUHD GDWHORU GH WLS DOIDQXPHULF HO SRDWHILIRORVLWvQFDOFXOHPDWHPDWLFHúLORJLFHvQLQWHUYDOXOGHYDORULQXPHULFHSHFDUHOH poate stoca. 3HQWUXWLSXULOHGHGDWHFHVWRFKHD]YDORULQXPHULFHvQWUHJLSUHVFXUWDWLQW VDXGXS cum am spus FKDUQXPUXOHVWHVWRFDWvQPHPRULHGLUHFWVXEIRUPDUH]XOWDWODFRQYHUVLD în binar. În cazul datelor de tip real, float, sau GRXEOHODFRQYHUVLDGLUHFWDQXPUXOXLvQED]D GRLVDUILSLHUGXWSUHDPXOWHORFD LLGHPHPRULHúLGHDFHHDHOHVH SVWUHD] VXE IRUPD úWLLQ LILF$FHDVWIRUPVHUHIHUODIDSWXOFH[LVWR]RQGHPHPRULHFDUH LQHPDQWLVD úL R ]RQ GH PHPRULH FDUH LQH H[SRQHQWXO 8Q H[HPSOX GH UHSUH]HQWDUH úWLLQ LILF GLQ punct de vedere matematic ar fi: 123344445455454=1.23344445455454 * 1014 =1.23344445455454E+14 unde mantisa=1,23344445455454 iar exponentul = 14. 0DLMRVSUH]HQWPRVHULHGHWLSXULGHGDWHSUHGHILQLWHGLPHQVLXQLOHORUúLGRPHQLXO YDORULORU FDUH SRW IL VWRFDWH 6H REVHUY F VDX IRORVLW PRGLILFDWRUL GH WLS 3ULP PRGLILFDWRULGHWLSVHvQ HOHJFXYLQWHFKHLHFDUHDXFDHIHFWODFRPSLODUHFDGLPHQVLXQHD vQRFWH L SHFDUHHVWHUHSUH]HQWDWXQWLSGHGDWHVFUHDVFFXXQIDFWRUFDUHHVWHVDXR putere a lui doi)). char ∈{–128, 127} unsigned char ∈{0, 255} IUVHPQ signed char ∈{–128, 127} semn int ∈{–32768, 32767 } cuvânt (2 octeti) unsigned int ∈{0, 65535} semn signed int ∈{–32768, 32767} semn short int ∈{–32768, 32767} semn unsigned short int ∈{0, 65535} semn
1 octet 1 octet 1
octet
cu
1 FXYkQW IU 1 cuvânt cu 1 cuvânt cu FXYkQW IU
18
Introducere in programare
signed short int –32768 to 32767 semn long int ∈{–2147483648, 2147483647} signed long int ∈{–2147483648, 2147483647} float ∈{–3.4E–38, 3.4E+38} double ∈{–1.7E–308, 1.7E+308} long double ∈{–3.4E–4932, 1.1E+4932}
1 cuvânt cu
2 cuvinte 2 cuvinte cu semn 2 cuvinte 4 cuvinte 5 cuvinte
2EVHUYD LH $FHVWH LQWHUYDOH VXQW GH RELFHL VSHFLILFH VLVWHPXOXL GH RSHUDUH VXE FDUH OXFUHD]FRPSLODWRUXOúLPDúLQLLSHFDUHDFHVWDHVWHH[HFXWDW'RPHQLLOHGHPDLVXVVXQW valabile pentru compilatorul de C++ realizat de Borland. 2SHUDWRUXOYLUJXO a,b ÙHYDOXHD]DHYDOXHD]E $FHVWRSHUDWRUSHUPLWHHYDOXDUHDDGRXVDXPDLPXOWHH[SUHVLLGLVWLQFWHLQORFXULOHLQ FDUHRVLQJXUH[SUHVLHHVWHDGPLVDILIRORVLWDVWDHUD LXQHDLQWURGXFHULLOXL De ex: for ( j=0,k=100 ; k-j>0 ; j++,k-- ) este chivalent cu j=0 ; k=100; j=0 , k=100 { { … … j++; sau j++; k--; k--; } } 8QDOWH[HPSOXIXQF LDbreak_line: break_line (int interval) { int c,j; for (c=getchar(),j=0 ; c!=EOF ; c=getchar (),putchar (c),j++) if (j % interval = = 0 ) printf( “\n” ) ; } 4.2. Date alfanumerice ÌQ FDGUXO SURLHFWULL XQHL DSOLFD LL SRW DSDUH SH OkQJ FHULQ H OHJDWH GH FDOFXOH QXPHULFHFRPSOH[HúLFHULQ HSHQWUXSURFHVDUHDGHWH[WHVDXDXQRUED]HGHGDWH ToWDOLWDWHDFDUDFWHUHORUFHSRWILDILúDWHVDXOLVWDWHvQFD]XOOXFUXOXLVXEXQVLVWHP GH RSHUDUH FODVLF 81,; VDX '26 VH QXPHVF FDUDFWHUH DOIDQXPHULFH DOIDEHWLFH úL QXPHULFH )LHFDUH FDUDFWHU DUH R LPDJLQH SH HFUDQ $FHDVW LPDJLQH HVWH VWRFDW vQ memoria QHYRODWLODFDOFXODWRUXOXLúLDUHDWDúDWXQFRGXQLFODFDUHVHIDFHDSHOFkQGVH GRUHúWHDILúDUHDVDXOLVWDUHDHL 'DWRULW QHFHVLW LL FD XQ WH[W HGLWDW SH XQ VLVWHP IDEULFDW GH R DQXPH ILUP VúL SVWUH]H vQ HOHVXO VD WUHFXW OD VWDQGDUGL]DUHD FRGULL LPDJLQLORU FDUDFWHUHORU $VWIHO D UH]XOWDWVWDQGDUGXO$6&,,IRORVLWODRUDDFWXDOGHWRDWHFDOFXODWRDUHOHGLQVHULD; ([LVWGHFDUDFWHUHvQDFHVWVWDQGDUG1XYRPIDFHUHIHULULODVHWXULOH H[WLQVHGH caractere specifice editoarelor de teste sau sistemelor de operare de tip Windows sau XWindows.
19
Introducere in programare
3HQWUXVWRFDUHDXQXLúLUGHFDUDFWHUHOLPEDMXO&IRORVHúWHXQWDEORXmonodimensional GH FDUDFWHUH FDUH SRDWH VWRFD FRGXULOH D PD[LPXP GH FDUDFWHUH R GDW vQ FD]XO GHFODUULLVWDWLFH 2EVHUYD LD 'DF VD GHILQLW YDULDELOD V FD DYkQG FDSDFLWDWHD GH VWRFDUH GH GH caractere iar la citirea ei sunt introduse mai mult de 25, atunci numai pentru primele 25 QXH[LVWULVFXOSLHUGHULLORUSHSDUFXUVXOH[HFX LHLSURJUDPXOXL ÌQ DFHVWFD] SRW DSUHD erori mascate în cod. 2EVHUYD LDÌQFD]XOvQFDUHQXDPGHILQLWQXPUXOGHFDUDFWHUHFDUHSRDWHILVWRFDW vQ UHVSHFWLYD YDULDELO IXQF LH GH PRGXO GH FRQVWUXF LH DO FRPSLODWRUXOXL ILH VH DORF GLPHQVLXQHDPD[LPSRVLELOILHODSULPDLQL LDOL]DUHVHDORFH[DFWFkWHVWHQHFHVDUGDU nu peste maximul definit. 2EVHUYD LD 1XH[LVWQLFLROHJWXUvQWUHYDORDUHDQXPHULF[úLFDUDFWHUXO[FDUH apare pe ecran. De exemplu 2 ≠ ‘2’. ,QL LDOL]DUHDXQHLGDWHFXRYDORDUHXWLOSRDWHDYHDORFvQXUPWRDUHOHPRGXUL - la declararea ei; - la citirea datelor de intrare; - oriunde în program . 4.2.1. Caractere speciale de control 3HOkQJFDUDFWHUHOHGLUHFWDILúDELOHPDLH[LVWvQOLPEDMXO&RVHULHGHFDUDFWHUHGH FRQWURO$FHVWHDSURYLQGLQQHFHVLWDWHDGHFRQWURODXQHLLPSULPDQWHúLVLPXOWDQDXIRVW IRORVLWHúLODDILúDUHDSHWHUPLQDOHOHDOIDQXPHULFH \a \b \f
Cod 07h 08h 0Ch
\n \r
0Ah 0Dh
\t \v \\ \’ \” \? \ooo \xhh
09h 0Bh 5Ch 27h 22h 3Fh ooo hh
6HFYHQ D
Caracter BEL (alert produce un semnal sonor) BS (backspace) FF (form feed PXW FXUVRU OD XUPWRDUHD SDJLQ
LF CR (carriage return, salt la inceput de linie QRX HT (horizontal tab) VT (vertical tab) \ (backslash) ’ (apostrof) ” (ghilimele) ? (semnul vQWUHEULL) Caracterul cu codul ooo în octal Caracterul cu codul hh în hexa
3HQWUX MXVWLILFDUHD LQWURGXFHULL DFHVWRU FDUDFWHUH GH FRQWURO VH YD SUH]HQWD XUPWRUXO FD]HUDLQXWLOWULPLWHUHDXQLQXPUIL[GHFDUDFWHUHLGHQWLFHGHILHFDUHGDWFkQGVHGRUHD XQ WDEXODWRU XQ DQXPLW QXPU GH VSD LL JRDOH OD vQFHSXWXO XQXL SDUDJUDI úL DWXQFL VD FRQYHQLW V VH IRORVHDVF XQ FDUDFWHU GH FRQWURO XUPkQG FD LPSULPDQWD VO UHFRQYHUWHDVF LQWHUQ vQ VSD LL JRDOH $QWHULRU VD SUH]HQWDW XQ WDEHO FH FRQ LQH DFHVWH caractere. 2SHUD LXQLVLPSOHGHFLWLUHVDXDILúDUHDGDWHORU)
20
Introducere in programare
/LPEDMXO&DUHODGLVSR]L LHRVHULHGHIXQF LLSHQWUXFLWLUHDGDWHORUGHODWDVWDWXU VDX SHQWUX DILúDUHD ORU SH HFUDQ &HOH PDL IRORVLWH GLQWUH HOH VXQW JUXSDWH vQ GRX biblioteci conio.h (&21VROH,QSXW2XWSXW+HDGHU úL StandardInputOutput.Header) stdio.h. )XQF LLGHLQWUDUHVDXLHúLUHFXXWLOL]DWRUXO)
Pentru a putea discuta primele programe este necesar studierea modului în care un SURJUDP SRDWH VFKLPED GDWH GLVSR]LWLYHOH VWDQGDUG GH LQWUDUH VDX LHúLUH PRQLWRU WDVWDWXU 'LQDFHVWSXQFWGHYHGHUHH[LVWGRXFODVHGHIXQF LL -
-
XQF LL GH LQWUDUH FX DMXWRUXO FURUD VLVWHPXO GH FDOFXO SULPHúWH GH OD WDVWDWXU YDORUL QXPHULFH FRPHQ]L úL RULFDUH DOW WLS GH LQIRUPD LH FH SRDWH IL IXUQL]DW SULQ intermediul acelui dispozitiv. XQF LLGHLHúLUHFXDMXWRUXOFURUDVLVWHPXOGHFDOFXOOLYUHD]UH]XOWDWHOHDILúHD] IRORVLQGXQGLVSR]LWLYVWDQGDUGGHLHúLUHPRQLWRUVDXLPSULPDQW
3HQWUXDFLWLRGDWVDXXQúLUGHGDWHGHODWDVWDWXUDYHPRIXQF LHVSHFLDOL]DWFDUH poate citi orice tip SUHGHILQLWGHGDWH)XQF LDDUHSURWRWLSXOSUH]HQWDWPDLMRV int scanf (const char *format [,address, ...]); 7LSXOGHGDWHHVWHVSHFLILFDWGHFWUHSURJUDPDWRUSULQPRGLILFDUHDXQRUSDUDPHWULDL IXQF LHL0DLMRVHVWHGDWXQVFXUWH[HPSOX #include <stdio.h> #include void main (void) { int a; char c; char b[20]; float d; FOUVFU úWHUJHHFUDQXO VFDQI³G´ D FLWHúWHXQvQWUHJúLOGHSR]LWHD]vQ // variabila a VFDQI³F´ F FLWHúWHXQFDUDFWHU VFDQI³V´E FLWHúWHXQúLUGHFDUDFWHUH VFDQI³I´ G FLWHúWHXQQXPU]HFLPDO getch(); } $FHDVW IXQF LHOD DSHODUHSUHLDLQIRUPD LDLQWURGXV GH OD WDVWDWXU GXS DSVDUHD WDVWHL (17(5 &D HIHFW XWLOL]DWRUXO YHGH GRDU F SURJUDPXO vúL RSUHúWH GHUXODUHD úL
21
Introducere in programare
DúWHDSW FHYD GDU IU D SULPL XQ PHVDM FDUH V vL FRPXQLFH FH WUHEXLH V IDF VDX LQIRUPD LLGHVSUHQDWXUDGDWHORUFDUHWUHEXLHVFOLYUDWH 0DLMRVHVWHSUH]HQWDWSURWRWLSXOXQHLIXQF LLGHVIRORVLWvQDILúDUHDGLYHUVHORUWLSXUL de date. int printf (const char *format [, argument, ...]); Ex: SULQWI³$ILúHD]FHOHQXPHUHGGG´QUQUQU
Vom prezenta un prim exemplu despre unele din SRVLELOLW LHGHDILúDUHDOHIXQF LHLprintf . #include <stdio.h> main() { int a; /*întreg simplu */ long int b; /* întreg lung */ short int c; /* întreg scurt */ unsigned LQWG vQWUHJIUVHPQ char e; /* caracter simplu */ IORDWI QXPU]HFLPDOvQVLPSOSUHFL]LH GRXEOHJ QXPU]HFLPDOvQGXESUHFL]LH a = 1023; b = 2222; c = 123; d = 1234; e = 'X'; f = 3.14159; g = 3.1415926535898; printf("a = %G?QD DILúDUHIRORVLQGED]DGHQXPHUD LH printf("a = %R?QD DILúDUHIRORVLQGED]DGHQXPHUD LH printf("a = %[?QD DILúDUHIRORVLQGED]DGHQXPHUD LH printf("b = %OG?QE DILúDUHVSHFLILFSHQWUXvQWUHJOXQJ printf("c = %G?QF DILúDUHVSHFLILFSHQWUXvQWUHJVFXUW printf("d = %X?QG DILúDUHVSHFLILFSHQWUXvQWUHJIUVHPQ printf("e = %F?QH DILúDUHVSHFLILFSHQWUXFDUDFWHU printf("f = %I?QI DILúDUHVSHFLILFSHQWUXQXPUvQVLPSOSUHFL]LH printf("g = %I?QJ DILúDUHVSHFLILFSHQWUXQXPUvQGXEOSUHFL]LH printf("\n"); /*salt la line QRX printf("a = %G?QD DILúDUHVSHFLILFSHQWUXvQWUHJ SULQWID G?QD DILúDUHVSHFLILFSHQWUXXQvQWUHJGDUDORFkQG caractere în dreapta */
22
Introducere in programare
SULQWID G?QD DILúDUHVSHFLILFSHQWUXXQvQWUHJGDU alocând 7 caractere în stânga */ c = 5; d = 8; printf("a = %*d\n",c,a); /* folosim un câmp de 5 caractere */ printf("a = %*d\n",d,a); /* folosim un câmp de 8 caractere */ printf("\n"); printf("f = %I?QI DILúDUHSHQWUXQXPUvQVLPSOSUHFL]LH printf("f = %12f\n",f); /* folosim un câmp de 12 caractere */ SULQWII I?QI IRORVLPXQFkPSGHFDUDFWHUHGXS YLUJXO SULQWII I?QI IRORVLPXQFkPSGHFDUDFWHUHGXS YLUJXO SULQWII I?QI DILúDUHVSHFLILFSHQWUXXQQXPUvQ VLPSOSUHFL]LHGDUDORFkQGFDUDFWHUHvQVWkQJD } 6UHOXPH[HPSOXOSUH]HQWDWODFLWLUHDGDWHORULQWURGXFkQGGHDFHDVWGDWúL mesajele pentru utilizator. #include <stdio.h> #include void main (void) { int a; char c; char b[20]; float d; FOUVFU úWHUJHHFUDQXO printf(“Introduceti o valoare intreaga”) VFDQI³G´ D FLWHúWHXQvQWUHJúLOGHSR]LWHD]vQ variabila a printf(“\nValoarea citita este %d”, a); printf(“\nDati un caracter ”) VFDQI³F´ F FLWHúWHXQFDUDFWHU printf(“\nCaracterul citit este %c”,c); prinf(“\nDati un sir de caractere ”); VFDQI³V´E FLWHúWHXQúLUGHFDUDFWHUH printf(“\nS-a citit urmatoarea propozitie %s”,b); printf(“\nDati un numar zecimal”); VFDQI³I´ G FLWHúWHXQQXPU]HFLPDO printf(“\n Sa citi numarul %f”,d) printf(“\nApasa orice tasta pentru a continua”) getch(); }
23
CAPITOLUL 5
Introducere in programare
5. Operatori ai limbajului C 8QRSHUDWRUUHSUH]LQWGLQSXQFWGHYHGHUHDOSURJUDPULLDFHOVLPEROFDUHDOWXUDWOD XQDVDXGRXQXPHGHYDULDELOHVSHFLILFRSHUD LDFHWUHEXLHUHDOL]DWDVXSUDORU)XQF LH GHQXPUXOGHYDULDELOHGHFLGHGDWH DVXSUDFUXLDVHDSOLFRSHUDWRULLSRWILFODVLILFD L vQGRXPDULFDWHJRULL -
operatori XQDULDFURUIRORVLUHSRDWHILFDPDLMRV - nume_var_i oper_unar nume_var_j; - oper_unar nume_var_i, unde i, j ∈N;
-
RSHUDWRULELQDULDFURUIRORVLUHHVWHFDPDLMRV - nume_var_i=nume_var_j oper_binar nume_var_k, unde i, j, k ∈N.
Limbajul C are mai multe tipuri de operatori: -
RSHUDWRULDULWPHWLFLFDUHGDXSRVLELOLWDWHDUHDOL]ULLRULFURUFDOFXOHDULWPHWLFH RSHUDWRUL UHOD LRQDOL FH VXQW IRORVL L SHQWUX WHVWDUHD UHOD LLORU FH SRW H[LVWD vQWUH FRQ LQXWXODGRXYDULDELOH RSHUDWRULORJLFLIRORVL LSHQWUX - DSXWHDUHDOL]DH[SUHVLLFRPSOH[HvQFDGUXOWHVWULLUHOD LLORUGLQWUHYDULDELOH - a procesa la nivel de bit variabilele.
5.1. Operatori aritmetici $FHVW WLS GH RSHUDWRUL UHDOL]HD] FDOFXOH PDWHPDWLFH VLPSOH 'HVLJXU F SUHFL]LD vQ FDUH YD IDFH FDOFXOXO UHVSHFWLYD DSOLFD LH LQH GH WLSXO FRQW GH GDW DOHV 2SHUDWRULL VXQW SUH]HQWD LvQOLVWDGHPDLMRV6FRQVLGHUPGRXYDULDELOHDúLEFDUHODXQPRPHQWGDW FRQ LQYDORULOHD úLE DWXQFL - ‘+’ adunare y=a+bÆy=8 - ‘-‘ VFGHUH y=a-bÆy=-2 - ‘*’ vQPXO LUH y=a*bÆy=15 - ‘/’ vPSU LUH y=a/bÆy=0.6000000…0 - ‘%’ modulo y=a%bÆ\ UHVWXOvPSU LULLOXLDODE 5.2 Expresiile 2H[SUHVLHFRQVWGLQWUXQFRQJORPHUDWUHDOL]DWGLQ Æ operanzi Æ operatori vQVFRSXOFDOFXOULLXQHLYDORUL %ORFXULOHGHED]FRQVWUXFWLYHDOHH[SUHVLLORUSRWIL − variabile − constante
24
− DSHOXULIXQF LH
Introducere in programare
([LVWSDWUXPDULWLSXULLPSRUWDQWHGHexpresii:. 1. Expresii constanteFDUHUH LQQXPDLYDORULFRQVWDQWH De exemplu 5 5+6*13/30 ‘a’ 2. Expresii întregi care VXQWH[SUHVLLFDUHGXSUHDOL]DUHDWXWXURUFRQYHUVLLORUDXWRPDWH sau explicite de tip, produc un rezultat care este de tip între. Presupunând j,k întregi DWXQFLXUPWRDUHOHH[SUHVLLvQWUHJL j j*k j/k+3 k-‘a’ 3+(int)5.0 3. ([SUHVLLvQYLUJXOPRELOFDUHVXQWH[SUHVLLFDUHGXSWRDWHFRQYHUVLLOHDXWRPDWHVDX H[SOLYLWHGHWLSSURGXFXQUH]XOWDWFDUHHVWHvQYLUJXOPRELO'DF[HVWHfloat sau GRXEOHDWXQFLXUPWRDUHOHH[SUHVLLVXQWGHDFHVWWLS x x+3 x/y*5 3.0 3.0-2 3+(float) 4 4. Expresii de tip pointer FDUHVXQWH[SUHVLLFDUHvQXUPDHYDOXULLGDXRDGUHV'H H[HPSOXGDFSHVWHpointer, iar j este întreg: p, p+1, (char* ) 0x00ffffff, &j, “abc” sunt expresii de tip pointer. 2UGLQHDGHHYDOXDUHDXQHLH[SUHVLLIXQF LHGHSUHFHGHQ DRSHUDWRULORUG 8QXOGLQSULPHOHOXFUXULGHFDUHWUHEXLH LQXWVHDPDFkQGVHVFULXH[SUHVLLDULWPHWLFH FRPSOH[H HVWH IDSWXO F VSD LLOH DOEH VSD LX WDEXODWRU VDOW OD OLQLH QRX QX DX vQ HOHV QXVXQWOXDWHvQFRQVLGHUDUH 'HH[HPSOXFHOHGRXH[SUHVLLGHPDLMRVVXQWHFKLYDOHQWH y=x*x+2*x-5*x/y; y=x * x + 2 * x – 5 * x / y; 2DPHQLL IRORVHVF FkWHRGDW VSD LX vQ ORF GH SDUDQWH]H SHQWUX D VSHFLILFD PRGXO GH HYDOXDUHDXQHLH[SUHVLL1R LXQHDGHHYDOXDUHVHUHIHUODRUGLQHDvQFDUHVHYRUUHDOL]D FDOFXOHOH GLQWUR H[SUHVLH PDWHPDWLF ÌQ FDGUXO XQXL OLPEDM GH SURJUDPDUH QXPDL
25
Introducere in programare
SDUDQWH]HOHMRDFDFHVWUROÌQOLSVDSDUDQWH]HORUGLQWURH[SUHVLHVHYRUUHVSHFWDUHJXOLOH GHSUHFHGHQ LPSOLFLW &DXQH[HPSOXVGLVFXWPPRGXOGHHYDOXDUHDH[SUHVLHGHPDLVXVGDFVHYD LQH VHDPD QXPDL GH SUHFHGHQ HOH LPSOLFLWH 'HRDUHFH RSHUDWRULL ^ ` DX DFHHDúL SUHFHGHQ FDUHvQVHVWHPDLPDUHFDDRSHUDWRULORU^`HYDOXDUHDH[SUHVLHLVHYDIDFH GHODVWkQJDODGUHDSWDGXSFXPXUPHD] 1. 2. 3. 4. 5. 6.
VHFDOFXOHD]YDORDUHDOXLx*x; VHFDOFXOHD]YDORDUHDOXL [ VHFDOFXOHD]YDORDUHDOXL [ VHFDOFXOHD]YDORDUHDOXL [ \ se face suma (x*x)+(2*x); VHIDFHVFGHUHDx*x)+(2*x) - (5*x)/y.
1R LXQHD GH SUHFHGHQ VH UHIHU OD PRGXO vQ FDUH LDX GHFL]LL vQ HYDOXDUHD XQHL H[SUHVLLGDFQXDPSXVHSDUDQWH]H0DLMRVHVWHSUH]HQWDWWDEHOXOFXWRDWHSUHFHGHQ HOH implicite: Operatori () [] Æ ! ~ ++ -- + - (unary) & (type) sizeof * / % + << >> < <= > >= == != & ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= ,
2UGLQHDGHSUHFHGHQ de la stânga la dreapta de la dreapta la stânga de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la stânga la dreapta de la dreapta la stânga de la dreapta la stânga de la stânga la dreapta
5.3. Tipuri aritmetice de date 1XPHUHOHvQWUHJLVXQWUHSUH]HQWDWHGLIHULWGH FHOHvQYLUJXO IORWDQW'H DFHHDDSDUH XUPWRDUHDSUREOHPFRQYHUVLDQXPHUHORUdintr − un format într-altul. 'DF VH GRUHúWH FD UH]XOWDWXO V ILH VWRFDW vQWUXQ DOW IRUPDW PDL PXOW VDX PDL SX LQ FXSULQ]WRUDWXQFLDFHVWDWUHEXLHGHSXVîntr−RYDULDELOGHUHVSHFWLYXOWLSúLFRQYHUVLDVH YDIDFHDXWRPDW([LVWFD]XULvQFDUHFRPSLODWRUXOGHúLDFFHSWFRQYHUVLDGXQPHVDM 26
Introducere in programare
GH DWHQ LRQDUH $FHDVW VLWXD LH DSDUH DWXQFL FkQG VH SRDWH DOWHUD YDORDUHD XQRU GDWH deoarece formatul în care fac conversia este mai mic. 'DF SURJUDPDWRUXO HVWH VLJXU F GRUHúWH UHVSHFWLYD FRQYHUVLH DWXQFL WUHEXLH V UHDOL]H]H R FRQYHUVLH H[SOLFLW SHQWUX FD UHVSHFWLYXO PHVDM GH DWHQ LRQDUH V GLVSDU 'HRDUHFHODFRQYHUVLLOHLPSOLFLWHVHSRDWHDOWHUDYDORDUHDFDOFXODWSRDWHDSUHDRHURDUH JUHXGHWHFWDELO S1= (int)((double)z/x+0.5); ÌQH[HPSOXOGHPDLVXVVHIRU HD]WUXQFKLHUHDGHODRUHSUH]HQWDUHGHWLSdouble la o reprezentare în numere întregi. 'DWHOHQXSRWILDOWHUDWHGDFVHUHDOL]HD]RFRQYHUVLHGHWLSvQWUHXQWLSGHGDWFX GRPHQLXGHYDORULPDLPLFúLXQWLSGHGDWDOFUXLGRPHQLXHVWHPDLPDUH 5.4. Operatorii unari ++úL- 'XS FXP VD PHQ LRQDW DFHúWL RSHUDWRUL VH QXPHVF XQDUL GHRDUHFH VH DSOLF DVXSUDXQHLVLQJXUHYDULDELOHH[SUHVLL ODXQPRPHQWGDW(LUHDOL]HD]LQFUHPHQWDUHDVDX decrementarea valorii respective cu o unitate. /LPEDMXO & PDL DUH XQ DYDQWDM DFHúWL RSHUDWRUL SRW IL IRORVL L SHQWUX SRVW VDX preincrementare. Acest lucru este prezentat în exemplu de mai jos: 'DF[ DWXQFL Preincrementare Predecrementare Postincrementare Postdecrementare
y=++x y=--x y=x++ y=x--
ÅÆ x=x+1=5 úL\ [ ÅÆ x=x-1=3 úL\ [ ÅÆ y=x=4 úL[ [ ÅÆ y=x=4 úL[ [
5.5. Operatorii op= $X IRW LQWURGXúL SHQWUX VLPSOLILFDUHD VFULHULL FRGXOXL 2 LQVWUXF LXQH JHQHULF GH tipul rez = rez op opd1 ÅÆ UH]RS RSG$FHúWLRSHUDWRULDSDU LQXUPWRDUHLFODVH^ += -= = /= %= &= ^= |=} de exemplu: x=x*7 ÅÆx*=7; 2SHUDWRULUHOD LRQDOLúLORJLFLG Pentru a se putea lua decizii într-un program, deci pentru a realiza ramificarea IOX[XOXL GH SUHOXFUDUH D GDWHORU WUHEXLH V H[LVWH R PRGDOLWDWH GH FRPSDUDUH D LQIRUPD LLORU úL GH DVHPHQL GH FRPELQDUH D H[SUHVLLORU ORJLFH SULPLWLY vQ H[SUHVLL PDL complexe. 'HH[HPSOXGDFvQYDULDELOHOH[úL\DPVWRFDWYDORDUHDDWXQFLH[SUHVLDx==y va fi $'(95$7úLYDDYHDODHYDOXDUHDH[SUHVLHLYDORDUHDQXPHULFGDF[ úL\ atunci expresia [ \YDIL)$/6úLGHFLvQXUPDHYDOXULLYDILvQORFXLWFXYDORDUHD
27
Operatori UHOD LRQDOL == != > < >= <= Operatori logici || && !
Introducere in programare ([SOLFD LH
Egal cu Diferit de Mai mare ca Mai mic ca Mai mare sau egal Mai mic sau egal SAU ù, NU
5.7. Expresii logice complexe 'H RELFHL HVWH LQVXILFLHQW R VLQJXU H[SUHVLH ORJLF VLPSO ÌQ VFULHUHD XQXL SURJUDPHVWHQHFHVDUGHPXOWHRULFRPSXQHUHDDPDLPXOWHGHFL]LL8QH[HPSOXFODVLF HVWHFD]XOvQFDUHWUHEXLHYHULILFDWDSDUWHQH DYDORULLVWRFDWHvQWURYDULDELOODXQDQXPLW interval. De exemplu x∈[5,20] este echivalent cu x≥úL[≤20 ceea ce folosind operatori ORJLFLúLUHOD LRQDOLVHSRDWHVFULH[! [ 7DEHOHOHGHDGHYUSHQWUXRSHUDWRULLORJLFLDLOLPEDMXOXLVXQW X Y 0 0 1 1
0 1 0 1
X&&Y
X||Y
!X
0 0 0 1
0 1 1 1
1 1 0 0
8UPHD]SUH]HQWDUHDXQSURJUDPVLPSOXFDUHUHDOL]HD]GLYHUVHRSHUD LLORJLFHFXXQ VHW GH YDORUL LQL LDOL]DWH GLUHFW vQ SURJUDP $FHDVW PHWRG GH LQL LDOL]DUHD DUH GH]DYDQWDMXOFSURJUDPXOQXSRDWHSULPLSHQWUXFDOFXODOWHYDORULGHFkWGDFVHPRGLILF FRGXOVXUV void main(void) { char mask; char number[6];// tablou de 6 elemente de tip char char and,or,xor,inv,index; QXPEHU>@ [LQL LDOL]DUHvQFRG number[1] = 0x11; number[2] = 0x22; number[3] = 0x44; number[4] = 0x88; number[5] = 0xFF; printf(" numerele asupra carora am aplicat diverse operatii logice folosind o masca sunt: \n");
28
Introducere in programare
mask = 0x0F; for (index = 0;index <= 5;index++) { and = mask & QXPEHU>LQGH[@úLORJLF or = mask | number[index];//sau logic xor = mask ^ number[index];// sau exclusiv inv = ~number[index];// not bit cu bit printf("%5x %5x %5x %5x %5x %5x\n",number[index], mask,and,or,xor,inv); } printf("\n"); mask = 0x22; for (index = 0;index <= 5;index++) { and = mask & number[index]; or = mask | number[index]; xor = mask ^ number[index]; inv = ~number[index]; printf("%5x %5x %5x %5x %5x %5x\n",number[index], mask,and,or,xor,inv); } }
3HQWUXUHVWXOWLSXULORUGHRSHUDWRULSUH]HQWD LYRPGDXQDOWH[HPSOXGHIRORVLUHDORU void main(void) { int x = 0,y = 2,z = 1025; float a = 0.0,b = 3.14159,c = -37.234; /* incrementare */ [ [ [VHLQFUHPHQWHD] x++; /* x se SRVWLQFUHPHQWHD] ++x; /* x se SUHLQFUHPHQWHD][ z = y++; /* z = 2, y = 3 */ z = ++y; /* z = 4, y = 4 */ /* decrementare */ \ \ \VHGHFUHPHQWHD] y--; /* y este postdecrementat */ --y; /* y este predecrementat */ y = 3; z = y--; /* z = 3, y = 2 */ z = --y; /* z = 1, y = 1 */ RSHUD LLDULWPHWLFH D D VHDGXQODD D VHDGXQvQFODD
29
Introducere in programare
D DVHvQPXO HúWHFX a -= b; /* se scade b din a */ a /= 10.0; /* se împarte a la 10.0 */
H[SUHVLLFRQGL LRQDOH D E! " DFHDVWH[SUHVLH LIE! HVWHLGHQWLFFXWRDWLQVWUXF LXQHDif… */ a = 2.0; /* din punct de vedere al rezultatului*/ else a = 10.5; c = (a > E"DE FSULPHúWHPD[LPXPGLQWUHDúLE c = (a > E"ED FSULPHúWHPLQLPXOGLQWUHDúLE }
30
CAPITOLUL 6
Introducere in programare
,QVWUXF LXQLGHVHOHF LHGHFL]LH 6 ,QVWUXF LXQHVLPSOGHGHFL]LH )LHXUPWRDUHDSUREOHPFHWUHEXLHUH]ROYDWSULQLQWHUPHGLXOXQXLSURJUDP VHUHDOL]H]HXQSURJUDPFDUHIDFHDXWRPDWFDOFXOXOPHGLHLGHOLFHQ úLDPHGLHL6 generale. 3URJUDPXOSULPHúWHWUHLPHGLLGHOHH[DPHQHOHGHOLFHQ úLQRWDSULPLWODVXV LQHUHD SURLHFWXOXLGHVWDW(OWUHEXLHVFDOFXOH]HPHGLDvQWUHQRWHOHGHODH[DPHQHOHGHOLFHQ UH]XOWkQGRSULPPHGLHFDUHDSRLVHPHGLD]FXQRWDGHODSURLHFWXOGHVWDWUH]XOWkQG PHGLDJHQHUDO ([LVWROLPLWDUHDVXSUDGDWHORUGHLQWUDUHFDUHWUHEXLHUHDOL]DWGHSURJUDPúLDQXPH GDFXQDGLQPHGLLOHGHODH[DPHQXOGHOLFHQ HVWHPDLPLFFDFLQFLVQXPDLUHDOL]H]H FDOFXOXOPHGLLORUILQDOHúLVOGHFODUHSHUHVSHFWLYXOFDQGLGDWUHVSLQV 2VROX LHGHUH]ROYDUHDSUREOHPHLODQLYHOGHSVHXGRFRGDUDWFDPDLMRV LQFOXGHOLEUULLOHQHFHVDUHOXFUXOXLvQPRGWH[W6 void main(void) {//început program GHFODUYDULDELOHOHQQQQVPPGHWLSUHDO úWHUJHHFUDQXO FLWHúWHQRWDvQYDULDELODQ FLWHúWHQRWDvQYDULDELODQ FLWHúWHQRWDvQYDULDELODQ GDFQ! úLQ! úLQ! DWXQFL6 { FLWHúWHQRWDVWDWvQYDULDELODns; UHDOL]HD]PHGLDP QQQ 6 UHDOL]HD]PHGLDP PQV 6 DILúHD]PHGLDGHOLFHQ DP6 DILúHD]PHGLDJHQHUDOP6 } DOWIHODILúHD]³&DQGLGDWUHVSLQV´ DúWHDSWRWDVW6 `VIkUúLWGHSURJUDP 6H REVHUY F D IRVW QHYRLH GH R UDPLILFDUH OD QLYHOXO SURFHVULL GDWHORU UDPLILFDUH FDUHDSDUHGLQQHYRLOHXWLOL]DWRUXOXL(VWHQHYRLHFDODXQPRPHQWGDWVKRWUkPPRGXUL PXOWLSOHGHSUHOXFUDUHSHQWUXDFHODúLIOX[GHGDWHGHLQWUDUH ÌQ OLPEDMXO & SHQWUX D LPSOHPHQWD DFHVW WLS GH GHFL]LL D IRVW LQWURGXV LQVWUXF LXQHDif… cu sintaxa ca mai jos:
31
Introducere in programare
if (FRQGL LH DGHYUDW 6 { LQVWUXF LXQL6 H[HFXWEORFLQVWUXF LXQL6 } else { H[HFXWEORFLQVWUXF LXQL6 };
GDFFRQGL LDHVWHDGHYUDW H[HFXW EORF 6
altfel
H[HFXWEORFLQVWUXF LXQL6
6LQWD[D LQVWUXF LXQLL HVWH LGHQWLF FX SVHXGRFRGXO ELQHvQ HOHV F VH IRORVHúWH OLPED HQJOH]
)LJD5HSUH]HQWDUHDSULQVFKHPDORJLFDLQVWUXF LXQLLGHGHFL]LH
)LJE5HSUH]HQWDUHDSULQVFKHPDORJLFDLQVWUXF LXQLLGHGHFL]LHVLPSOH ÌQ ILJXULOH D úL E HVWH SUH]HQWDW UHSUH]HQWDUHD OD QLYHO GH VFKHP ORJLF D LQVWUXF LXQLLGHGHFL]LH6SUHGHRVHELUHGHDOWHOLPEDMHGHSURJUDPDUHFXPDUIL3$6&$/ GHH[HPSOXOLPEDMXO&QXIRORVHúWHYDULDELOHGHWLSORJLFSHQWUXUH]XOWDWXOHYDOXULLXQHL expresii. Valorile comparate pot rezulta din valoarea UHWXUQDW GH R IXQF LH GLQ FHD VWRFDWvQWURYDULDELOVDXGLUHFWGDWvQSURJUDP 'DFvQORFXO FRQGL LHLvQXUPDHYDOXULLH[SUHVLHLUH]XOWXQQXPUGLIHULW GH ]HUR DFHVWDYDILFRQVLGHUDWFDFRQGL LHvQGHSOLQLWúLGHFLVHYDH[HFXWDEORFXOGHLQVWUXF LXQL GDF HVWH ]HUR FRQGL LD QX HVWH VDWLVIFXW DWXQFL VH YD H[HFXWD EORFXO GH LQVWUXF LXQL $FHVW OXFUX FRQIHU XQ PDUH DYDQWDM SULQ IDSWXO F VH SRW LQWURGXFH GLUHFW H[SUHVLL PDWHPDWLFH VDX DSHOXUL GH IXQF LL FH UHWXUQHD] UH]XOWDWH QXPHULFH vQ ]RQD GH WHVW D FRQGL LHL 1XvQWRWGHDXQDHVWHQHYRLHGH DPEHOHSU LDOHLQVWUXF LXQLLGHH[HPSOXFkQGVH H[HFXW R RSHUD LXQH QXPDL GDF HVWH vQGHSOLQLW R DQXPLW FRQGL LH 'H IDSW VWUXFWXUD ORJLF GH FRQWURO DU WUHEXL SUHY]XW úL FX R UDPXU GH else dar acest lucru nu ar duce GHFkW OD VXSUDvQFUFDUHD FRGXOXL FHHD FH vQJUHXQHD] PXOW GHSDQDUHD SURJUDPXOXL UH]XOWkQGVLQWD[DSUH]HQWDWPDLMRV
32
Introducere in programare
if (FRQGL LH DGHYUDW 6 { LQVWUXF LXQL6 H[HFXWEORFLQVWUXF LXQL6 };
GDFFRQGL LDHVWHDGHYUDWDWXQFL6 e[HFXW EORF 6
ÌQFD]XOvQFDUHVXQWLPSOLFDWHPDLPXOWHGHFL]LLVLPSOHFDUHWUHEXLHVFOXDWHIXQF LH GH GLIHULWH YDORUL DOH XQHL VLQJXUH YDULDELOH OLPEDMXO HVWH SUHY]XW FX LQVWUXF LXQH VSHFLDOL]DWFDVH«'LVFX LDDFHVWHLLQVWUXF LXQLVHYDIDFHXOWHULRU 0DLMRVHVWHSUH]HQWDWSURJUDPXOFRUHVSXQ]WRUpseudocodului prezentat anterior. #include <stdio.h> #include void main(void) { float n1,n2,n3,ns,m1,m2; clrscr(); printf(“Dati prima nota:”); scanf(“%f”,&n1); printf(“\nDati a doua nota:”); scanf(“%f”,&n2); printf(“\nDati prima nota:”); scanf(“%f”,&n1); if( (n1>5) && (n2>5) && (n3>5)) { printf(“\nDati nota de la stat:”); scanf(“%f”,&ns); m1=(n1+n2+n3)/3; m2=(m3+ns)/2; printf(“ media de licenta %f”, m1); printf(“ media generala %f”,m2); } else printf(“Candidat respins”); getch(); } 2EVHUYD LL6 1. ÌQ DFHDVW ID] IDSWXO F VD IRORVLW R UHIHULQ SHQWUX FLWLUHD XQHL YDULDELOH YH]L Q QXSRDWHILH[SOLFDWVXILFLHQWúLYDWUHEXLOXDWFDDWDUHvQFD]XOFLWLULLYDULDELOHORU simple. 2. Operatorul ‘?Q¶UHDOL]HD]XQVDOWODOLQLHQRX
33
Introducere in programare
3. 6H REVHUY F SH UDPXUD GH else nu s-au introdus delimitatorii de bloc (acoladele) SHQWUX F GDF DFHúWLD QX VXQW SUHFL]D L SULPD LQVWUXF LXQH HVWH DXWRPDW FRQVLGHUDW DSDU LQkQGUDPXULLUHVSHFWLYH 4. 3HQWUX SURJUDPDWRULL PDL DYDQVD L vQ PRPHQWXO vQ FDUH EORFXO GH LQVWUXF LXQL HVWH IRUPDWGLQWURVLQJXULQVWUXF LXQHVHSRDWHIRORVLRSHUDWRUXO"FDvQH[HPSOXOGHPDL jos, unde : if(a>b) x=25; else yx=34; poate fi echivalat prin: a>b (x=25) : (y=34); 8Q DOW H[HPSOX GH IRORVLUH D RSHUDWRUXOXL " HVWH R OLQLH GH FRG FDUH FDOFXOHD] PD[LPXOGLQWUHYDUúLYDUúLOGHSXQHvQYDULDELODPD[ max=(var1>var2) ? var1 : var2; Mai jos vom prezenta un program simplu cu exemple despre evaluarea unor expresii compuse într-un program: void main(void) { int x = 11,y = 11,z = 11; char a = 40,b = 40,c = 40; float r = 12.987,s = 12.987,t = 12.987; LI[ \ ] H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFW] LI[!] D $ H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFWD LI[!] D % H[HFX LDLQVWUXF LXQLLQXYDDYHDQLFLXQHIHFW LIE F U H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFWU LIU V W F H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFWW LI[ U V ] H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFW IDSWXOF[YDOXDYDORULSR]LWLYHLDU] LI[ \ ] H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFW [ \úL] LI[ ] H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFW] LI[ ] H[HFX LDLQVWUXF LXQLLYDDYHDFDHIHFW] x = y = z = 77; LI[ \ [
] H[HFX LDLQVWUXF LXQLLYDDYHD ca efect z = 33 */ LI[!\ __]! ] H[HFX LDLQVWUXF LXQLLYDDYHD6 ca efect z = 22 */ LI[ \ ] ] H[HFX LDLQVWUXF LXQLLYDDYHD6 ca efect z = 11 */ LI[ \ ] U H[HFX LDLQVWUXF LXQLL6
34
LI[
Introducere in programare
va avea ca efect x = 1, y = 2, z = 3, r = 12.00 */ \ ] U H[HFX LDLQVWUXF LXQLLQX6 va avea nici un efect */
if (x == x); z = 27.345; /* z se va modifica întotdeauna */ LI[ [ ] \QXVHYDPRGLILFDQLFLRGDW LI[ ] [YDOXDYDORDUHD]YDUPkQHQHVFKLPEDW } ,QVWUXF LXQHGHGHFL]LHPXOWLSOVHOHF LH 6 ([LVW VLWXD LL vQ FDUH UH]ROYDUHD XQHL SUREOHPH LPSXQH FD R GHFL]LH V FRQGXF OD H[HFX LDDPDLPXOWGHGRXUDPXULGHSURJUDP&DH[HPSOXVFRQVLGHUPFD]XO FHOHL PDLVLPSOHIRUPHGHPHQLXFDUHSRDWHILRIHULWXQXLXWLOL]DWRU 6HFLWHúWHXQQXPUGHODWDVWDWXU )XQF LHGHYDORDUHDGDWVHSRDWHDOHJHH[HFX LD XQHLDGLQWUHRS LXQLOHSUH]HQWDWHXWLOL]DWRUXOXL0DLMRVHVWHSUH]HQWDWXQPHQLXVLPSOX folosind metoda pseudocodului. úWHUJHHFUDQ6 DILúHD]PHVDM³$SDVXQDGLQWDVWHSHQWUXDDOHJHRSHUD LXQHDGRULW´6 DILúHD]PHVDM³&LWLUHGLQILúLHU´6 DILúHD]PHVDM³6FULHUHLQILúLHU´6 DILúHD]PHVDM³3URFHVDUHGDWH´6 DILúHD]PHVDM³DILúDUHJUDILFUH]XOWDW´6 FLWHúWHRWDVW IXQF LHGHWDVWFLWLW GDFHDSHOHD]IXQF LDGHFLWLUHGLQILúLHUDSRLVWRS6 GDFHDSHOHD]IXQF LDGHVFULHUHLQILúLHUDSRLVWRS6 GDFHDSHOHD]IXQF LDGHSURFHVDUHGDWHDSRLVWRS6 GDFHDSHOHD]IXQF LDGHDILúDUHJUDILFDUH]XOWDWHDSRLVWRS6 GDFQXHQLFLXQXOGLQQXPHUHOHGHPDLVXV6 DILúHD]PHVDM³2S LXQHLQYDOLG´6 6H REVHUY F VH SRDWH UHDOL]D LPSOHPHQWDUHD úL FX LQVWUXF LXQL if… dar, acest OXFUXQXHVWHMXVWLILFDWDWkWWLPSFkW DYHPRLQVWUXF LXQHVSHFLDOL]DWSHQWUXDFHVWWLS GHVLWXD LL$FHDVWDHVWHLQVWUXF LXQHDFDVH«FXXUPWRDUHDVLQWD[ sZLWFKRSHUDQG IXQF LHGHRSHUDQG6 { FDVHYDOGDFRSHUDQGXOLDYDORDUHD EORFGHLQVWUXF LXQL break; // stop case vDOGDFRSHUDQGXOLDYDORDUHD EORFGHLQVWUXF LXQL break; // stop
35
Introducere in programare
... case YDOQGDFRSHUDQGXOLDYDORDUHDQ EORFGHLQVWUXF LXQLQ break; // stop GHIDXOWSHQWUXRULFHDOWYDORDUH EORFGHLQVWUXF LXQLFHWUDWHD]UHVWXOYDORULORU ce le poate avea operandul } // stop
2IRUPGHUHSUH]HQWDUHODQLYHOGHVFKHPDORJLFDUILFHDSUH]HQWDWvQILJXUD
)LJ6FKHPDORJLFSHQWUXUHSUH]HQWDUHDXQHLVWUXFWXUL6 GHVHOHF LHPXOWLSO 0DL MRV HVWH SUH]HQWDW XQ SURJUDP FDUH FLWHúWH GRL RSHUDQ]L GH OD WDVWDWXU DSRL QH SHUPLWHVHOHF LDRSHUD LHLPDWHPDWLFHFHWUHEXLHUHDOL]DWFDvQILQDOVDILúH]HUH]XOWDWXO #include <stdio.h> #include void main(void) { int op1, op2; float rez; char oper; clrscr(); printf(“Dati primul operand:”); scanf(“%d”,&op1); printf(“\nDati al doilea operand:”); scanf(“%d”,&op2); printf(“\nSelectionati operatia dorita”); oper=getch(); printf(“/n1- Adunare”); printf(“/n2- Scadere”); printf(“/n3- Inmultire”); printf(“/n4-Impartire ”); switch(oper) 36
Introducere in programare
{ case ’ 1’:rez=op1+op2;break; case ’ 2’:rez=op1-op2; break; case ’ 3’:rez=op1*op2; break; case ’ 4’:rez=op1/op2; break; default: clrscr(); printf(“Operatie nepermisa”); } printf(“\nRezultatul este: %d”, rez); }
37
CAPITOLUL 7
Introducere in programare
,QVWUXF LXQLGHFLFODUH,QWURGXFHUH 6DGLVFXWDWSkQ DFXP GHVSUH VWUXFWXUL GH FRQWURO FDUH SHUPLW DOHJHUHD FRQGL LRQDW vQWUH H[HFX LD PDL PXOWRU VHWXUL GH LQVWUXF LXQL 6LVWHPHOH GH FDOFXO DX DYDQWDMXO F SRW UHSHWDRGDW³LQVWUXLWHFHúLFkW´SURJUDPDWH XQQXPUGHRSHUD LXQLGH FkWHRUL HVWH QHYRLH 2ULFH OLPEDM GH SURJUDPDUH HVWH DUH LPSOHPHQWDW DFHDVW SRVLELOLWDWH ([LVW GRXFODVHGHLQVWUXF LXQLGHFLFODUHUHSHWDUH 2SULPFDWHJRULHVH UHIHUODLQVWUXF LXQLOH FDUH UHSHW XQ EORF GH LQVWUXF LXQL SkQ FkQGHVWHvQGHSOLQLWRFRQGL LHSULQFRQGL LHSXWHPvQ HOHJHRH[SUHVLHPXOWLSOFHYDIL HYDOXDW GH FWUH FRPSLODWRU 'H RELFHL HVWH IRORVLW SHQWUX LQWHUDF LXQL UHSHWLWLYH FX XWLOL]DWRUXOVDXvQFDOFXOXOQXPHULFSULQDSUR[LPD LLúLGHDFHHDLQVWUXF LXQLOHUHVSHFWLYH VHQXPHVFLQVWUXF LXQLFXQXPUQHFXQRVFXWGHSDúL 2 D GRXD FDWHJRULH GH LQVWUXF LXQL UHSHWLWLYH HVWH DFHHD D LQVWUXF LXQLORU FX QXPU FXQRVFXWGHSDúL'LQSXQFWGHYHGHUHDOOLPEDMXOXLQXDUILVWULFWQHYRLHGHDFHVWWLSGH LQVWUXF LXQL SHQWUX F VH SRDWH UHDOL]D DFHVW OXFUX FX LQVWUXF LXQLOH GLQ SULPD FODV 7RWXúL GXS FXP VH YD REVHUYD PDL MRV LPSOHPHQWDUHD FX LQVWUXF LXQL FX QXPU QHFXQRVFXWGHSDúL SHQWUX DFHDVW D GRXD FODV SUHVXSXQH XQ FRG FDUH YD IL LGHQWLF OD DEVROXW WRDWH LQWHUSUHWULOH 3HQWUX D XúXUD VFULHUHD FRGXOXL úL DO IDFH PDL FODU VD LPSOHPHQWDWRLQVWUXF LXQHQRXIRU... . ,QVWUXF LXQLFXQXPUQHFXQRVFXWGHSDúL6 ,QVWUXF LXQHDwhile (VWHRLQVWUXF LXQHFDUHSHUPLWHUHSHWDUHDVDXQXDH[HFX LHLXQHLEXF LGHFRGIXQF LH GHYDORDUHDGHDGHYUDXQHLH[SUHVLL 8QH[HPSOXvQFDUHDUILQHFHVDUDFHVWWLSGHLQVWUXF LXQLDUILUHVWULF LDGRPHQLXOXL de valori în cazul datelor care le poate introduce un utilizator. Acest lucru este necesar SHQWUXFGHRELFHLXWLOL]DWRUXOQX LQHFRQWGHWRDWHUHVWULF LLOHUHODWLYODLQWHUYDOXOvQFDUH poate da o valoare de LQWUDUHVDX SRDWH GD GLQ JUHúHDO R YDORDUH LQFRUHFW 7RWXúL PRGHOXOPDWHPDWLFVSHFLILFFODUFDOJRULWPXOOXFUHD]FRUHFWQXPDLvQLQWHUYDOXO>a,b]. 3HQWUX XWLOL]DWRU IDSWXO F SURJUDPXO G XQ UH]XOWDW HURQDW GDWRULW XQHL YDORUL GH LQWUDUH LQWURGXV JUHúLW vQVHDPQ F SURJUDPXO QX IXQF LRQHD] FRUHFW (VWH QHFHVDU FD SURJUDPDWRUXO V QX SHUPLW LQWURGXFHUHD DOWRU GDWH GHFkW FHOH VSHFLILFDWH GH PRGHOXO matematic. 2EVHUYD LH OLPEDMXO & VSUH GHRVHELUH GH OLPEDMXO 3DVFDO QX JHQHUHD] XQ PHVDM GH HURDUH GDF WLSXO GH GDW LQWURGXV QX FRLQFLGH FX VSHFLILFDWRUXO GDW IXQF LHL scanf. Din DFHVWPRWLYvQPRPHQWXOvQFDUHVHSUHJWHúWHRYHUVLXQHEHWDSULPDYHUVLXQHDDSOLFD LHL FDUH YD IL IRORVLW GH FWUH XWLOL]DWRU WUHEXLHVF LQWURGXVH WRDWH UHVWULF LLOH GH WLSXO PHQ LRQDWPDLVXV/DQLYHOGHSVHXGRFRGPHWRGDSRDWHILGHVFULVFDPDLMRV FLWHúWHYDORDUHDGHLQWUDUH atâtaWLPSFkWYDORDUHDGHLQWUDUHQXVHDIOvQLQWHUYDOXO>a,b] HPLWHXQPHVDMGHDWHQ LRQDUHFWUHXWLOL]DWRU6
38
UHFLWHúWHYDORDUHD6
Introducere in programare
3HQWUXDFHDVWDVHSRDWHIRORVLLQVWUXF LXQHDZKLOH«DFUHLVLQWD[HVWHSUH]HQWDW mai jos: while(FRQGLWLH DWkWDWLPSFkWFRQGL LDHVWHDGHYUDWH[HFXW { EORFGHLQVWUXF LXQL6 } ÌQILJXUDHVWHSUH]HQWDWVFKHPDORJLFFRUHVSXQ]WRDUHLQVWUXF LXQLLwhile.
LQVWUXF LXQLLwhile
)LJ6FKHPDORJLFD6
ÌQH[HPSOXOGHPDLMRVHVWHYRUEDGHFLWLUHDXQHLYDORULvQWUHJLFDUHWUHEXLHVDSDU LQ intervalului [0,255]. #include <stdio.h> #include void main(void) { unsigned char value; clrscr(); printf(“Nr1:”); scanf(“%d”,&op1); while((value>255)&&(value<0)) { printf(“Eroare ! Numarul trebuie sa fie intre 0 si 255 ”); printf(“Nr1:”); scanf(“%d”,&op1); } XUPHD]UHVWXOSURJUDPXOXL6 } ,QVWUXF LXQHDGR«while 'XSFXPVDPHQ LRQDWDQWHULRULQVWUXF LXQHDZKLOH«WHVWHD]PDLvQWkLYDORDUHD GH DGHYU D H[SUHVLHL GH HYDOXDW úL DSRL IXQF LH GH DFHDVWD H[HFXW VDX QX EORFXO GH LQVWUXF LXQL'HDFHHDLVHPDLVSXQHLQVWUXF LXQHGHFLFODUHFXWHVWDQWHULRU
39
Introducere in programare
'DFVHDQDOL]HD]H[HPSOXOGHPDLVXVGLQSXQFWGHYHGHUHDOVLPSOLW LLFRGXOXL VH REVHUY F EORFXO GH LQVWUXF LXQL SHQWUX FLWLUHD YDULDELOHL D IRVW VFULV GH GRX RUL 3HQWUXDHYLWDVFULHUHGHFRGLQXWLOvQDFHDVWVLWXD LHSVHXGRFRGXOWUHEXLHVILHFDPDL jos: FLWHúWHYDORDUHD6 YHULILFGDFVHDIOvQLQWHUYDOXOGRULW6 GDFGDFRQWLQXSURJUDPXO6 GDFQXUHLDGe la citirea variabilei 3HQWUXDSXWHDUH]ROYDHOHJDQWVLWXD LLOHGHDFHVWJHQVDPDLLQWURGXVRLQVWUXF LXQHúL anume do…ZKLOHDFUHLVLQWD[HVWHSUH]HQWDWPDLMRV GRH[HFXW { EORFGHLQVWUXF LXQL }ZKLOHFRQGL LH DWkWDWLPSFkWFRQGL LDHVWHDGHYUDW ÌQILJXUDDYHPSUH]HQWDWVFKHPDORJLFSHQWUXLQVWUXF LXQHDGR«while.
)LJ6FKHPDORJLFDLQVWUXF LXQLLGR«while )RORVLQGDFHDVWLQVWUXF LXQHFRGXOSUH]HQWDWPDLVXVGHYLQH #include <stdio.h> #include void main(void) { unsigned char value; clrscr(); do { printf(“Nr1:”); scanf(“%d”,&op1); if( value>255)&&(value<0)) printf(“Eroare ! Numarul trebuie sa fie intre 0 si 255 ”); } while((value>255)&&(value<0)); XUPHD]UHVWXOSURJUDPXOXL6 }
40
Introducere in programare
'HFLVHFLWHúWHQXPUXOúLGDFDFHVWDQXHVWHFRUHFWVHUHLDFLWLUHD3HQWUXYDOLGDUHD DILúULL PHVDMXOXL GH HURDUH VD SUHY]XW YHULILFDUHD FRQGL LHL SHQWUX F GDF XWLOL]DWRUXO LQWURGXFHFRUHFWYDORDUHDGHSULPDRDUQXHVWHSHUPLVDILúDUHDPHVDMXOXLGHHURDUH $FHDVWLQVWUXF LXQHH[HFXWPFDURGDWEORFXOGHLQVWUXF LXQLúLGHDELDDSRLIDFH HYDOXDUHDH[SUHVLHLSHQWUXDSXWHDYHGHDGDFDFHVWDVHPDLUHSHWVDXQX'HDFHHDLVH PDLVSXQHúLLQVWUXF LXQHGHFLFODUHFXWHVWSRVWHULRU ,QVWUXF LXQLFXQXPUFXQRVFXWGHSDúL,QVWUXF LXQHDIRU6
'LQFHOHSUH]HQWDWHSkQDFXPUH]XOWFVHSRDWHUHSHWDXQEORFGHLQVWUXF LXQLSkQ FkQGHVWHvQGHSOLQLWRFRQGL LH([LVWFD]XULFkQGVHFXQRDúWHGHODvQFHSXWFkWHYDORULVH GRUHVFD ILSUHOXFUDWH ILH SULQ VSHFLILFDUHD GLUHFW D XWLOL]DWRUXOXL ILH FD UH]XOWDW D XQRU FDOFXOH DQWHULRDUH'HRELFHLDFHVWOXFUX VH DSOLF OD FDOFXOXO YHFWRULDO VDX PDWULFLDO GDU nu numai. ÌQWUXFkWQXVDGLVFXWDWvQFGHVSUHDFHVWWLSGHGDWHVHYDOXDvQGLVFX LHXQH[HPSOX PDL VLPSOX FXP DU IL UHDOL]DUHD XQXL SURJUDP FH YD ULGLFD XQ QXPU OD R SXWHUH 3VHXGRFRGXOúLDSRLSURJUDPXOVXQWSUH]HQWDWHPDLMRV FLWHúWHQXPUXOnr) FLWHúWHSXWHUHDODFDUHYDILULGLFDWpw) LQL LDOL]HD]YDULDELODUH]XOWDWFHYDSVWUDnr la putere, cu 1 (rez=1) DWkWDWLPSFkWYDULDELODFHDSULPLWSXWHUHD!I rez=rez*nr; pw=pw-1; UHSHW DILúHD]UH]XOWDWXO &RGXOVXUVFRUHVSXQ]WRU #include <stdio.h> #include void main(void) { int nr, pw; float rez; clrscr(); printf(“Numarul”); scanf(“%d”,&nr); printf(“\nPuterea”); scanf(“%d”,&op2); rez=1; //**** while(pw>0)//***** { rez=rez*nr;
41
Introducere in programare
pw=pw-1;//***** } printf(“Numarul %d la puterea %d este %f”,nr,pw,rez); getch(); }
,QVWUXF LXQLOHPDUFDWHFXDVWHUL[VHUHSHWvQIRDUWHPXOWHVLWXD LLFkQGQXPUXOGHSDúL HVWHFXQRVFXW'HDFHHDVDLPSOHPHQWDWLQVWUXF LXQHDIRU«DYkQGXUPWRDUHDVLQWD[ for(FRQWRU YDORDUHLQL LDOFRQWRUVDX!YDORDUHILQDO contor=contor+sau -pas) { EORFGHLQVWUXF LXQL6 } /DQLYHOGHVFKHPDORJLFH[HFX LDLQVWUXF LXQLL for HVWHSUH]HQWDWvQILJXUD
)LJ6FKHPDORJLFLQVWUXF LXQLLfor $FXP VH REVHUY F DFHDVW LQVWUXF LXQH VFXWHúWH SURJUDPDWRUXO GH D VFULH FRG suplimentar. Programul anterior UHVFULVIRORVLQGDFHDVWLQVWUXF LXQHHVWHGDWPDLMRV #include <stdio.h> #include void main(void) { int nr, pw,i; float rez; clrscr(); printf(“Numarul”); scanf(“%d”,&nr); printf(“\nPuterea”); scanf(“%d”,&op2); for(i=0;i0;i=i-1) // rez=rez*nr; printf(“Numarul %d la puterea %d este %f”,nr,pw,rez); 42
getch(); }
Introducere in programare
([LVWXQFRQWUROWRWDODVXSUDGHVIúXUULLEXFOHLH[DFWFDúLvQFD]XO while dar codul scris este mult mai succint. 2EVHUYD LD'DFEORFXOGHLQVWUXF LXQLFRQVWvQWURVLQJXULQVWUXF LXQHSDUDQWH]HOHQX mai sunt necesare 2EVHUYD LD 'DF VH SXQH ³´ GXS IRU DFHVWD YD FRQVLGHUD F DUH R EXFO YLG FRPSLODWRUXO QX YD UDSRUWD HURDUH VLQWDFWLF GDU FHHD FH HVWH VFULV vQ EORFXO GH LQVWUXF LXQLYDILGRDURVLQJXUGDWH[HFXWDW $OWHLQVWUXF LXQL6 ,QVWUXF LXQHDbreak (IHFWXOLQVWUXF LXQLLEUHDNFRQVWvQSUVLUHDLPHGLDWDFHOHLPDLDSURSLDWHWH[WXDO LQVWUXF LXQLswitch, ZKLOHGRIRUvQFRQMXUWRDUH ,QVWUXF LXQHDFRQWLQXH ,QVWUXF LXQHD FRQWLQXH QX VH SRDWH XWLOL]D GHFkW vQWU-un ciclu while, do sau for. (IHFWXOLQVWUXF LXQLLFRQWLQXHFRQVW -
întrXQVDOWODHYDOXDUHDFRQGL LHLGHFLFODUHGDFFHDPDLDSURSLDWWH[WXDO LQVWUXF LXQH GHFLFODUHvQFRQMXUWRDUHHVWHwhile sau do. întrXQ VDOWOD HYDOXDUHD H[SUHVLHL GLQ DWUHLD SR]L LH GDF FHD PDL DSURSLDW WH[WXDO LQVWUXF LXQHGHFLFODUHvQFRQMXUWRDUHHVWHIRU 3UH]HQWPPDLMRVXQH[HPSOXVLPSOXGHIRORVLUHDLQVWUXF LXQLLEUHDNúLFRQWLQXH void main(void) { int xx; for(xx = 5;xx < 15;xx = xx + 1){ if (xx == 8) break; printf("In bucla oprita fortat , xx are valoarea %d\n",xx); } for(xx = 5;xx < 15;xx = xx + 1){ if (xx == 8) continue; printf("Prin aplicarea continue in bucla, xx devine %d\n",xx); } }
,QVWUXF LXQHDgoto QXPHBHWLFKHW (IHFWXOLQVWUXF LXQLLJRWRFRQVWvQWUXQVDOWQHFRQGL LRQDWODLQVWUXF LXQHDFXHWLFKHWD FLWDWvQLQVWUXF LXQHDGHVDOWFDUHWUHEXLHVVHDIOHvQDFHHDúLIXQF LH
43
Introducere in programare
,QVWUXF LXQHDreturn
(IHFWXO LQVWUXF LXQLL UHWXUQ FRQGXFH OD WHUPLQDUHD LPHGLDW D H[HFX LHL IXQF LHL FXUHQWHúLODUHYHQLUHDODIXQF LDDSHODQW'DFH[SUHVLDHVWHSUH]HQWHDWUHEXLHVILHGHXQ WLS FDUH SRDWH IL FRQYHUWLW OD WLSXO IXQF LHL vQ FDUH DSDUH LQVWUXF LXQHD return. Valoarea H[SUHVLHLHVWHYDORDUHDvQWRDUVGHIXQF LH 7.3.5. Constante simbolice
Cu ajutorul directivei "#define" se pot defini la începutul programului nume sau FRQVWDQWHVLPEROLFHFDUHVXQWXQúLUSDUWLFXODUGHFDUDFWHUH'XSDFHHDFRPSLODWRUXO YD vQORFXL WRDWH DSDUL LLOH QHSXVH vQWUH JKLOLPHOH DOH QXPHOXL SULQ úLUXO FRUHVSXQ]WRU ÌQORFXLUHDHIHFWLYDQXPHOXLSRDWHILRULFHWH[WHDQXVHOLPLWHD]ODQXPHUH #GHILQH0,1YDORDUHDPLQLPDGPLV #GHILQH0$;YDORDUHDPD[LPDGPLV # define AND && void main(void) { int x; printf(“Dati un numar”); scanf(“%d”,&x) if((x>=MIN)AND(x<=MAX))printf(“\nNumarul este in interval”); else printf(“Numarul nu este in interval”); getch(); } &DQWLW LOH 0,1 úL 0$; VXQW FRQVWDQWH DúD vQFkW HOH QX DSDU vQ GHFODUD LL 1XPHOHVLPEROLFHVHVFULXvQPRGQRUPDOFXOLWHUHPDULDúDFSRWILXúRUGLVWLQVHGH QXPHOH GH YDULDELOH FDUH VH VFULX FX OLWHUH PLFL /D VIkUúLWXO XQHL GHILQL LL 18 VH SXQH SXQFWúLYLUJXO
44
CAPITOLUL 8
Introducere in programare
8. Masive de date. Tablouri ÌQPDMRULWDWHDFDOFXOHORUúWLLQ LILFHVHIRORVHúWHQR LXQHDGHYHFWRUPDWULFHOXFUXOvQ VSD LL QGLPHQVLRQDOH /LPEDMXO & D IRVW SUHY]XW DUH SRVLELOLWDWHD GH D OXFUD FX XQ PDVLYHGHGDWHGHDFHODúLWLSPDVLYHFHSRWILUHFXQRVFXWHVXEXQQXPHXQLF/LVHPDL VSXQHúLWDEORXULGHGDWH([LVWSRVLELOLWDWHDGHILQLULLVWDWLFHGHWDEORXULFXPD[LPXPWUHL GLPHQVLXQL'HFODUDUHDXQXLWDEORXVHUHDOL]HD]FDPDLMRV -
tablou monodimensional: WLSGDWQXPHWDEORX>nr-elemente]; (ex: char s[10]) tablou bidimensional: WLSGDWQXPHWDEORX>nr-linii][nr-coloane]; (ex: int m[20][20]) tablou tridimensional: WLSGDWQXPHWDEORX>coord x][coord y][coord z];
(ex:
float
punct[10][10][10]). $FHVWHGHFODUD LLDXFDHIHFWUH]HUYDUHDXQXLVSD LXGHPHPRULHSHQWUXGHSR]LWDUHD caractere sau 100 de întregi sau 1000 de valori reale pentru cazurile prezentate ca exemplu. $FFHVXO OD ILHFDUH ]RQ D WDEORXOXL VH IDFH SR]L LRQDO (OHPHQWHOH VXQW GHSXVH XQXO GXS DOWXOúLDWXQFLUHIHULUHDVHIDFHSULQWUXQLQGLFHFDUHVSHFLILFSR]L LDHOHPHQWXOXLUHVSHFWLY DOFkWHOHDHVWHvQúLU 'HFLSRWH[LVWDUHIHULULGHJHQXO s[3]=’2’ sau m[2][2]=30 sau punct[0][0][0]=0.233445. Indicele poate fi furnizat prin intermediul unei variabile. Acest lucru permite IRORVLUHD XQRU LQVWUXF LXQL FLFOLFH SHQWUX SUHOXFUDUHD GDWHORU GLQ FDGUXO XQXL WDEORX GH GDWH 2 VLWXD LH PDL VSHFLDO HVWH vQ FD]XO WDEORXULORU PRQRGLPHQVLRQDOH úLUXULORU GH caractere. Cum, în general, prelucrarea acestora nu se face caracter cu caracter, ele fiind GHRELFHLIRORVLWHvQPDQLSXODUHDLQIRUPD LHLVFULVHOLPEDMXO&DIRVWSUHY]XWFXRVHULH GHIXQF LLVSHFLDOL]DWHvQSUHOXFUDUHDJOREDODORU 8.1. Tablouri monodimensionale 'XS FXP DP VSXV WDEORXULOH PRQRGLPHQVLRQDOH SRW IL IRORVLWH úL SHQWUX FDOFXOH PDWHPDWLFH vQ VSD LL QGLPHQVLRQDOH XQGH VH OXFUHD] FX XQ VHW GH Q FRRUGRQDWH 9RP SUH]HQWDFkWHYDLPSOHPHQWULDOHRSHUD LXQLORUVLPSOHFXYHFWRUL 3UHOXFUULVLPSOHFXYHFWRUL6 ÌQ FD]XO GHFODUULL XQXL YHFWRU VH UH]HUY XQ VSD LX SHQWUX QXPUXO PD[LP GH HOHPHQWH FDUH VDU SXWHD V DLE QHYRLH XWLOL]DWRUXO SURJUDPXOXL 'H H[HPSOX FD]XO XQXL SURJUDP VSHFLDOL]DW SHQWUX FDOFXOH DVXSUD XQXL QXPU GH PVXUWRUL XWLOL]DWRUXO SRDW V IDFvQWUXQFD]PVXUWRULRHVWLPDUHPDLJURVLHU úLvQDOWXO'HFLVHYDGHILQLXQ YHFWRU FX GH HOHPHQWH GDU vQ DFHODúL WLPS WUHEXLH FD SURJUDPXO V FHDU VSHFLILFDUHD QXPUXOXLGHYDORULFHWUHEXLHLQWURGXVHúLVIDFSURFHVDUHDQXPDLDVXSUDORU9DWUHEXLV IRORVLPRLQVWUXF LXQHIRU«GHRDUHFHDUHQXPUXOGHSDúLFXQRVFXWÌQFD]XOvQFDUHVHSRDWH VSHFLILFDQLFLROLPLWSHQWUXQXPUXOGHYDORULFHWUHEXLHVFVWRFDWHVDXSUHOXFUDWHQXVHPDL SRW IRORVL WDEORXUL GHILQLWH VWDWLF FL OLVWH vQOQ XLWH 0DL MRV SUH]HQWP XQ SURJUDP FDUH
45
Introducere in programare
FLWHúWHGRLYHFWRULGHDFHHDúLGLPHQVLXQHGHODWDVWDWXUúLUHDOL]HD]RVHULHGHRSHUD LXQL VLPSOHFXHLDILúkQGGHILHFDUHGDWUH]XOWDWHOH #include <stdio.h> #include void main (void) { int v1[100], v[100],v2[100],i,j,n; clrscr(); printf(“Dati dimensiunea vectorilor n=”); scanf(“%d”,&n); for(i=0;iv[j]) { t=v[i]; v[i]=v[j]; v[j]=t; } printf(“\nVectorul ordonat:\n”) for(i=0;i
0DLMRVHVWHSUH]HQWDWFRGXOFDUHLPSOHPHQWHD]DOJRULWPXO³PHUJe sort”, bazat pe WHKQLFD³vPSDUWHúLVWSkQHúWH´vQWURPDQLHUUHFXUVLY #include <stdio.h> int a[100],n; // variabile globale vizibile úLîn main úLîn interiorul //RULFUHLIXQF LLGHILQLWHGHutilizator
46
Introducere in programare
merge_sort(int st,int dr); // prototipurile IXQF LLORUproprii merge(int st,int mij,int dr); void main(void) { int i; printf("\n introd dimensiunea sirului de sortat : "); scanf("%d",&n); for(i=0;imij) for(k=j;k<=dr;k++) {b[i]=a[k];i++;} else for(k=h;k<=mij;k++) {b[i]=a[k];i++;} for(k=st;k<=dr;k++) a[k]=b[k]; 47
} ùLUXULGHFDUDFWHUH
Introducere in programare
6DGLVFXWDWGHVSUHVHWXOGHFDUDFWHUH$6&,,úLVHWXOGHFDUDFWHUHGHFRQWURO$FHVWHD VXQWLQWURGXVHSHQWUXPDQLSXODUHDXQHLLQIRUPD LLFDUHSRDWHILGRDUDILúDWSHHFUDQVDXOD LPSULPDQW6SHFLILFDFHVWHLLQIRUPD LLHVWHFGHRELFHLPDQLSXOHD]vQSDFKHWHGHPDLPXOW GHXQFDUDFWHUÌQDFHVWHFRQGL LLWUHEXLHIRORVLWXQWDEORXGHFDUDFWHUH$FHVWDHVWHGHQXPLW úLUGHFDUDFWHUH(ODUHvQSOXVID GHXQYHFWRUDGXJDWvQWRWGHDXQDODVIkUúLWXOGDWHORUXWLOH un caracter special ‘\0’ QXPLW WHUPLQDWRU GH úLU $ IRVW LQWURGXV SHQWUX D XúXUD VFULHUHD IXQF LLORUGHELEOLRWHFVDXXWLOL]DWRU IRORVLWHvQSUHOXFUDUHDXQXLúLUGHFDUDFWHUH ùLUXULOH GH FDUDFWHUH DX H[DFW DFHOHDúL FDUDFWHULVWLFL FD RULFDUH WDEORX monodimensional. IQL LDOL]DUHDXQLúLUGHFDUDFWHUHVHSRDWHIDFHSULQPDLPXOWHPHWRGH -
ODGHFODUDUHIUGHILQLUHDGLPHQVLXQLLWDEORXOXL char s[]=”.doc”
-
vQDFHVWFD]GLPHQVLXQHDWDEORXOXLYDILH[DFWQXPUXOGHFDUDFWHUHFXFDUHHLQL LDOL]DW úLUXO la declarare cu definirea dimensiunii tabloului char s[20]=”doi”
-
direct în program : s=”Dati valoarea” SULQFLWLUHGHODWDVWDWXU
-
FXSURFHGXUgets(s) FXIXQF LHscanf(“%s”,s); SULQLQL LDOL]DUHFDUDFWHUFXFDUDFWHUvQFD]XOXQHLSUHOXFUULvQSURJUDP for(i=0;i<strlen(s1);i++)s2[i]=s1[i]+0x25;
ÌQXOWLPXOFD]DSDUHR SUREOHP vQ FD]XO úLUXOXL VQX HVWH SXV DXWRPDW FDUDFWHUXO µ?¶úLGHFLWUHEXLH2%/,*$725,8LQWURGXVH[SOLFLWÌQH[HPSOXOGHPDLVXVDFHVWOXFUXVH UHDOL]HD]SULQDGXJDUHDLQVWUXF LXQLL s[i]=’\0’; 3UH]HQWPPDLMRVXQSURJUDPVLPSOXFHLQL LDOL]HD]vQFRGHOHPHQWFXHOHPHQW XQúLUGHFDUDFWHUHDSRLDILúHD]GLYHUVHSR LXQLGLQHO #include <stdio.h> void main(void) { FKDUQXPH>@ GHILQHVFXQúLUGHFDUDFWHUH nume[0] = 'M'; nume[1] = 'i'; nume[2] = 'h'; nume[3] = 'a';
48
Introducere in programare
QXPH>@ FDUDFWHUXOFHPDUFKHD]VIkUúLWXO printf("Numele este %s\n",nume); printf("O litera din el este %c\n",nume[2]); printf("O parte din nume este %s\n",&nume[1]); getch(); }
7DEORXULELGLPHQVLRQDOH,PSOHPHQWDUHDQR LXQLLGHPDWULFH 3HQWUX FDOFXOH PDWHPDWLFH WDEORXULOH ELGLPHQVLRQDOH LPSOHPHQWHD] H[DFW FRQFHSWXO GHPDWULFHúLSRWILPDQLSXODWHvQFRGDSURDSHODIHOFDvQPDWHPDWLF Apare aceeaúL SUREOHP UHODWLY OD PRGXO vQ FDUH DOHJHP GLPHQVLXQHD PD[LP D unei PDWULFLFDúLvQFD]XOYHFWRULORU([LVWFHOHGRXFD]XUL - 8WLOL]DWRUXOúWLHH[DFWFkWHVWH GLPHQVLXQHD PD[LP D matricilor care vor fi folosite, acesta este cazul ideal care este relativ rar. - 8WLOL]DWRUXO DUH QLúWH FHULQ H YDJL úL SURJUDPDWRUXO SULQ GLVFX LH GLUHFW SRDWH DIOD DFHVW PD[LP VDX SRDWH VúL IDF R LGHH GHVSUH HO FD] vQ FDUH YD OXD R YDORDUH acoperitoare. ÌQRULFH FD]RULFDUHDUILGLPHQVLXQHDXQXLWDEORXHODUHXQQXPUWRWDOGH HOHPHQWH FDUHYDILDORFDWVWDWLF$FHVWQXPUVHSRDWHDIODFRQIRUPIRUPXOHORUGHPDLMRV -
tablou monodimensional: Nr_elem=dim_vect*sizeof(tip_data); tablou bidimensional: Nr_elem=dim_x*dim_y*sizeof(tip_data); tablou tridimensional Nr_elem = dim_x* dim_y *dim_z * sizeof(tip_data).
'HMDDSDUHRSUREOHPUHODWLYODVSD LXOPD[LPFDUHSRDWHILRFXSDWGHWRWDOXOGDWHORU GHILQLWHVWDWLFDGLFFXPDPYRUELWSkQDFXP úLDQXPHIDSWXOFFKLDUVXEPRGHOXOGH FRPSLODUH +8*( QX VH SRDWH DORFD PDL PXOW GH 0R RFWH L SHQWUX GDWH Deci implicit apare automat o limitare asupra dimensiunii maxime care o poate lua un tablou. 'HFL LQGLIHUHQW GH FHULQ HOH XWLOL]DWRUXOXL QX VH SRDWH GHSúL DFHDVW GLPHQVLXQH ÌQ FD]XOvQFDUHVSHFLILFD LLOHGHSúHVFDFHVWHGLPHQVLXQLH[LVWGRXPHWRGH -
metoda swap-ului; PHWRGDOLVWHORUGHRUGLQQXQGHQHVWHFRQIRUPQHFHVLW LORU
ÌQ JHQHUDO DFHVWH PHWRGH VXQW PL[DWH FKLDU úL OD QLYHOXO DSOLFD LLORU DFWXDOH 0HWRGD VZDSXOXL VH FRQFUHWL]HD] SULQ VFULHUHD vQWUXQ ILúLHU D GDWHORU FH GHSúHVF DFHVWH dimensiuni maxime. Practic dispozitivul de stocare devine un fel de prelungire a PHPRULHLRSHUDWLYH DYkQG vQV GH]DYDQWDMXO F WLPSLL GH DFFHV VXQW FDP GH RUL PDL PDULGHFkWvQFD]XOOXFUXOXLFX 5$0 FHHD FH YD GXFH OD VFGHUHD YLWH]HL GH H[HFX LH D UHVSHFWLYHL DSOLFD LL 0HWRGD OLVWHORU VH ED]HD] SH DORFDUHD GLQDPLF FDUH QH SHUPLWH DFFHVXOODWRDWPHPRULDIL]LF5AM QHRFXSDWGHDOWHSURJUDPH Mai jos vom prezenta un scurt exemplu de folosire a tablouri pentru implementarea de RSHUD LXQLVLPSOHFXmatrici.
49
#include <stdio.h> #include
Introducere in programare
void main(void) { int m1[5][5],m2[5][5],m3[5][5],m4[5][5],i,j,k,m,n,p; clrscr(); printf(“n=”); scanf(“%d”,&n);// se poate scrie úLDúDvQFRG printf(“m=”); scanf(“%d”,&m);// dar nu este recomandat printf(“p=”); scanf(“%d”,&p);// am citit dimensiunile matricilor for(i=0;i
50
CAPITOLUL 9
Introducere in programare
9. Tipuri de date definite de utilizator 6DGLVFXWDWSkQDFXPGHVSUHWDEORXULGHGDWHPRQRVDXPXOWLGLPHQVLRQDOHFDUHvQV FRQ LQHDXQXPDLGDWHGHDFHODúLWLS6XQWPXOWHFD]XULvQFDUHHVWHQHYRLHFDGDWHGHWLSXUL GLIHULWHVILHJUXSDWHVXEXQQXPHFRPXQ $FHDVWSUREOHPVDUH]ROYDWSULQLQWURGXFHUHDQR LXQLLGHVWUXFWXUGHGDWHFDUH SHUPLWH XWLOL]DWRUXOXL VúL FUHH]H QRL WLSXUL GH GDWH FkW PDL DSURSLDWH GH RELHFWHOH GLQ OXPHDUHDO$FHVWOXFUXVHPDLQXPHúWHDEVWUDFWL]DUHDGDWHORU /DIHOFXPIXQF LLOHSHUPLWGHVFRPSXQHUHDFRPSOH[LW LLXQXLSURJUDPvQWHUPHQL GHRSHUD LXQLSHGDWHVWUXFWXULOHVXQWIRORVLWHSHQWUXDEVWUDF LXQL. 9.1. Declararea unei structuri 2VWUXFWXUHVWHXQWLSGHGDWJHQHUDOL]DWGHILQLWGHXWLOL]DWRUcare poate fi folosit pentru gruparea diferitelor tipuri de date într-un nou concept. De exemplu un tip de date FHVHUHIHUODXQHOHGLQFDUDFWHULVWLFLOHXQXLVWXGHQWHVWHSUH]HQWDWPDLMRV struct Student { char name[20]; // numele studentului unsigned short int age; // virsta unsigned char n_lab; // nota la laborator unsigned char n_ex; // nota la examen } ([LVWRFRQYHQ LHFRPXQOHJDWGHQXPHOHXQHLVWUXFWXULúLDQXPHDFHVWDVvQFHDS FXOLWHUPDUH8QH[HPSOXGHGHFODUD LLFXDFHVWWLSGHGDWHHVWHGDWPDLMRV struct Student tmp, an99[20]; 9.2. Operatorul typedef Comanda W\SHGHIFDUHHVWHIRORVLWODredenumirea unor variabile HQXPHUDWH SRDWH IL IRORVLW úL vPSUHXQ FX FXYkQWXO FKHLH VWUXFW 3HQWUX R VWUXFWXU WUHEXLH V DSDU GXS FH HVWH GHILQLW úL vQDLQWH GH RULFH GHFODUD LH GH YDULDELOH 8]XDO noul tip de date se UHGHQXPHúWHLVHDWDúHD]RHWLFKHWHFKLYDOHQW FDPDLMRV typedef struct Student { char name[20]; // numele studentului unsigned short int age; // virsta unsigned char n_lab; // nota la laborator unsigned char n_ex; // nota la examen } stud;
51
Introducere in programare
Deci noul tip de date a fost redenumit ca VWXGvQDFHVWHFRQGL LLGHFODUD LLUHODWLYHODHO devin: 8QDOWH[HPSOXGHGHILQLUHù Stud tmp,an99[20];
Definesc noul tip de date struct my_data { char name[19]; secol[11]; short month,day,year; } Folosesc noua defini ie struct my_data test,tab[100],*ptab;
Deoarece my_data nu este un tip predefinit de date ,ci este un conglomerat definit de programator trebuie specificat úi cuvântul cheie struct. Creearea unui nou tip de date se face astfel: typedef struct { char name[19]; secol[11]; short month,day,year; }pers; Începând de acum pers se va folosi direct frDOWHVSHFLILFD LLvQGHILQLUHDXQRUHOHPHQWH de acest tip. pers test,tab[100],*ptab; IQL LDOL]DUHDXQHLVWUXFWXUL6 &DúLvQFD]XOWDEORXULORUVDXDOWRUWLSXULGHGDWHSRDWHILUHDOL]DWDVWIHO: : La declararea unui element Ex: pers eu={“MIKE”, “354263”,3,5,1978}; : Oriunde in cadrul programului Obs: Ini LDOL]DUHDXQHLVWUXFWXULQXSRDWHILUHDOL]DWODGHILQLUHDQRXOXLWLSGH GDW(cu typedef)(YLGHQWH[LVW o diferen FODULQWUHGHILQLUHDXQXLQRXWLSGHGDW úLGHILQLUHDXQHLYDULDELOHGHXQDQXPLWWLS 9.3. Accesarea membrilor unei structuri 'HRDUHFH DFHVW WLS GH GDWH HVWH FUHDW GH SURJUDPDWRU FRPSLODWRUXO VDX IXQF LLOH H[LVWHQWHQXúWLXVOXFUH]HGHFkWFXWLSXULOHpredefinite de date. De aceea este necesar ca
52
Introducere in programare
SUHOXFUDUHD V VH IDF WRW OD QLYHO GH PHPEUL FkPSXUL 3HQWUX DFHDVWD WUHEXLHVF FRQVWUXLWHIXQF LLVSHFLDOL]DWHvQRSHUD LXQLOHFXQRXOWLSGHGDWH ÌQFD]XOGHFODUD LHLXQXLHOHPHQWGHWLSXOUHVSHFWLYRSHUDWRUXOGHDFFHVODRULFDUH din membri este ‘.’. Accesul se face prin referire la numele global urmat de numele PHPEUXOXL FD vQ H[HPSOXO GH PDL MRV XQGH HVWH UHDOL]DW FLWLUHD GH OD WDVWDWXU D XQXL tablou de elemente de tip student. … for(i=0;i<10;i++) { clrscr(); printf(“Nume”); scanf(“%s”, an99[i].nume); printf(“\n Nota la laborator:”); scanf(“%d”,an99[i].n_lab); printf(“\n Nota la examen:”); scanf(“%d”,an99[i].n_ex); } 2VWUXFWXUvQLQWHULRUXODOWHLVWUXFWXULnested structures) ([LVW FD]XUL FkQG HVWH QHFHVDU FD LQIRUPD LD V ILH FkW PDL LHUDUKL]DW RE LQkQGXVH DVWIHO XQ FRG FkW PDL FODU ÌQ FD]XO GLQ H[HPSOXOXL DQWHULRU GDF GRUHVF V UHDOL]H] R DSOLFD LHSHQWUXXQVHFUHWDULDWDPQHYRLHúLGHQXPUXOJUXSHL([LVWGRXPHWRGHGHD UH]ROYDSUREOHPD8QDDUILLQWURGXFHUHDDvQFXQXLFkPSFDUHVUH LQQXPUXOJUXSHL GDU PRG GH UH]ROYDUHD SRDWH GXFH SH PVXU FH WRW PDL DGDXJ FDUDFWHULVWLFL QRL vQ VWUXFWXUODXQFRGJUHXGHXUPULW$OWFDOHDUILFUHDUHDXQHLQRLVWUXFWXULGXSFXPHVWH prezentat mai jos. typedef struct Grupa { int nume; // numarul grupei int an; // an de studii stud t[35]; grupa contine maximum 35 de studenti }gr; gr temp; Accesul la membri în acest caz se face prin compunere ca mai jos temp.nume //umele grupei temp.t[3].name QXPHOHFHOXLGHDOWUHLOHDVWXGHQWGLQJUXS6 0DLMRVSUH]HQWPvQFXQH[HPSOX typedef struct {char name[19],snum[11]; struct {short day; short month; 53
short year; }birth_date
Introducere in programare
}pers; O referire la day se va face pentru persoana eu: eu.birth_date.day Element VXEVWUXFWXUFPS VWUXFWXU mare Obs: numele cPSXULORUXQHLVWUXFWXULQXVXQWY]XWHGLUHFWvQDIDUúLGHFLRUHIHULUH: struct x {int x; ) x; este legalODDFFHVDUHx.x=5. Aten LHÌQFHSWRULLVHYLWHDFHVWOXFUXGHRDUHFHFUHD]RDPELJXLWDWHQHUHFRPDQGDWvQ cod. 9.5 Structuri autoreferite 2VWUXFWXU18SRDWHFRQ LQHLQVWDQ LHULDOHDOHHLGDUSRDWHFRQ LQHSRLQWHULFWUH LQVWDQ LHULDOHHL struct p struct p {int a,b; {int a,b; float c; float c; struct p next; struct p* next; } }
la analiza lexicalVHLQWUvQFLFOXLQILQLW
OK!!Acest lucru este posibil deoarece pointerul este o YDULDELOFDUHUH LQHDGUHVD altei variabile
7UDQVIHUXOVWUXFWXULORUFDDUJXPHQWHGHIXQ LL6 : transfer chiar structura (prin valoare) : transfer un pointer cWUHHD(prin referin ) Ex: pers om; func1 (om) func2 (&om) 7UDQVIHUXOSULQYDORDUHVHUHFRPDQGLQXUPWRDUHOHFD]XUL: − structura este mic − JDUDQWDUHDIDSWXOXLFIXQ LDDSHODQWQXPRGLILFVWUXFWXUDDSHODW La apelul prin valoare compilatorul JHQHUHD]Rcopie a respectivei structuri (IXQF LD DSHODQWpoate deci modifica doar DFHDVWcopie). DacDPGRULVVSHFLILFPWUDQVIHUXOSULQUHIHULQ LQFODU(vezi func2(&om)) este suficient ca sGHFODUPSHUV* om úLDWXQFLIXQFRP HVWHFRUHFW Atunci în acest caz func1(om) nu ami este corect pentru cRPHVWHXQSRLQWHUúLQXR valoare. 9.7 Returnarea unei structuri K&R nu permite returnarea unei structuri dar ANSI permite acest lucru. 54
Deci: pos by value
pos by reference
Introducere in programare pers f(….) { pers x; … return x; }
pers* f1(…..) { static pers* p; … return &p; }
&kPSXULGHEL L6 Membrii unei structuri de tip int sau XQVLJQHGSRWILDORFD LvQFkPSXULGHSkQOD EL L%L LLVXQWDORFD LvQFHSkQGFXFHOPDLSX LQVHPQLILFDWLYELWDOILHFUXLFXYkQWGH EL L6LQWD[DHVWH WLS>LGHQWLILFDWRU@OUJLPH /UJLPHDFkPSXOXLWUHEXLHVILHRH[SUHVLHvQWUHDJFRQVWDQWFXSULQVLQWUHVL vQVHDPQDOLQLHUHODXUPWRDUHDOLPLWGHFXYkQW 'DFQXVHVSHFLILFXQQXPHSHQWUX FkPSXOGHEL LFkPSXOHVWHDORFDWGDUQXHVWHDFFHVLELOVHIRORVHúWHSHQWUXDOLQLHUL struct special_word { LQWLYDILUHSUH]HQWDWSHEL LLEE XQVLJQHGMYDILUHSUH]HQWDWSHEL LLESkQODE LQWYDILUHSUH]HQWDWSHEL LLESkQODE6 int k :1 ; // va fi reprezentat pe bitul b11 XQVLJQHGPYDILUHSUH]HQWDWSHEL LLESkQODE }; 2 UHVWULF LHLPSRUWDQW DVXSUDRSHUD LLORU FDUH VH SRW IDFH FX FkPSXULOH GHEL L HVWH DFHHDFQXOLVHSRDWHDIODDGUHVDFXDMXWRUXORSHUDWRUXOXL DGUHVDXQXLELWQXDUHVHQV 9.9. Uniuni 8QLXQLOH FRUHVSXQG FX vQUHJLVWUULOH FX YDULDQWH GLQ DOWH OLPEDMH GH SURJUDPDUH (Pascal, Modula-2, etc.); o uniune este un tip “hibrid”, valoarea variabilei uniune fiind YDORDUHD PHPEUXOXL DFWLY DO XQLXQLL FHLODO L PHPEUL ILLQG QHGHILQL L 'LQ SXQFW GH YHGHUH VLQWDFWLF GHILQL LD XQXL WLS XQLXQH VHDPQ FX GHILQL LD XQXL WLS VWUXFWXU FX GLIHUHQ D F indicatorul VWUXFWVHvQORFXLHúWHFXXQLRQVXQWSHUPLVHúLFkPSXULGHEL L Ca implementare sunt similare cu structurile, cu diferen DFPHPEULLVXQWVXSUDSXúLXQXO GXS DOWXO vQ PHPRULH 6SD LXO GH PHPRULH HVWH IRORVLW OD PD[LPXP vQ UDSRUW FX 55
Introducere in programare
VWUXFWXULOH 'H IDSW SHUPLW FD DFHHDúL ]RQ GH PHPRULH V ILH LQWHUSUHWDW GLIHULW IU D folosi cast. 'LPHQVLXQHDVSD LXOXLGHPHPRULHDORFDWXQLXQLLHVWHHJDOFXGLPHQVLXQHDVSD LXOXL alocat celui mai mare membru. Accesul la membrii uniunii se face sub forma unor componente selectate, foloVLQG DFHLDúL RSHUDWRUL úL DFHOHDúL UHJXOL FD vQ FD]XO VWUXFWXULORU 1XPHOHGHXQLXQLUHVSHFWDFHOHDúLUHJXOLFDúLQXPHOHGHXQLXQLVSD LXGHQXPHXWLOL]DUH etc.). ÌQUHJLVWUULYDULDELOH În cazurile în care se poate ca într-o ba]GHGDWHGRXVDXPDLPXOWHFPSXULV se DXWRHOLPLQHUHFLSURFIXQF LHGHGDWHOHLQWURGXVHvQHD VHSRDWHRE LQHRSWLPL]DUHD VSD LXOXLJUXSDWvQPHPRULHGHXQHOHPHQWDOVWUXFWXULLSULQJUXSDUHDDFHORUFPSXUL într-RXQLXQHFHDSDU LQHVWUXFWXULL Ex.: typedef union { struct { char c1,c2; }s long j; float x; }u 1000 C1
1001 C2
1002
1003
J X &RPSLODWRUXODORFVXILFLHQWPHPRULHSHQWUXDSXWHDUH LQHFHOPDLPDUHPHPEUXúLWR L PHPEULLvQFHSODDFHHDúLDGUHV'DWHOHVWRFDWHîntr-o uniune depind de care membru al uniunii se va utiliza. pas.s.c1=’a’ pas.s.c2=’b’ 1000 1001 1002 1003 a b ... Unused
pas.j=5
VXSUDVFULHFHOHFDUDFWHUHúLGHSXQHSHWR LFHLRFWH LWLSlong int) 1000 1001 1002 1003 0
0
0
6XSRUWDFHOHDúLUHJXOLVLQWDFWLFHFDVWUXFWXULOH 56
5
Introducere in programare
([LVWDSOLFD LLGHED]SHQWUXXQLXQL: 1. FUHHD]VWUXFWXULIOH[LELOHYDULDQWDUHFRUGVvQFDUHSRWUH LQHGLIHULWHWLSXULGH date) 2. interpretea]DFHHDúLPHPRULHvQPRGXULGLIHULWH Exemplu pentru cazul 1: Union myunion { int i ; double d ; char c ; } mu ; sizeof ( myunion ) --> 8 sizeof ( mu ) --> 8 sizeof ( mu .i ) --> 2 sizeof ( mu .c ) --> 1 sizeof ( mu .d ) --> 8
5HVSHFWkQGFRQYHUVLLOHQHFHVDUHSXWHPVSXQHFXQSRLQWHUFWUHRXQLXQHHVWHXQ SRLQWHUFWUHRULFDUHGLQWUHPHPEULLHL (double *) & mu == & mu .d O uQLXQH QX SRDWH IL LQL LDOL]DW GHFkW SULQ LQWHUPHGLXO SULPXOXL VX PHPEUX GHFODUDW 9RP SUH]HQWD PDL MRV XQ SURJUDP FDUH FLWHúWH FXYLQWXO VSHFLDO FDUH UH LQH FRPSRQHQWHOHKDUGZDUHGHED]LQVWDODWHvQWUXQVLVWHPGHFDOFXO$FHVWDHVWHLQL LDOL]DWvQ urma DXWRGHWHF LHL IFXWH vQ VHFYHQ D GH SRUQLUH FH DUH ORF OD SXQHUHD VXE WHQVLXQH D sistemului. #include<stdio.h> #include #include<dos.h> union { unsigned w; struct bit_field { unsigned fd :1; unsigned dimR :2; unsigned modV :2; unsigned nrF :2; unsigned DMA :1; unsigned nrRS :3; unsigned game :1; unsigned serP :1; unsigned nrLpt :2; 57
} b; } eqp;
Introducere in programare
void main(void) { unsigned far *ptr; clrscr(); ptr=MK_FP(0,0410); eqp.w=*ptr; // urmeazDILúDUHDILHFUXLHOHPHQWDOuniunii conform // cu vQ HOHVXOdat … getch(); } Cazul 2 Interpretarea GLIHULWDdatelor Ca un exemplu VFRQVLGHUPproblema FRPXQLFD LHLseriale, unde datele vin octet cu octet. Uniunile permit o modalitate de grupare, a RFWH LORUvPSUHXQastfel vQFWVSRDW fi reconstruite în forma lor RULJLQDO'Hexemplu, Vpresupunem Fget_byte( ) este o func LHFDUHvQWRDUFHXQRFWHWFLWLWdintr-un device de comunica LH O valoare GRXEOHSHRFWH L SRDWHILH[WUDVGLQdevice prin 8 apeluri recursive DOHIXQF LHLGXSFXPVHYHGH: union double { char c[]; double val; } double get_double( ) { extern char get_byte( ); //optimal – depinde de modul de structurare a //fiúLHUXOXL int j; union double d; for (j=0; j<8 ; j++) d.c[j]=get_byte( ); return d.val; } Aten LH!!! (VWHRGLIHUHQ PDMRUvQWUHDFHVWPRGGHLQWHUSUHWDUHúLFRQYHUVLLOHSULQcast. ,QL LDOL]DUHDXQLXQLLVSHFLILF$16, : union ex {int I; float f; 58
Introducere in programare
}; union ex test={1}; sau vQFD]XOXQLXQLFHUH LQHúLRVWUXFWXU union n { struct {int i; float f;}s; char ch[6]; } union n test1 = {1, 1.10} (QXPHUUL6
Tipurile de date enumerate sunt folosite în principal pentru a da nume mnemonice unor constante întregi. În exemplul de mai jos enum zile { luni,marti,miercuri,joi,vineri,simbata,duminica } oricezi ; VH GHILQHúWH XQ QRX WLSvQWUHJ QXPLW HQXP ]LOH XQ QXPU GH FRQVWDQWH GH DFHVW WLS OXQL PDU L FXYDORULGHDFHVWWLSúLVHGHFODURYDULDELOGHWLSHQXP]LOHQXPLWoricezi. ÌQ SUH]HQWD RS LXQLL E VDX D FRPHQ]LL FRUHVSXQ]WRare din mediul integrat) FRPSLODWRUXO DUH OLEHUWDWHD VD DORFH VDX RFWH L SHQWUX R YDULDELOD GH WLS HQXPHUDUH LQ IXQF LHGHYDORULOHFRQVWDQWHORUGHILQLWH YDORULOHFRQVWDQWHORUGHILQLWHVXQWGHWLSunsigned FKDUVDXLQWÌQ&XQHLYDULDELOHGHWLSHQXPHUDUHLVHSRDWHDWULEXLRULFHYDORDUHvQWUHDJ enum days this_day ; days another_day ;
// legal si in C si in C++ // legal numai in C++
Constantele de enumerare au implicit valori începând cu 0 care cresc din 1 in 1 (luni PDU L GXPLQLFD /DGHILQL LDWLSXOXLRULFHFRQVWDQWGHHQXPHUDUHSRDWHIL HYHQWXDOXUPDWGHXQLQL LDOL]DWRUVXEIRUPDXQHLH[SUHVLLvQWUHJL FRQVWDQWH SR]LWLYH VDX QHJDWLYH FDUHVWDELOHúWHYDORDUHDFRQVWDQWHL&RQVWDQWHOHGHHQXPHUDUHXUPWRDUHFDUHQX DXRYDORDUHH[SOLFLWFUHVFGLQLQ6XQWSHUPLVHYDORULGXSOLFDWH1XPHOHFRQVWDQWHORU GHHQXPHUDUHVHDIOvQDFHODúL VSD LXGH QXPH FD úL QXPHOHGH YDULDELOH IXQF LLúL WLSXUL Valori de tip enumerare pot apare oriunde sunt permise valori de tip întreg. enum coins { penny = 1, // 1 tuppence, // 2 nickel = penny + 4, // 5 dime = 10, // 10 quarter = nickel * nickel // 25 }; Tipuri enumerative Se folosesc atunci cand se doreste creerea unui set mic de valori care pot fi asociate cu o variabila .Compilatorul va genera o eroare daca se incearca asigurarea unei valori care nu face parte din multimea valorilor specificate in declaratii.
59
De ex.:
Introducere in programare
enum { red,blue,green,yellow } my_colors enum { bright,medium,dark } my_intensity color = yellow color = bright
OK ERROR
Aceste valori numerice pot fi si direct specificat ,caz in care compilatorul aplica sumarea FXSR]L LHQXPDLODFHOHnespecificate. De ex.: enum { apple,arong=10,lemon,grope=-5,melon } va avea ca rezultat urmatoare atribuire de catre compilator: apple=0; orange=10; lemon=11; grape=-5; melon=-4;
60
CAPITOLUL 10
Introducere in programare
1R LXQHDGHIXQF LH 'HILQL LH2IXQF LH&HVWHRFROHF LHGHRSHUD LLVSHFLILFHOLPEDMXOXL6 3kQDFXPVDGLVFXWDWGHVSUHSURJUDPHH[WUHPGHVLPSOH'HDVHPHQHDVDXIRORVLW DSHOXULGHIXQF LLSUHGHILQLWHDOFUXLFRGHVWHJDWDVFULV6HSRDWHVSXQHFRIXQF LHHVWH REXFDWGHSURJUDPVHSDUDWVXESURJUDP FDUHHVWHIRORVLWvQPDLPXOWHVFRSXUL 5HDOL]DUHDXQXLFRGVXUVFkWPDLFODU 6LPSOLILFDUHDSURLHFWULLXQHLDSOLFD LLSULQVWUXFWXUDUH 5HDOL]DUHDGHELEOLRWHFLGHIXQF LLGHFLFUHúWHUHDYLWH]HLGHGH]YROWDUHDDDSOLFD LLORU XOWHULRDUH 3HQWUXLQWURGXFHUHDDFHVWXLFRQFHSWvQPRGFRUHFWWUHEXLHGLVFXWDWHvQWkLXQPLQLPGH WHKQLFL GH SURLHFWDUHD D XQHL DSOLFD LL úL DFHDVWD GHRDUHFH R IXQF LH QX VH LQWURGXFH vQ FDGUXOXQXLSURJUDPGLQPRWLYH³DUWLVWLFH´FLDUHRMXVWLILFDUHODQLYHOORJLFIRDUWHFODU &DWHKQLFLGHSURLHFWDUHH[LVWGRXPHWRGHWRSGRZQGHVXVvQMRV úLERWWRPXS GHMRVvQVXV 3URLHFWDUHDXQHLDSOLFD LLFRQVWvQGHVFRPSXQHUHDORJLFSHVXEPRGXOH )XQF LH GH FRPSOH[LWDWHD DFHVWHL DSOLFD LL GHVFRPSXQHUHD SRDWH FRQWLQXD UHFXUVLY OD QLYHOXOILHFUXLPRGXOUH]XOWDWSkQFkQG
DFHVWDHVWHH[WUHPGHVLPSOXGHUHDOL]DW HVWHGHMDLPSOHPHQWDWUHVSHFWLYDSDUWHvQWURELEOLRWHFDFRPSLODWRUXOXL HVWHGHMDGH]YROWDWDQWHULRU GDWRULWVLPSOLW LLSURJUDPXOXLQXPDLDUHVHQV
ÌQ FD]XO PHWRGHL GH SURLHFWDUH WRSGRZQ SURLHFWDUHD GHFXUJH H[DFW FXP DP GHVFULV PDL VXV 8]XDO vQ SURJUDPDUHD & VH IRORVHúWH DFHDVW PHWRG ÌQ FD]XO ERWWRPXS VH SRUQHúWH GH OD XQ VHW GH IXQF LL úL RSHUD LXQL H[LVWHQWH úL VH FRQVWUXLHVF PRGXOH FDUH OD UkQGXOORUYRUFRQVWUXLUHFXUVLYDOWHPRGXOHSkQFkQGUH]XOWDSOLFD LD$FHDVWPHWRG HVWHIRORVLWX]XDOvQFD]XOSURLHFWULLDSOLFD LLORUSHQWUXED]HOHGHGDWH 7RWXúLHVWHJUHXGHLPSXVXQDQXPLWWLSGHJkQGLUHFkQGVHUHDOL]HD]SURLHFWDUHDXQHL DSOLFD LHL ([LVW R WHRULH IRDUWH ELQH IXQGDPHQWDW úL GRFXPHQWDW SULYLQG DFHVWH SUREOHPHÌQWURSULPID]HVHQ LDOHVXQWH[SHULHQ DDYXWvQSURLHFWDUHDXQXLDQXPLWWLS GHDSOLFD LHúLDSRLH[SHULHQ DGHSURJUDPDUH 'HMD SHQWUX DSOLFD LL IRDUWH PDUL VH OXFUHD] vQ HFKLSH FX PHPEUL H[WUHP GH VSHFLDOL]D L vQ FkWH R ID] D SURFHVXOXL GH SURLHFWDUH LPSOHPHQWDUH WHVWDUH ÌQV LQGLIHUHQWFkWGHFRPSOLFDWDUILGLVWULEX LDXQHLHFKLSHSURJUDPDWRUXOXLSURSULX]LVLVH GDX ILH VSHFLILFD LLOH UHODWLY OD FH YD WUHEXL V IDF SDUWHD GH FRG FDUH R GH]YROW ILH vQ DQXPLWH FD]XUL DOJRULWPXO ÌQ DPEHOH VLWXD LL PRGXO vQ FDUH vúL GHVFRPSXQH DSOLFD LD SHQWUXDR SXWHD UHDOL]D QX SULYHúWH SH QLPHQL /D VIkUúLW DFHDVWD WUHEXLH V IXQF LRQH]H FRUHFWúLDWkW 3DVXOLPHGLDWXUPWRUvQDSOLFDUHDPHWRGHLWRSGRZQHVWHUDILQDUHDSDúLORU ([HPSOX&XPVDUSURLHFWDXQSURJUDPFDUHV³1XPHUHFXYLQWHOHGLQWUXQILúLHU´"
61
Introducere in programare
Descriere în pseudocod 'HVFKLGHILúLHUXO DWkWDWLPSFkWPDLVXQWFXYLQWHLQILúLHUI ^ FLWHúWHXQFXYkQW FUHúWHQXPUXOGHFXYLQWHFX ` ÌQFKLGHILúLHUXO $ILúHD]QXPUXOGHFXYLQWH 5DILQDUHDSDúLORU6
'HVFKLGHILúLHUXO $WkWDWLPSFkWPDLVXQWFXYLQWHLQILúLHU ^ FLWHúWHFDUDFWHUHSkQVHvQWkOQHúWHXQFDUDFWHUGLIHULWGH¶¶ FLWHúWHFDUDFWHUHOHSkQJVHVFXQµµ LQFUHPHQWHD]QUBFXY ` ÌQFKLGHILúLHUXO $ILúHD]QUBFXY 'HFODUDUHDúLGHILQLUHDIXQF LLORU6 )XQF LD PDLQ D IRVW IRORVLW vQ WRDWH H[HPSOHOH GH SkQ DFXP 'H FkWH RUL XQ SURJUDP&HVWHH[HFXWDWHOvQFHSHFXSULPDLQVWUXF LXQHGLQIXQF LDPDLQ %LEOLRWHFLOH& QH SXQ OD GLVSR]L LH R VXP YDULDELO GH UXWLQH SUHFRPSLODWH 7RWXúL SHQWUX D SXWHD UH]ROYD SUREOHPH PDL FRPSOH[H HVWH QHFHVDU V VH SURLHFWH]H GHFODUDUH úL GHILQLUH SURSULLOHIXQF LL 'HFODUDUHDIXQF LLORU6 2 IXQF LH HVWH GHFODUDW H[DFW FD R YDULDELO LDU WLSXO GH GDW SH FDUHO UHWXUQHD] WUHEXLHVSHFLILFDWÌQ&WUHEXLHDYXWJULMFDQXPHOHXQHLIXQF LLSURSULLVQXFRLQFLGFX QXPHOH XQHL IXQF LL GH ELEOLRWHF ÌQ FD] FRQWUDU OLQNHGLWRUXO QX YD úWL OD JHQHUDUHD GH OHJWXUL vQWUH ILúLHUHOH RELHFW GDF DSHOXO UHVSHFWLYHL IXQF LL VH UHIHU OD UXWLQD SUHFRPSLODWH[LVWHQWGHMDVDXODFRUSXOIXQF LHLSURSULL6HSRDWHFKLDUPDLUXVQXVH JHQHUH]HXQPHVDMGHHURDUHDFHVWOXFUXVHvQWkPSOFkQGSURWRWLSXULOHFHORUGRXIXQF LL VHSRWULYHVFLDUDFHVWWLSGHHURDUHHVWHH[WUHPGHJUHXGHGHWHFWDW 'HDVHPHQHDWUHEXLHVSHFLILFDWHúLDUJXPHQWHOHFHYRUILWUDQVPLVHIXQF LHLODDSHO WLSXORUGLQHDORU 2IRUPJHQHUDOSHQWUXGHFODUDUHDXQHLIXQF LLHVWH WLSBGDW!QXPHBIXQF LHOLVWBDUJXPHQWH!
XQGH OLVWBDUJXPHQWH! ^WLSBGDW QXPHBDUJ WLSBGDW QXPHBDUJ « WLSBGDW QXPHBDUJQ`
62
Introducere in programare
)XQF LLOH VXQW GHFODUDWH vQDLQWH GH IXQF LD PDLQ $FHVW OXFUX SHUPLWH FD RULFDUH IXQF LHVILHDSHODWGHRULFDUHDOWDLQFOXVLYGHHDvQVúLFD]XOIXQF LLORUUHFXUVLYH 6HSRWGHFODUDIXQF LLúLvQLQWHULRUXODOWRUIXQF LLGDUvQDFHVWFD]HOHQXSRWILDSHODWH GHFkWvQLQWHULRUXOIXQF LHLXQGHDXIRVWGHFODUDWH'HDFHHDPHWRGDGHGHFODUDUHJOREDOD ORUHVWHFHOPDLGHVIRORVLW (VWH QHFHVDU V VH UHDOL]H]H VSHFLILFDUHD WLSXOXL GH GDW SHQWUX DUJXPHQWH úL YDORDUHD UHWXUQDW $FHVW OXFUX SHUPLWH FRPSLODWRUXOXL V YHULILFH GDF IXQF LLOH VXQW DSHODWHFRUHFWúLVUHDOL]H]HRULFHFRQYHUVLHLPSOLFLWDUILQHFHVDU 'HH[HPSOXXQDSHOGHIXQF LHFDPDLMRV LQFOXGHPDWKK! « GRXEOH[ « [ VTUW «
SRDWH IL FRQVLGHUDW FD LQFRUHFW GHRDUHFH IXQF LD UDGLFDO DUH FD SDUDPHWUX GH LQWUDUH R YDULDELO GH WLS GRXEOH 'DWRULW IDSWXOXL F IXQF LD HVWH GHFODUDW vQ FDGUXO ELEOLRWHFLL PDWKKFDUHHVWHLQFOXVvQSURJUDPFRPSLODWRUXOYDIDFHFRQYHUVLDGHODvQWUHJODGRXEOH DYDORULLDVWIHOvQFkWODDSHOIXQF LDUDGLFDOYDSULPLRYDORDUHFRUHFW 'HFODUD LLLPSOLFLWH 'DFQXHVWHVSHFLILFDWQLFLXQWLSGHGDWODYDORDUHDSHFDUHWUHEXLHVRUHWXUQH]HR IXQF LH DWXQFL FRPSLODWRUXO YD FRQVLGHUD F IXQF LD UHWXUQHD] XQ vQWUHJ 6LPLODU GDF IXQF LD QX HVWH GHFODUDW FRPSLODWRUXO YD FRQVLGHUD F UHWXUQHD] XQ vQWUHJ úL YD GD HURDUHGDFvQSURJUDPHVWHIRORVLWDOWIHO 7RWXúL GDF VH LQH FRQW QXPDL SH WLSXO LPSOLFLW FRPSLODWRUXO QX YD úWL V IDF FRQYHUVLLOHGHWLSODDSHO 'HILQLUHDIXQF LHL6 'HILQLUHDXQHLIXQF LLVHUHIHUODREXFDWGHFRG&FDUHUH]ROYRDQXPLWSUREOHP DFHHDSHQWUXFDUHVDLQWURGXVIXQF LD $FHDVWGHILQL LHVHIDFHvQDIDUDFRUSXOXLIXQF LHL PDLQ VDXDODOWHLIXQF LLúLDUHIRUPDJHQHUDOGDWPDLMRV
63
Introducere in programare
6WUXFWXUDJHQHULFDXQHLIXQF LL& 7LSXO IXQF LHL
1XPHOH IXQF LHL
/LVW DUJXPHQWH
^
'HFODUD LL
LQVWUXF LXQL
`
([HPSOX WLS QXPH GHFODUD LHDUJXPHQW LQWSZLQWQXP ^ QXPHDUJXPHQW LQWUH] GHFODUD LHYDULDELOORFDO UH] QXP QXP UHWXUQUH] LQVWUXF LXQL& ` FRUSXOIXQF LHL VDX
WLSBGDW!QXPHBIXQF LHOLVWDUJXPHQWH! ^ GHFODUD LLYDULDELOHORFDOH! LQVWUXF LXQL! UHWXUQYDULDELOH[SUHVLH! `
3ULPD OLQLH WUHEXLH V ILH LGHQWLF FX FHD GLQ GHFODUD LH *UXSXO GH YDULDELOH ORFDOH DXWRPDWH FDUHSRWILIRORVLWHvQLQWHULRUXOXQHLIXQF LLFRQVWvQFHOHGHFODUDWHvQOLVWDGH DUJXPHQWHVDXODvQFHSXWXOIXQF LHL
64
Introducere in programare
&D úL vQ FD]XO IXQF LHL PDLQ YDULDELOHOH WUHEXLHVF GHFODUDWH LPHGLDW GXS SDUDQWH]D GHVFKLVúLvQDLQWHGHDOWHLQVWUXF LXQL &D XQ H[HPSOX GH IXQF LH V OXP FD]XO FDOFXOXOXL UGFLQLL SWUDWH GDU FX VSHFLILFDUHD SUHFL]LHL GRULWH 6H YD IRORVL PHWRGD 1HZWRQ 5DSKVRQ SHQWUX DSUR[LPDUHD UGFLQLL LQFOXGHVWGLRK! LQFOXGHFRQLRK! LQFOXGHPDWKK! GRXEOHUDGLFDOGRXEOH[IORDWSUHFL]LH YRLGPDLQYRLG ^ IORDWQUSUHF GRXEOHUH]XOWDW FOUVFU SULQWI³'DWLQXPDUXO´ VFDQI³I´ QU SULQWI³?Q'DWLSUHFL]LD´ VFDQI³I´ SUHF VZLWFKUH]XOWDW UDGLFDOQUSUHF ^ FDVH±SULQWI³(URDUH1XPDU1HJDWLY´ EUHDN FDVH±SULQWI³(URDUH3UHFL]LDHVWHLQWUHVL´ EUHDN GHIDXOWSULQWI³?Q5DGLFDOXOHVWHOI´UH]XOWDW ` SULQWI³?Q3UHVVDQ\NH\WRFRQWLQXH´ JHWFK ` GRXEOHUDGLFDOGRXEOH[IORDWSUHFL]LH ^ GRXEOH[BYHFKL[BLQLWLDO [ LI[ UHWXUQ± LI[ UHWXUQ[ LISUHFL]LH! __SUHFL]LH UHWXUQ± GR ^ [BYHFKL [ [ [[BLQLWLDO[ `ZKLOHIDEV[[BYHFKL [ !SUHFL]LH UHWXUQ[ `
65
Introducere in programare
6HREVHUYFDSURDSHMXPWDWHGLQFRUSXOIXQF LHLWUDWHD]HURULOHFHSRWDSUHD'HúL ODSULPDYHGHUHDFHVWOXFUX DSDUHFDXQHIRUW VXSOLPHQWDU QHMXVWLILFDW vQ SURJUDPDUH vQ PRPHQWXOvQFDUHHVWHFD]XOXQHLDSOLFD LLFXSHVWHIXQF LLFDUHVH DSHOHD]XQHOHSH DOWHOHFRGXOHVWHUHODWLYJUHXGHXUPULWúLGHSDQDWvQFD]FRQWUDU ÌQ FRQGL LLOH vQ FDUH VD SUHY]XW ILHFDUH IXQF LH GH OD vQFHSXW FX DFHVWH YHULILFUL DWXQFL HVWH H[WUHP GH XúRU FD V VH SRDW GHSLVWD HURULOH 'DF DFHDVW SROLWLF HVWH DSOLFDWODRULFHQLYHODOSURJUDPXOXLYDFRQGXFHODVFULHUHDXQXLFRGVWDELO6WDELOLWDWHD VHUHIHUODIDSWXOFGDFDSDUHXQGHYDvQFXUVXOH[HFX LHRHURDUHGDWRUDWILHFRQGL LLORU H[WHUQH LQWUUL JUHúLWH ILH FRQGL LLORU LQWHUQH GH H[HPSOX HURDUH GH SURJUDPDUH VDX YLUXV DFHDVWDYDILVHPQDODWXWLOL]DWRUXOXL(DVHYDSURSDJDSHFDOHLHUDUKLFGLQDSHO vQDSHOGHIXQF LHIUFDSURJUDPXOVLDVGLQSDUDPHWULQRUPDOLGHIXQF LRQDUHQXVH SLHUGHFRQWUROXODVXSUDDSOLFD LHL 3HQWUXUHDOL]DUHDXQRUSURJUDPHFDUHVSULPHDVFSDUDPHWULGHLQWUDUHGLQOLQLDGH FRPDQGIXQF LDPDLQ SRDWHSULPLXQVHWGHSDUDPHWULFDUHVFRQ LQH[DFWVLUXULOHGH FDUDFWHUHGLQOLQLDGHFRPDQG0DLMRVHVWHGDWXQH[HPSOXVLPSOXGHIRORVLUHDDDFHVWHL IDFLOLW LúLDQXPHXQSURJUDPFDUHYDDILúDSDUDPHWULLSULPL LSULQOLQLDGHFRPDQG LQFOXGHVWGLRK! YRLGPDLQLQWDUJFFKDU
DUJY ^ LQWL SULQWIDUJF G?Q?QDUJF IRUL LDUJFL SULQWIDUJY>G@V?QLDUJY>L@ ` 6HREVHUY F SULPXO DUF VSHFLILF QXPUXO GH OHPHQWH GLQ FDUH HVWH FRPSXV OLQLD GH FRPDQG LDU DUJY UH LQH DGUHVD GH vQFHSXW D ]RQHL GH PHPRULH vQ FDUH VXQW VWRFDWH DFHVWHD
7LSXOGHGDWvoid Geneza pointerului void $IRVWLQWURGXVGHVWDQGDUGXO$16,SHQWUXDUH]ROYDXUPWRDUHDDPELJXLWDWHGHVLQWD[ ùWLPFDYHPSRVLELOLWDWHDGHDDORFDPHPRULH )XQF LD vQWRDUFH XQ SRLQWHU FWUH vQFHSXWXO ]RQHL GH PHPRULH DORFDW LQGLIHUHQW F VXQWHPVXE'26:,1'2:6VDX81,; ÌQWUHEDUHDHVWHGHFHWLSHVWHDFHVWSRLQWHUVDX WUDGXVFXPYDILLQWHUSUHWDW]RQDFRQWLJXDGHPHPRULHUH]HUYDW &DILLQGJUXSDWvQHOHPHQWHGHOXQJLPHRFWHWVLJQHGVDXXQVLJQHGVDXFKDU &DILLQGJUXSDWvQHOHPHQWHGHOXQJLPHRFWH LVLJQHGVDXXQVLJQHG úDPG 3HQWUX D UH]ROYD DFHDVW VHPLDPELJXLWDWH GSGY DO FRPSLODWRUXOXL úL QX QXPDL VD GHFLVFDSRLQWHUXOUHWXUQDWVILHGHWLSYRLG8QWLSLQH[LVWHQWGDUFRQYHUWLELOILHH[SOLFLW SULQFDVW. 5VWDQGDUG ILHLPSOLFLWSULQDVLJQDUH$16, ODWLSXOYDULDELOHLFDUHLDLVH DVLJQHD] ÌQ OLPEDMXO & R IXQF LH WUHEXLH V UHWXUQH]H XQ WLS GH GDWH OD WHUPLQDUHD H[HFX LHL 7RWXúLH[LVWFD]XULFkQGQXVHIDFFDOFXOHGLUHFWHFLVHUHDOL]HD]DQXPLWHDF LXQLFXPDU
66
Introducere in programare
IL GH H[HPSOX DILúDUHD XQXL PHVDM ÌQ DFHVWH FD]XUL WRWXúL WUHEXLH VSHFLILFDW FRPSLODWRUXOXLIDSWXOFIXQF LDQXUHWXUQHD]QLPLF ([LVW úL FD]XO LQYHUV DGLF R IXQF LH FDUH QX SULPHúWH QLFL XQ SDUDPHWUX GDU UHWXUQHD]UH]XOWDWHFD]XOFLWLULLXQXLSRUWGHGDWH 3HQWUXDSXWHDVSHFLILFDDFHVWHVLWXD LL VHIRORVHúWHWLSXOGHGDWYRLGFDUHvQWUDGXFHUHvQVHDPQOLSVLWGHFRQ LQXWQLPLF
Transferul Argumentelor $UJXPHQWHOH XQHL IXQF LL UHSUH]LQW FLOH GH WUDQVIHU D GDWHORU FWUH IXQF LH 0XOWH OLPEDMH GH SURJUDPDUH WUDQVIHU DUJXPHQWHOH SULQ UHIHULQ H FHHD FH vQVHDPQ F WULPLW XQ SRLQWHU FWUH DUJXPHQW ÌQ & DUJXPHQWHOH VXQW WULPLVH SULQ YDORDUH FHHD FH vQVHDPQFRFRSLHDDUJXPHQWXOXLHVWHWULPLVIXQF LHL)XQF LDSRDWHPRGLILFDYDORDUHD FRSLHLGDUQXSRDWHPRGLILFDYDORDUHDFDUHRDUHDUJXPHQWXOvQUXWLQDDSHODQW
)LJXUD DUDW GLIHUHQ D VH REVHUY F VJH LOH GH OD WUDQVIHUXO SULQ UHIHULQ VXQW ELGLUHF LRQDOHFHHDFHVSHFLILFIDSWXOFPRGLILFULOHORFDOHVHUHSHUFXWHD]GLUHFW $UJXPHQWXO WULPLV HVWH GH RELFHL QXPLW DUJXPHQW DFWXDO vQ WLPS FH FRSLD UHFHS LRQDWHVWHQXPLWDUJXPHQWIRUPDOVDXSDUDPHWUXIRUPDO 'HRDUHFH&WUDQVIHUDUJXPHQWHOHSULQYDORDUHRIXQF LHSRDWHDVLJXUDQRLYDORUL ODDUJXPHQWHOHIRUPDOHIUDOHDIHFWDSHFHOHDFWXDOH'HH[HPSOX LQFOXGHVWGLRK! YRLGPDLQYRLG ^H[WHUQYRLGI LQWD ID SULQWI³\ G?Q³D H[LW `
67
Introducere in programare
YRLGILQWDUJ ^ DUJ ` YD DYHD FD UH]XOWDW DILúDUHD YDORULL PRGLILFDUHD FRSLHL QHPDLDIHFWkQG YDORDUHD GLQ IXQF LDDSHODQW'DFVHGRUHúWHFDIXQF LDVSRDWVFKLPEDYDORDUHDXQXLRELHFWDWXQFL WUHEXLHFDDFHDVWDVSULPHDVFDGUHVDRELHFWXOXLGHFLVLVHWULPLWXQSRLQWHUODRELHFW FDvQH[HPSOXOXUPWRU YRLGVZDSLQW [LQW \ ^ UHJLVWHULQWWHPS WHPS [ *x=*\
\ WHPS ` YRLGPDLQYRLG ^ LQWD E VZDS D E SULQWI³D GE G?Q´DE ` XQGHVHYDDILúDD úLE &XWUDQVIHUXOSULQUHIHULQ QHDPvQWkOQLWGHODSULPHOHOHF LLVFDQI³G´ QU
'HFODUD LLúLDSHOXUL6 )XQF LLOHSRWDSUHDvQWUXQSURJUDP : 'HILQL LH RGHFODUD LHFDUHGHILQHúWHFHIDFHIXQF LDSUHFXPúLQXPUXOúLWLSXO DUJXPHQWHORUHL : )XQFWLRQDOOXVLYHVHGHFODURIXQF LHGHILQLWvQDOWSDUWH$FHVWOXFUXVSHFLILF FH WLSXUL GH YDORUL vQWRDUFH IXQF LD SUHFXP úL FH WLSXUL GH YDORUL SULPHúWH : $SHOXOIXQF LHL DSHOSURSULX]LVODvQWkOQLUHDOXLvQFRQWH[WXOH[HFX LHLSURJUDPXOXL 6DUH OD ]RQD FRUSXOXL IXQF LHL H[HFXW FH HVWH DFROR úL DSRL VH UHvQWRDUFHODORFXODSHOXOXL Declararea argumentelor 'HFODUDUHD DUJXPHQWHORU VH VXSXQH OD DFHOHDúL UHJXOL FD RULFDUH GHFODUDUH GH YDULDELOHFXXUPWRDUHOHH[FHS LL VLQJXUDFODVGHVWRFDUHSHUPLVHVWHUHJLVWHU FKDU úL VKRUW VXQW FRQYHUWLWH OD vQWUHJL IORDW VXQW FRQYHUWLWH OD GRXEOH 1RLOHIDFLOLW L$16,&SHUPLWGH\DFWLYDUHDDFHVWRUFRQYHUVLLDRWRPDW XQ DUJXPHQW IRUPDO GHFODUDW FD IXQF LH HVWH FRQYHUWLW OD SRLQWHU FWUH IXQF LH VHSRDWHVQXLQFOXGXQLQL LDOL]DWRUvQWURGHFODUD LHGHDUJXPHQWH
68
Introducere in programare
&RUSXOIXQF LHL &RUSXOXQHLIXQF LLHVWHGHOLPLWDWGHXQVHWGHSDUDQWH]H^`(OSRDWHILJROIDSWFH HVWHIRORVLWRUvQGLYHUVHSHULRDGHDGH]YROWULLXQXLSURJUDP 8QXOGLQSULPHOHOXFUXULvQFD]XOSURLHFWULLXQHLDSOLFD LLPDULHVWHGHILQLUHDXQXL VHWGHRSHUD LLGHQLYHOvQDOWFHFRUHVSXQGFDIXQF LRQDUHÌQDFHVWHVWDJLLHVWHIRORVLWRUV DPXQSURJUDPIXQF LDGHFODUD LLSDUDPHWULLVSHFLILFD LGDUFRUSXOJRO/DDSHOHDQXYD H[HFXWDQLPLF8OWHULRUDFHVWHFRUSXULYRUILFRPSOHWDWH 9DORULOHUHWXUQDWHvQWRDUVH 2IXQF LHSRDWHUHWXUQDRVLQJXUYDORDUHSULQLQWHUPHGLXOFXYkQWXOXLFKHLHUHWXUQ $FHDVWYDORDUHSRDWHILXQSRLQWHUSHQWUXWDEORXYDORULPXOWLSOH VDXRIXQF LH ,QGLUHFW vQVúLDFHVWHDSRWILvQWRDUVHGDFVHIRORVHúWHXQSRLQWHUODDFHVWHDFDYDORDUHDWLQV2 VWUXFWXU VDX XQLXQH SRW IL UHWXUQDWH GLUHFW GHúL DFHVW OXFUX QX HVWH UHFRPDQGDW GDWRULW GLPHQVLXQLORUPDULDOHORU &RUSXO XQHL IXQF LL SRDWH FRQ LQH RULFkWH DSHOXUL GH UHWXUQ /D SULPXO vQWkOQLW H[HFX LDIXQF LHLHVWHvQFKHLDWvQOLSVDWRWDODXQXLUHWXUQvQWRUFHUHDvQIXQF LDDSHODQW VHIDFHDWXQFLFkQGVHvQWkOQHúWHPDUFDWRUXOGHVIkUúLWGHEORF³`³ 9DORDUHD vQWRDUV WUHEXLH V ILH FRPSDWLELO FX WLSXO IXQF LHL ([ GH FD] vQ FDUH YDORDUHD vQWRUV HVWH GLIHULW HVWH GDW PDL MRV GDU vQ DFHVW FD] FRQYHUVLD OD YDORDUHD OD YDORDUHDUHWXUQDWVHIDFHDXWRPDW $7(1,( $FHVWOXFUXSRDWHFUHDRJURD]GHQHSOFHULILLQGRHURDUHH[WUHPGHJUHX GHJVLW )ORDWIYRLG ^ LQWD FKDUF IORDWI LID!F[ ^ I DDFRQYHUWLWODIORDW UHWXUQD ` HOVH ^ I FFFRQYHUWLWODIORDW UHWXUQF ` ` /LPEDMXO & HVWH IRDUWH ULJXURV UHODWLY OD SRWULYLUHD WLSXULORU GH SRLQWHUL ÌQ H[HPSOXO XUPWRU I HVWH GHFODUDW FD R IXQF LH FH vQWRDUFH XQ SRLQWHU FWUH FKDU VXQW SUH]HQWHXQHOHSRVLELOLW LOHJDOHVDXLOHJDOHGHvQWRDUFHUHGHUH]XOWDW
69
Introducere in programare
&KDU I ^ FKDU
FP FY FYFD>@ LQW LS LS FY FYDGPLVDXDFHODúLWLS UHWXUQFYDGPLVFYHVWHFKDU sau
sau
sau
sau
FY FPDGPLVDXDFHODúLWLS UHWXUQ FPDGPLV FPHVWHFKDU FY FDDGPLVDXDFHODúLWLS UHWXUQFDDGPLVFDHVWHFKDU FY FYHURDUHFYHVWHFKDU LDU FYHVWHFKDU
UHWXUQ FYHURDUHGLQDFHVWPRWLY FY LSHURDUHFKDU ≠ LQW UHWXUQ$WHQ LH3URGXFHXQFRPSRUWDPHQWLPSUHGLFWLELOVHDúWHDSWR YDORDUHVILHvQWRDUV
$OX]LLODIXQF LL6 2DOX]LHODIXQF LHUHSUH]LQWRGHFODUD LH DXQHLIXQF LLFDUH HVWHGHILQLWvQDOW SDUWHGHRELFHLvQWUXQILúLHUVXUVGLIHULW,PSOLFLWWRDWHIXQF LLOHVXQWSUHVXSXVHDUHWXUQD LQW 6FRSXOSULQFLSDODOUHGHFODUULLHVWHGHDLQGLFDSURWRWLSXOFDFRPSLODWRUXOVúWLH FXP V WUDQVPLW YDULDELOHOH OD DSHOXO HL SUHFXP úL FODVD GH VWRFDUH ÌQ FD]XO vQ FDUH FODVD GH VWRFDUH QX HVWH SUHFL]DW HD HVWH FRQVLGHUDW H[WHUQ LPSOLFLW $FHVW OXFUX vQVHDPQFGHILQL LDIXQF LHLSUHFXPúLFRUSXOHLSRWDSUHDvQRULFDUHGLQILúLHUHOHVXUV D DSOLFD LHL LQFOXVLY vQ FHO FXUHQW 'DF VH RPLWH úL VSHFLILFDUHD WLSXOXL DWXQFL LPSOLFLW HVWHLQW 6FRSXO vQ FD]XO XQHL DOX]LL OD IXQF LH HVWH WLSLF FD OD RULFDUH DOW YDULDELO DSHOXULOHvQEORFDXVFRSEORFUHVWXODXVFRSXOILúLHU Conversiile automate de argument ÌQ DEVHQ D SURWRWLSXOXL DOW PRWLY SHQWUX DOX]LLOH OD IXQF LL WRDWH DUJXPHQWHOH PDLPLFLFDLQWVXQWFRQYHUWLWHODLQWLDUWRDWHDUJXPHQWHOHIORDWVXQWFRQYHUWLWHODGRXEOH 'HFLSRDWHILRVXUVELQHDVFXQVGHHURUL 3RLQWHULODIXQF LL6 6XQW LQVWUXPHQWH SXWHUQLFH SHQWUX FD QH SHUPLW R PRGDOLWDWH HOHJDQW GH DSHO SHQWUXGLIHULWHIXQF LLED]kQGXVHSHGDWHOHGHLQWUDUH ÌQDLQWHGHDGLVFXWDSRLQWHULLODIXQF LHWUHEXLHYRUELWSX LQGHVSUHPRGXOvQFDUH FRPSLODWRUXOLQWHUSUHWHD]GHFODUD LLOHIXQF LLORUSUHFXPúLDSHOXULOHDFHVWRUD 6LQWD[D SHQWUX GHFODUDUHD úL LQYRFDUHD IXQF LLORU HVWH IRDUWH DSURSLDW GH VLQWD[D IRORVLW vQ FD]XO WDEORXULORU 'H H[ vQ GHFODUD LD LQW Y>@ Y HVWH SRLQWHU FWUH SULPXO
70
Introducere in programare
HOHPHQWDOWDEORXOXL$WXQFLFkQGVLPEROXOHVWHXUPDWGHXQQXPHQXPUvQWUHSDUDQWH]H SRLQWHUXO HVWH LQGH[DW FX DFHO QXPU DSRL UHIHULW 8Q SURFHV VLPLODU DSDUH úL vQ FD]XO IXQF LLORU )LHGHFODUD LDLQWI vQDFHVWFD]IHVWHXQSRLQWHUODIXQF LH&kQGHOHVWHXUPDW GHROLVWGHDUJXPHQWHLQFOXVvQWUHSDUDQWH]HHO HVWHUHIHULW DOWPRGGHDVSXQH FR IXQF LHHVWHDSHODW 'HDVHPHQHDGDFLQWY>@VHUHIHUODXQSRLQWHUFRQVWDQWODIHOúL vQFD]XOLQWI IHVWHWRWXQSRLQWHUFRQVWDQWvQVHVWHLOHJDOVVHDVLJQH]HRYDORDUHOXL I 3HQWUX D GHFODUD R YDULDELO SRLQWHU FWUH R IXQF LH QXPHOH SRLQWHUXOXL WUHEXLH SUHFHGDWGH GHH[HPSOXLQW SI HVWHXQSRLQWHUFWUHRIXQF LHFHvQWRDUFHXQvQWUHJ 'HIDSWVHGHFODURYDULDELOGHWLSSRLQWHUFDUHHVWHFDSDELOVUH LQXQSRLQWHUFWUHR IXQF LHFHvQWRDUFHXQvQWUHJ 3DUDQWH]HOHVXQWQHFHVDUHGHRDUHFHvQOLSVDORUSIDUILFRQVLGHUDWRIXQF LHFDUH vQWRDUFHXQSRLQWHUODvQWUHJ $VLJQDUHDXQHLYDORULSHQWUXSRLQWHULLODIXQF LLVHUHDOL]HD]DVWIHOILHIXQF LDLQW IDWBER\ úL SRLQWHUXO OD R IXQF LH FH vQWRDUFH XQ vQWUHJ LQW SI $VLJQDUHD VH UHOL]HD]GLUHFWSI IDWBER\'HYLQDVWIHOFODUHXUPWRDUHOHHURUL SI I XQGHSIHVWHSRLQWHUODIXQF LHLDUI HVWHXQvQWUHJ SI I QX VH SRDWH RE LQH DGUHVD UH]XOWDWXOXL XQHL IXQF LL GHRDUHFHWUDQVIHUXOVHIDFHSULQVWLY SI IXQGHSIHVWHSRLQWHUODLQWLDU IHVWHSRLQWHUODSRLQWHU &RQYHQ LLGHWLSvQFD]XOGHvQWRDUFHUHSDUDPHWULL6 1XWUHEXLHXLWDWFWLSXOGHGDWHvQWRUVGHIXQF LHWUHEXLHVILHvQFRLQFLGHQ FX WLSXOGHSRLQWHUODIXQF LHGHFODUDW 'HH[ILHGHFODUD LLOH LQWLI LI SLI IORDWII SII FKDUFI SFI úLVHFYHQ DGHDVLJQUL SLI LIOHJDOWLSXULOHVXQWLGHQWLFH SFI FILOHJDOQHSRWULYLUHGHWLS SII LILOHJDOQHSRWULYLUHGHWLS SFI FIOHJDO LI LILOHJDOVHvQFHDUFDVLJQDUHDXQHLQRLYDORULODRFRQVWDQW $SHOXOXQHLIXQF LLIRORVLQGpointeri ([ LQWI LQW SI LQWUH] « SI IDPDVLJQDWSIODIXQF LH UH] SI D DPDSHODWIXQF LDIFXDFDSDUDPHWUX
71
Introducere in programare
Obs: SI D HVWH(&+,9$/(17FX
SI 6LQWD[D$16,SHUPLWHFDvQORFGH SI D VIRORVHVFSID 3HQWUXSRUWDELOLWDWHDXQXLSURJUDPHVWHLQGLFDWVVHIRORVHDVF SULPDIRUP Întoarcerea ca rezultat de SRLQWHUODIXQF LL6 2 IXQF LH SRDWH vQWRDUFH XQ SRLQWHU OD DOW IXQF LH GDU WUHEXLH DYXW JULM GHVSUH PRGXOvQFDUHVHYDIDFHGHFODUDUHDDFHVWHLIXQF LL'HH[HPSOX ,QW IIORDW[IORDW\ sau LQW I[\ IORDW[\ ^ « ` GHFODURIXQF LHFHDUHYDULDELOHGHLQWUDUH[\úLFDUHvQWRDUFHXQSRLQWHUFWUHRIXQF LH FHvQWRDUFHXQvQWUHJ 6OXPXQH[HPSOXPDLFRQFUHWGHIRORVLUHDDFHVWRUIDFLOLW LúLDQXPHRIXQF LH GHVRUWDUHRSWLPDOGLQSXQFWGHYHGHUHDOYLWH]HL 6 DQDOL]P SX LQ SUREOHPD ([LVW PDL PXO L DOJRULWPL GH VRUWDUH ILHFDUH FX DYDQWDMHúLGH]DYDQWDMH 4XLFNVRUW : IRDUWHUDSLGSHQWUXGDWHDOHDWRUDUDQMDWH : LQHILFLHQWGDFGDWHOHVXQWSDU LDOVRUWDWHSHJUXSXUL 0HUJHVRUW : XQXOGLQWUHFHLPDLHILFLHQ LGDUFHUHPHPRULH +HDSVRUW : FHO PDL LQGLFDW SHQWUX DULL PDVLYH GH GDWH GHRDUHFH QHFHVLW FHD PDL SX LQ PHPRULH 3S F vQ XUPD XQRU DQDOL]H SXWHP GHFLGH FDUH GLQWUH FHL WUHL DOJRULWPL YRU IL XWLOL]D L 3RDWHILVFULVXUPWRDUHDIXQF LH 9RLG P\BVRUWIORDWLQBGDWD ^ H[WHUQYRLGTXLFNBVRUW PHUJHBVRUW KHDSBVRUW KHDSBVRUWSRDWHOLSVL VH UHDOL]HD] DQDOL]D GDWHORU 6 SUHVXSXQHP F GHFL]LD YD IL GHSXVvQWUXQIODJ VZLWFKIODJ FDVHUHWXUQTXLFNBVRUW FDVHUHWXUQPHUJHBVRUW FDVHUHWXUQKHDSBVRUW GHIDXOWSXWV´(UURUXQNQRZQ´ H[LW EUHDN ` ODDSHOP\BVRUWOLVW OLVW
72
Introducere in programare
Recursivitatea 2IXQF LHUHFXUVLYHVWHXQDFDUHVHDXWRDSHOHD]'HH[ YRLGUHFXUVYRLG ^ VWDWLFFRQW SULQWI³G?Q³FRQW FRQW UHFXU ` YRLGPDLQYRLG ^ UHFXU `
YRLGUHFXUYRLG ^ VWDWLFFRQW LIFRQW! UHWXUQ HOVH ^ SULQWI³G?Q³FRQW FRQW UHFXU ` `
/D ILHFDUH DSHO GH IXQF LH vQ VWLY VXQW VDOYD L R VHULH GH SDUDPHWULL 3URJUDPXOGH PDL VXV YD DXWRDSHOD IXQF LD OD ∞ GDU VWLYD 3& HVWH ILQLW DYkQG FD UH]XOWDW R EORFDUH JHQHUDO'HDFHHDRULFHIXQF LHUHFXUVLYWUHEXLHVDLERFRQGL LHQXPLWFD]GHED] FDUHVSHUPLWVWRSDUHDDFHVWXLSURFHV8QH[HPSOXVLPSOXDUILvQFD]XOQRVWUXOLPLWDUHD FRQWRUXOXL'HFL ÌQWUHEUL &HVHvQWPSOGDFúWHUJHPHOVHúLFHOHGRXDFRODGHGHGXSHO" &HVHvQWPSOGDFFRQWQXHGHFODUDWVWDWLF" )RORVLUHD YDULDELOHORU IL[H HVWH R FDOH GH D FRQWUROD UHFXUVLYLWDWHD GXS FXP DP Y]XW DQWHULRU ÌQ H[HPSOXO GH PDL MRV VH FDOFXOHD]
Q
∑L L
=
L∈1UHFXUVLY ,QWVXPLQWQ ^ LIQ UHWXUQQ HOVHUHWXUQQVXPQ ` 3W Q V YHGHP FXP VH H[HFXWSDVFXSDV
73
Introducere in programare
(Clase de stocare) Storage classes
0DMRULWDWHD DSOLFD LLORU PDUL VXQW VFULVH GH HFKLSH GH SURJUDPDWRUL 'XS SURLHFWDUHD VFKL DUHD DSOLFD LHL vQ FRPXQ ILHFDUH SURJUDPDWRU SOHDF úL UHDOL]HD] R EXFDWL]RODWGHSURJUDP,]RODUHDVHUHIHUODIDSWXOFODUHDOL]DUHDDFHOXLSURJUDPQX VH LQH FRQW GH FHLODO L FL OD IXQF LLOH FDUH WUHEXLH V OH vQGHSOLQHDVF SURJUDPXO /D VIkUúLWDFHVWHSURJUDPHVXQWXQLWHOLQNHG vPSUHXQSHQWUXDIRUPDDSOLFD LDGRULW 3HQWUX FD vQ DFHDVW ID] V QX DSDU SUREOHPH PDMRUH WUHEXLH V H[LVWH XQ PHFDQLVPFHDVLJXUFDYDULDELOHOHGHFODUDWHGHXQSURJUDPDWRUVQXLQWUHvQFRQIOLFWFX YDULDELOHOHFX DFHODúLQXPHIRORVLWHGHXQ DOW SURJUDPDWRU GH H[ YDULDELOHOH GH FRQWRU DOHXQHLEXFOHLMN 3HGHDOWSDUWHHVWHQHFHVDUFDDQXPLWHGDWHVILHFRPXQHYL]LELOH úLVDXPRGLILFDELOHGHFWUHPDLPXOWHVXUVHFHSURYLQGHODDXWRULGLIHUL L'HFLWUHEXLHV H[LVWH XQ PHFDQLVP FDUH V DVLJXUH F QXPHOH YDULDELOHL GHFODUDWH vQ GLYHUVH ILúLHUH VH UHIHUODDFHOHDúLORFD LLPHPRULH ÌQ&VHGHILQHúWH : &DQGWUHEXLHVILHRYDULDELOFRPXQVDXvPSDU LWVKDUHG : ÌQ FHSDUWHGH FRGHVWH YDODELO DFHVW OXFUX SULQ DWULEXLUHD XQXL VFRS VFRSH XQHL GHFODUD LL ³6FRSH´ HVWH XQ WHUPHQ WHKQLF FDUH GHQRW UHJLXQHD WH[WXOXL VXUVHL & vQ FDUH QXPHOH GHFODUDW DOXQHLYDULDELOHHVWHDFWLYH[LVW $OW SURSULHWDWH D YDULDELOHORU HVWH GXUDWD FDUH VH UHIHU OD GHVFULH GXUDWD GH YLD vQ PHPRULHDXQHLYDULDELOH 9DULDELOHOH FXGXUDW IL[VXQW JDUDQWDWH FúL UH LQ GDWHOH FKLDU GXS FH úLDX vQGHSOLQLW VFRSXO ÌQVFKLPEvQFD]XOYDULDELOHORUFXGXUDWDXWRPDWDFHVWOXFUXQXPDLSRDWHILJDUDQWDW 'HILQL LH3ULQ6 FODV6 GH6 VWRFDUH6 storage FODVV 6 VH6 vQ HOHJH6 VFRSXO6 úL6 GXUDWD6 XQHL6 variabile. )LHXUPWRUXOVHJPHQWGHSURJUDP YRLGIYRLG ^LQWM VWDWLFLQWYHFW>@ ^` « ` 6H REVHUY F H[LVW GRX YDULDELOH M úL YHFW (OH DX XQ VFRS GH EORF GHRDUHFH VXQW GHFODUDWH vQ LQWHULRUXO DFHVWXLD 9DULDELOHOH FX VFRS GH EORF VXQW GH RELFHL QXPLWH YDULDELOHORFDOH,PSOLFLWYDULDELOHOHORFDOHDXGXUDWDDXWRPDWÌQFD]XOOXLMHOYDDYHD DWULEXLWRQRXDGUHVGHPHPRULHODILHFDUHH[HFX LH D UHVSHFWLYXOXL EORF ÌQ FD]XO OXL YHFWGDWRULWIRORVLULLFXYkQWXOXLFKHLHVWDWLFDWULEXLUHDXQHLDGUHVHGHPHPRULHVHIDFHR VLQJXUGDWúLVHSVWUHD]SHWRDWGXUDWDH[HFX LHLSURJUDPXOXL
74
Introducere in programare
'XUDWDIL[ID GHFHDDXWRPDW 'XSFXPRSUHFL]HD]úLQXPHOHRYDULDELOIL[HVWHXQDVWD LRQDUvQWLPSFH SHQWUXRYDULDELODXWRPDWLF]RQDGHVWRFDUHvQPHPRULHHVWHDOHDVDXWRPDWvQWLPSXO H[HFX LHLSURJUDPXOXL'HFLRYDULDELOIL[ DUH PHPRULH DORFDW PHPRULD OD vQFHSHUHD H[HFX LHLSURJUDPXOXLúLúLSVWUHD]DFHDVWDORFDUHSHWRDWGXUDWDH[HFX LHLDFHVWXLD 9DULDELODDXWRPDWLFVHUHIHU OD ]RQD GH PHPRULH DWDúDW HL GRDU DWkWD WLPS FkW FRGXOvQFDUHúLDUHVFRSXO HVWH H[HFXWDW 2 GDW vQFKHLDW VFRSXO ]RQD XQGH HUD IL[DW FRPSLODWRUXODVLJXUHOLEHUDUHD]RQHLGHPHPRULHRFXSDWGHDFHDVWD 9DULDELOHOH ORFDOH FX VFRSXO OLPLWDW OD QLYHO GH EORF VXQW LPSOLFLW DXWRPDWLFH (OHSRWILIRU DWHVOXFUH]HVWDWLFSULQIRORVLUHDFXYkQWXOXLFKHLHVWDWLF ,QL LDOL]DUHDYDULDELOHORU6 'LIHUHQ DGLQWUHYDULDELOHOHIL[HúLFHOHDXWRPDWHHVWHLPSRUWDQWvQVSHFLDOSHQWUX YDULDELOHOHQHLQL LDOL]DWHÌQFD]XOFHORUDXWRPDWHHOHYRUILUHLQL LDOL]DWHODILHFDUHWUHFHUH SULQ]RQDORUGHH[LVWHQ YRLGPFYRLG ^ LQWM VWDWLFLQWN MN SULQWI³M G?WN G?Q´MN ` YRLGPDLQYRLG ^ PF M N PF M N PF M N ` $OWGLIHUHQ DSDUHODLQL LDOL]DUHDLPSOLFLWDYDULDELOHORU : YDULDELOHOHDXWRPDWHQXVXQWLQL LDOL]DWH : YDULDELOHOHVWDWLFHVXQWLPSOLFLWLQL LDOL]DWHFX ([ YRLGPFYRLG YRLGPDLQYRLG ^ ^ LQWM PF VWDWLFLQWN PF MN PF SULQWI³M G?WN G?Q´MN ` ` &XUH]XOWDWXO M N M N M N $OW GLIHUHQ vQWUH YDULDELOHOH FX GXUDW IL[ úL FHOH DXWRPDWH VH UHIHU OD WLSXULOH GH H[SUHVLLFHSRWILIRORVLWHvQXWLOL]DUH
75
Introducere in programare
'HH[XUPWRDUHOHGHFODUD LLVXQWOHJDOH LQWM N LQWP MN IORDW[ vQWLPSFHXUPWRDUHOHQXGHRDUHFHMúLNDSDUvQH[SUHVLLvQDLQWHGHDILGHFODUDWH LQWP MN LQWM N 5HJXOLOH GH LQL LDOL]DUH D YDULDELOHORU FX GXUDW IL[ VXQW IRDUWH VWULFWH ,QL LDOL]DUHD VH SRDWHIDFHQXPDLFXRH[SUHVLHFRQVWDQWGHFL18SRDWHFRQ LQHQXPHGHYDULDELO )RORVLUHDYDULDELOHORUFXGXUDWIL[ 8QH[HPSOXFODVLFGHIRORVLUHDDFHVWRUDHVWHFRQWRUL]DUHDQXPUXOXLGHH[HFX LL DOHXQHLIXQF LLSHQWUXDLSXWHDPRGLILFDH[HFX LDODLQWHUYDOHUHJXODWH 'HH[VSUHVXSXQHPFDYHPXQSURJUDPFDUHIRUPDWHD]XQWH[WFLWLWGLQWUXQ ILúLHU úL VFULH UH]XOWDWXO vQ DOW ILúLHU 8QD GLQ IXQF LL HVWH HVWH SULQWBKHDGHU DSHODW OD vQFHSXWXO ILHFUXL SURJUDP QRX (YHQWXDO DFHDVWD SRDWH IL GLIHULW IXQF LH GH SDULWDWHDLPSDULWDWHDQXPUXOXLGHSDJLQL GHILQH2'' GHILQH(9(1 SULQWBKHDGHUFKDU FKRSBWLWOH ^ VWDWLFFKDUSDJHBW\SH 2'' LISDJHBW\SH 2'' ^ SULQWI³?W?W?W?WV?Q?Q´FKRSBWLWOH SDJHBW\SH (9(1 ` HOVH ^SULQWI³V?Q?Q´FKRSBWLWOH SDJHBW\SH 2'' ` ` “Scopul” unei variabile (Scope) ÌQFDGUXOSURLHFWULLXQXLSURJUDPWUHEXLH LQXWFRQWGHIDSWXOFH[LVWWLSXULGH UHJLXQLILHFDUH FXXQVFRSELQH GHILQLW vQ FDUH VH SRDWH DFFHVD R YDULDELO SULQ QXPH úL DQXPHSURJUDPIXQF LHEORFILúLHU
La nivel de program (program scope)vQVHDPQFYDULDELODHVWHDFWLYvQWUHGLIHULWHOH ILúLHUHVXUVFHFRUHVSXQGSURJUDPXOXLH[HFXWDELO9DULDELOHOHGHDFHVWWLSVXQWFXQRVFXWH GHRELFHLVXEQXPHOHGHYDULDELOHJOREDOH /DQLYHOGHILúLHUILOHscope)VHUHIHUODIDSWXOFUHVSHFWLYDYDULDELOHVWHDFWLYGHOD SXQFWXOGHGHFODUD LLSkQODVIkUúLWXOILúLHUXOXLVXUVFHRFRQ LQH /DQLYHOGHIXQF LHfunction scope) YDULDELODHVWHDFWLYGRDUvQFDGUXOIXQF LHL
76
Introducere in programare
/D6 QLYHO6 GH6 EORF6 GH6 LQVWUXF LXQL6 block scope) YDULDELOD H DFWLY GH OD SXQFWXO GH GHFODUD LHSkQODGHOLPLWDWRUXOGHVIkUúLWGHEORF.
ÌQJHQHUDOVFRSXOXQHLYDULDELOHHVWHGHWHUPLQDWGHORFXOvQFDUHVHUHDOL]HD]GHFODUDUHD DFHVWHLD
3URJUDP 6FRSH )LOH 6FRSH )XQFWLRQ 6FRSH
([ LQW, 3URJUDP6FRSH VWDWLFLQWM )LOH6FRSH IXQFLQWN 3URJUDP6FRSH ^LQWP %ORFN6FRSH VWDUW )XQFWLRQ6FRSH «
%ORFN 6FRSH
$FHDVWLHUDUKL]DUHSHUPLWHFDvQGRXIXQF LLGLIHULWHVDYHPYDULDELOHFXDFHODúLQXPH VDXGHH[ LQWM PDLQ ^ LQWM IRUM MM SULQWI³M G?Q´M OXFUHD]QRUPDOGDUGSGYDODOWXL ` ILúLHUVXUVM 10.5. Variabile globale (program sau file scope) 3kQ DFXP VD GLVFXWDW QXPDL GHVSUH YDULDELOH ORFDOH ([LVW WRWXúL FD]XUL FkQG VH GRUHúWH FD R YDORDUH GHSR]LWDW vQWUR YDULDELO V ILH YL]LELO VDX PRGLILFDELO GLQ LQWHULRUXO RULFUHL IXQF LL 3HQWUX DFHDVWD HVWH VXILFLHQW FD UHVSHFWLYHOH YDULDELOH V ILH GHFODUDWHvQDIDUDFRUSXOXLRULFUHLIXQF LLúLHOHYRUFSWDDVWIHOXQFDUDFWHUJOREDO$FHVW WLSGHYDULDELOHVXQWDXWRPDWLQL LDOL]DWHFXYDORDUHD]HUR ÌQ JHQHUDO vQ SURJUDPDUHD VWUXFWXUDW VH UHFRPDQG HYLWDUHD H[FHVXOXL GH YDULDELOH JOREDOHvQWUXFkWQXVXQWQHDSUDWQHFHVDUHGHFkWHYHQWXDOFDVVFULHPPDLUHSHGHúLXúRU XQ FRG GDU DFHVW FRG QX HVWH QHDSUDW FHO PDL EXQ VDX FHO PDL VWDELO 0DL MRV YRP SUH]HQWDXQH[HPSOXVLPSOXUHODWLYODIRORVLUHDYDULDELOHORUJOREDOH
77
LQFOXGHVWGLRK! LQFOXGHFRQLRK!
Introducere in programare
LQWJOREDOBY LQWRULBGRLLQW[ YRLGPDLQYRLG ^ LQWORFDOBY FOUVFU SULQWI³9DULDELODJOREDODDUHYDORDUHDG?Q´JOREDOBY SULQWI³9DULDELODORFDODDUHYDORDUHDG?Q´JOREDOBY ORFDOBY RULBGRLORFDOBY SULQWI³9DULDELODJOREDODDUHYDORDUHDG?Q´JOREDOBY SULQWI³9DULDELODORFDODDUHYDORDUHDG?Q´JOREDOBY ` LQWRULBGRLLQW[ ^ LQWUHWYDO UHWYDO [ JOREDOBY UHWXUQUHWYDO ` Tematica claselor de stocare ANSI ORFXO XQGH
VH GHFODU
ÌQ DIDUD
vQ LQWHULRUXO
DUJXPHQWH
IXQF LHL
IXQF LHL
IXQF LH
6FRSEORFN
6FRS EORF
6SHFLILFDWRU
GH FODV DXWR
1(3(50,6
VDX UHJLVWHU 6WDWLF
6FRSILúLHU
([WHUQ
'XUDWD DXWRPDWLF
'XUDWDDXWRPDWLF
6FRS EORF
1(3(50,6
'XUDWDIL[
'XUDWD IL[
6FRS SURJUDP
6FRS EORF
1(3(50,6
'XUDWD IL[
'XUDWD IL[
1LFL XQ
6FRS SURJUDP
6FRS EORF
VSHFLILFDWRU
'XUDWD IL[
6FRS EORF
'XUDWD GLQDPLF
'XUDWD DXWRPDWLF
SUH]HQW
10.6. Macrouri ÌQPRGQRUPDOFkQGRIXQF LHHVWH DSHODW VH FUHHD] VHWXO GH YDULDELOH ORFDOH DSRL DFHVWHD VH LQL LDOL]HD] $FHVW OXFUX GXUHD] vQ XQHOH FD]XUL PDL PXOW GHFkW H[HFX LD FRUSXOXLIXQF LHL$SDUHvQWUHEDUHDFXPSRDWHHYLWDSLHUGHUHDGHYLWH]úLWRWXúLVUH]XOWH XQSURJUDPVWUXFWXUDW
78
Introducere in programare
/LPEDMXO & HVWH SUHY]XW FX PDFURXUL PDFURGHILQL LL FDUH SHUPLW LPSOHPHQWDUHD UDSLGGHRSHUD LLVLPSOHIUGH]DYDQWDMHOHGHWLPSGLQFD]XOIXQF LLORU 'DF VH GRUHúWH GHFODUDUHD XQHL YDORUL FRQVWDQWH VH SRDWH IRORVL GLUHFWLYD SUHSURFHVRUGHILQHFDPDLMRV GHILQHSL
/DvQWkOQLUHDUHVSHFWLYHLGLUHFWLYHSUHSURFHVRUXOYDvQORFXLILHFDUHPHQ LRQDUHvQFRGD OXLSLFXvQVúLYDORDUHD6LPLODUVHYDvQWkPSODúLFXFRUSXOXQXLPDFUR 2EVHUYD LH R YDULDELO SRDWH IL GHFODUDW IL GHFODUDW FD ILLQG VWDWLF DGLF QXúL YD PRGLILFD YDORDUHD SH SDUFXUVXO H[HFX LH FX DMXWRUXO FXYkQWXOXL FKHLH VWDWLF SXV vQDLQWHD VSHFLILFDWRUXOXLGHWLS )LHH[HPSOHOHGHPDLMRVXQGHVXQWSUH]HQWDWHGRXPDFURXULXQXOFDOFXOHD][LDU DOWXOUHDOL]HD]PD[LPXPGLQWUHGRXQXPHUH GHILQHP\BPD[[\ [ !\ "[ \ GHILQHP\BVTU[ [ [ 'DFVHYRUIRORVLDFHVWHPDFURXULvQORFXODSHOXOXLXQHLIXQF LLVHYDLQVHUDGLUHFWFRGXO FRUHVSXQ]WRULPSOHPHQWULLGHILQL LLORUGLQGUHDSWD 7RWXúL IRORVLUHD PDFURXULORU DVFXQGH XQ PDUH SHULFRO SHQWUX FLQHYD FDUH QX DUH VXILFLHQWH[SHULHQ 'DFvQFD]XOXQHLIXQF LLVHUHDOL]HD]vQWkLHYDOXDUHDSDUDPHWULORU OD DSHOXO XQXL PDFUR DFHVW OXFUX QX VH PDL IDFH $SDU SUREOHPH GH RUGLQH D H[HFX LHL RSHUD LLORUFDvQH[HPSOHOHGHPDLMRV UH] P\BVTU[ HYDOXDUHDHVWHUH] [ [vQORFGH [ [ VDX UH] ]P\BVTU[ HYDOXDUHDHVWHUH] ][ [vQORFGH][ [ 'LQDFHVWPRWLYODFRPSLODWRDUHOHGH223VDLQWURGXVQR LXQHDGH IXQF LH³LQOLQH´ FDUHHOLPLQGH]DYDQWDMHOHPDFURXULORUGDUVHFRPSRUWFDDFHVWHD 7UHEXLHPHQ LRQDWFODGHSDQDUHDSDVFXSDVVHSRDWHLQWUDvQLQWHULRUXOXQXLDSHOGH PDFURFHHDFHIDFHFDHURULOHGHWLSXOSUH]HQWDWPDLVXVVILHJUHXGHSLVWDELOH 10.7 Macrouri predefinite 10.7.1. ASSERT )RORVLUHDDFHVWHLPDFURGHILQL LLHVWHLQGLFDWGLQXUPWRDUHOHPRWLYH
FUHúWHIRDUWHPXOWYLWH]DGHGHSDQDUHDXQXLSURJUDP SURJUDPDUHDvQ&ODRUDDFWXDOHVWHUHDOL]DWVXEFRPSLODWRDUHGH&FDUHDFFHSWúL FRG&FODVLFFRPSLODWRDUHFDUHRDXSUHGHILQLW
79
Introducere in programare
(VWHXQPDFURFDUHIXQF LRQHD]H[DFWFDRLQVWUXF LXQHLIGDUVSUHGHRVHELUHDGH DFHDVWD vQ PRPHQWXO vQ FDUH FRQGL LD GH HYDOXDW LD YDORDUHD ]HUR EORFKHD] H[HFX LD SURJUDPXOXL DSHOkQG IXQF LD DERUW /D VWRSDUHD H[HFX LHL VH WULPLWH FWUH GLVSR]LWLYXO VWDQGDUG GH LHúLUH XQ PHVDM VSHFLILF FDUH SUHFL]HD] QXPHOH ILúLHUXOXL VXUV úL OLQLD GLQ FDGUXODFHVWXLDFDUHDJHQHUDWHURDUHD 'DF YDORDUHD HYDOXDW HVWH GLIHULW GH ]HUR DVVHUW QX DUH QLFL R LQIOXHQ DVXSUD H[HFX LHLSURJUDPXOXL3URWRWLSXOHVWHGHILQLWvQELEOLRWHFD$66(57+úLDUDWFDPDLMRV YRLGDVVHUWLQWWHVW
'HRELFHLHVWH IRORVLWSHQWUXDWHVWDGDFRvQFHUFDUHGHDORFDUHGLQDPLF DXQHLQRL ]RQH GH PHPRULH D UHXúLW VDX QX 'XS FXP DP VSXV HO HVWH IRORVLW vQ FDGUXO GHSDQULL XQHL DSOLFD LL 2 GDW WHUPLQDW DFHDVW ID] VH SRDWH IDFH FD SUHSURFHVRUXO & V LJQRUH OLQLLOH GLQ FRGXO VXUV FH FRQ LQ DSHOXUL GH DFHVW WLS SULQ LQFOXGHUHD vQ FRGXO VXUV D GLUHFWLYHL GHILQH1'(%8* vQDLQWHD GLUHFWLYHL FH LQFOXGH ELEOLRWHFD $66(57+ 8Q H[HPSOX VFXUW GH IRORVLUH HVWH SUH]HQWDWPDLMRV DLFLVHYDLQFOXGHGHILQH1'(%8* LQFOXGHDVVHUWK! LQFOXGHDOORFK! YRLGPDLQYRLG ^ FKDU V V FKDU PDOORF VL]HRIFKDU DVVHUWV V 18// DVVHUWV ` /D H[HFX LD DFHVWXL SURJUDP VH YD RE LQH DFWLYDUHD DVVHUW GHDELD vQ FHD GHD GRXD VLWXD LH 10.7.2. Macrouri specializate în clasificarea caracterelor. $XIRVWLQWURGXVHSHQWUXDXúXUDJkQGLUHDúLVFULHUHDFRGXOXLvQFD]XOvQ FDUHVHIDFSUHOXFUUL SH úLUXUL GH FDUDFWHUH $FHVWH SUHOXFUUL VXQW GHVHRUL vQWkOQLWH 6D SUHIHUDW LPSOHPHQWDUHD FD PDFURXUL SHQWUX D RE LQH R YLWH] PD[LP GH H[HFX LH 9RP SUH]HQWD PDL MRV FHOH PDL IRORVLWH PDFURXUL SXVH OD GLVSR]L LH GH ELEOLRWHFD &7<3(+ 7RDWH DFHVWH PDFURXUL UHWXUQHD] R YDORDUH GLIHULW GH ]HUR vQ FD]XO vQ FDUH VLUXO GH FDUDFWHUHHYDOXDWDUHUHVSHFWLYDFDUDFWHULVWLFWHVWDWGHPDFURXVDX]HURvQFD]FRQWUDU
80
LQWLVORZHULQWF
Introducere in programare
7HVWHD]GDFFDUDFWHUXOFHVWHGLQFDWHJRULDOLWHUHORUPLFLF∈>D«]@ LQWLVDOSKDLQWF 7HVWHD]GDFFDUDFWHUXOFHVWHOLWHUF∈>D«]@VDXF∈>$«=@ LQWLVXSSHULQWF 7HVWHD]GDFFDUDFWHUXOFHVWHOLWHUPDUHF∈>$«=@ LQWLVSULQWLQWF 7HVWHD]GDFFDUDFWHUXOFHVWHWLSULELOF∈>[«[H@ LQWLVDVFLLLQWF 7HVWHD]GDFFDUDFWHUXORFWHWXOORZDOOXLFH[WHvQLQWHUYDOXO>«@ LQWLVSXQFWLQWF 7HVWHD]GDFFDUDFWHUXOFHVWHXQFDUDFWHUGHSXQFWXD LH LQWLVVSDFHLQWF 7HVWHD]GDFFDUDFWHUXOFHVWHVSD LXWDE&5VDOWODOLQLHQRXWDEYHUWLFDOVDX IRUPIHHGDGLFF∈>[«[G[@ LQWLVGLJLWLQWF 7HVWHD]GDFFDUDFWHUXOFHVWHFLIUF∈>«@ LQWLVJUDSKLQWF 6LPLODUFXLVSULQWQXPDLFVHHOLPLQVSD LLOH LQWLV[GLJLWLQWF 7HVWHD]GDFFDUDFWHUXOFDSDU LQHED]HLF∈>«@VDXF∈>D«I@VDXF∈>$«)@ 10.7. 3. 0DFURGHILQL LLpredefinite 8QXO GLQ XUPWRDUHOH VLPEROXUL HVWH SUHGHILQLW vQ IXQF LH GH PRGHOXO GH PHPRULH DOHVSHQWUXFRPSLODUH BB&203$&7BBBB0(',80BBBB+8*(BB 81
Introducere in programare
BB/$5*(BBBB60$//BBBB7,1
BBFSOXVSOXVGHILQLWGDFVHFRPSLOHD]XQSURJUDP& BB'$7(BBGDWDFRPSLOULLFDPPPGG\\\\ BB),/(BBQXPHOHILúLHUXOXLVXUVFXUHQWDIHFWDWGHOLQH FDXQúLUGHFDUDFWHUH BB/,1(BBQXPUXOOLQLHLFXUHQWHDIHFWDWGHOLQH FDXQQXPUvQWUHJ BB06'26BBvQWRWGHDXQDGHILQLW BB29(5/$
82
CAPITOLUL 11
Introducere in programare
11. 3RLQWHULúLDORFDUHDGLQDPLF 11.1. Introducere. Primele PLQLVLVWHPH FX PLFURSURFHVRDUH SH EL L DYHDX ko memorie RAM. $FHDVW GLPHQVLXQH HUD DOHDV GLQ FDX]D XQHL OLPLWUL GDWRUDWH QXPUXOXL Q GH EL L necesari pentru construirea adresei 2 = 2 * 2 = 2 ko = 64 NRGHFLEL LGHDGUHV ÌQWUXFkWSURFHVRUXOOXFUDSHEL LSHQWUXDDYHDDFFHVODRGDWRDUHFDUHGLQPHPRULHHO WUHEXLD V IDF XQD VDX GRX RSHUD LL GHSLQ]kQG GH FRQVWUXF LD LQWHUQ 6LVWHPHOH FX SURFHVRDUH SH EL L 9 DYHDX XQ QRX VHW GH LQVWUXF LXQL HUDX PDL UDSLGHúLDFFHVDXPDLPXOWPHPRULH5$0 3UREOHPDHUDFGHMDVHFKHOWXLVHIRDUWHPXOWWLPSúLEDQLSHQWUXDGH]YROWDSURJUDPH SHQWUX VLVWHPHOH DQWHULRDUH 0XOWH DSOLFD LL PDL LPSRUWDQWH WUHEXLDX UHVFULVH FX XQ SUH PLQLPÌQWRDWHSURJUDPHOHVH LQXVHFRQWvQOXFUXOFXGDWHOHGLQPHPRULHGHOLPLWDUHDGH 64 ko. &HOPDLPLFHIRUWGHSURJUDPDUHVHIFHDGDFVHSURLHFWDXQVLVWHPGHRSHUDUHFDUH V ³YDG ³ PHPRULD 5$0 FD ILLQG DOFWXLW GLQ EXF L VHJPHQWH GH FkWH ko. $FHDVWDDIRVWLGHDGHED]DVLVWHPXOXLGHRSHUDUH06'26XQGHDGUHVDGHPHPRULHD RULFUHLGDWHHVWHFRPSXVGLQGRXSU L -
DGUHVDGHVHJPHQWDUDWDFkWDORFD LHGLQFHOH ORFD LLDOHXQXLVHJPHQWHVWHGDWD XWLO adresa de RIIVHW DUDW vQ DO FkWHOHD VHJPHQW GLQ FHOH Q segmente din memorie, ne DIOPXQGH Q=
GLP
PHP5$0 [NR] . [NR ]
ÌQ SURJUDPDUHD GH QLYHO vQDOW OD DFHD GDW YH]L & 3DVFDO %DVLF &RERO úDPG DFWXDO QXPLWH OLPEDMH GH QLYHO PHGLX VD LQWURGXV QR LXQHD GH pointer. Aceasta este o YDULDELO FDUH vQ ORF V UH LQ R GDW XWLO GLUHFW vQ FDOFXOH UH LQH DGUHVD vQ PHPRULH D unei date utile. 1R LXQHDGHpointer 8QDGLQFHOHPDL³SXWHUQLFH´PRGDOLW LGHDFFHVODGDWHVHED]HD]SHSRVLELOLWDWHDGH a avea acces direct la adresa de memorie a unei date stocate acolo. 2YDULDELOFDUHVWRFKHD]DGUHVDDOWHLYDULDELOHVHQXPHúWHpointer. $YDQWDMXOIRORVLULLGHFWUHSURJUDPDWRUDXQXLDVHPHQHDFRQFHSWHVWHDFHODFvQ FD]XOPDQLSXOULLXQRUPDVLYHGHGDWHGHPDULFDUHODUkQGXOORUFRQ LQHOHPHQWHGHPDUL GLPHQVLXQL YH]L FD]XO XQHL VWUXFWXUL QX VH YD PDL WUDQVIHUD WRDW ]RQD GH PHPRULH RFXSDWGHRGDWFLQXPDLFXDGUHVDHLGHvQFHSXW$FHVWHDVHYRUREVHUYDúLvQ HOHJHPDL clar din exemple.
83
Introducere in programare
Pentru a vedea mai clar folosirea conceptului de SRLQWHUVFRQVLGHUPSURJUDPXO GHPDLMRV$FHVWSURJUDPGHFODUXQYHFWRUGHHOHPHQWHvQWUHJLGHFLUHSUH]HQWDWHSH RFWH LILHFDUH SUHFXPúLXQSRLQWHUODXQvQWUHJ$FHDVWDvQVHDPQFFRPSLODWRUXOYDúWL vQFD]XOPDQLSXOULORUPDWHPDWLFHFXDGUHVDFDUHRFRQ LQHVIDFFDOFXOHOHFRUHFWGXS cum vom vedea mai jos. Geneza operatorului &op Se traduce prin “adresa lui” aplicat XQXL RSHUDWRU RS HVWH R PRúWHQLUH GLQ YUHPHD DECPDP11 care folosea acest sistem. (VWHQHSOFXWFWHUPLQRORJLDV-a pVWUDWGHRDUHFH DPELJXLWDWHDDSDUHvQIDSWXO F QX úWLP FRQFUHW GHVSUH FH DGUHV HVWH YRUED /D FH QH UHIHULP: cea ce poate reprezenta o DGUHVDUHSHUIHFWQRUPDOSHQWUXSURFHVRUXO;SRDWHVILHLPSRVLELO pentru controllerul SURGXVGH<SHQWUXRPDúLQGHVSODWFDUHIRORVHúWHDGUHVDUHDGH17 EL L . Deci problema SRUWDELOLW LL DEVROXWH V-a SVWUDW SHQWUX F D LQYHQWD XQ QRX WHUPHQ DU IL EXOYHUVDW PDL PXOWVLWXD LD Un SRLQWHUHVWHIRORVLWRUGDFHVWHRFDOHGHDFFHVODYDORDUHDDFUHLDGUHVDRUH LQH /LPEDMXO&IRORVHúWHSHQWUXDFHDVWDRSHUDWRUXO*.
vQFHSH]RQDLQFOXGHULGHELOLRWHFL
LQFOXGHVWGLRK! LQFOXGHFRQLRK!
VIkUúLW]RQDLQFOXGHULGHELOLRWHFL
QXDPIXQF LLSURSULL QXDPYDULDELOHJOREDOH
LQFHSH]RQDSURJUDPXOXLSULQFLSDO
YRLGPDLQYRLG ^ LQW SSRLQWHUODRYDULDELOGHWLSvQWUHJ LQWY>@WDEORXGHHOHPHQWHGHWLSvQWUHJ LQWQLYDULDELOHQHFHVDUHSUHOXFUULLPDVLYXOXLGHGDWH FOUVFU úWHUJHFUDQ SULQWIGDWLGLPHQVLXQHDYHFWRUXOXL1 VFDQIG Q FLWHVFGLPHQVLXQHDYHFWRUXOXL IRUL LQL ^ SULQWI?Q9>G@ L VFDQIG Y>L@ `FLWHVFYHFWRUXO S YGHSXQDGUHVDGHvQFHSXWDPDVLYXOXLYvQYDULDELODS IRUL LQL ^ SULQWI?Q&RQWLQXWXOOXL YG GL YL SULQWI?Q$GUHVDOXL9>G@HVWHSLYL ` IRUL LQL ^
84
Introducere in programare
SULQWI?Q&RQWLQXWXOOXL SG GL YL SULQWI?Q$GUHVDOXLSGHVWHSLSL ` JHWFK `
VIkUúLW]RQDSURJUDPSULQFLSDO
QHDYkQGIXQF LLSURSULLQXDPDLFLQLPLF
,HúLUHD DFHVWXLSURJUDPvQFD]XOH[HFX LHL SHQWUX XQ YHFWRU GH WUHL HOHPHQWH DUDW FD mai jos: GDWLGLPHQVLXQHD1 9>@ 9>@ 9>@ &RQWLQXWXOOXLY $GUHVDOXL9>@HVWH&()' &RQWLQXWXOOXL Y $GUHVDOXL9>@HVWH&()' &RQWLQXWXOOXL Y $GUHVDOXL9>@HVWH&()' &RQWLQXWXOOXL S $GUHVDOXLSHVWH&()' &RQWLQXWXOOXL S $GUHVDOXLSHVWH&()' &RQWLQXWXOOXL S $GUHVDOXLSHVWH&()' 2SHUD LLDULWPHWLFHFXpointeri 2SHUD LLOHDULWPHWLFHSHUPLVHVXQW -
adunarea unui pointer cu un întreg VFGHUHDXQXLvQWUHJGLQWU-un pointer VFGHUHDDGRLpointeri compararea unui SRLQWHUFXFRQVWDQW18//YDORDUHDvQWUHDJD compararea a doi pointeri.
6LQJXUDRSHUD LHDULWPHWLFSHUPLVDVXSUDSRLQWHULORUFWUHIXQF LLHVWHFRPSDUDUHDFX constanta NULL.
85
Introducere in programare
Adunarea unui pointer cu un întreg are ca rezultat un SRLQWHUGHDFHODúLWLSÌQFD]XO în care SRLQWHUXODUHFDYDORDUHDGUHVDXQXLHOHPHQWGHWDEORXUH]XOWDWXODGXQULLHVWHDGUHVD HOHPHQWXOXL DO FUXLLQGLFH HVWH HJDO FXVXPDGLQWUH LQGLFHOH HOHPHQWXOXL FWUH FDUH LQGLF pointerul, plus valoarea întregului. 'LQ PRWLYH OHJDWH GH PRGXO GH JHQHUDUH D DGUHVHORU IL]LFH GH FWUH SURFHVRU UH]XOWDWXOQXHVWHYDOLGGHFkWGDFLQGLFHOHUH]XOWDWGLQFDOFXOHVWHPDLPDUHVDXHJDOFX]HUR úLPDLPLFVDXHJDOFXQXPUXOGHHOHPHQWHGLQWDEORX
6FGHUHD6unui întreg dintr-un SRLQWHU 6H IDFH FD úL FXP V-ar aduna la pointer YDORDUHDvQWUHDJUHVSHFWLYFXVHPQVFKLPEDW 6FGHUHD a doi pointeri. Cei doi SRLQWHULWUHEXLH VLQGLFH FWUH GRX HOHPHQWH GLQ DFHODúLWDEORXUH]XOWDWXOHVWHXQvQWUHJHJDOFXGLIHUHQ DLQGLFLORUFHORUGRXDHOHPHQWH Compararea a doi pointeri. Cei doi SRLQWHULWUHEXLH VLQGLFH FWUH GRXD HOHPHQWH GLQDFHODúLWDEORXUH]XOWDWXOFRPSDUD LHLHVWHHJDOFXUH]XOWDWXOFRPSDUD LHLLQGLFLORUFHORU doua elemente de tablou. 9DORULOH DSDU LQkQGXQXL WLS SRLQWHU SRW IL FRQYHUWLWH FWUH RULFH DOW WLS pointer prin folosirea unei conversii explicite. 11.1.3 Modele de memorie Structura procesoarelor 80x86 a determinat maniere diferite de generare a codului de FWUHFRPSLODWRDUHLQIXQF LHGHPRGXULOHGHDGUHVDUHXWLOL]DWH SHQWUXDFFHVXOODGDWHVDXGHPRGXOGHUHDOL]DUHDDSHOXULORUGHIXQF LL 'DFSURJUDPDWRUXOFXQRDúWHFRPSOH[LWDWHDaplicatiei si volumul de date manipulat, el poate impune o anumita modalitate de generare a codului, astfel incit el sa fie optim. $úDDDSUXWQR LXQHDGHPRGHOGHPHPRULHVSHFLILFPDMRULWDULLFRPSLODWRDUHORUGHSHR PDúLQFRPSDWLELO,%0-PC. Pentru manipularea datelor se folosesc, adesea, SRLQWHUL ÌQ IXQF LH GH PRGXULOH GH adresare utilizate, ei pot avea atributele: - near pointerSHEL LUHSUH]HQWkQGGHIDSWXQoffset in interiorul unui segment; - far pointerSHEL LUHSUH]HQWkQGRFRPELQD LHVHJPHQWRIIVHWDGLFRDGUHVUHDO FRPSOHW XQpointer "far" poate avea orice valoare (in limita a 1 MB de memorie), dar în calcule, segmentul nu poate fi modificat; - huge pointer este similar pointerului "far", dar în calcule segmentul poate fi modificat, astfel LQFkW V SRDW IL DGUHVDW WRDW PHPRULD IU VFKLPEDUHD H[SOLFLW D VHJPHQWXOXL $FHDVW FDUDFWHULVWLF HVWH GDW GH RSHUD LD GH QRUPDOL]DUH OD FDUH VXQW VXSXúL pointerii "KXJHvQRULFHRSHUD LLGHFDOFXO 2EVHUYD LH ÌQFOFDUHD UHJXOLORU GH PDL VXV QX HVWH VHPQDODW vQWRWGHDXQD SURPSW GH compilator. Rezultatele pot fi incerte. Modelele de memorie, utilizate de Turbo C, sunt: 7,1<60$//0(',80&203$&7/$5*(úL+8*('LIHUHQ LHUHDORUHVWHGDW GHPRGXOGHXWLOL]DUHD]RQHORUGHFRGGDWHVLVWLYDSUHFXPúLGHDWULEXWHOHLPSOLFLWHDOH SRLQWHULORUXWLOL]D LvQPRGHOXOUHVSHFWLY9RPIDFHGLVWLQF LHvQWUH]RQHOHGHGDWHVWDWLFHúL zonele de date alocate dinamic (KHDS &DUHJXOLJHQHUDOHSHQWUXDORFDUHDGDWHORUVH LQH FRQWGHXUPWRDUHOH
86
Introducere in programare
GDWHOHVWDWLFHVHDORFvQVHJPHQWXOGHGDWH'6 úLVXQWOLPLWDWHODFHOPXOWkB; GDWHOHDORFDWHGLQDPLFVHSODVHD]ILHvQVHJPHQWXOGHGDWHODPRGHOHOH7,1<60$// MEDIUM), fie in "heap" (la modelele COMPACT, LARGE, HUGE); GDWHOHDXWRVHDORFvQVHJPHQWXOGHVWLY66 úLVXQWOLPLWDWHODFHOPXOWkB. $ORFDUHGLQDPLF6 11.2.1. Introducere 3kQ DFXP VD GLVFXWDW GHVSUH FHHD FH VH QXPHúWH DORFDUH VWDWLF $FHVW WHUPHQ VH UHIHU OD IDSWXO F R GDW DORFDW R YDULDELO vQ SURJUDP PHPRULD FRUHVSXQ]WRDUH HL numai poate fi UHIRORVLW GDF OD XQ PRPHQW GDW QX DU IL QHYRLH GH HD SH SDUFXUVXO H[HFX LHLSURJUDPXOXL'HRDUHFHIRDUWHPXOWHRULPHPRULD5$0HUDH[WUHPGHVFXPS LDUQHFHVLW LOHGHPHPRULHIRDUWHPDULVDvQFHUFDWUH]ROYDUHDDFHVWXLFRQIOLFW 3ULPD PHWRG HUD FD DWXQFL FkQG QX DMXQJHD PHPRULD 5$0 VH IRORVHD R XQLWDWH H[WHUQ GH VWRFDUH FX SRVLELOLW L GH VFULHUH VDX FLWLUH $FHDVW PHWRG HVWH úL DFXP IRORVLW6LQJXUXOGH]DYDQWDMDODFHVWHLPHWRGHHVWHIDSWXOFRSHUD LLOHFXGLVFXOVXQWPXOW mai lente decât cu memoria RAM. De aceea s-a pus problema folosirii cât mai optime a acestei memorii. În acest scop SURLHFWDQ LL XQXL VLVWHP GH RSHUDUH DX SUHY]XW vQ FDGUXO DFHVWHL IXQF LL VSHFLILFH GH gestionare a memoriei. 3ULQWUHIDFLOLW LOHGHED]DOHDFHVWRUIXQF LLVXQW -
alocare de memorie; dealocare de memorie; YHULILFDUHDVSD LXOXLGHPHPRULHUPDVnealocat .
Alocarea memoriei 3ULQ WHUPHQXO GH DORFDUH VH vQ HOHJH UH]HUYDUHD XQHL ]RQH GH PHPRULHGHRDQXPLWGLPHQVLXQHDFHDVW]RQGHPHPRULH³GLVSDUH´SUDFWLFGLQ]RQD GH PHPRULH vQ FDUH VLVWHPXO FLWHúWH VDX VFULH IU UHVWULF LL /D DORFDUH VH UHWXUQHD] adresa de început a acestei zone. Dezalocarea memoriei. Prin termenul de GH]DORFDUH VH vQ HOHJH IHQRPHQXO LQYHUV DORFULL6HWULPLWHFWUHVLVWHPDGUHVDGHvQFHSXWDXQXLEORFDORFDWUH]HUYDW úLLVHFHUH HOLEHUDUHD OXL DGLF UHLQWHJUDUHD vQ VSD LXO GH PHPRULH FH SRDWH IL DFFHVDW GH RULFH DOW program. 9HULILFDUHDVSD LXOXLGHPHPRULHnealocat'XSXQQXPURDUHFDUHGHDORFULIUD PDL IDFH úL GH]DORFUL VSD LXO UPDV OLEHU WLQGH FWUH ]HUR )XQF LLOH GH DORFDUH vQ PRPHQWXO vQ FDUH OL VH FHUH V DORFH R DQXPLW ]RQ QRFWH L úL QX PDL DX VXILFLHQW VSD LX OLEHU UHWXUQHD] 18// SHQWUX D VHPQDOL]D DFHDVW LPSRVLELOLWDWH 7RWXúL DFHDVWD UHSUH]LQWRVLWXD LHFULWLFúLQXVHPDLSRDWHIDFHPDUHOXFUX'HDFHHD FDWHKQLFGH SURJUDPDUH VH SRDWH IDFH GLQ FkQG vQ FkQG vQ SXQFWHOH FKHLH YHULILFDUHD VSD LXOXL GH PHPRULHUPDVOLEHUSHQWUXDFDOFXODDXWRPDWGDFSURJUDPXOSRDWHFRQWLQXDVDXQX
87
Introducere in programare
(URDUHGHJkQGLUHDvQFHSWRUXOXLUHODWLYODRSHUDWRUXOsizeof $FHVW RSHUDWRU úWLH V vQWRDUF QXPUXO GH RFWH L SH FDUH HVWH VWRFDW XQ WLS GH GDW DEFINITILHLPSOLFLWWLSXULOHGHGDWLPSOLFLWHchar , int , long, double) fie cele definite de utilizator (structurile). Dar, NU POATEFDOFXODGLPHQVLXQHDXQHL]RQHGHVSUHFDUHQXDUHLQIRUPD LLFODUHvQ PRPHQWXOFRPSLOULL Exemplu clasic : char S[10]; ....... gets(S); n=sizeof(S) // eroare // S poate avea fie 255 caractere (caz vQ FDUH VH SVWUHD] sigur numai // primele 10, fie nici un caracter // deci nu poate fi aplicat. 'HDFHHDVDLQWURGXVIXQF LDstrlen(S) în acest caz. O versiune de strlen(S) ar fi nr_cars=0; while ( *S[i++]!=’\0’) nr_cars++; return nr_cars; 0DLMRVSUH]HQWPRDOWFODVLILFDUHDWLSXULORUGHGDWHù
$ORFDUHDGLQDPLF 2ULFHWLSGHGDWpredefinit sau definit de utilizator poate fi alocat static VDX GLQDPLF )XQF LD GH ED] vQ OLPEDMXO & IRORVLW SHQWUX DORFDUHD GLQDPLF DUH XUPWRUXOSURWRWLS
88
Introducere in programare
YRLG PDOORFVL]HBWVL]H YRLGIDU IDUPDOORFXQVLJQHGORQJQE\WHV
$ORFDUHD GLQDPLF D XQXL EORF GH PHPRULH GH OXQJLPH GDW size, sau nbytes). "malloc" permite alocarea din segmentul de date a cel mult 64 kB, iar "farmalloc" permite alocarea din "heap" a cel mult 1 MB (de fapt in limitele memoriei ramase GLVSRQLELOH )XQF LLOHUHWXUQHD]SRLQWHU18//GDFQXPDL H[LVWVXILFLHQWPHPRULH pentru alocare. YRLG FDOORFVL]HBWQHOHPVL]HBWHOVL]H YRLGIDU IDUFDOORFXQVLJQHGORQJQXQLWVXQVLJQHGORQJXQLWV] $ORFDUHDGLQDPLFDXQXLEORFGHPHPRULHSHQWUXXQQXPUGDWGHHOHPHQWHnelem, QXQLWV GH GLPHQVLXQH FXQRVFXW elsize, XQLWV] 'LPHQVLXQHD WRWDO DORFDW HVWH produsul celor doi parametri. "malloc" permite alocarea din segmentul de date a cel mult 64 kB, iar "farmalloc" permite alocarea din "heap" a cel mult 1 MB (de fapt în limitele PHPRULHL UPDVH GLVSRQLELOH )XQF LLOH returneaza SRLQWHU 18// GDF QX PDL H[LVW VXILFLHQWPHPRULHSHQWUXDORFDUH YRLG UHDOORFYRLG EORFNVL]HBWVL]H YRLGIDU IDUUHDOORFYRLGIDU ROGEORFNXQVLJQHGORQJQE\WHV Modificarea dimensiunii unui bloc de memorie deja alocat. Pointerul block (ROGEORFN WUHEXLH V IL IRVW RE LQXW FX R IXQF LH GH DORFDUH DSHODW DQWHULRU malloc" ("farmalloc"), "calloc" ("IDUFDOORF 'DF UHGLPHQVLRQDUHD QX VH SRDWH IDFH IXQF LLOH UHWXUQHD]18//(VWHSRVLELOFDQRXOSRLQWHUUH]XOWDWVILHGLIHULWGHSRLQWHUXOLQL LDO YRLGIUHHYRLG EORFN YRLGIDUIUHHYRLGIDU EORFN (OLEHUHD]XQEORFGHPHPRULHGHMDDORFDWPointerul EORFNWUHEXLHVILIRVWRE LQXW FX R IXQF LH GH DORFDUH DSHODW DQWHULRU malloc" ("farmalloc"), "calloc" ("farcalloc"). ÌQFHUFDUHDGHDDSHODXQDGLQIXQF LLOHGHPDLVXVFXXQSRLQWHURE LQXWSHDOWHFLSRDWH duce la rezultate imprevizibile. ÌQ SURJUDPHOH FH XWLOL]HD] DORFDUH GLQDPLF VH UHFRPDQG FD RULFH ]RQ DVWIHO REWLQXWVILHHOLEHUDWGXSXWLOL]DUH 2EV&RPSLODWRUXOQXVHVL]HD]HURULGHWLSXOFHORUPHQ LRQDWHPDLVXVGHSDQDUHDHVWHGH DVHPHQHDGLILFLO XQVLJQHGFRUHOHIWYRLG XQVLJQHGORQJIDUFRUHOHIWYRLG (YDOXHD]GLVSRQLELOXOGHPHPRULHSRVLELOGHDILIRORVLWvQDORFDUHDGLQDPLF)XQF LD "coreleft" UHWXUQHD] PHPRULD OLEHU GLQ VHJPHQWXO GH GDWH LDU farcoreleft" memoria OLEHUGLQheap".
89
Introducere in programare
)XQF LLOH GH DORFDUH SULPHVF QXPUXO H[DFW GH RFWH L FH VH GRUHVF D IL DORFD L úL UHWXUQHD]DGUHVDGHvQFHSXWDUHVSHFWLYHL]RQH'LQPRWLYHGHportabilitate a programului QXHVWHLQGLFDWVVHFDOFXOH]HGLPHQVLXQHD]RQHLGHDORFDWFXIRUPXOD 1UBHOHPHQWH QUBRFWH LBRFXSDWBGHBUHVSHFWLYXOBWLSBGDW
ùLDFHVWOXFUXGHRDUHFHIXQF LHGHDUKLWHFWXUDVLVWHPXOXLGHFDOFXOQXPUXOGHRFWH LSH FDUHHVWHUHSUH]HQWDWDFHODúLWLSGHGDWHSRDWHGLIHULW'HFLSURJUDPXODUWUHEXLPRGLILFDW în cazul UHFRPSLOULLVXEDOWSODWIRUP Portabilitatea unui program 6H UHIHU OD PRGXO GH VFULHUH D XQXL FRG VXUV DVWIHO vQFkW OD recompilarea sau la H[HFX LDOXLVXEGLIHULWHSODWIRUPHKDUGZDUHVDXVRIWZDUHPRGLILFULOHFHWUHEXLHIFXWH SHQWUXDOIDFHIXQF LRQDOVILHPLQLPHVDXLQH[LVWHQWH Pentru a asigura SRUWDELOLWDWHDGLQSXQFWGHYHGHUHDODORFULLGLQDPLFHVHYDIRORVL XQ RSHUDWRU VSHFLDO DO OLPEDMXOXL & úL DQXPH sizeof(WLSBGDW 1XPHOH VH WUDGXFH SULQ ³PULPHDOXL´(OUHWXUQHD]QXPUXOGHRFWH LSHFDUHHVWHUHSUH]HQWDWUHVSHFWLYXOWLSGH GDWÌQDFHVWHFRQGL LLUH]XOWFODUF]RQDGHPHPRULHFHVHGRUHúWHDILDORFDWWUHEXLH FDOFXODWFXXUPWRDUHDIRUPXO 1UBRFWH LBGHBDORFDW nr_elemente*VL]HRIWLSBGDW $FHVWRSHUDWRUúWLHVIDFFDOFXOXOQXPUXOXLGHRFWH LRFXSD LúLvQFD]XOXQXLWLSGHGDW definit de utilizator. $ORFDUHDGLQDPLFDWDEORXULORUGHGDWH 'XSFXPúWLPvQFD]XOWLSXULORUGHGDWHDORFDWHVWDWLFVHSRWGHILQLWDEORXULFDUHV DLESkQODWUHLGLPHQVLXQL([LVWvQUHVSHFWLYXOFD]ROLPLWDUHD]RQHLGHPHPRULHFDUH SRDWHILDVWIHODORFDWúLDQXPHvQWUHNRúL0RIXQF LHGHPRGHOXOGHPHPRULHDOHV SHQWUXFRPSLODUH'DFvQFD]XOGHFODUULLVWDWLFHFRPSLODWRUXOVHRFXSGHWRWSURFHVXO vQFD]XODORFULLGLQDPLFHDFHVWSURFHVGHYLQHFHYDPDLFRPSOLFDW([LVWGRXPHWRGHGH alocare: Metoda I $ORFDUHD VH IDFH R VLQJXU GDW SHQWUX WRDWH HOHPHQWHOH úL HVWH R VLQJXU LQVWUXF LXQH0HWRGDDUHSHQWUXWDEORXULOHDFURUGLPHQVLXQLVXQW! GH]DYDQWDMXOF accesul la un element se poate face numai prin intermediul unui SRLQWHU úL WUHEXLH V IRORVHVFRIRUPXOGHFDOFXODDGUHVHLHOHPHQWXOXL$FHDVWIRUPXOWUHEXLHV LQFRQW GHDGUHVDGHvQFHSXWDPDVLYXOXLGHLQGLFLLHOHPHQWXOXLúLGHQXPUXOGHRFWH LSHFDUH HVWH UHSUH]HQWDW $úD VH OXFUD OD QLYHOXO OLPEDMXOXL GH DVDPEODUH XQGH QX DYHDP DOWH SRVLELOLW L )RUPXODGHHFKLYDOHQ ar[i] ↔ *(av+i).
90
Introducere in programare
Vom prezenta un scurt exemplu de folosire a primei metode în cazul unui tablou monodimensional de caractere. #include <string.h> #include <stdio.h> YRLGPDLQYRLG ^ FKDUVWUJ>@ WKHUHRQHWZR LQW SWOLVW>@LQGH[ VWUFS\VWUJ$FHVWDHXQVLUGHFDUDFWHUH RQH VWUJ>@ RQHúLWZRVXQWLGHQWLFLFDYDORDUH WZR VWUJ SULQWIRQHWZR FF?QRQHWZR RQH VWUJ>@ RQHúLWZRVXQWLGHQWLFL WZR VWUJ SULQWIRQHWZR FF?QRQHWZR WKHUH VWUJ VWUJHVWHLGHQWLFFXVWUJ>@ SULQWIVWUJ>@ F?QVWUJ>@ SULQWIWKHUH F?Q WKHUH IRULQGH[ LQGH[LQGH[ OLVW>LQGH[@ LQGH[ SW OLVW SULQWIOLVW>@ G?QOLVW>@ SULQWI SW G?Q SW JHWFK ` Metoda II
3RUQHúWH GH OD REVHUYD LD F FRPSLODWRUXO IDFH DXWRPDW FDOFXOXO DGUHVHL HOHPHQWXOXL vQ úLUXO GH HOHPHQWH vQ FD]XO GHFODUULL VWDWLFH 6H SRDWH IRORVL úL vQ FD]XO DORFULL GLQDPLFH DFHDVW IDFLOLWDWH D FRPSLODWRUXOXL FX FRQGL LD FD PRGXO vQ FDUH VH UHDOL]HD] DORFDUHD V UHVSHFWH PRGDOLWDWHD LQWHUQ IRORVLW GH FRPSLODWRU vQ FD]XO DORFULLVWDWLFH$FHVWOXFUXSHUPLWHFDGHúLVHYDDORFDGLQDPLFXQWDEORXXOWHULRUvQFRG DFFHVXO OD RULFDUH HOHPHQW V SRDW IL UHDOL]DW IRORVLQG PHWRGD LQGLFHDO WLSLF FD]XOXL DORFULLVWDWLFH 11.2.3.1. Alocarea tablourilor monodimensionale În acest caz alocarea se face conform formulei de mai jos:
91
Introducere in programare
Nume_tablou=(tip_elem_tablou)malloc(nr_elem_tab *sizeof(tip_el_tablou)) Un exemplu de aplicare în program a acestei formule YRLGPDLQYRLG ^ LQWY « Y LQW )PDOORF VL]HRILQW « `
6HREVHUYFYDORULLreturnate de “PDOORF´vLDSOLFPXQRSHUDWRUGHFDVWÌQPRG QRUPDOFRQYHUVLDGHWLSVHIDFHDXWRPDWWRWXúLvQFD]XODORFULLHVWHLQGLFDWVILHIFXW explicit. 9HFWRUXOYDSXWHDILIRORVLWFRQIRUPFHORUGRXPHWRGHFDPDLMRV 1. *(v+4*sizeof(int))=0x32; 2. v[4]=0x32; 8Q FD] VSHFLDO HVWH FD]XO DORFULL GLQDPLFH DO XQXL WDEORX monodimesional de FDUDFWHUH 2ULFH úLU GH FDUDFWHUH HVWH SUHY]XW FX XQ WHUPLQDWRU VSHFLDO µ?¶ $FHDVWD SURYLQH GLQ OLPEDM GH DVDPEODUH DYkQG vQ YHGHUH IDSWXO F IXQF LLOH '26 GH OXFUX FX úLUXULOHGHFDUDFWHUHVXQWJkQGLWHVSULPHDVFDGUHVDGHvQFHSXWGLQ]RQDGHPHPRULHúL V SUHOXFUH]H HOHPHQW FX HOHPHQW úLUXO SkQ FkQG HVWH vQWkOQLW WHUPLQDWRUXO µ¶ 'DF LQL LDOL]DUHD VH YD IDFH HOHPHQW FX HOHPHQW vQ SURJUDP ‘\0’ trebuie depus explicit. )RORVLUHD IXQF LLORU GH ELEOLRWHF G DYDQWDMXO F WHUPLQDWRUXO HVWH SXV DXWRPDW GH UHVSHFWLYHOHIXQF LL 11.2.3.2. Alocarea tablourilor bidimensionale Formula de HFKLYDOHQ ar[m][n] ↔ *(ar[m]+n) ↔ *(*(ar+m)+n) DaFDUHVWHGHvQWUHJLint ar[m][n] atunci ar[i][j] este echivalent cu *(int*)((char*)ar + (i*a*sizeof(int) + j*sizeof(int)) Cast-urile trebuiesc puse pentru FVHREVHUYFFDOFXOHOHGHGHSODVDPHQW LQkQGFRQWGH GHVSD LXORFXSDWGHXQHOHPHQWGLQWDEORXDXIRVWIFXWHH[SOLFLW ÌQDFHVWFD]DORFDUHDHVWHPDLFRPSOLFDWvQVXOWHULRUH[LVWDYDQWDMXOGHDVHSXWHD OXFUDúLSULQVSHFLILFDUHDSR]L LHLHOHPHQWXOXL
92
Introducere in programare
LQWIDU
WBDOORFLQWOLQLQWFRO ^ LQWL UHJLVWHULQWIDU
SOLQ SGDWD
SGDWD LQWIDU IDUFDOORFOLQ FROVL]HRILQWIDU LISGDWD LQWIDU 18// HUU1XH[LVWDVSDWLX SOLQ LQWIDU
IDUFDOORFOLQVL]HRILQWIDU LISOLQ LQWIDU
18// HUU1XH[LVWDVSDWLX IRUL LOLQL ^ SOLQ>L@ SGDWD SGDWD FRO ` UHWXUQSOLQ `
YRLGWBIUHHLQWIDU
SD ^ IDUIUHH SD IDUIUHHSD ` ([HPSOXO GH PDL VXV QH SHUPLWH V SRW DFFHVD vQ SURJUDP XQ HOHPHQW GLQ WDEORX vQ maniera m[i][j] ceea face codul mai clar. 11.2.3.3. Tablouri tridimensionale 6H WUDWHD] GXS DFHHDúL JkQGLUH FD úL FHOH ELGLPHQVLRQDOH PDL MRV YRP SUH]HQWD XQ exemplu de tranfer de parametru în cazul tablou tridimensional. fie void main(void) { int m [5][6][7]; … f2(m, x, y, z); } void f2 ( int *** argm, dim1, dim2, dim3) {int x; … atunci x=2 + *((int x) argm + i*dim3*dim2 + j*dim2 + k) (echivalent cu argm[i][j] )
93
11.3. Pointeri la structuri
Introducere in programare
$YkQG vQ YHGHUH GHILQL LD SRLQWHUXOXL QX H[LVW QLPLF FDUH V vPSLHGLFH GHFODUDUHD unor SRLQWHUL OD R VWUXFWXU ÌQ DFHVWH FRQGL LL WLSXO GH GDWH JUXS úL VWXGHQW SUH]HQWDWH anterior se vor rescrie ca mai jos. W\SHGHIVWUXFW6WXGHQW ^ FKDU QDPH XQVLJQHGVKRUWLQWDJH XQVLJQHGFKDUQBODE XQVLJQHGFKDUQBH[ `VWXG W\SHGHIVWUXFW*UXSD ^ LQWQXPH LQWDQ VWXG W `JU JU S,*S 11.3.1. Accesarea membrilor 'HDFHDVWGDWVHYDIRORVLÆvQORFGHSXQFWUHVWXOUHJXOLORUGHDFFHVDUHUPkQkQG identice. Acest nou operator de acces a fost introdus doar pentru a scrie cod cât mai clar GHRDUHFHGDFDYHPVWXG S; atunci SÆQXPH este perfect echivalent cu S QXPH din punct de vedere sintactic. 11.3.2. Alocarea structurilor de date 7UHEXLHDYXWJULMFDWRWVSD LXGHFDUHHVWHQHYRLHVILHDORFDW([LVWGRXQLYHOHGH DORFDUHSULPXOVHUHIHUODDORFDUHDGHVSD LXSHQWUXQRXOWLSGHGDWDOGRLOHDQLYHOVH UHIHUODDORFDUHDGHVSD LXXQGHHVWHFD]XOSHQWUXPHPEULLQRXOXLWLSGHGDW Alocarea la primul nivel: *U S S JU PDOORF VL]HRIJU 1LYHOXOGRLGHDORFDUHHVWHFODUGHDELDvQID]DGHLQL LDOL]DUHDXQHLVWUXFWXUL ,QL LDOL]DUHúLGLVWUXJHUHVWUXFWXUL6 'DF DORFDUHD GLQDPLF SHQWUX WLSXULOH GH GDWH SUHGHILQLWH H UHODWLY VLPSO vQ PDMRULWDWHD FD]XULORU vQ FD]XO VWUXFWXULORU GH GDWH WUHEXLH FD SURJUDPDWRUXO VúL FRQVWUXLDVFSURSULLOHIXQF LLGHLQL LDOL]DUHHOLEHUDUHDGDWHORU
94
Introducere in programare
6HYRUSUH]HQWDIXQF LLOHGHLQL LDOL]DUHSHQWUXXQRELHFWGHWLSJUXSD6HREVHUY FXQRELHFWGHWLSJUXSDFRQ LQHXQWDEORXGHRELHFWHGHWLSVWXGHQWúLGHFLWUHEXLHvQWkL VFULVIXQF LDFDUHLQL LDOL]HD]SHDFHVWD 6WXG LQLWBVWXGFKDU QDPHXQVLJQHGVKRUWLQWDJH XQVLJQHGFKDUQBODEQBH[ ^ VWXG S LIS VWXG PDOORFVL]HRIVWXG HUURU³1RWHQRXJKPHPRU\´ LIS!QDPH FKDU PDOORFVWUOHQQDPH VL]HRIFKDU HUURU³1RWHQRXJKPHPRU\´ VWUFS\S!QDPHQDPH S!DJH DJH S!QBODE QBODE S!QBH[ QBH[ UHWXUQ S ` YRLGIUHHBVWXGVWXG S ^ IUHHS!QDPH IUHHS ` JU LQLWBJULQWQXPHVWXG VWFKDUQDPHLQWDJHQQ ^ JU S LIS JU PDOORFVL]HRIJU HUURU³1RWHQRXJKPHPRU\´ S!QXPH QXPH S!DQ DQ S!VWXG VW UHWXUQS ` YRLGHUUFKDU V ^ FOUVFU SXWVV H[LW `
'XSFXPVHREVHUYFHOHGRXIXQF LLDORFFkWHXQHOHPHQW$ORFDUHDSHQWUXWDEORXO GH VWXGHQ L DO FUXL pointer este primit în LQLWBJU QX D IRVW SUH]HQWDW SHQWUX F VH IRORVHúWHIXQF LDGHDORFDUHDXQXLVWXGHQWSHQWUXILHFDUHGLQHOHPHQWHúLHOHSRWILGHSXVH vQWUXQWDEORXDORFDWVWDWLFVDXGLQDPLFVDXvQWUROLVWvQOQ XLW
95
Introducere in programare
'HFODUD LLFRPSOH[HvQ&6
3HVWH XQ DQXPLW QLYHO GH H[SHULHQ H[LVW WHQGLQ D IRORVLULL XQRU GHFODUD LL FRPSDFWH GDU FRPSOH[H úL DFHDVWD FRPSLODWRUXO OH DFFHSW DFHVW OXFUX FRQGXFH OD VFGHUHD FODULW LL FRGXOXL GH DFHHD UHFRPDQGP HYLWDUHD SH FW SRVLELO 'H H[HPSOX XUPWRDUHDGHFODUD LHGHFODUSH[FDILLQGXQSRLQWHUODRIXQF LHFDUHvQWRDUFHXQpointer FWUHDOFLQFLOHDHOHPHQWGLQWUXQWDEORXGHpointeri la întregi. Complicat , NU? int *(*(*x)()[5]; Mai IRVSUH]HQWPRPHWRGVLPSOSHQWUXHYLWDUHDDFHVWXLWLSGHFRPSOLFD LL typedef int *AP[5] //al 5-lea element dintr-un tablou de pointeri la întreg W\SHGHI$3 )3 IXQF LDFHYDreturna un pointer la al 5-lea element al unui tablou de SRLQWHULFWUHvQWUHJL FP *x // pointer la...... 0RWLYXO SULQFLSDO SHQWUX FDUH GHFODUD LLOH GH WLSXO FHORU SUH]HQWDWH PDL VXV VXQW DúD FRPSOLFDWH vQ & HVWH GDWRUDW IDSWXOXL F RSHUDWRUXO pointer { * } este de tip prefix, iar RSHUDWRULLGHIXQF LHúLWDEORXVXQWGHWLSpostfix. 3HQWUX D GHVFLIUD GHFODUD LL FRPSOH[H WUHEXLH SULYLW GLQXQWUX vQVSUH DIDU DGXJQG vQVWQJDúL VDX>@vQGUHDSWD 'HVFLIUDUHDGHFODUD LLORUFRPSOH[HvQ&6 &HDPDLEXQVWUDWHJLHvQGHVFLIUDUHDGHFODUD LLORUFRPSOH[HHVWHVSRUQLPGHOD QXPHOHYDULDELOHLúLDSRLVDGXJPV LQHPFRQWGHHIHFWXO ILHFDUHSDUWHRSHUDWRU DL H[SUHVLHSRUQLQGFXRSHUDWRULLFHLPDLDSURSLD LGHQXPHOHYDULDELOHL În liSVDSDUDQWH]HORUVHYD LQH FRQW vQWL GH RSHUDWRULL GH WLS IXQF LH VDX WDEORX GLQ GUHDSWD YDULDELOHL GHRDUHFH DX SULRULWDWH PDL PDUH úL GHDELD OD VIUúLW VH YD LQH cont de efectul operatorilor de tip prioritar) De exemplu: FKDU
>@YDILGHVFLIUDWvQXUPWRULLSDúL 1. x[ ] este un tablou 2. **[ ] este un tablou de pointeri 3. char *x[ ] este un tablou de SRLQWHULFWUHFDUDFWHUH 3DUDQWH]HOH SRW IL IRORVLWH ILH SHQWUX FODULWDWH ILH SHQWUX VFKLPEDUH D SUHFHGHQ HL 'H exemplu: LQW
>@ YDILGHVFRPSXVGXSFXPXUPHD] 1. x[ ] este un tablou 2. ( **[ ] ) este un tablou de pointeri 3. ( **[ ] ) ( ) este un tablou de SRLQWHULFWUHIXQF LL 4. int ( (*x [ ] ) ( ) este un tablou de SRLQWHULFWUHIXQF LLFHUHWXUQHD]XQvQWUHJ &RPSXQHUHDGHFODUD LLFRPSOH[H 3HQWUXDFRPSXQHGHFODUD LLFRPSOH[HSURFHVXOHVWHLGHQWLF FDODGHVFRPSXQHUH 'H H[HPSOX GDF VH GRUHúWH GHFODUDUHD XQXL SRLQWHU FWUH XQ WDEORX GH SRLQWHUL FWUH IXQF LL FDUH vQWRUF SRLQWHUL OD WDEORXUL GH VWUXFWXUL FX QXPHOH 6 VH YRU IRORVL XUPWRULL SDúL 1. ( *x) este un pointer 2. ( *x)[ ] este un pointer la un tablou
96
Introducere in programare
3. ( *(*x[ ])) este un pointer la un tablou de pointeri 4. ( *(*x[ ])) ( )este un pointer la un tablou de SRLQWHULODIXQF LL 5. ( *( *(*x[ ])) ( ) )este un pointer la un tablou de SRLQWHUL OD IXQF LL FH vQWRUF pointeri 6. ( *( *(*x[ ])) ( ) ) [ ] este un pointer la un tablou de SRLQWHULODIXQF LLFHvQWRUF pointeri catre tablouri 7. struct S ( *( *(*x[ ])) ( ) ) [ ] este un pointer la un tablou de SRLQWHULODIXQF LLFH întorc pointeri catre tablouri de structuri cu numele S
ÌQ VFRSXO XúXUULL vQ HOHJHULL VH YD SUH]HQWD XQ WDEORX FX diverese expresii complexe X]XDOHvQFRGXULOH&úLDQXPLWHJUHúHOLFRPXQH • • • • • • • • • • • • • • • • • • • • • • • • • • • •
int i; un întreg int* p; pointer la întreg int a[ ]; tablou de întregi LQWI IXQF LHFHvQWRDUFHXQvQWUHJ int **p; pointer la SRLQWHUFWUHvQWUHJ int ( *pa ) [ ]; pointer la tabloul de întregi int ( *pf ) ( ); SRLQWHUODRIXQF LHFHvQWRDUFHXQvQWUHJ int *ap [ ]; un tablou de SRLQWHULFWUHvQWUHJL int aa [ ] [ ] un tablou de tablou de întregi int DI>@ XQWDEORXGHIXQF LLFHvQWRUFvQWUHJ NEPERMIS int *IS RIXQF LHFHvQWRDUFHXQpointer la întreg LQWID >@RIXQF LHFHvQWRDUFHXQWDEORXGHvQWUHJL NEPERMIS LQWI RIXQF LHFHvQWRDUFHRIXQF LHFHvQWRDUFHXQint NEPERMIS int ***p; pointer la pointer la pointer la int int ( **ppa) [ ]; pointer la SRLQWHUFWUHXQWDEORXGHint int ( **ppf) [ ]; pointer la SRLQWHUFWUHRIXQF LHFHvQWRDUFHint int * ( *pap) [ ]; SRLQWHUFWUHXQWDEORXGHpointeri la întregi int (* paa) [ ] [ ]; pointer la un tablou de SRLQWHULFHUH LQvQWUHJL int (* paf) [ ] ( ); SRLQWHUODXQWDEORXGHIXQF LLFHvQWRUFvQWUHJL NEPERMIS int *( *pfp) ( ); un SRLQWHUODRIXQF LHFHvQWRDUFHvQWUHJ int (* pfa ) ( ) [ ]; SRLQWHUODRIXQF LHFHvQWRDUFHXQWDEORXGHvQWUHJL NEPERMIS int IDI >@ IXQF LHFHvQWRDUFHXQWDEORXGHIXQF LLFHvQWRUFvQWUHJL NEPERMIS int *IIS IXQF LHFHvQWRDUFHRIXQF LHFHvQWRDUFHXQpointer la int NEPERMIS int (* pff) ( ) ( ); un SRLQWHUODRIXQF LHFHvQWRDUFHRIXQF LHFHvQWRDUFHXQvQWUHJ NEPERMIS int **app[ ]; un tablou de pointeri la SRSLQWHULFWUHvQWUHJL int ( *apa[ ] ) [ ]; un tablou de SRLQWHULFWUHXQWDEORXGHvQWUHJL int (*apf[ ] ) ( );un tablou de SRLQWHULFWUHIXQF LLFHvQWRUFvQWUHJL int *aap[ ] [ ];un tablou de tablouri de pointeri la întregi 97
• • • • • • • • • •
Introducere in programare
int aaa[ ] [ ] [ ]; un tablou de tablouri de tablouri de întregi int DDI>@>@ XQWDEORXGHWDEORXULGHIXQF LLFHvQWRUFvQWUHJL NEPERMIS int *DIS>@ XQWDEORXGHIXQF LLFHvQWRUFpointeri la întregi int DID>@ >@XQWDEORXGHIXQF LLFHvQWRUFWDEORXULGHvQWUHJL NEPERMIS int DII>@ XQWDEORXGHIXQF LLFHvQWRUFIXQF LLFDUHvQWRUFvQWUHJL NEPERMIS int **ISS RIXQF LHFHvQWRDUFHXQpointer la pointer de întregi int ( *ISD >@RIXQF LHFHvQWRDUFHXQSRLQWHUFWUHWDEORXULGHvQWUHJL int ( * ISI IXQF LHFHvQWRDUFHXQSRLQWHUFWUHRIXF LHFHvQWRDUFHXQvQWUHJ int IDD >@>@IXQF LHFHvQWRDUFHXQWDEORXGHWDEORXULGHvQWUHJL NEPERMIS int *IDS >@IXQF LHFHvQWRDUFHXQWDEORXGHSRLQWHULFWUHvQWUHJL NEPERMIS
Câteva tehnici de FUHúWHUHDvitezei de H[HFX LHDunui cod Propagarea copiei (se pune la asignare) Fie urmWRDUHDVHFYHQ GHFRG: x=y z=1.+x corect este: x=y z=1.+y (eliminLQWHUGHSHQGHQ HOHGLQWUHFHOHGRXUHOD LL Reducerea puterii Ex: y = pow (x,2) -> ia foarte mult timp ÌQORFXL LFXy=x*x sau j=k*2 -> ia mult timp vQFRPSDUD LHFXDGXQDUHDM=k+k sau cu deplasarea la stQJDj=k<<2 Redenumirea variabilelor CâWHRGDWFQGVHYUHDYLWH]PD[LP HVWHPDLELQHVHYLWPrefolosirea variabilelor x=y*z q=r+x+z x=a+b A b x=y*z x0=y*z q=r+x+z q=r+x0+x0 x=a+b x=a+b Compilatorul reali]HD]DXWRPDWDFHVWHOXFUXULGDUSHQWUXFDOFXOHGHILQH HHVWHELQHV facem personal optimizarea codului. Deoarece a) PQQFWLPSGHFDOFXOLDUE PQQF PHPRULHvQRS LXQLOHFRPSLODWRUXOXLH[LVW: 1. optimi]DUHSHQWUXYLWH]VWLOE 2. optimizare pentru dimensiune (stil a) 98
Introducere in programare
Eliminarea subexpresiilor comune Ex.
D=C*(A+B) E=(A+B)/2 F=sqr(A+B)
t=A+B D=C*t E=t/2 F=sqr(t) Aten LHODRUGLQHDvQFDUHVHVFULXsubexpresiile deoarece pt. compilator a+b+c poate fi diferit de %&DFKLDUFkQGVHIDFHRSWLPL]DUHDXWRPDW ----->
Eliminarea codurilor constante din bucle for (i=0;i
99
Introducere in programare
adresa = adresa+temp
/XFUXOFXILúLHUHOHGHGDWH,2),/( Streamuri (streams) &QXIDFHGLVWLQF LHvQWUHGLYHUVHGLVSR]LWLYHFXPDUILWHUPLQDO=,3+'' În toate cazurile I/O este realizat prin aceste VWUHDPXULFDUHVXQWDVRFLDWHFXILúLHUHVDXFX dispozitive. 'HI: Un streamFRQVWGLQWURvQúLUXLUHRUGRQDWGHRFWH L3RDWHILJQGLWFXPYD FDXQYHFWRUQXPDLFvQDFHVWFD]QXDPDFFHVSDUDOHOFLVHULDOODILHFDUHHOHPHQW Pentru a realiza accesarea unui stream trebuie declarat un SRLQWHUODRVWUXFWXUVWDQGDUG QXPLW),/(GHILQLWLQstdio.h. (DFRQ LQH – ,QIRUPD LLGHVSUHQXPHOHILúLHUXOXL – Modul de acces – Un SRLQWHUFWUHXUPWRUXOFDUDFWHUGLQstream $FHVWH LQIRUPD LL VXQW QHFHVDUH VLVWHPXOXL GH RSHUDUH GLQ SXQFWXO GH YHGHUH DO programatorului accesul se face prin intermediul unui SRLQWHU OD DFHDVW VWUXFWXU QXPLW SRLQWHUODILúLHUILOHpointer). Un program poate avea unul sau mai multe streamuri deschise simultan. Unul din câmpurile din FILE este indicatorul dHSR]L LHvQILúLHUFDUHVHUHIHUOD XUPWRUXORFWHWFHYDILVFULVFLWLWvQGLQILúLHU 'XS ILHFDUH RSHUD LH 5: 62 PRGLILF DXWRPDW DFHVW pointer. Accesarea se SRDWH IDFH GLUHFW GDU DFHVW FD] SDUWLFXODU GH FDUH WUHEXLH LQXW VHDPD YD IDFH FD XQ SURJUDPVQXPDLILHSRUWDELO $WHQ LH: A nu se confunda SRLQWHUXO OD ILúLHU FX LQGLFDWRUXO GH SR]L LH 8QXO HVWH XQ pointer altul este un deplasament. Streamuri standard ([LVW streamuri care sunt deschise automat pentru fiecare program. Ele se numesc stdin, stdout, VWGHUUÌQPRGQRUPDOHOHVHUHIHUODWHUPLQDOGDUPXOWHVLVWHPHGH operare permit redirectarea lor. ([XQPHVDMGHHURDUHSRDWHILVFULVvQWUXQILúLHUvQORFGHWHUPLQDOSULQWI úLscanf() folosesc streamurile VWGLQúLVWGRXW$FHVWHIXQ LLSRWILIRORVLWHSHQWUXOXFUXOFXILúLHUHOH prin redirectarea VWGLQ úL VWGRXW FWUH UHVSHFWLYHOH ILúLHUH IRORVLQG BBBBBB 'DU PDL FRPRGHVWHIRORVLUHDIXQF LLORUIVFDQI úLfprintf(). )RUPDWWH[WúLIRUPDWELQDU6 Datele pot fi accesate îQ XQXO GLQ FHOH GRX IRUPDWH VWDQGDUG $16, ±WH[W VDX binar. Un VWUHDPGHWLSWH[WFRQVWGLQWURVHULHGHOLQLLXQGHILHFDUHOLQLHHVWHWHUPLQDW FXXQFDUDFWHUGHVDOWODOLQLHQRXµ\n’. 2ULFH 62 SRDWH DYHD DOW SROLWLF GH VWRFDUH D OLQLLORU SH GLVF GHFL WHUPLQDWRUXO µ\n’ nu este obligatoriu. Dar din punct de vedere al programatorului este ca mai sus, restul fiind DVFXQVGHFRPSLODWRUXO&7RWXúLOXFUXOvQPRGWH[WQXJDUDQWHD]RSRUWDELOLWDWHWRWDOD
100
Introducere in programare
XQXLSURJUDPÌQIRUPDWXOELQDUFRPSLODWRUXOQXUHDOL]HD]QLFLRLQWHUSUHWDUHDEL LORUvL FLWHúWHVDXVFULHH[DFWFXPDSDU
Bufferarea (folosirea unei zone de memorie ca tampon) 'HRDUHFHFRPSDUDWLYFXPHPRULDGLVSR]LWLYHOHGHVWRFDUHvQPDVVXQWH[WUHPGH vQFHWHVHvQFHDUFUHGXFHUHDQXPUXOXLGHRSHUD LLGH ,2 FXGLVFXOBufferarea este cea PDLVLPSOPHWRGSHQWUXDUHDOL]DDFHDVWD Un EXIIHU HVWH R ]RQ XQGH VXQW VWRFDWH WHPSRUDU GDWHOH vQDLQWH GH D IL WULPLVH FWUHGHVWLQD LDORUILQDO7RDWH62IRORVHVFbuffere pentru a citi/scrie din dispozitivele ,2QXPDLFvQDFHVWFD]EXIIHUXODUHGLPHQVLXQHIL['HRELFHLHVWHGHVDX RFWH L$FHVWOXFUXvQVHDPQFFKLDUGDFVHGRUHúWHFLWLUHDXQXLVLQJXUFDUDFWHUGLQWUXQ ILúLHU62FLWHúWHWRWEORFXOFDUHFRQ LQHUHVSHFWLYXOFDUDFWHU %LEOLRWHFD&FRQ LQHXQVWUDWDGL LRQDOGHbufferare care apare în 2 forme: – buffere de linie – buffere de bloc La EXIIHUDUHDvQOLQLHVLVWHPXOVWRFKHD]FDUDFWHUHOHSQODSULPXOµ\n’ sau SQ se umple EXIIHUXO úL DSRL WULPLWH vQWUHDJD OLQLH OD 62 SHQWUX SURFHVDUH $FHVW OXFUX VH vQWPSOúLFQGFLWHVFGDWHGHODWHUPLQDO La EXIIHUDUHD vQ EORF VLVWHPXO VWRFKHD] FDUDFWHUHOH SQ FQG DFHO EORF HVWH umplut. Implicit VWUHDPXULOH ,2 UHODWLY OD ILúLHUH VXQW bufferate în bloc, iar cele care se UHIHUODWHUPLQDOstdin, stdout) sunt bufferate în linie. 'HDVHPHQHDELEOLRWHFD&LQFOXGHúLXQPDQDJHUGHEXIIHUFDUH LQHDFHObuffer FW PDLPXOWSRVLELOvQPHPRULHGHFLGDFVHDFFHVHD]DFHHDúLSRU LXQHGLQWUXQstream de PDLPXOWHRULH[LVWPDULúDQVHFDVVHHYLWHDFFHVXOODGLVSR]LWLYXOGHVWRFDUH $WHQ LH$FHVWOXFUXSRDWHFUHDSUREOHPHGHFRQVLVWHQ GDFILúLHUXOHVWHIRORVLW în comun de mai multe procese. Pentru sincronizarea între procese este nevoie de: – scrierea în ASM a propriilor IXQF LL – IRORVLUHDIXQF LLORUVSHFLILFH62SHFDUHVHOXFUHD] În ambele moduri de bufferare, bufferul poate fi golit la cerere cu ajutorul lui fflush() WULPL QGDVWIHOGDWHOHGLQHOODGHVWLQD LDORU 'HúL DFHVW PRG GH bufferare este mai eficient GHFW SURFHVDUHD LQGLYLGXDO D ILHFUXL FDUDFWHU H[LVW FD]XUL vQ FDUH QH GHUDQMHD] úL DQXPH FQG VH GRUHúWH H[SOLFLW SURFHVDUHDLQVWDQWDQHHDXQXLFDUDFWHUILHODLQWUDUHILHODLHúLUH&HOPDLVLPSOXH[HPSOX este atunci FQGVHGRUHúWHSURFHVDUHDFDUDFWHUHORULQWURGXVHGHOD.%'IUDPDLDúWHSWD ‘\n’. C permite modificarea memoriei bufferului chiar la acest nivel,tocmai pentru a SXWHDUH]ROYDúLSUREOHPHOHGHDFHVWWLS &LWLUHDúLVFULHUHDGDWHORU6 2GDWFHVDGHVFKLVXQILúLHUVHYDIRORVLSRLQWHUXOODILúLHUSHQWUXDSXWHDUHDOL]D RSHUD LLGHVFULHUHFLWLUH([LVWQLYHOHGHJUDQXODULWDWHILQH H 1. la nivel de caracter – FkWHXQFDUDFWHURGDW 2. la nivel de linie − FkWHROLQLHRGDW
101
Introducere in programare
3. la nivel de bloc – FkWHXQEORFRGDW
2 UHJXO FDUH VH DSOLF OD WRDWH QLYHOHOH GH ,2 HVWH DFHHD F QX SRW FLWL GLQWUXQ VWUHDPúLDSRLVFULXvQHOIUDDSHODfseek() , rewind() sau IIOXVK /DIHOúLvQFD]XO LQYHUV $FHVW OXFUX HVWH QHFHVDU SHQWUX F WUHEXLH JROLW EXIIHUXO vQDLQWHD RULFUHL RSHUD LLLSHQWUXDHYLWDSUREOHPHOHGHLQFRQVLVWHQ 1. Lucrul la nivel de caracter JHWF PDFURFLWHúWHXQFDUDFWHU putc() - macro , scrie un caracter
fgetc() fputc()
de la stream-ul implicit
identice cu anterioarele lor dar sunt ILQF LL
'HúLSULPHOHVXQWPDFURXULGHFLPXOWPDLUDSLGHSRWH[LVWDSUREOHPHGHex: putc(‘x’, fp[M@ VDUSXWHDVQXPHDUJFRUHFW 'H DFHHD HVWH LQGLFDW F DWXQFL FQG VH OXFUHD] FD vQ H[HPSOX V IRORVLP fputc() sau fgetc(); 2. La nivel de linie )XQF LLIJHWV úLISXWV úLVHVSHFLILFSDUDPHWUL s -> pointer la tabloul de caractere n -> nr. Maxim de caractere ce vor fi procesate stream -> VWUHDPXOFXFDUHVHOXFUHD] 3. La nivel de bloc Un bloc poate fi JQGLWFDXQWDEORX&QGFLWLPVFULHPvQEORFWUHEXLHVVSHFLILFPDWW QXPUXOGHHOHPHQWHDOEORFXOXLFWúLGLPHQVLXQHDILHFUXLHOHPHQW )XQF LLIUHDG úLIZULWH FXXUPWRULLSDUDPHWUL ptr -> SRLQWHUFWUHWDEORXOFHVWRFKHD]FDUDFWHUHOH VL]H!PULPHQUGHRFWH LDOWLSXOXLGHGDWIRORVLWUHFRPDQGsizeof() pentru portabilitatea programului QXPUHOHPHQWHFHWUHEXLHSURFHVDW pointer la stream I/O Nebufferat $QWHULRU DP PHQ LRQDW IDSWXO F H[LVW VLWXD LL vQ FDUH bufferarea datelor nu GHUDQMHD]([HPSOXFODVLFHVWHHGLWRUXOGHWH[WFQGVHVFULHXQWH[WúLVHDSDVWDVWHOH de navigare prin el. Pentru a elimina buferarea pentru stdin se poate folosi fie setbuf(), fie setvbuf(). Setbuf(stdin,NULL) sau stat= setvbuf (stdin, NULL, IONBUF, O).
102
Introducere in programare
$FFHVXODOHDWRUvQWUXQILúLHU6 (VWHDVLJXUDWGHIXQF LDIVHHNFDUHSR]L LRQHD]SRLQWHUXOODILúLHUODXQGHSODVDPHQWGRULW ID GHvQFHSXWXODFHVWXLD Ftell UHWXUQHD]YDORDUHDSRLQWHUXOXLGHILúLHU
103
CAPITOLUL 12
Introducere in programare
)DFLOLW LGHQLYHOVF]XWDOHOLPEDMXOXL& 5XWLQHOH XWLOL]DWH GH VLVWHPXO GH RSHUDUH 06'26 SHQWUX WUDWDUHD RSHUD LLORU GH LQWUDUHVDXLHúLUHúLJHVWLXQHDUHVXUVHORUVLVWHPXOXLSRWILDSHODWHúLGLQWU-un program scris intr-un limbaj GHQLYHOvQDOWFXPHVWH&/LPEDMXOSHUPLWHúLOHJDUHDFXPRGXOHVFULVH in limbaj de asamblare. Rutinele DOS sunt apelate prin întreruperi software. Întreruperea 21h (INT 21h) este serviciul pentru apelul IXQF LLORU'26Pentru apelul unei IXQF LLaceste registre (nu toate) se SUHJWHVFFXGLYHUVHvalori: con LQHQXPUXOIXQF LHL con LQHQXPUXOVXEIXQF LHLGDFeste cazul)
AH AL
Celelalte registre se vQFDUFdiferit de la caz la caz. Se JHQHUHD]apoi, întreruperea INT 21H. Similar cu IXQF LLOH'26VHSRWrealiza úLapeluri de IXQF LLBIOS pregWLQGregistrele intr-un mod convenabil. Sunt astfel accesibile majoritatea IDFLOLW LORU pe care programatorul le avea numai la nivelul limbajului de asamblare. Vom descrie in continuare câteva din func LLOH cuprinse in biblioteca C ale FURU prototipuri se afla in fisierele dos.h, bios.h, setjmp.h, process.h. 12.1. )XQF LLpentru acces direct la memorie unsigned FP_OFF(farpointer); unsigned FP_SEG(farpointer); void far *MK_FP(unsigned seg, unsigned ofs); Aceste trei macrouri sunt folosite pentru a returna: -offsetul unui pointer far; -segmentul unui pointer far; -un pointer far cu segmentul seg si ofsetul ofs. Exemplu: #include <dos.h> #include <stdio.h> void main(void) { char far *ptr; unsigned seg,off; ptr=MK_FP(0x8000,0); seg=FP_SEG(ptr);
104
Introducere in programare
off=FP_OFF(ptr); printf("farptr=%Fp, segment=%04x, offset=%04x\n", ptr,seg,off); } int peek(unsigned segment,unsigned offset); char peekb(unsigned segment,unsigned offset); void poke(unsigned segment,unsigned offset,int value); void pokeb(unsigned segment,unsigned offset,char value);
Cele patru IXQF LLGHmai sus UHDOL]HD] citirea (peek, peekb) respectiv scrierea (poke, pokeb) unui cuvânt sau caracter de la sau la adresa de memorie segment:offset. 12.2. )XQF LLpentru accesul la porturile de intrare sau LHúLUH int inp(int portid); int outp(int portid, int value); int inport(int portid); unsigned char inportb(int portid); void outport(int portid,int value); void outportb(int portid,unsigned char value); Toate cele úDVH IXQF LL GH mai sus au drept scop citirea (inport) respectiv scrierea (outport) a unui cuvânt, sau caracter (inp, inportb, outp, outportb) de la, sau la portul de intrare sau LHúLUHspecificat prin variabila portid. )XQF LLSHQWUXJHVWLXQHDLQWHUYDOHORUGHWLPS void delay(unsigned miliseconds); )XQF LDVXVSHQGH[HFX LDprogramului în intervalul specificat (în milisecunde). void sleep(unsigned seconds); )XQF ia suspendH[HFX LDprogramului curent pe durata seconds secunde. 12.4. Functii pentru controlul sistemului de întreruperi void disable(void); $FHDVWIXQF LHLQYDOLGHD]întreruperile, OVkQGYDOLG numai întreruperea QHPDVFDELO NMI. Este echivalentFXLQVWUXF LXQHD&/,GLQlimbajul de asamblare. void enable(void);
105
Introducere in programare
$FHDVW IXQF LH YDOLGHD] întreruperile oferind programatorului controlul flexibil al întreruperilor hardware. Este echivalentFXLQVWUXF LXQHD67,GLQlimbajul de asamblare. 12.5. )XQF LLpentru alocarea memoriei (prin DOS) int allocmem(unsigned size, unsigned *segp);
)XQF LDDSHOHD]'26[pentru a aloca o zonGHmemorie OLEHUGHdimensiune size*16 RFWH L Ea UHWXUQHD] adresa de segment a blocului alocat în ORFD LD SRLQWDW GH segp. În caz de succes UHWXUQHD] -1. În caz contrar se VHWHD] Bdoserrno si variabila JOREDOerrno. int setblock(unsigned segx, unsigned newsize); $FHVW IXQF LH PRGLILF dimensiunea DORFDW unui bloc de memorie, de la adresa de segment segx UPkQkQGDORFD Ldoar newsize*16 RFWH LÎn caz de succes IXQF LDUHWXUQHD] -1. )XQF ia face apelul DOS 0x4A. int freemem(unsigned segx); )XQF LD HOLEHUHD] un bloc de memorie alocat cu allocmem. Parametrul segx este adresa de segment UHWXUQDW de aceasta din XUPÎn caz de succes IXQF ia UHWXUQHD]-1. )XQF LDface apelul DOS 0x44. 12.6. Întreruperi software. Accesul la registrele procesorului. void geninterrupt(int intr_num); Acest macrou JHQHUHD] întreruperea software intr_num. Starea registrelor GXS apel depinde de întreruperea DSHODW. Accesul la registrele procesorului se face prin variabilele pseudoregistru prezentate mai jos: _AX, _BX, _CX, _DX, _SI, _DI, _BP, _SP, _DS, _CS, _ES, _SS, _FLAGS care le copie starea. Aceste variabile trebuie folosite cu precau LH6HUHFRPDQGDVHevita alterarea variabilelor _DS, _SS, _CS si _SP. În orice caz, se va XUPULcodul generat úLVH va depana cu DWHQ LHprogramul. int int86(int intno, union REGS *inregs, union REGS *outregs); Aceasta IXQF LH H[HFXW întreruperea software VSHFLILFDW în intno. Înaintea H[HFX LHL întreruperii, se copie în registre valorile specificate în reuniunea SRLQWDWGHinregs. 'XS execu LHvalorile registrelor sunt copiate în outregs; de asemenea, úLstarea bitului Carry în x.cflag (1=de obicei LQGLFReroare). De notat Foutregs poate pointa pe DFHHDúLstructura ca inregs. )XQF ia UHWXUQHD]valoarea din AX la terminarea întreruperii. Reuniunea REGS este GHILQLWastfel:
106
Introducere in programare
struct WORDREGS { unsigned int ax,bx,cx,dx,si,di,cflag,flags;}; struct BYTEREGS { unsigned char al,ah,bl,bh,cl,ch,dl,dh;};
union REGS { struct WORDREGS x; struct BYTEREGS h; }; int int86x(int intno, union REGS *inregs, union REGS *outregs, struct SREGS *segregs); Aceasta IXQF LH este LGHQWLF FD IXQF LXQH cu cea DQWHULRDU SHUPL kQG în plus folosirea registrelor de segment pentru transmiterea de parametri (VDOYHD]si UHVWDXUHD] automat ES si DS). 5HWXUQHD]DFHOHDúLvalori ca int86. Structura SREGS este GHILQLW astfel: struct SREGS { unsigned int es; unsigned int cs; unsigned int ss; unsigned int ds;}; void intr(int intno, struct REGPACK *preg); )XQF LDUHDOL]HD]RDOWHUQDWLYpentru LQWHUID area cu întreruperile software, QXPUXO întreruperii fiind specificat în variabila intno. Structura REGPACK este GHILQLWastfel: struct REGPACK { unsigned r_ax,r_bx,r_cx,r_dx; unsigned r_bp,r_si,r_di,r_ds,r_es,r_flag;}; void segread(struct SREGS *segp); )XQF ia returneazîn structura pointata de segp valorile registrelor de segment. )XQF LLSHQWUXFLWLUHDVDXPRGLILFDUHDYHFWRULORUGHvQWUHUXSHUH void interrupt (*getvect(int interruptno))(); Fiecare procesor din familia 80x86 include un set de vectori de întrerupere, QXPHURWD L de la 0 la 255. Valoarea pe 4 RFWH LGLQfiecare vector este o DGUHVreprezentând adresa rutinei de tratarea a întreruperii. getvect FLWHúWH valoarea vectorului de întrerupere specificat de interruptno úLUHWXUQHD]DFHDVWvaloare ca un pointer far la o UXWLQGHtratare a întreruperii.
107
Introducere in programare
void setvect(int interruptno, void interrupt (*isr)()); $FHDVWIXQF LHVHWHD]valoarea vectorului de întrerupere specificat prin interruptno cu o valoare QRX isr, care este un pointer far la adresa altei IXQF LL GH tratare a întreruperii. Obligatoriu aceasta QRXIXQF LHtrebuie sa fie GHFODUDW de tip interrupt (cu modificatorul interrupt). Exemplu: #include <stdio.h> #include <dos.h> void interrupt (*oldvect)(); void interrupt get_out(); void capture_prtscr(void interrupt (*func)()); int looping=1; void main(void) { puts("Pentru a termina apasati <Shift>"); capture_prtscr(get_out); while(looping); puts("Succes"); } /*noua rutina de tratare intrerupere*/ void interrupt get_out() { setvect(5,oldfunc); looping=0; } void capture_prtscr(void interrupt (*func)()) { oldfunc=getvect(5); setvect(5,func); } 12.8. Apeluri de IXQF LL DOS. int bdos(int dosfun, unsigned dosdx, unsigned dosal); )XUQL]HD]accesul direct la multe IXQF LL'26&Dargumente: dosfun = QXPUXOIXQF LHL'26 dosdx = valoarea registrului DX; dosal = valoarea registrului AL.
108
Introducere in programare
)XQF ia UHWXUQHD]valoarea registrului AX. Exemplu: #include <stdio.h> #include <dos.h> char current_drive(void); main() { printf("DRIVERUL %c\n",current_drive()); getch(); } char current_drive(void) { char drive; drive = bdos(0x19,0,0); return ('A'+drive); }
int bdosptr(int dosfun, void *argument, unsigned dosal); )XQF LDIXUQL]HD]accesul la IXQF LLOH'26prin argumentele sale: dosfun = QXPUXOIXQF LHL'26 argument = pointer cerut de IXQF LHLQregistrul DS:DX; dosal = valoarea registrului AL. 5HWXUQHD]DFHOHDúLvalori ca bdos. int intdos(union REGS *inregs, union REGS *outregs); )XQF LD DSHOHD] întreruperea INT 21h pentru a realiza func LD sistem VSHFLILF în registrul AH (inregs->h.ah). Structura outregs este DFWXDOL]DWsetându-se in caz de eroare indicatorul carry. int intdosx(union REGS *inregs, union REGS *outregs, struct SREGS *segregs); Este VLPLODU IXQF LHL intdos, SHUPL kQG în plus úL transmiterea parametrilor prin registrele de segment (DS,ES). Aceste registre sunt restaurate la terminarea apelului. Exemplu: #include <stdio.h> #include <dos.h>
109
Introducere in programare
int delete_file(char far *filename);
main() { int err; err=delete_file("Temp1.$$$"); printf("S-a sters fisierul Temp1.$$$:%s\n",(!err)?"DA":"NU"); } int delete_file(char far *filename) { union REGS regs; struct SREGS sregs; int ret; regs.h.ah=0x41; /*delete file*/ regs.x.dx=FP_OFF(filename); sregs.ds=FP_SEG(filename); ret=intdos(®s,®s,&sregs); return(regs.x.cflag?ret:0); } )XQF LLSHQWUXWHUPLQDUHDVDXODQVDUHDSURJUDPHORU void exec(int code); )XQF LD GHWHUPLQ terminarea QRUPDO D programului úi furnizarea unui cod de retur (code) la LHúLUH Acest cod poate fi interpretat ulterior în ILúLHUHOH de comenzi. Este HFKLYDOHQWFXIXQF ia DOS 0x4C. void abort(void); 3URYRDFterminarea DQRUPDODprogramului, cu DILúarea unui mesaj FRUHVSXQ]WRUpe consola sistemului. void keep(unsigned char status,unsigned size); Apelul acestei IXQF LL permite terminarea programului curent cu transmiterea VWULL procesului SULQWHîn variabila status, precum úLsetarea lui ca program TSR (Terminate and Stay Resident) de dimensiune size paragrafe. Restul memoriei este eliberat. )XQF ia este VLPLODUapelului DOS 0x31. int system(const char *cmd); /DQVHD] &200$1'&20 úL H[HFXW R FRPDQG LQWHUQ sau o comand H[WHUQ sau un ILúLHUGHcomenzi. ùirul de caractere, folosit ca parametru, FRQ LQHnumele complet al comenzii úLtoate argumentele necesare H[HFX LHL
110
unsigned getpsp(void);
Introducere in programare
)XQF LDUHWXUQHD]VHJPHQWXOSUHIL[3URJUDPSegment Prefix) al programului curent. (D UHDOL]HD] IXQFWLD DOS 0x62. ÌQ ORFXO DFHVWHL IXQF LL vQV VH SRDWH IRORVL GLUHFW valoarea variabilei globale _psp. int bioskey(int cmd); Pot fi executate serviciile specifice INT 16h. &X DMXWRUXO DFHVWHL IXQF LL VH SRW FLWL FRGXULOHGHVFDQDUHFRGXULOHH[WLQVHDOHWDVWHORUVHSRWFLWLFDUDFWHUHIUH[WUDJHUHGLQ bufferul tastaturii, sau se poate afla starea tastelor speciale (SHIFT, CTRL, ALT): -
FPG FLWHúWHFRGXOGHVFDQDUHVLVFRDWHFDUDFWHUXOGLQEXIIHUXOWDVWDWXULLGDFQXPDL VXQWFDUDFWHUHVHDúWHDSWDSVDUHDXQHLQRLWDVWH FPG FLWHúWHFRGXOGHVFDQDUHIUDVFRDWHFDUDFWHUXOGLQEXIIHUXOWDVWDWXULLGDFQX PDLVXQWFDUDFWHUHIXQF LDUHWXUQHD] FPG FLWHúWH%,266KLIW6WDWHVWDUHDWDVWHORUVSHFLDOH6+,)7&75/$/7 int biosprint(int cmd, int abyte, int lpt_port);
5HXQHúWH WRDWH IXQF LLOH VSHFLILFH ,17 K DFFHVXO OD SRUWXO SDUDOHO /37[[ )XQF LD permite FLWLUHDVWULLWULPLWHUHDXQXLFDUDFWHUDE\WH VDXLQL LDOL]DUHDLPSULPDQWHL - FPG WULPLWH FDUDFWHUXO DE\WH OD SRUWXO SDUDOHO FX QXPUXO GDW GH OSWBSRUW /37 1=LPT2, etc). - FPG LQL LDOL]HD]LPSULPDQWDFRQHFWDWSHOSWBSRUWFXYDORULOHGLQDE\WH - FPG FLWHúWHVWDUHDLPSULPDQWHLFRQHFWDWHSHOSWBSRUW int bioscom(int cmd, int abyte, int com_port); 5HXQHúte toate IXQF LLOH specifice INT 14h (accesul la portul serial COMxx). )XQF LD permite citirea VWULLtrimiterea unui caracter (abyte) pe FRPXQLFD LHcitirea unui caracter de pe FRPXQLFD LHsau setarea unor controale úLparametri de IXQF LRQDUH -
cmd=0 LQL LDOL]DUHD portului de FRPXQLFD LH com_port (0=COM1, 1=COM2) cu parametrii GD LGHabyte. cmd=1 trimite caracterul abyte pe linia de FRPXQLFD LHGDWDGHcom_port. cmd=2 FLWHúWHcaracterul UHFHS LRQDWpe linia de FRPXQLFD LHGDWGHcom_port. cmd=3 citeste starea portului de FRPXQLFD LHcom_port. long biostime(int cmd, long newtime);
)XQF LDpermite citirea (cmd=0), sau modificarea (cmd=1) timpului sistem, exprimat în incrementi de 55ms, de la pornirea sistemului.
111
Introducere in programare
'H]YROWDUHDGHFRGvQWUXQOLPEDMPL[W&úL$60
(VWH VSHFLILF PHGLXOXL GH GH]YROWDUH %RUODQG & FDUH RIHU SRVLELOLWDWHD LQVHUULL GH FRG$60vQSURJUDPHVXUV&)LHFDUHLQVWUXF LXQH$60WUHEXLHSUHFHGDWGHFXYkQWXO cheie DVPVDXWUHEXLHLQFOXVvQWUXQEORFGHWLS$60GHIRUPD asm { LQVWUXF LXQL$60 } Acest model de dezvoltare este foarte comod, deoarece vizibilitatea obiectelor definite în &VHH[WLQGHúLDVXSUDEORFXULORU$60$VWIHOSXWHPIRORVLQXPHGHSDUDPHWULIRUPDOLDL IXQF LLORUHWF IU D PDL VFULH FDUDFWHUXO ³B´ vQ ID D QXPHORU UHVSHFWLYH 6H HYLW DVWIHO VHFYHQ HOHGHLQWUDUHúLUHYHQLUHGLQIXQF LLFDUHVXQWUHDOL]DWHDXWRPDWGHFRPSLODWRUXO& ([LVWUHVWULF LLGHXWLOL]DUHDHWLFKHWHORU0DLSUHFLVHWLFKHWHOHWUHEXLHGHILQLWHFD etichete C, deci în afara blocurilor ASM. &D úL OD GH]YROWDUHD SH PRGXOH VHSDUDWH SURJUDPDWRUXO WUHEXLH V JHVWLRQH]H explicit modelele de memorie, în sesnsul definirii precise a adreselor far sau near. Pentru DFHDVWDVHXWLOL]HD]PRGHOHOHGHPHPRULH&úLVDXFXYLQWHOHFKHLHQHDUúLIDU 3HQWUX H[HPSOLILFDUH V GLVFXWP LPSOHPHQWDUHD XQHL IXQF LL GH FXWDUH IRORVLQG DWkWPHWRGDFODVLFFkWúLPHWRGDFXFRGGHDVDPEODUH )XQF LDUHVSHFWLYVFULVvQ&HVWHSUH]HQWDWPDLMRV int my_search(int *x, size_t n, int y) { int i; for(i=0;i
112
et: asm { add si,2 cmp [si], dx loopnz et jnz nu mov ax,n sub ax,cx dec ax jmp m_end }
Introducere in programare
} nu: asm mov ax,-1 m_end: asm { pop dx pop cx pop si } return _AX } 6H REVHUY F LQGLFHOH JVLW D IRVW FDOFXODW FD GLIHUHQ GLQWUH GLPHQVLXQHD Q D WDEORXOXL úL YDORDUHD LQFUHPHQWDW D FRQWRUXOXL &[ OD LHúLUHD GLQ EXFO 5HYHQLUHD GLQ EXFO VH IDFH IRORVLQG YDORDUHD VWRFDW vQ UHJLVWUXO $; OD FDUH DP DFFHV SULQ preudoregistrul _AX. 0HWRGD DUH GH]DYDQWDMXO VFGHULL SRUWDELOLW LL OD recompilarea sub alte compilatoare.
12.11 Tratarea dispozitivelor bus asincrone 6 SUHVXSXQHP F VH GH]YROWD XQ FRG SHQWUX FRQWUROXO XQXL GLVSR]LWLY KDUGZDUH XQ driver de exemplu). Acest lucru este realizat prin depunerea de diverse valori în registre hard la adrese absolute ( existente fizic independent de modul virtual de manipulare a PHPRULHLSXVODGLVSR]L LHGH62 6SUHVXSXQHPFDFHVWGLVSR]LWLYDUHUHJLVWUHILHFDUHfiecare având lungimea de 16 EL LODDGUHVHGHPHPRULHFUHVFWRDUH 0HWRGDFODVLFGHDFFHVDUHHVWHFDPDLMRV: struct devregs { unsigned short csr // control si stare unsigned short data // portul de date }
113
#define ER 0x1 #define RDY 0x2 #define RES 0x4
Introducere in programare
// adresa absoluta a dispozitivului #define DEVADR (( struct dev regs*)ox ffff0000 // QXPUXOGHGLVSR]LWLYHGHDFHVWWLSGLQVLVWHP #define NDEVS 4 // RIXQF LHFHFLWHúWHXQRFWHWGLQGLVSR]LWLYXOQIRORVLQGSURWRFROGHWLSbusy/wait. $úWHDSWpâna când apare semnal de RDY (ready) sau ER(error); dacQX HVWHHURDUHúL un UHDG\ DFWLY vQVHDPQ F SRW FLWL XQ RFWHW DOWIHO YD DQXOD HURDUHD GDU YRL vQWRDUFH 0xffff. unsigned int read_dev ( unsigned devno) { unsigned devregs *dvp=DEVADR + devno; if ( devno >= NDEVS ) return (0xffff) QXH[LVWXQDVHPHQHDGLVSR]LWLYvQORW while (( dvp -> csr& (RDY | ER )) == 0); // EXFODYHFWRUFDUHDúWHDSWSULPDXQDGLQFRQGL LLHVWHYDOLGDW if dvp -> csr & ER // a fot eroare { dvp -> csr= RES // reset return 0xffff; } return (dvp-> data) & 0xffff; // nu DIRVWHURDUHúLGLVSR]LWLYXOHJDWDVHPLWRFWHWXO }
114
CAPITOLUL 13
Introducere in programare
13. Preprocesorul 2ULFHSURJUDP&vQID]DGHFRPSLODUHWUHFHvQWkLSULQWURSUHSURFHVDUHúLDELDDSRL VH YD JHQHUD FRGXO HFKLYDOHQW vQ DVDPEODUH 3UHSURFHVRUXO GXS FXP vL VSXQH úL QXPHOH IDFHSULPDSURFHVDUHDVXSUDVXUVHL&FRQIRUPGLUHFWLYHORULQFOXVHGLUHFWVDXLQGLUHFWSULQ LQFOXGHUHGHELOLRWHFLGHFWUHSURJUDPDWRU 'LUHFWLYHOHSUHSURFHVRUXOXLVXQWOLQLLVXUVDOFURUSULPFDUDFWHUGLIHULWGHVSD LLOH DOEHHVWHGLH] /LQLLOHFDUHFRQ LQGLUHFWLYHDOHSUHSURFHVRUXOXLVXQWHOLPLQDWHGLQWH[WXO VXUVvQDLQWHFDDFHVWDVILHFLWLWGHFRPSLODWRU3UHSURFHVRUXOvQORFXLHúWHRULFHVHFYHQ GH VSD LLDOEHLQFOXVLYFRPHQWDULL FXXQVLQJXUVSD LX 'LUHFWLYDQXO 2 OLQLH FDUH FRQ LQH QXPDL FDUDFWHUXO vQ DIDU GH HYHQWXDOHOH VSD LL DOEH HVWH LJQRUDWGHSUHSURFHVRU 13.2. Directivele #GHILQHúLundef GHILQHLGHQWLILFDWRU>>OLVWDBDUJXPHQWH @FRUS@ XQGHILGHQWLILFDWRU 'LUHFWLYD GHILQH GHILQHúWH R PDFURLQVWUXF LXQH 'DF PDFURLQVWUXF LXQHD DUH DUJXPHQWH SDUDQWH]D VWkQJ WUHEXLH V DSDU vQ GHILQL LH OLSLW GH QXPHOH GHILQLW $UJXPHQWHOH VH VHSDU SULQ YLUJXO &RUSXO PDFURGHILQL LHL QX SRDWH DYHD PDL PXOW GH R OLQLHVXUVVHSRDWHFRQWLQXDOLQLDFX? ÌQID]DGHSUHSURFHVDUHDSDUL LDQXPHOXLGHPDFURvQWH[WXOVXUVHYHQWXDOXUPDWGH R OLVW GH DUJXPHQWH DFWXDOH LQ SDUDQWH]H HVWH vQORFXLW FX FRUSXO GH PDFUR vQ FDUH SDUDPHWULLIRUPDOLDXIRVWvQORFXL LFXWH[WXOSDUDPHWULORUDFWXDOL 'XS VXEVWLWX LH WH[WXO RE LQXW HVWH FLWLW GLQ QRX SkQ FkQG QX PDL VXQW QHFHVDUH VXEVWLWX LLGLUHFWLYHOHSUHSURFHVRUXOXLQXVXQWUHFXQRVFXWHvQWH[WXOSURGXVSULQH[SDQGDUHD XQHL PDFURLQVWUXF LXQL 1XPHOH GH PDFUR QX VXQW UHFXQRVFXWH vQ úLUXUL GH FDUDFWHUH VDX FRPHQWDULL2PDFURLQVWUXF LXQHQXVHSRDWHDSHODUHFXUVLY /DDSHOXOXQHLPDFURFXDUJXPHQWHQXPDLYLUJXOHOHGHSHSULPXOQLYHOVXQWOXDWHFD VHSDUDWRDUH GH DUJXPHQWH QX úL FHOH FDUH DSDU vQ SDUDQWH]H VDX vQWUH DSRVWURIH VDX JKLOLPHOH ÌQ FRUSXO XQXL PDFUR VH SRW IRORVL XUPWRULL RSHUDWRUL FDUH QX DF LRQHD] GHFkW vQ ID]DGHSUHSURFHVDUH
FDUHGLVSDUHWRWDOQHUPkQvQGQLFLPFDUXQVSD LXúL FDUH SODVDW LQ ID D XQXL DUJXPHQW IRUPDO vO vQORFXLHúWH FX DUJXPHQWXO DFWXDO 3/$6$7Ì1*+,/,0(/(
'LUHFWLYDXQGHIúWHUJHRPDFURGHILQL LHGLQWDEHODGHPDFURGHILQL LL
115
13.3. Directiva #include
Introducere in programare
#include #include "nume_fisier" #include nume_macro 1XPHOH GH PDFUR SkQ OD XUP WUHEXLH V VH H[SDQGH]H vQ FHYD GH IRUPD QXPHBILVLHU! VDX QXPHBILVLHU (IHFWXO GLUHFWLYHL FRQVW GLQ LQVHUDUHD vQ XQLWDWHD GH FRPSLODUHvQORFXOGLUHFWLYHLLQFOXGHDWH[WXOXLGLQILúLHUXOUHVSHFWLY7H[WXOLQVHUDWSRDWH FRQ LQHúLHOGLUHFWLYHGHSUHSURFHVDUHLQFOXVLYGLUHFWLYDLQFOXGH 'DFvQQXPHOHGHILúLHUDSDUHúLRFDOHGHDFFHVVHYDFXWDQXPDLDFROR$OWIHO GDFVDIRORVLWIRUPDFXJKLOLPHOHVHFDXWvQGLUHFWRUXOFXUHQW8UPHD]GDFWRWQXVD ORFDOL]DWILúLHUXOVVHFDXWHSHUkQGvQGLUHFWRDUHOHGLQOLVWDGDWODLQVWDODUHVDXSHOLQLDGH FRPDQGVDXvQPHGLXOLQWHJUDW 'LUHFWLYHGHFRPSLODUHFRQGL LRQDW "#" "LIH[SUHVLHBFRQVWDQWB [ text_1 ] [ "#" "HOLIH[SUHVLHBFRQVWDQWB text_2 ]... [ "#" "else" text_else ] "#" "endif" 'DF H[SUHVLDBQXHVWHQXO DWXQFLVHFRPSLOHD]WH[WBDOWIHO GDF H[SUHVLDB QXHVWHQXODWXQFLVHFRPSLOHD]WH[WBHWFDOWIHOGDFHVWHSUH]HQWGLUHFWLYDHOVHVH FRPSLOHD]WH[WBHOVH'LUHFWLYHOHGHFRPSLODUHFRQGL LRQDWSRWILvPEULFDWH ([SUHVLDFRQGL LRQDOSRDWHFRQ LQHRSHUDWRUXOGHSUHSURFHVDUHGHILQHG$FHVWDDUH XQDUJXPHQWXQQXPHGHPDFURHYHQWXDOvQSDUDQWH]H9DORDUHDH[SUHVLHLGHILQHGQXPH HVWHGDFDQXPHOHHVWHGHILQLWFXGHILQHGVDXGDFQXH[LVWRPDFURFXDFHOQXPH8Q LGHQWLILFDWRUGHILQLWFXXQFRUSGHPDFURYLGVHFRQVLGHUGHILQLW #LIGHIQXPHHFKLYDOHQWFXif defined (nume) #LIQGHIQXPHHFKLYDOHQWFXif !defined (nume) 13.5. Directiva #line #line QXPUBOLQLH>nume_fisier"] (IHFWXOGLUHFWLYHLFRQVWvQDFHHDFvQILúLHUXOSURGXVGH&SUHSURFHVRUXO&RIHULW FDXWLOLWDU VDXLQPHVDMHOHGHHURDUHHWFOLQLLOHFDUHXUPHD]GXSOLQHYRUILUHIHULWHFDúL FXPDUvQFHSHFXOLQLDQXPUXOQXPUBOLQLHúLDUYHQLGLQILúLHUXOQXPHBILúLHU
116
Introducere in programare
'H RELFHL GLUHFWLYD OLQH QX DSDUH vQ SURJUDPHOH VFULVH FX PkQD FL PDL DOHV vQ SURJUDPHOHJHQHUDWHDXWRPDW'DFQXPHOHGHILúLHUOLSVHúWHHOUPkQHFHOYDODELOvQDFHO PRPHQW ÌQDUJXPHQWHOHGLUHFWLYHLOLQHPDFURLQVWUXF LXQLOHVXQWH[SDQGDWHFDúLODGLUHFWLYD LQFOXGH 13.6. Directiva #error #error mesaj (IHFWXOHLFRQVWvQJHQHUDUHDXQXLPHVDMGHHURDUHGHIRUPD (UURUQXPHBILúLHUOLQHQXPUBOLQLH(UURUGLUHFWLYHPHVDM
&RPSLODUHD XQHL GLUHFWLYH HUURU RSUHúWH FRPSLODUHD 'H RELFHL DSDUH vQ ILúLHUHOH DQWHW LQFOXVHFXGLUHFWLYDLQFOXGH vQFDGUXOXQHLVHFYHQ HFRPSLODWHFRQGL LRQDW'HH[HPSOX #if !defined ( __cplusplus ) #error Nu se poate compila decit in C++ #endif 13.7. Directivele #pragma #pragma argsused 3RDWH DSDUH QXPDL vQ DIDUD XQHL IXQF LL $UH FD HIHFW VXSULPDUHD PHVDMXOXL GH DYHUWLVPHQW UHIHULWRU OD QHXWLOL]DUHD XQXL SDUDPHWUX IRUPDO GDU QXPDL vQ SULPD IXQF LH FRPSLODWGXSDSDUL LDGLUHFWLYHL #pragma exit nume_functie [prioritate] #pragma startup nume_functie [prioritate] ,QVHUHD] IXQF LLOH FLWDWH GHILQLWH FD YRLG QXPHBIXQFWLH YRLG SHQWUX DSHO vQDLQWH GH PDLQ UHVSHFWLY GXS WHUPLQDUHD IXQF LHL PDLQ 3ULRULWDWHD HVWH XQ vQWUHJ GH OD OD )XQF LLOH FX SULRULW L PDL PLFL YRU IL DSHODWH PDL GHYUHPH OD ODQVDUH úL PDL WkU]LX OD WHUPLQDUHDH[HFX LHL3ULRULW LOHGHODODVXQWUH]HUYDWHSHQWUXUXWLQHOHGLQELEOLRWHFLOH 7XUER 'DF SULRULWDWHD OLSVHúWH VH LD )XQF LLOH FLWDWH WUHEXLH GHFODUDWH vQDLQWHD DSDUL LHLGLUHFWLYHL SUDJPDLQOLQH $QXQ FRPSLODWRUXO F vQ SURJUDP H[LVW FRG LQOLQH vQ OLPEDM GH DVDPEODUH (VWH HFKLYDOHQW FX RS LXQHD GH FRPSLODUH % ÌQWkOQLUHD GLUHFWLYHL SUDJPD LQOLQH GDF RS LXQHD%QXHVWHDFWLYDUHFDHIHFWUHODQVDUHDFRPSLOULLFXRS LXQHD%DFWLYDW #pragma option
117
Introducere in programare
#pragma option optiune [,optiune]...
2S LXQL GH FRPSLODUH FDUH SRW DSDUH vQ SUDJPD RSWLRQ 180$, Ì1$,17( '( )2/26,5($ 818, 180( '( 0$&52 &$5( Ì1&(3( &8 '28 /,1,8( '( 68%/,1,(5(6$8Ì1$,17('($3$5,,$35,08/8,$720/(;,&$/& (QXPHBDVDPEORU IRS LXQHBIORWDQW LQXPUBFDUDFWHUHBVHPQLILFDWLYH PPRGHOBPHPRULH QGLUHFWRUBLHúLUH RQXPHBILVLHUBLHúLUH XJHQHUDUHGHOLQLX HGHVXEOLQLHUHSHQWUXJOREDOL ]QXPHBVHJPHQW 2S LXQLFDUHSRWDSDUHQXPDLGDFDSDUvQDIDUDRULFURUGHFODUD LLGHRELHFWHVDXIXQF LL LQVWUXF LXQL LQVWUXF LXQL DFRQWUROXODOLQLHULL IIFRQWURO³UDSLG´DORSHUD LLORUvQYLUJXODPRELO *RSWLPL]DUHSHQWUXYLWH] NFRQWUROQRUPDODOVWLYHL 1FRQWUROXOVWLYHL 2FRQWUROXORSWLPL]ULL SVHFYHQ GHDSHO3DVFDO UDORFDUHUHJLVWUH YFRQWUROSHQWUXGHSDQDUH \FRQWURODVXSUDQXPHUHORUGHOLQLLvQFRGXORELHFW 2S LXQLFDUHSRWILGDWHRULXQGHVLDXHIHFWLPHGLDW $VHWXOGHFXYLQWHFKHLH &FRPHQWDULLvPEULFDWH GFRQWRSLUHDúLUXULORUGXSOLFDWH JQXPURSULUHGXSQXPUDYHUWLVPHQWH MQXPURSULUHGXSQXPUHURUL .WLSXOFKDUHVWHIUVHPQ #pragma warn #pragma warn +xxx $YHUWLVPHQWHOH[[[VXQWDFWLYDWH #pragma warn -xxx $YHUWLVPHQWHOH[[[VXQWLQDFWLYDWH 'XSFXPDPY]XWSUHSURFHVRUXOSHUPLWHúLGHFODUDUHDDQXPLWRU]RQHGHFRGDVWIHOvQFkW HOHVDILHFRPSLODWHvQDQXPLWHFRQGL LLFXPDUIL Î QXVXQWGHILQLWHDQXPLWHFRQVWDQWHVDXYDULDELOHORFDOH Î FRGXOHVWHXWLOGDUSHQWUXGHSDQDUH
118
Introducere in programare
Î GRUHVFVLPSLHGLFIRORVLUHDXQRUIXQF LLGLQELEOLRWHFvQWUXFkWVXQWUHVFULVHGH RULFLQHVXSUDvQFUFDUHDIXQF LLORU 6LQWD[DJHQHUDOD
([HPSOX SHQWUXKHDGHUHSHUVRQDOHV]LFHPILúLHUXOWHVWK #ifndef __TEST #define __TEST ....... corpul ....... #endif Sau pentru depanare #if DEBUG printf(“.......”); #endif Sau daca nu am definite ni;te valori speciale #ifndef M_PI #define M_PI 3.1415.... #endif.
119
Introducere in programare
SUH]HQWDUH D UHJXOLORU GH ED]
6FXUW
GH FDUH WUHEXLH LQXW FRQW OD SURLHFWDUHD XQHL DSSOLFD LL
6WLO GH SURJUDPDUH QHUHFRPDQGDW
6WLO UHFRPDQGDW
3XQHUHD GHFODUD LLORU FX H[WHUQ vQ ILúLHUXOF
H[WHUQ
5HIHULULOH OD IXQF LHYDULDELO 6H SXQ vQ ILúLHUHOH KHDGHUK
$FFHVXO FRPXQ OD GDWH vQWUH IXQF LL UHDOL]DW SULQ
$FFHVXO FRPXQ VH IDFH SULQ WUDQVIHU GH DUJXPHQW
YDULDELOH JOREDOH
SULQ GHFODUDUH GH WLS VWDWLF
)RORVLUHD GH GDWH JOREDOH F QG QX H FD]XO
6H UH]ROY
&UHDUHD D QXPHURDVH IXQF LL FX VFRSXUL VSHFLDOH
&UHHDUHD XQRU SULPLWLYH JHQHUDOH
IU
8WLOL]DUHD QXPHORU GH ILúLHU VDX YDULDELO
FX FRQWH[WXO
)RORVLUHD GH QXPH FDUH VH DSURSLH GH SVHXGRFRG
OHJ WXU
)RORVLUHD FRQVWDQWH QXPHULFH
6H GHILQHVF QLúWH QXPH SW DFHOH YDORUL
)RORVLUHD JRWR
)RORVLUHD VWUXFWXULORU GH FRQWURO D IOX[XULORU
6FULUHD GH FRG UHGXQGDQW
6H IRORVHVF IXQF LL vQ FD]XO vQ FDUH VH REVHUY DFHODúL OXFUX
120
Introducere in programare
// calculul randamentului si puterii pentru un fir conectat la un generator de cc #include <stdio.h> #include #include <math.h> void main(void) { // incepe zona declarare variabile int e,ri,l,s,imax; float r,pext,eta,i,g; //sfirsit zona declarare variabile // incepe zona citire date de intrare clrscr(); printf("\nTensiunea gen. E="); scanf("%d",&e); printf("\nRez. interna gen. ri="); scanf("%d",&ri); printf("\nLungimea fir L="); scanf("%d",&l); printf("\nSectiunea fir S="); scanf("%d",&s); printf("\nCurentul maxim admis Imax="); scanf("%d",&imax); printf("\nConductivitatea gama="); scanf("%f",&g); // sfirsit zona citire date de intrare //incepe zona de calcul conform algoritmului r=l/(g*s); i=e/(r+ri); pext=r*sqrt(i); eta=pext/(e*i); // sfirsit zona de calcul conform algoritmului //incepe zona afisarii rezultatelor printf("\nPext=%3.8f",pext); printf("\nRandament=%3.8f",eta); if(iImax",i); //sfirsit zona afisarii rezultatelor printf("\nApasati orice tasta ca sa terminati programul"); getch(); }
121
Introducere in programare
/*********************************************************************** ************************************************************************ ***************************/ //exemplu de calcul a unei functii cu mai multe ramuri; #include <stdio.h> #include void main(void) { int x,f; clrscr(); printf("x="); scanf("%d",&x); if(x<-5)f=x-1; else if(x==-5)f=-6; else f=2*x+4; printf("\nf(x)=%d",f); getch(); } /*********************************************************************** ************************************************************************ ***************************/ // repetarea de un numar specificat de ori a calcului // pentr o functie_multipla; #include <stdio.h> #include void main(void) { int x,n,f,i; begin clrscr; printf("Numarul de repetari a calculului functiei f n="); scanf("%d",&n); for(i:=0;i
122
if(x<-5)f=x-1; else if(x==-5)f=-6; else f=2*x+4;
Introducere in programare
printf("\nf(x)=%d",f); } getch(); } /*********************************************************************** ************************************************************************ ***************************/ // exemplu de lucru cu sirurile de caractere #include <stdio.h> #include #include <string.h> void main(void) { char s[10],a,c; int i,b,d; clrscr(); printf("Introduceti sirul de prelucrat s="); gets(s); a=s[0]; b=strlen(s); c=s[b-1]; if ((a>='A')&&(a <='Z')) { s[0]+=32; s[b-1]+=32; } else { s[0]-=32; s[b-1]-=32; } printf("\nprima litera %c",a); printf("\nultima litera %c",c); printf("\nsirul prelucrat %s",s); getch();
123
Introducere in programare
//obs programul nu ia in calcul situatia in care in sir de litere apar si cifre } /*********************************************************************** ************************************************************************ ***************************/ // program care implementeaza functia de stergere a unui caracter #include <stdio.h> #include #include <string.h> int strdel(char s[], int pos); void main(void) { char s[20]; int n; clrscr(); printf("Dati sirul s="); gets(s); printf("dati pozitia de eliminare "); scanf("%d",&n); if(strdel(s,n))puts("Eroare, pozitia este in afara sirului "); else printf("\n Noul sir este=%s",s); getch(); } int strdel(char s[], int pos) { if(pos>=strlen(s))return -1;// eroare de apelare pos--; while(s[pos]!='\0') { s[pos]=s[pos+1]; pos++; } return 0;// functia s-a terminat corect } /*********************************************************************** ************************************************************************ ***************************/ // program care implementeaza functia de stergere a unui caracter // existenta in pascal #include <stdio.h> #include #include <string.h> int strdelpas(char s[], int pos, int nrcars); void main(void)
124
Introducere in programare
{ char s[20]; int n,nrc; clrscr(); printf("Dati sirul s="); gets(s); printf("dati pozitia de eliminare "); scanf("%d",&n); printf("dati nr de caractere ce se vor sterge "); scanf("%d",&nrc);
if(strdelpas(s,n,nrc))puts("Eroare, pozitia este in afara sirului"); else printf("\n Noul sir este=%s",s); getch(); } int strdelpas(char s[], int pos, int nrcars) { if(pos>=strlen(s))return -1;// eroare de apelare pos--; while(s[pos]!='\0') { s[pos]=s[pos+nrcars]; pos++; } return 0;// functia s-a terminat corect } /*********************************************************************** ************************************************************************ ***************************/ //program pentru eliminarea caracterelor indentice; #include <stdio.h> #include #include <string.h> void main(void) { char s[25]; int i=0,j,n,pos; clrscr(); printf("Introduceti sirul de prelucrat s="); scanf("%s",s); n=strlen(s); do {
125
Introducere in programare
j=i+1; do {
if(s[i]==s[j]) { pos=j; while(s[pos]!='\0') { s[pos]=s[pos+1]; pos++; }
n--; } else j++; }while (j #include #include <string.h> int strdel(char s[], int pos); void main(void) { char s[25]; int i=0,j,n,pos; clrscr(); printf("Introduceti sirul de prelucrat s="); scanf("%s",s); n=strlen(s); do {
126
j=i+1; do {
Introducere in programare
if(s[i]==s[j]) { strdel(s,j); n--; } else j++; }while (j=strlen(s))return -1;// eroare de apelare while(s[pos]!='\0') { s[pos]=s[pos+1]; pos++; } return 0;// functia s-a terminat corect } /*********************************************************************** ************************************************************************ ***************************/ // program pentru ordonarea unui vector // algoritm FOARTE lent pentru valori mari ale lui n #include <stdio.h> #include void main(void) { float v[100],temp; int i,n,j; clrscr(); printf("Introduceti dimensiunea vectorului n="); scanf("%d",&n); for(i=0;i
127
Introducere in programare
{ printf("V[%d]=",i); scanf("%f",&v[i]); } for(i=0;iv[j]) { temp=v[i]; v[i]=v[j]; v[j]=temp; }; puts("Vectorul ordonat"); for(i=0;i #include void main(void) { int n,m,i,j,k; float a[10][10], b[100]; clrscr(); printf("n="); scanf("%d",&n); printf("m="); scanf("%d",&m); while ((n<0)||(n>10)||(m<0)||(m>10)) { puts("0<m,n<10"); printf("n="); scanf("%d",&n); printf("m="); scanf("%d",&m); } for(i=0;i
128
Introducere in programare
for(j=0;j<m;j++) { printf("m[%d,%d]=",i,j); scanf("%f",&a[i][j]); };
k=0; for(i=0;i1) b[k++]=a[i][j]; if(!k)puts("\nMatricea nu are elemente > 1"); else { puts("vectorul elem. >1 este:"); for(i=0;i #include void main(void) { int m,n,p,q,i,j,k; float a[10][10], b[10][10],c[10][10]; clrscr(); printf("m="); scanf("%d",&m); printf("n="); scanf("%d",&n); printf("p="); scanf("%d",&p); printf("q="); scanf("%d",&q); while (n!=p) { puts("Date eronate, n trebuie sa fie egal cu p"); printf("m="); scanf("%d",&m);
129
printf("n="); scanf("%d",&n); printf("p="); scanf("%d",&p); printf("q="); scanf("%d",&q);
Introducere in programare
} for(i=0;i<m;i++) for(j=0;j #include
130
void main(void) { int n,m,i,j,k; float a[10][10], b[10],s;
Introducere in programare
clrscr(); printf("n="); scanf("%d",&n); printf("m="); scanf("%d",&m); for(i=0;i
for(j=0;j<m;j++) { s=0; for(i=0;i #include #include <math.h>
131
#include
Introducere in programare
float rezolv(float x, float y); void main(void) { float a,b,x; clrscr(); printf("a="); scanf("%f",&a); printf("b="); scanf("%f",&b); x=rezolv(a,b); printf("\nSolutia este %8.3f",x); getch(); } float rezolv(float x, float y) { float fx,fy,fc,c,eps; fx=x*sqrt(x)+5*sqrt(x)+2*x-7; fy=y*sqrt(y)+5*sqrt(y)+2*y-7; if(fx*fy<0) return MAXDOUBLE; // semnalez o eroare de calcul do { c=(x+y)/2; fc=c*sqrt(c)+5*sqrt(c)+2*c-7; if (fx*fc>0) { fx=fc; x=c; } else y=c; eps=abs(x-y); }while(eps>0.001); return c; } /* program care realizeaza sumarea a doi vectori de aceeasi dimensiune, versiune cu for */ #include <stdio.h> #include
132
void main(void) { int i,n; int x[20],y[20],z[20];
Introducere in programare
clrscr(); printf("n="); scanf("%d",&n); for(i=0;i #include void main(void) { int i,n; int x[20],y[20],z[20];
133
clrscr(); printf("n="); scanf("%d",&n);
Introducere in programare
i=0; while(i #include
134
void main(void) { int i,n; int x[20],y[20],z[20];
Introducere in programare
clrscr(); printf("n="); scanf("%d",&n); i=0; do { printf("x[%d]=",i); scanf("%d",&x[i]); }while(i++ #include void main(void) {
135
int i,n; int x[20],y[20],z[20];
Introducere in programare
clrscr(); printf("n="); scanf("%d",&n); for(i=0;i #include void main(void) { int i,n,s=0; int x[20],y[20]; clrscr(); printf("n=");
136
scanf("%d",&n);
Introducere in programare
for(i=0;i #include void main(void) { int i,j,n,t; int x[20]; clrscr(); printf("n="); scanf("%d",&n); for(i=0;i
137
Introducere in programare
for(i=0;i #include void main(void) { int i,j,n,t; int x[20]; clrscr(); printf("n="); scanf("%d",&n); for(i=0;i
138
Introducere in programare
for(j=i+1;jx[j]) { t=x[i]; x[i]=x[j]; x[j]=t; } } } printf("Vectorul ordonat crescator este: \n"); for(i=0;i #include void main(void) { int i,n,m,dim; int x[20],y[20],z[20]; clrscr(); printf("n="); scanf("%d",&n); printf("m="); scanf("%d",&m); for(i=0;i
139
Introducere in programare
for(i=0;i<m;i++) { printf("y[%d]=",i); scanf("%d",&y[i]); } if(n<m) { for(i=0;i<m;i++) { if(i
for(i=0;i #include void main(void) { int i,n,m,dim; int x[20],y[20],z[20];
140
clrscr(); printf("n="); scanf("%d",&n); printf("m="); scanf("%d",&m);
Introducere in programare
for(i=0;i
141
Introducere in programare
/* program care realizeaza cautarea unei valori intr-un vector, si anunta prima pozitie pe care este gasita valoarea */ #include <stdio.h> #include void main(void) { int i,n,t,val,pos=-1; int x[20]; clrscr(); printf("n="); scanf("%d",&n); for(i=0;i0)printf("El se gaseste in pozitia a %d a",pos+1); else puts("Nu s-a gasit in vector"); getch(); }
142
Seturi de teme in programare Introducere SET1 1. Scrie LXQSURJUDPFDUHDILúHD]XQPHVDM 2. 6FULH LRIXQF LHFDUHvQWRDUFHDUJXPHQWXOODSXWHUHDDWUHLDFXSURWRWLSXO int pw3 ( int num ). X= pw3 (y) <-> x = y 3 . 3. Scrie LRIXQF LHFHFDOFXOHD] x = y 4 IRORVLQGIXQF LDsqrt(double x) 4. Scrie LXQSURJUDPFDUHFLWHúWHGHODWDVWDWXUXQvQWUHJúL-l afiúHD]ODSXWHUHDD treia prin folosirea pw3. 5. 6FULH LvQSVHXGRFRGXQSURJUDPFDUHHOLPLQFRPHQWDULLOHdintr-o surs& 6. (OLPLQD LHURULOHGLQXUPWRDUHDVHFYHQ : main (x) { VFDQI´&k LJQGDFLVXQW"“ , nr_gQGDFL); printf ( “ Acest program are %d probleme “, nr_gQGDFL ú } SET2 1. Atunci câQGVHDILúHD]RYDULDELOGHWLSfloat sau double folosind specificatorul de format %f cWHFLIUH]HFLPDOHVXQWDILúate ? printf() realizeazURWXQMLUHDVDXWUXQFKLHUHDYDORULLDILúDWH 2. 6FULH LXQSURJUDPFHFRQ LQHXUPWRDUHOHGHFODUD LL: char c; int j; float f; úLDILúHD]DGUHVDILHFUHLYDULDELOH9RUEL LGHVSUHPRGXOvQFDUHFRPSLODWRUXO DORFPHPRULHSHQWUXYDULDELOH 3. 6FULH LHFKLYDOHQWXORFWDOKH[D]HFLPDOSHQWUXXUPWRDUHOHQXPHUHELQDUH: 1. 0001.0010 2. 0110.0101 3. 0110.1011 4. 1011.1011 5. 0011.1111 6. 0101.1111 7. 0000.0010.0110.0100 în ambele cazuri FQGVHFRQVLGHUFQXPHUHOHVXQWGDWHvQ a) FRPSOHPHQWID GH b) FRPSOHPHQWID GH 4. 6FULH LGHFODUD LLOHSHQWUXXUPWRDUHOHIRUPDWH: a) ÌQWUHJOXQJIUVHPQ b) 9DULDELOvQYLUJXOPRELOFXGXEOSUHFL]LH c) Pointer la caracter d) &DUDFWHULQL LDOL]DWFX‘x’ e) Func ie H[WHUQce întoarce un întreg IUsemn
143
Introducere in programare
5. 6FULH LXUPWRDUHOHQXPHUHvQELQDUvQFRPSOHPHQWID GHVDXvQFRPSOHPHQW ID GH: 1, -1, 255, 256, 511, 512, 513, 128, -128, 0xFF, 0x7F.
SET3 1. )LHFDUHFDOFXODWRUHVWHOLPLWDWvQSUHFL]LDPD[LPFDUHRSRDWHIRORVLvQ UHSUH]HQWDUHDQXPHUHORUvQYLUJXOPRELO'LQDFHVWPRWLYSHQWUXXQHSVLORQ VXILFLHQWGHPLFXUPWRDUHDH[SUHVLHYDILDGHYUDW 1.0 = 1.0 + eps SVHVFULHXQSURJUDPFDUHJVHúWHFHDPDLPDUHYDORDUHSHQWUXeps pe computerul pe care e executat. Obs.: aceastYDORDUHGLIHUvQWUHIORDWúLGRXEOH6HYDIRORVLSHQWUXWHVWHúLQX SHQWUXFPDMRULWDWHDFDOFXODWRDUHORUQXDXLQVWUXPHQWHGHOXFUXVSHFLILFHSHQWUX manipularea aritmeticii lui 0. 2. 6VHUHVFULHXUPWRUXOSURJUDPIUDVHIRORVLLQVWUXF LXQLOHEUHDNFRQWLQXHúL go to. SURJUDPXOQXPUFDUDFWHUXO‘a’ din intrare #include <stdio.h> #include void main (void) { int nr_a=0; char c; c=getch(); while(1) { if(c== ’\n’) break if (isdigit(c)) continue if (c==’a’) goto add_a; get_ch: c=getchar(); goto end; add_a: nr_a++; goto get_ch; end: ; } } 3. 6FULH LRIXQF LHFDUHDFFHSWXQvQWUHJODLQWUDUHúLVFULHXQQXPUGHVSD LLHJDO FXSDVGHLQWUDUH)RORVLQGDFHHDúLIXQF LHVVHVFULHXQSURJUDPFDUHFLWHúWH FDUDFWHUHGHODLQWUDUHDVWDQGDUGúLOHDILúHD]GDUvQORFXLHúWHWDEXULOHFXVSD LL 4. 0XOWHSURJUDPHFDUHQHFHVLWWUDQVIHUGHFDUDFWHUHGLQWUXQORFvQDOWXOIRORVHVFXQ mecanism de FKHFNVXPVXPGHFRQWURO SWDYHULILFDFRUHFWLWXGLQHD WUDQVIHUXOXL$FHDVWDVHUHDOL]HD]SULQVXPDUHDYDORULORUWXWXURUFDUDFWHUHORU WUDQVPLVHúLWULPLWHUHDHLvPSUHXQFXúLUXOUHVSHFWLY'DFVXPDVHSRWULYHúWH H[LVWRRDUHFDUHSUREDELOLWDWHFDHOHVILIRVWWUDQVIHUDWHFRUHFW6FULH LXQ SURJUDPFDUHVIRORVHDVFRIXQF LH
144
Introducere in programare
unsigned int CS (char *s)
care Vcalculeze checksum-ul pentru o linie LQWURGXVGHODWDVWDWXU 5. 6FULH Lun program care FODVLILFcaracterele citite într-una din XUPWRDUHOH categorii: white _Space (space, ‘\n’, ‘\r’, ‘\t’) punctuator ( “,!;:()?) alfabet (a-z , A-Z) numeric (0-9) uncknown (oricare alt caracter) Deci la orice WDVWDWLQVVDSDUpe ecran codul tastei úLclasa din care face parte. 5.1. 3URJUDPXOVILHUH]ROYDWQXPDLFXif, HOVHúLreturn 5.2. 3URJUDPXOVILHUH]ROYDWQXPDLFXVZLWFKúLreturn. 6. 6FULH LXQSURJUDPFDUHDILúHD]OLWHUHOHPLFLúLPDULSUHFXPúLYDORULOHORU zecimale. SET 4 1. ,QWURGXFH Lparanteze în expresiile de mai jos DvVfie specificat (cu ajutorul lor ) explicit modul de evaluare implicit folosit de compilator.(Se va folosi tabela de preceden a) a=b*c==2 b) a=f(x)&&a>100 c) a==b&&x!=y d) a=b+=2+f(2) e) a=b>>2+4 f) a=s.f+y.x g) a=b&&a>z?x=y:z h) a=*++p*p i) a=b^c&d 2. 6 se construiasca o expresie ce va genera un long int (4 octe L) prin apelul repetat al func LHLgetbt() care întoarce un singur octet. Ordinea de aranjare este ca primul apel al lui getbt() va genera octetul cel mai semnificativ din întreg (nr=Expresie (getbt()). 3. Care este ieúLUHDXUPtorului program ? void main(void) { short i=0; printf (“y.d\n”,(i+1)*(1=1)); } 4. 6VHVFULHRIXQF LHunsigned long int circular_shift (unsigned long int a,int n); care face deplasare stanga cu UHFLUFXODUHFXQSR]L LLDQXPUXOXLD a=0001.0110.0011.1010.0111.0010.1110.0101
145
Introducere in programare
DSOLFQGFLUFXODU_shift (a,5) spre VWQJD ! 1100.0111.0100.1110.0101.1100.1010.0010
5. Folosind <<sau>> sVHGHWHUPLQHFDUHHVWHFHOPDLPDUHvQWUHJFDUHSRDWHIL reprezentat pe calculator. 6. 6FULH LRIXQF LHFDUHSULPHúWHXQvQWUHJúL-ODILúHD]vQELQDUIRORVL Lsizeof (int) pentru a afla nr de bi L 7. 6FULH LRIXQF LHFDUHFLWHúWHXQQUvQIRUPDELQDUúL-OFRQYHUWHúWHvQIRUPhexa. 8. 6FULH LRIXQF LHpack care compune un long int din 4 caractere de intrare long int pack (char a,char b, char c, char d)
Bublle sort # define FALSE 0 #define TRUE 1 #include <stdio.h> void bubble_sort (int*v,int n) { int j,k,temp,sorted=FALSE; while (!sorted) { sorted=TRUE //sc pp vect sortat for (j=0;jv[j+1]) { // cel pu in un element nu este în ordine sorted =FALSE; temp=v[j]; v[j]=v[j+1]; v[j+1]=temp; } } } } int strlen1(char *s) { int i=0; while (s[i])++i; return i; }//întoarce nr elemente dintr-un nr mai pu LQ‘\0’
146
Introducere in programare
void strcopy1(char *s1, char *s2) { int i; for (i=0;*(s1+1];++I)*(s2+i)=*(s1+I); s2[++I]=’0’; } //classical sau void strcopy1(char *s, char *s2) { while (*s2++=*s1++); } //professional
echivalentul func iei pas din Pascal , str str un contor un nr într-altul …. Int strstr1(char *s1,char *s2) { char *p,*q, *subs; for (subs=s1;*subs; subs++) { p=subs; q=s2; while (*q) if(!(*q++!=*p++)) return subs –s1; else break; } return( –1); } //de verificat
Set 5 1. 6VHH[SOLFHFHYDUH]XOWDvQXUPDHYDOXULLXUPWRDUHORUH[SUHVLL: Fie static int ar []={10,15,4,25,3,-4} int *p; p=&ar [2]; a) *(p+1) b) p[-1] c) (ar-p) d) ar [*p++] e) *(ar+ar[2]) 2. &HHVWHJUHúLWvQXUPWRDUXOFD]: Int j , v[5]={1,2,3,4,5} …… for (j=1; j<5; ++j) printf(“x.d\n”,v[j])
147
Introducere in programare
3. MRGLILFD Lbubble sort DvvQORFVrearanjeze elementele într-XQWDEORXHOV VWRFKH]HQXPDLRUGLQHDFRUHFWGHDUDQMDUH 0
1
2
3
4
Deci v[]={13 , 25 ,11, 2 ,14 } vQXUPDDSOLFULLbubble sort modificarea se UHDOL]HD]vQWDEORXO ord[] = {1,4,0,2,3} 6HREVHUY V[ord[0]]=V[1]=25 V[ord[1]]=V[4]=14 V[ord[2]]=V[0]=13 . . 4. Scrie LRIXQF LH void sort_concat(double *a, double *b, double *c) care ia a,b douWDEORXULVRUWDWHúLOHGHSXQHvQFFDUHWUHEXLHVILHúLHOVRUWDW deasemenea. 5. 0RGLILFD LIXQF LDGHPDLVXVDVWIHOvQFkWVHOLPLQHYDORULOHHJDOH 6. 6FULH LRIXQF LH char * plussir (char *a, char *b); care concatenea]SHDFXE 7. 'HFODUD LLOH char S[10] úL char *S sunt la fel dacQXDUWD LGLIHUHQ HOHVFULLQGXQSURJUDPvQFDUHHOHQXSRWIL înlocuite. 8. 6FLH LRIXQF LHFDUHVRUWHD]XQWDEORXGHúLUXULGHFDUDFWHUHvQRUGLQHDOIDEHWLF ex. : S[][] = {{mike},{oana},{lili},{dana}} în urma apelului IXQF LHL6>@>@ ^^dana},{lili},{oana},{mike}}. 9. Fiind date urmWRDUHOHGHFODUD LL: static int a[2][3]={{-3,14,5},{1,-10,8}}; static int *b[]={a[0],a[1]}; int *p=b[1]; Cum vor fi evaluate urm[toarele expresii : a) *b[1] b) *(++p) c) *9*(--p-2) 10. Care din XUPWRDUHOHH[SUHVLLHVWHHFKYDOHQWFXa[j] ? : 1. *(a[j]+k) 2. **(a[j+k]) 3. (*a+j))[k] 4. (*(a+k))[j] 5. *((*a+j))+k) 6. **(a+j)+k 7. *(&a[0][0]+j+k)
148
Introducere in programare
Set 6 1. Scrie i o IXQF LHFDUHDILúHD]pe ecran din 20 în 20 de apeluri , de câte ori a fost apelata în total. 2. 6VHscrie 2 IXQF LL ¾ conversie integer -> BCD ¾ conversie BCD -> integer folosindu-se ckPSXULOHGHEL LELWfield). 3. 2VWLYHVWHROLVWPDLSDUWLFXODUvQVHQVXOF: ¾ DGXJDUHDVHSRDWHIDFHGRDUODFDSWXOOLVWHLRSHUD LHQXPLWpush) ¾ úWHUJHUHDPDLWRWGLQFRDGDOLVWHLRSHUD LHQXPLWSRS 6FULH LXQSURJUDPFDUH: ¾ crea]ROLVWGHDFHVWWLSIRORVLQGFHOHIXQF LL SXVKSHQWUXDGXJDUH SRSSHQWUXúWHUJHUH 7UHEXLHVFLPSOHPHQWDWHúLDFHVWHIXQF LL
Set7 1. 6VHVFULHXQSURJUDPFDUHVSULPHDVFvQOLQLDGHFRPDQGRIUD]SUHFXPúL switch-XULOH±FVDX±&IXQF LHGHDFHVWHDSURJUDPXOYRIHUIUD]DGLQOLQLDGHLQWUDUHFX OLWHUHPLFLVDXPDUL'DFswitch nu este precizat se va considera implicit –c adic DILúDUHDFXOLWHUHPLFL 2. 6FULH LRYHUVLXQHUHFXUVLYDIXQF LHLstrlen. Este UHFXUVLYLWDWHDPDLEXQFD YHUVLXQHDQRUPDO$UJXPHQWD L 3. 6FULH LRIXQF LHUHFXUVLYFDUHFDOFXOHD]FHOPDLPDUHGLYL]RUFRPXQDGRXQU pozitive întregi. 4. 6FULH LúLRYHUVLXQHLWHUDWLYSHQWUXSXQFWXO 5. 6FULH LRIXQF LHUHFXUVLYFDUHDFFHSWXQpointer la string ca argument, WUDQVIRUPUHVSHFWLYXOúLUîntr-ROLVWvQODQ XLWGHFDUDFWHUHúLvQWRDUFHSRLQWHUXOFWUH SULPXOHOHPHQWGLQOLVW 6. 6FULH LRIXQF LHUHFXUVLYFDUHQXPUHOHPHQWHOHdintr-oOLVWvQOQ XLW $UJXPHQWXOWUHEXLHVILHSRLQWHUXOFWUHSULPXOHOHPHQWGLQOLVW(YLGHQWFYDORDUHD UHWXUQDWYDILvQWUHDJ 7. 6FULH LRIXQF LHUHFXUVLYFDUHDILúHD]YDORULOHILHFUXLHOHPHQWdintr-ROLVW vQOQ XLW 8. 9HULILFD LOHJDOLWDWHDXUPWRDUHORUGHFODUD LL: a. *(*( ) ) [ ] b. * (** ) [ ] c. (*(*(*x( ) ) [ ] ) ( ) ) d. **x[ ] ( ) e. * ( x[ ] ) [ ] f. * ( * (x ( ) ) ( ) ) Set8 1. Explica LHIHFWHOH(úLWUDGXFHUHD) urmWRDUHORUPDFURXUL: 1.1. #define BS 1024 int buf [BS+1]
149
1.2.
1.3.
1.4.
1.5.
Introducere in programare
#define a[b] b+1 … a(1) + 1 # define a(b) b+1 … a(1) +1 #define cos(x) *cos(x) … cos(x) + cos ( cos(y) + 1) #define min(x,y) ((x) >= (y) ? x:y) min (1,min (a,b))
Set9 1. Scrie LXQSURJUDPFDUHVLPSOHPHQWH]Hcomanada # include “nume_fiúLHU” 2. Scrie LRIXQF LHFDUHHOLPLQWRDWHVSD LLOHVXSOLPHQWDUHdintr-o surs& 3. 6FULH LXQSURJUDPFDUHVYHULILFHLQFKLGHUHDFRUHFWDSDUDQWH]HORUîntr-RVXUV& ,QGLFD LH: stiva. 4. Scrie LXQSURJUDPFDUHQXPUFDUDFWHUHOHFXYLQWHOHúLOLQLLOHXQXLILúLHU 5. 6FULH LXQSURJUDPFDUHSULPHúWHFDDUJXPHQWHPDLPXOWHILúLHUHúLOHDILúHD]SH ecran (un fel de type cu mai multe inturi )
150
Introducere in programare
151
Introducere in programare
152
Introducere in programare
153
Introducere in programare
154
Introducere in programare
155
Introducere in programare
156
Introducere in programare
157
Introducere in programare
158
Introducere in programare
159
Introducere in programare
160
Introducere in programare
161
Introducere in programare
162
Introducere in programare
163
Introducere in programare
164
Introducere in programare
165
Introducere in programare
166
Cuprins Introducere in programare Capitolul 1 1 Introducere...................................................................................................1 1.1 Programarea si limbaje de programare.....................................................3 1.2 Arhitectura unui sistem de calcul…………………………………..……3 1.3 Functionarea unui sistem de calcul ………………………………..……4 1.4 Notiunea de algoritm ………………………………...……………..6 1.5 Reperezentarea algorimilor…………………………..………………….7 1.5.1 Metoda shemei logice………………………………………….……...7 1.5.2 Metoda pseudocodului………………………………………………...8 Capitolul 2 2 Limbajul C……………………………………………………………….9 2.1 Structura generica a unui program scris in C………………………….10 2.2 Etapele generarii unui program prornid de la o problema reala……….10 Capitolul 3 3 Notiunea de variabila si tipuri de variabile……………………………....15 3.1 Initializarea variabilelor………………………………………………..16 3.2 Tipuri de variabile……………………………………………………...17 Capitolul 4 4 Tipuri de date predefinite………………………………………………...18 4.1 Date numerice………………………………………………………….18 4.1.1 Operatorul virgula …………………………………………………...19 4.2 Date alfanumerice……………………………………………………...19 4.2.1 Caractere speciale de control…………………………………….......20 4.3 Operatiuni simple de citire sau afisare a datelor……………………….20 4.3.1 Functii de intrare sau iesire cu utilizatorul…………………………...21 Capitlul 5 5 Operatori ai limbajului C………………………………………………...24 5.1 Operatori aritmetici…………………………………………………….24 5.2 Expresiile ……………………………………………………………...24 5.2.1 Ordinea de evaluare a expresiei……………………………………...25 5.3 Tipuri aritmetice de date ………………………………………………26 5.4 Operatorii unari ++ si -- ……………………………………………….27 5.5 Operatorii op=………………………………………………………….27 5.6 Operatorii relationari si logici………………………………………….27 5.7 Expresii logice complexe………………………………………………28 Capitolul 6 6 Instructiuni de selectie(decizie)………………………………………….31 6.1 Instructiune simpla de decizie…………………………………………31 6.2 instructiune de decizie multipla………………………………………..35 Capitolul 7 7 Instructiuni de ciclare. Introducere………………………………………38 7.1 Instructiuni cu un numar necunoscut de pasi…………………………..38
7.1.1 Instructiunea while…………………………………………………...38 7.1.2 Instructiunea do .. while ……………………………………………..39 7.2 Instructiuni cu numar cunoscut de pasi ………………………………..41 7.3 Alte instructiuni………………………………………………………..43 7.3.1 Instructiunea break…………………………………………………..43 7.3.2 Instructiunea continue……………………………………………….43 7.3.3 Instructiunea goto nume_eticheta……………………………………43 7.3.4 Instructiunea return…………………………………………………..44 7.3.5 Constante simbolice………………………………………………….44 Capitolul 8 8 Masive de date. Tablouri…………………………………………………45 8.1 Tablouri monodimensionale…..……………………………………….45 8.1.1 Prelucrari simple cu vectori………………………………………….45 8.1.2 Siruri de caractere……………………………………………………48 8.2 Tablouri bidimensionale. Matrici………………………………………49 Capitolul 9 9 Tipuri de date definite de utilizator………………………………………51 9.1 Declararea unei structuri……………………………………………….51 9.2 Operatorul typedef……………………………………………………..51 9.3 Accesarea membrilor unei structuri……………………………………52 9.4 O structura in interiorul altei structuri…………………………………53 9.5 Structuri autoreferite…………………………………………………...54 9.6 Transferul structurilor ca argumente de functii………………………..54 9.7 Returnarea unei structuri……………………………………………….54 9.8 Campuri de biti………………………………………………………...55 9.9 Uniuni………………………………………………………………….55 9.10 Enumerari…………………………………………………………….59 Capitolul 10 10 Notiunea de functie…………………………………………………….61 10.1 Declararea si definirea functiilor…………………………………….62 10.1.1 Declararea functiilor……………………………………………….62 10.1.2 Declaratii implicite…………………………………………………63 10.2 Definirea functiilor…………………………………………………...63 10.3 Tipul de data void…………………………………………………….66 10.4 Recursivitatea………………………………………………………....73 10.5 Variabile globale……………………………………………………...77 10.6 Macrouri………………………………………………………………78 10.7 Macrouri predefinite………………………………………………….79 10.7.1 ASSERT……………………………………………………………79 10.7.2 Macrouri specializate in clasificarea caracterelor………………….80 10.7.3 Macrodefinitii predefinite………………………………………….81 Capitolul 11 11 Pointeri si alocare dinamica……………………………………………83 11.1 Introducere…………………………………………………………...83 11.1.1 Notiunea de pointer………………………………………………...83 11.1.2 Operatii aritmetice cu pointeri……………………………………...85 11.1.3 Modele de memorie………………………………………………...86
Introducere in programare
11.2 Alocare dinamica……………………………………………………..87 11.2.1 Introducere………………………………………………………….87 11.2.2 Alocarea dinamica………………………………………………….88 11.2.3 Alocarea dinamica a tablourilor de date……………………………90 11.2.3.1 Alocarea tablourilor monodimensionale………………………….91 11.2.3.2 Alocarea tablourilor bidimensionale……………………………...92 11.2.3.3 Alocarea tablourilor tridimensionale……………………………..93 11.3 Pointeri la structuri……………………………………………………94 11.3.1 Accesul membrilor………………………………………………….94 11.3.2 Alocarea structurilor de date………………………………………..94 11.3.3 Initializare si distrugere structuri…………………………………...94 11.4 Declaratii complexe in C……………………………………………..96 11.5 Descifrarea declaratiilor complexe in C……………………………...96 11.6 Lucrul cu fisierele de date (I/O FILE) Streamuri……………………100 11.6.1 Citirea si scrierea datelor………………………………………….101 11.6.2 Lucrul la nivel de character………………………………………..101 Capitolul 12 12 Facilitati de nivel scazut ale limbajului C……………………………..104 12.1 Functii pentru accesul direct la memorie……………………………104 12.2 Functii pentru accesul la porturile de intrare sau iesire……………..105 12.3 Functii pentru gestionarea intervalelor de timp……………………..105 12.4 Functii pentru controlul sistemului de intreruperi…………………..105 12.5 Functii pentru alocarea memoriei(prin DOS)……………………….106 12.6 Intreruperi software. Accesul la registrele procesorului…………….106 12.7 Functii pentru citirea sau modificare vectorilor de intrerupere……..107 12.8 Apeluri de functii DOS……………………………………………...108 12.9 Functii pentru terminarea sau lansarea programelor………………...110 12.10 Dezvoltarea de cod in limbaj mixt(C si ASM)…………………….112 12.11 Tratarea dispozitivelor bus asincrone……………………………...113 Capitlul 13 13 Preprocesorul………………………………………………………….115 13.1 Directiva nula……………………………………………………….115 13.2.Directivele #define si #undef……..…………………………………115 13.3 Directiva #include…………………………………………………...116 13.4 Directiva de compilare conditionata………………………………...116 13.5 Directiva line………………………………………………………..116 13.6 Directiva #error……………………………………………………...117 13.7 Directiva #pragma…………………………………………………..117 Exemple…………………………………………………………………………121 Seturi de teme…………………………………………………………………..143 Sintaxa ANSI……………………………………………………………………151
Introducere in programare