版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、A Rich HistoryMS-DOSBASICWindowsVisual BASICIE, IISVisual Studio1995Internet1990GUI1981PC2002XMLWeb Services第1页/共177页第一页,编辑于星期五:十六点 一分。Rapidly Changing TechnologyRapidly Changing TechnologyComputational power lCPU power doubling every 18 monthslGraphics 5x per yearlStorage 2x per yearlNetworking 4x
2、per yearNew deviceslMobile screens, cameras, Tablet PC, Pocket PCs, mobile phonesConnectivitylWirelesslHigh-speed Internet第2页/共177页第二页,编辑于星期五:十六点 一分。Chap. 2 C+: The fundamentals function main() include comments definitions simple interactive input and output integer, floating-point, and character ty
3、pes integer, floating-point, and character literalsKey ConceptsC+ namesdeclarationsexpressions usual unary conversionsusual binary conversionsoperator precedenceoperator associativity iostream insertion and extraction第3页/共177页第三页,编辑于星期五:十六点 一分。A FIRST PROGRAM/ Program 2.1: Display greetings/ Author:
4、 Bryan Zeng/ Date: 7/24/2002#include using namespace std;int main() cout Hello world! endl; return 0;Processed by the preprocessoroperandinsertion operator第4页/共177页第四页,编辑于星期五:十六点 一分。程序运行的结果name of the programoutput of the program第5页/共177页第五页,编辑于星期五:十六点 一分。A SECOND PROGRAM#include using namespace std
5、;int main() / Input pricecout Price;/ Compute and output sales taxcout Sales tax on $ Price is ;cout $ Price * 0.04 endl;return 0;extraction operatorinsertion operator第6页/共177页第六页,编辑于星期五:十六点 一分。Screen capture第7页/共177页第七页,编辑于星期五:十六点 一分。ASSIGNING A VALUExyWhat are we going to do?x-coordinatey-coordina
6、te第8页/共177页第八页,编辑于星期五:十六点 一分。ASSIGNING A VALUE#include using namespace std;int main() / Input line s parameterscout m;cout b;第9页/共177页第九页,编辑于星期五:十六点 一分。ASSIGNING A VALUE/ Input x-coordinate of interestcout x;/ compute and display y-coordinateint y;y = m * x + b;cout y = y when m = m ;cout b = b ; x
7、= x endl;return 0;第10页/共177页第十页,编辑于星期五:十六点 一分。Screen capture第11页/共177页第十一页,编辑于星期五:十六点 一分。FUNDAMENTAL C+ OBJECTS the integer objects the floating-point objects the character objects第12页/共177页第十二页,编辑于星期五:十六点 一分。Integer object types short (16 bits) int (32 bits) long (32 bits)the size of int is impleme
8、ntation dependent第13页/共177页第十三页,编辑于星期五:十六点 一分。Character object typesCharacters are encoded using some scheme where an integer represents a particular character. Foe example, the integer 98 might represent the letter a.a b c z0 1 2 9The operators defined on the integer types are defined on the charac
9、ter types as well.A + 1 gives BJ + 3 results in M第14页/共177页第十四页,编辑于星期五:十六点 一分。Floating-point object types float (32 bits) double (64 bits) long double (80 bits)第15页/共177页第十五页,编辑于星期五:十六点 一分。CONSTANTS String and character constants Integer constants Floating-point constants第16页/共177页第十六页,编辑于星期五:十六点 一分
10、。String and character constants“Hello World!”“Hello World!n” (“Hello World!012”)“Hello World!”Memory allocation for a string literalH elloW orld!0010040010041010042010052第17页/共177页第十七页,编辑于星期五:十六点 一分。Integer constants23 45 101 5523L 45L 101L 55L023 077L 045 010base 8 numbers038 093 0779not valid cons
11、tants0 x2a 0 x45 0 xffL 0 xA1ebase 16 numbers第18页/共177页第十八页,编辑于星期五:十六点 一分。Example of constants#include using namespace std;int main() cout Display integer constantsn endl;cout Octal 023 is 023 decimal endl;cout Decimal const 23 is 23 decimal endl;cout Hex const 0 x23 is 0 x23 decimal endl;return 0;第
12、19页/共177页第十九页,编辑于星期五:十六点 一分。Screen capture第20页/共177页第二十页,编辑于星期五:十六点 一分。Floating-point constants2.34 3.1415 .21L 45.e+23 2.3E-42310454103 . 2第21页/共177页第二十一页,编辑于星期五:十六点 一分。Example#include using namespace std;int main() cout 230.e+3 endl;cout 230E3 endl;cout 230000.0 endl;cout 2.3E5 endl;cout 0.23e6 en
13、dl;cout .23E+6 endl;return 0;第22页/共177页第二十二页,编辑于星期五:十六点 一分。NAMES Keywords (reserved words) Identifiers: a name defined by and given meaning to by the programmer.第23页/共177页第二十三页,编辑于星期五:十六点 一分。Some of the keywordsasmelsefloatoperatorautoenumforprivateboolexplicitfriendthrowbreakexterngototruecasefalse
14、inlinetypedef第24页/共177页第二十四页,编辑于星期五:十六点 一分。Examples of identifiersWordCount Time NumberOfStudents第25页/共177页第二十五页,编辑于星期五:十六点 一分。DEFINITIONSint x;int WordCnt, Radius, Height;float FlightTime, Mileage, Speed;第26页/共177页第二十六页,编辑于星期五:十六点 一分。Examples of definitions#include using namespace std;int main() fl
15、oat f;int i;char c;double d;cout fs value is f endl;cout is value is i endl;cout cs value is c endl;cout ds value is d endl;return 0;第27页/共177页第二十七页,编辑于星期五:十六点 一分。Initial valuesalways give objects an initial value!第28页/共177页第二十八页,编辑于星期五:十六点 一分。CASE STUDYCOMPUTING AVERAGE VELOCITYeElapsedTimDistanceV
16、elocity input: start and end milepost, elapsed time (h/m/s)output: average velocity (miles per hour).第29页/共177页第二十九页,编辑于星期五:十六点 一分。CASE STUDYStep 1. Issue the prompts and read the input.Step 2. Compute the elapsed time in hours.Step 3. Compute the distance traveled.Step 4. Compute the averaged veloc
17、ity.The steps to solving the problem:第30页/共177页第三十页,编辑于星期五:十六点 一分。#include using namespace std;int main() cout All inputs are integers!n;cout StartMilePost;cout EndHour EndMinute EndSecond; cout EndMilePost;第31页/共177页第三十一页,编辑于星期五:十六点 一分。float ElapsedTime = EndHour + (EndMinute / 60.0) + (EndSecond /
18、 3600.0);int Distance = EndMilePost - StartMilePost;float Velocity = Distance / ElapsedTime;cout nCar traveled Distance miles in ;cout EndHour hrs EndMinute min EndSecond secn;cout Average velocity was Velocity mph endl;return 0;expressionsassignment第32页/共177页第三十二页,编辑于星期五:十六点 一分。CHAPTER 3Modifying o
19、bjects assignment operation assignment conversions assignment precedence and associativity strings EzWindowsextraction operationsconst declarationscompound assignment operationsinput with cinincrement and decrement operation第33页/共177页第三十三页,编辑于星期五:十六点 一分。Assignmentint Score1 = 90;int Score2 = 75;int
20、temp = Score2;Score2 = Score1;Score1 = temp;9075907575909075759075Score1Score2tempto swap the values of Score1 and Score2第34页/共177页第三十四页,编辑于星期五:十六点 一分。Assignment conversionsint x = 0;x = 3.9;short s1 = 0;long i2 = 65535;s1 = i2;short m1 = 0;long n2 = 65536;m1 = n2;cout x s1 m13-10第35页/共177页第三十五页,编辑于
21、星期五:十六点 一分。Assignment precedence and associativityx = y = z + 2;x = (y = (z + 2);第36页/共177页第三十六页,编辑于星期五:十六点 一分。compound assignmenti = i + 5;i += 5;i = i + 1;i += 1;+i;i = i - 1;i -= 1;-i;第37页/共177页第三十七页,编辑于星期五:十六点 一分。Increment and Decrementint i = 4;int j = 5;int k = j * +i;cout k i;int i = 4;int j
22、= 5;int k = j * i+;cout k i;25 520 5第38页/共177页第三十八页,编辑于星期五:十六点 一分。The String Classstring Message1 = “Enter your password:”;string Message2 = Message1;string FirstName = “Zach”;string LastName = “Davidson”;string FullName = FirstName + “ ” + LastName;FirstName += LastName;string Date = “March 7, 1994
23、”;int length = Date.size();第39页/共177页第三十九页,编辑于星期五:十六点 一分。case studyconverting dates from American format to international formatDecember 29, 195329 December 1953MonthDayYearDay Month Year第40页/共177页第四十页,编辑于星期五:十六点 一分。solution/ Prompt for and read the datecout “Enter the date in American format ” “(e.
24、g., December 29, 1953): ”;char buffer100;cin.getline(buffer, 100);string Date = buffer;第41页/共177页第四十一页,编辑于星期五:十六点 一分。solutionto extract the month:int i = Date.find(“ ”);string Month = Date.substr(0, i);December 29, 1953第42页/共177页第四十二页,编辑于星期五:十六点 一分。solutionto locate and extract the day:int k = Date.
25、find(“,”);string Day = Date.substr(i+1, k-i-1);December 29, 1953第43页/共177页第四十三页,编辑于星期五:十六点 一分。solutionDecember 29, 1953to extract the year:string Year = Date.substr( k+2, Date.size() );第44页/共177页第四十四页,编辑于星期五:十六点 一分。solutionto display the date in the new format:string NewDate = Day + “ ” + Month + “
26、” + Year;cout “Original date: ” Date endl;cout “Converted date: ” NewDate endl;第45页/共177页第四十五页,编辑于星期五:十六点 一分。screen capture第46页/共177页第四十六页,编辑于星期五:十六点 一分。ezwin objectsY-coordinate:Distance from top of screenX-coordinate:Distance from left edge of screenHeight of windowWidth of window第47页/共177页第四十七页,编
27、辑于星期五:十六点 一分。Windows Api Demo/ Program 3.6: Api Demo#include #include #include using namespace std;int ApiMain() const int Width = 8; const int Height = 7; int LawnLength = 6; int LawnWidth = 5; int HouseLength = 3; int HouseWidth = 2;Click to view source第48页/共177页第四十八页,编辑于星期五:十六点 一分。 SimpleWindow W
28、indow(“Api Demo”, Width, Height); Window.Open(); RectangleShape Lawn(Window, Width/2.0, Height/2.0, Green, LawnLength, LawnWidth); Lawn.Draw(); RectangleShape House(Window, Width/2.0, Height/2.0, Yellow, HouseLength, HouseWidth); House.Draw(); cout Type a character followed by an return to remove th
29、e window and exit AnyChar; Window.Close(); return 0;第49页/共177页第四十九页,编辑于星期五:十六点 一分。CHAPTER 4Control constructsn bool typen Relational operatorsn short-circuit evaluationn if-else statementn switch statementn break statementn enum statementn for constructn while constructn do constructn infinite loops
30、n invariantsKey Concepts第50页/共177页第五十页,编辑于星期五:十六点 一分。A BOOLEAN TYPEA BOOLEAN TYPEbool P = true;bool Q = false;bool R = true;bool S = false;Boolean operators:P; / P has value trueP & R; / logical and is true when both operands are trueP | Q; / logical or is true when at least one of the operands
31、is trueP & S; / logical and is false when at least one of the operands is false!R ; / logical not is false when the operand is true第51页/共177页第五十一页,编辑于星期五:十六点 一分。The logical operators are also defined for the integral type objects such as int and i = 1;int j = 0;int k = -1;int m = 0;i /
32、i is nonzeroi & k / both operands are nonzero!j / not is true when operand is zeroThe following expressions are true.The following expressions evaluate to false.j | m / both operands are zero!k / not is false when the operand is nonzero第52页/共177页第五十二页,编辑于星期五:十六点 一分。Relational operatorsint i = 1;
33、int j = 2;int k = 2;char c = 2;char d = 3;char e = 2;The following expressions are true.c = e i != k i e j = kThe following expressions are false.i = j c != e j k d =k第53页/共177页第五十三页,编辑于星期五:十六点 一分。Operator precedencei + l j * 4 & ! P | QOperationUnary operatorsMultiplicative arithmeticAdditive a
34、rithmeticRelational orderingRelational equalityLogical andLogical orAssignmentPrecedence of selected operators arranged from highest to lowest(i+1) 5 )第55页/共177页第五十五页,编辑于星期五:十六点 一分。Conditional execution using the if-else statementExpressionAction1Action2truefalseif ( Expression ) Action1 else Action
35、2第56页/共177页第五十六页,编辑于星期五:十六点 一分。examplecout Value1 Value2;int Larger;if ( Value1 Value2 ) Larger = Value2;else Larger = Value1;cout The larger of Value1 and Value2 is Larger endl;第57页/共177页第五十七页,编辑于星期五:十六点 一分。conditional execution using the switch statementswitch (command) case u:cout Move up endl;br
36、eak;case d:cout Move down endl;break;case l:cout Move left endl;break;case r:cout Move right endl;break;default:cout Invalid command s) / prepare to process string s / process current string s / prepare to process next string/ finish string processingClick to view source第65页/共177页第六十五页,编辑于星期五:十六点 一分
37、。第66页/共177页第六十六页,编辑于星期五:十六点 一分。case study:A more complicated text processor:click to view sourceEcho input to standard output, converting uppercase to lowercase.第67页/共177页第六十七页,编辑于星期五:十六点 一分。screen capture第68页/共177页第六十八页,编辑于星期五:十六点 一分。Iteration using the for constructfor ( ForInit; ForExpression; Po
38、stExpression ) ActionInitialization step to prepare for the for loop evaluationPreparation for next iteration of the for loopLogical expression that determines whether the action is to be executedAction to be performed for each iteration of the for loop第69页/共177页第六十九页,编辑于星期五:十六点 一分。exampleCompute n!
39、:cout n;int nfactorial = 1;for (int i = 2; i = n; +i) nfactorial *= i;cout n ! = nfactorial endl;第70页/共177页第七十页,编辑于星期五:十六点 一分。CHAPTER 5Function basics functions value parameters invocation and flow of control header files function prototyping activation records define directives file inclusion condi
40、tional compilation iostream functionality pseudorandom numbersiomanip manipulatorsformatted outputfstream class ifstreamfstream class ofstreamfile manipulation stdlib libraryexit() functionassert librarytranslation unitcastingKey Concepts第71页/共177页第七十一页,编辑于星期五:十六点 一分。function basicsconsider the foll
41、owing quadratic expression:02cbxaxthe roots of the expression are given by:aacbb242click to view source第72页/共177页第七十二页,编辑于星期五:十六点 一分。function basicsdouble radical = sqrt(b*b - 4*a*c);function sqrt( )parameters (arguments)returns a value of type double第73页/共177页第七十三页,编辑于星期五:十六点 一分。interface specifica
42、tioninterface specificationdouble sqrt(double number);#include math libraryfunction interfacefunction type or return typefunction nameheader fileparameter(formal parameter)第74页/共177页第七十四页,编辑于星期五:十六点 一分。interface specificationinterface specificationFunctionType FunctionName ( ParameterList )Type of v
43、alue that the function returnsIdentifier name of functionA description of the form the parameters (if any) are to takeParameterDeclaration, , ParameterDeclarationDescription of individual parametersParameterType ParameterName第75页/共177页第七十五页,编辑于星期五:十六点 一分。Function prototypingint PromptAndExtract();fl
44、oat CircleArea(float radius);bool IsVowel(char CurrentCharacter);formal parameter第76页/共177页第七十六页,编辑于星期五:十六点 一分。examplescout sqrt(14) sqrt(12);double QuarticRoot = sqrt(sqrt(5);double x = sqrt( );double y = sqrt(5, 3);invalid invocations of functionactual parameter第77页/共177页第七十七页,编辑于星期五:十六点 一分。the fs
45、tream libraryopen a file to read dataopen a file to write datafile ioclick to view an example第78页/共177页第七十八页,编辑于星期五:十六点 一分。random numbers/ program 5.6: display pseudorandom numbers#include #include #include using namespace std;int main() srand( (unsigned int) time(0) ); for ( int i=1; i=5; +i ) cout
46、 rand() %100 0click to view source第88页/共177页第八十八页,编辑于星期五:十六点 一分。CHAPTER 7The class construct and object-oriented design class construct information hiding encapsulation data members member functions constructors inspectors mutators facilitators const functions access specification: public and privat
47、e object-oriented analysis and designKey Concepts第89页/共177页第八十九页,编辑于星期五:十六点 一分。programmer-defined typesclass ClassName public:/ Prototypes for constructors/ and public member functions/ and declarations for public/ data attributes go here. private:/ Prototypes for private data/ members and declarati
48、ons for/ private data attributes go here.;第90页/共177页第九十页,编辑于星期五:十六点 一分。user-defined class in actionclass RectangleShape public:RectangleShape(SimpleWindow &Window, float XCoord, float YCoord, color &color, float Width, float Height);void Draw( );color GetColor( ) const;float GetWidth( ) cons
49、t;void SetColor(const color &Color); private:float Width;color Color;data members (attributes)inspectorsmutatorfacilitatormember functionsconstructoraccess specifier第91页/共177页第九十一页,编辑于星期五:十六点 一分。user-defined class in action/ program 7.1: user-defined class#include SimpleWindow W(MAIN WINDOW, 8.0
50、, 8.0);int ApiMain() W.Open(); RectangleShape R(W, 4.0, 4.0, Blue, 2.0, 3.0); R.Draw();return 0;Click to view SourceInstantiation第92页/共177页第九十二页,编辑于星期五:十六点 一分。using the RectangleShape classClick to view source第93页/共177页第九十三页,编辑于星期五:十六点 一分。CHAPTER 8Implementing abstract data types data abstraction ab
51、stract data type rule of minimality principle default constructors copy constructors member assignment inspectors overloading insertion and extraction operators mutators facilitators const member functions destructors auxiliary functions and operators operator overloading reference return pseudorand
52、om number sequenceKey Concepts第94页/共177页第九十四页,编辑于星期五:十六点 一分。Rational ADT basicsA rational number is the ratio of two integersand is typically represented in the manner a/b.The basic arithmetic operations have the followingdefinitions:bdbcaddcbabdbcaddcbabcaddcbabdacdcba/第95页/共177页第九十五页,编辑于星期五:十六点 一分
53、。Rational ADT basicsAfter development of ADT Rational, we areable to do:Rational a(1, 2); / a = 1/2Rational b(2, 3); / b = 2/3cout a “ + ” b “ = ” (a + b) endl;第96页/共177页第九十六页,编辑于星期五:十六点 一分。Rational ADT basicsWhat we need to do:nConstruct the rational number with default or particular attributes.nAd
54、d, subtract, multiply, and divide the rational number to another rational number.nCopy the value of the rational number to another rational number.nCompare the rational number to another rational number.nDisplay the value of the rational number.nExtract the value of the rational number.第97页/共177页第九十
55、七页,编辑于星期五:十六点 一分。/ program 8.1: Demonstrate Rational ADT#include #include rational.husing namespace std;int main() Rational r; Rational s; cout r; cout s; Rational t(r); Rational Sum = r + s; Rational Product = r * s; cout r + s = Sum endl; cout r * s = Product endl; return 0;copy constructorextract
56、ion operationinsertion operationarithmetic operation第98页/共177页第九十八页,编辑于星期五:十六点 一分。Rational interface descriptionClick to view source第99页/共177页第九十九页,编辑于星期五:十六点 一分。Implementing the rational classClick to view source第100页/共177页第一百页,编辑于星期五:十六点 一分。CHAPTER 9Lists one-dimensional arrays array subscripting
57、arrays as parameters array elements as parameters character strings Standard Template Library (STL) container class vector vector subscripting vector resizing string subscripting iterators iterator dereferencing vector of vectors table matrices member initialization list multidimensional arraysKey C
58、oncepts第101页/共177页第一百零一页,编辑于星期五:十六点 一分。one-dimensional arraysBaseType ID SizeExp ;Type of Values in listName of listBracketed constant expression indicating number of elements in list第102页/共177页第一百零二页,编辑于星期五:十六点 一分。one-dimensional array examplesconst int N = 20;const int M = 40;const int MaxStringSi
59、ze = 80;const int MaxListSize = 1000;int A10;char BMaxStringSize;float CM*N;int ValuesMaxListSize;see examples第103页/共177页第一百零三页,编辑于星期五:十六点 一分。-A0 A1 A2 A3 A4 A5 A6 A7 A8 A9A(uninitialized)one-dimensional array examples第104页/共177页第一百零四页,编辑于星期五:十六点 一分。one-dimensional array examplesint i = 7;int j = 2;
60、int k = 4;A0 = 1;Ai = 5;Aj = Ai + 3;Aj+1 = Ai + A0;AAj = 12;1-863-512-A0 A1 A2 A3 A4 A5 A6 A7 A8 A9第105页/共177页第一百零五页,编辑于星期五:十六点 一分。Array initializationint Frequency5 = 0, 0, 0, 0, 0;int Total5 = 0;int Sub5(0, 0, 0, 0, 0);int Count5(0);int Digits = 0, 1, 3, 4, 5, 6, 7, 8, 9;int Zero = 0;char Alphabet = a, b, c, d, e;Rational
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 短剧拍摄场景布置与搭建手册
- 城市绿化养护与景观提升指南
- 按摩店突发情况应急处理手册
- 针织厂应急处置制度
- 拔罐禁忌症与注意事项
- 眼镜店加工废料处理方案及周边环境防护说明
- 军训拓展活动方案策划(3篇)
- 通山联谊活动策划方案(3篇)
- 临汾餐饮活动策划方案(3篇)
- 联名推广活动策划方案(3篇)
- (一模)济宁市2026年高三高考模拟考试语文试卷(含标准答案)
- 题型01 小说阅读主观题型归类(题型专练)-2026年高考语文二轮复习解析版
- 食品行业生产管理岗位的职责与能力要求概览
- 2026年湖南汽车工程职业学院单招职业技能考试题库及答案解析
- 船载危险货物申报员和集装箱装箱现场检查员从业行为规范(试行)2026
- 测绘应急保障方案
- 教育局安全管理培训课件
- 2025-2026年无人配送车技术应用与趋势洞察蓝皮书
- 2026年锡林郭勒职业学院单招职业适应性测试题库及完整答案详解1套
- 党建工作与业务工作深度融合课题报告
- 2025年成都辅警招聘考试真题含答案详解(能力提升)
评论
0/150
提交评论