C++面向对象程序设计双语教程(第3版)课件 class 1-Introduction_第1页
C++面向对象程序设计双语教程(第3版)课件 class 1-Introduction_第2页
C++面向对象程序设计双语教程(第3版)课件 class 1-Introduction_第3页
C++面向对象程序设计双语教程(第3版)课件 class 1-Introduction_第4页
C++面向对象程序设计双语教程(第3版)课件 class 1-Introduction_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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(维护)–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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论