C++面向?qū)ο蟪绦蛟O(shè)計(jì)雙語(yǔ)教程(第3版)課件 class 1-Introduction_第1頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)雙語(yǔ)教程(第3版)課件 class 1-Introduction_第2頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)雙語(yǔ)教程(第3版)課件 class 1-Introduction_第3頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)雙語(yǔ)教程(第3版)課件 class 1-Introduction_第4頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)雙語(yǔ)教程(第3版)課件 class 1-Introduction_第5頁(yè)
已閱讀5頁(yè),還剩19頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

ObjectivesTounderstandwhatacomputerprogrammingisToknowhowtowriteaprogramTorecognizetwoprogrammingmethodologies01HowtoWriteaProgram?02Two

ProgrammingMethodologies01HowtoWriteaProgram?WhatisProgramming?Problem(orTask):Abouta

student’sgradesRequirements:Astudentlearnstwocoursesatthisterm,thatis,OOP,math.Youneedtoinputtwogradesdisplaytwogradesdisplaymessagescondition:agrade<60,displaymessages“youfailedxxx”;otherwise,displaymessage“youpassedxxx”Calculatethesumoftwogradesandaverage,anddisplayresultsCaseStudy1ComputerProgramming–planningorschedulingtheperformanceofataskoraproblem,thatis,itisaprocessthatleadsfromoriginalcomputingproblemtoexecutablecomputerprogram.HowtoWriteaProgram?Phase1:Problem-solving-planningtheprocessoftask(1)Inputtwogrades;(2)Displaythesetwogrades;(3)Ifoneoftwogradesislessthan60,display“youfailedxxx”,otherwisedisplay“youpassedxxx”(4)Calculatethesumoftwogradesandaverage;(5)Displaytheresults.PseudocodeStep1.AnalysisandspecificationHowtoWriteaProgram?Step2.Generateanalgorithmbyusingaflowchart

gradeOfOPP<60yesnoinputgradeOfOPP,gradeOfMathoutputsum,averageStartEndgradeOfMath<60sum=gradeOfOPP+gradeOfMathoutputgradeOfOPP,gradeOfMathoutput“youpassedOPP”yesnooutput“youfailedOOP”output“youfailedMath”output“youpassedMath”average=sum/2Algorithm:astep-by-stepprocedureforsolvingaproblem.Step3.VerifyHowtoWriteaProgram?Step1.Youmightconsidersomequestionsinthecontextofprogramming.1.WhatdoIhavetoworkwith–thatis,whatismydata?gradeOfOOP,gradeOfMath,sum,average2.Whatdothedataitemslooklike?–data

types,int,double3.HowwillIknowwhenIhaveprocessedallthedata?–algorithm4.Howmanytimesistheprocessgoingtoberepeated?-structure5.Whatshouldmyoutput

looklike?6.Whatspecialerrors

mightcomeup?Phase2:Implementation–writeaprogramHowtoWriteaProgram?Step2.TranslatethealgorithmtoaprogramwithCStep3.Testtheprogram#include

<stdio.h>intmain(){intgradeOfOOP,gradeOfMath,sum;floataverage;scanf("%d%d",&gradeOfOOP,&gradeOfMath);printf("OOP:%dMath:%d\n",gradeOfOOP,gradeOfMath);if(gradeOfOOP<60) printf("youfailedOOP\n");else printf("youpassedOOP\n");if(gradeOfMath<60) printf("youfailedMath\n");else printf("youpassedMath\n");sum=gradeOfOOP+gradeOfMath;average=(float)sum/2;printf("sum%daverage%6.2f\n",sum,average);return0;}(1)Input:50,69(2)Input:85,55(3)Input:80,69HowtoWriteaProgram?Phase3:Maintenance(維護(hù))–Modifytheprogramtomeetchangingrequirementsorcorrecterrors.#include

<stdio.h>intmain(){intgradeOfOOP,gradeOfMath,sum;floataverage;scanf("%d%d",&gradeOfOOP,&gradeOfMath);printf("OOP:%dMath:%d\n",gradeOfOOP,gradeOfMath);if(gradeOfOOP<60) printf("youfailedOOP\n");else printf("youpassedOOP\n");if(gradeOfMath<60) printf("youfailedMath\n");else printf("youpassedMath\n");sum=gradeOfOOP+gradeOfMath;average=(float)sum/2;printf("sum%daverage%6.2f\n",sum,average);return0;}Whaterrorsmightcomeup?

Formanystudents?Practice-orientedHowtoWriteaProgram?Phase1:Problem-solving:(1)analysisandspecification(2)generatesolution(algorithm)(3)verificationPhase2:Implementation:(1)concretesolution(writingprogram)(2)testPhase3:Maintenance:

(1)reuse

(2)maintainWritingaprogramhasthethree-phaseprocess02ProgrammingMethodologiesProgrammingMethodologies1.thestructuredprogramming(modular,procedural)e.g.C/C++,Pascal2.theobject-orientedprogramming(OOP)e.g.C++,Java,C#Abstractioniscrucialtobuildingthecomplexsoftwaresystems.Dataabstractioncanbesummedupasconcentratingontheaspectsrelevanttotheproblemandignoringthosethatarenotimportantatthemoment.DataAbstractionTwopopularmethodologiestoprogrammingStructuredProgrammingProblem:solveaquadraticequationDatavariables:a,b,c,root1,root2Datatype:float/doubleInputdata:a,b,cOutputdata:root1,root2Algorithm:

Problem-solvingCaseStudy2StructuredProgrammingImplementation#include

<stdio.h>#include

<math.h>intmain(){floata,b,c;floatroot1,root2;printf("Enterthevaluesofa,bandc:\n");scanf("%f%f%f",&a,&b,&c);if(fabs(a)<=1e-6||fabs(b)<=1e-6) printf("theequationhasnorealroots!\n");else{floatdisc=b*b-4*a*c;if(disc<0)printf("theequationhasnorealroots\n");if(disc==0){root1=root2=-b/(2*a);printf("therootis%6.2f",root1);}if(disc>0){root1=(-b+sqrt(disc))/(2*a);root2=(-b-sqrt(disc))/(2*a);printf("therootis%6.2fand%6.2f",root1,root2); }}return0;}StructuredProgramming#include

<stdio.h>#include

<math.h>>boolquadratic(float

a,float

b,float

c,float*root1,float*root2){if(fabs(a)<=1e-6||fabs(b)<=1e-6)return

false;else{floatdisc=b*b-4*a*c;if(disc<0)return

false;if(disc==0){*root1=*root2=-b/(2*a);return

true;}if(disc>0){*root1=(-b+sqrt(disc))/(2*a);*root2=(-b-sqrt(disc))/(2*a);return

true;}}}intmain(){float_a,_b,_c;float_root1,_root2;printf("Enterthevaluesofa,bandc:\n");scanf("%f%f%f",&_a,&_b,&_c);if(quadratic(_a,_b,_c,&_root1,&_root2))printf("therootsare%6.2fand%6.2f",_root1,_root2);elseprintf("theequationhasnorealroot.\n");return0;}Dividingaproblemintosmallersub-problemsiscalledstructureddesign.Thisprocessofimplementingastructureddesigniscalledstructuredprogramming.StructuredProgrammingStructuredProgrammingDividingaproblemintosmallersub-problems.Eachsub-problemisthenanalysed,andasolutionisobtainedtosolvethesub-problem.Thesolutionstoallsub-problemsarecombinedtosolvetheoverallproblem.Characteristics:Top-downdesign,stepwiserefinementandmodularprogrammingBenefit:improvingtheclarity,quality,anddevelopmenttimeofa

computerprogram.StructuredProgrammingquadratic(…){a,b,c,root1,root2}intmain(){……}(function)(function1)(function2)float_a,_b,_c,_root1,root2StructuredProgrammingProblem:Student’sgrades(1)Inputtwogrades;(2)Displaythesetwogrades;(3)Ifoneoftwogradesislessthan60,display“youfailedxxx”,otherwisedisplay“youpassedxxx”(4)Calculatethesumoftwogradesandaverage;(5)Displaytheresults.MainfunctionInputgradeoutputgradeDisplaypassMCalculationsum_averageoutputsum_averageCaseStudy1Object-OrientedProgramming(OOP)#include

<stdio.h>#include

<math.h>class

quadratic_equation{public:quadratic_equation(float

aa,float

bb,float

cc):a(aa),b(bb),c(cc){}boolquadratic(float*root1,float*root2){if(fabs(a)<=1e-6||fabs(b)<=1e-6)return

false;else{floatdisc=b*b-4*a*c;if(disc<0)return

false;if(disc==0){*root1=*root2=-b/(2*a);return

true;}if(disc>0){*root1=(-b+sqrt(disc))/(2*a);*root2=(-b-sqrt(disc))/(2*a);return

true;}}}private:floata,b,c;};class,auser-defineddatatypeintmain(){float_a,_b,_c;float_root1,_root2;printf("Enterthevaluesofa,bandc:\n");scanf("%f%f%f",&_a,&_b,&_c);quadratic_equationqe(_a,_b,_c);if(qe.quadratic(&_root1,&_root2))printf("therootsare%6.2fand%6.2f",_root1,_root2);elseprintf("theequationhasnorealroot.\n");return0;}Themethodofobjectqeintmain(){float_a,_b,_c;float_root1,_root2;printf("Enterthevaluesofa,bandc:\n");scanf("%f%f%f",&_a,&_b,&_c);if(quadratic(_a,_b,_c,&_root1,&_root2))printf("therootsare%6.2fand%6.2f",_root1,_root2);elseprintf("theequationhasnorealroot.\n");return0;}Usingobjectsandtheirinteractions(methods)todesignprogramsiscalledOOdesign.ThisprocessofimplementinganOOdesigniscalled

OOprogramming.ObjectsObject-OrientedProgramming(OOP)qe1qe2qe4qe3MainfunctionObject-OrientedProgramming(OOP)Object-orientedprogramming(OOP

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論