版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、会计学1C程序设计英文程序设计英文MS-DOSBASICWindowsVisual BASICIE, IISVisual Studio1995Internet1990GUI1981PC2002XMLWeb Services第1页/共177页第2页/共177页Key ConceptsC+ namesdeclarationsexpressionsusual unary conversionsusual binary conversionsoperator precedenceoperator associativityiostream insertion and extraction第3页/共17
2、7页/ Program 2.1: Display greetings/ Author: 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页#include using namespace std;int main() / In
3、put pricecout Price;/ Compute and output sales taxcout Sales tax on $ Price is ;cout $ Price * 0.04 endl;return 0;extraction operatorinsertion operator第6页/共177页第7页/共177页xyWhat are we going to do?x-coordinatey-coordinate第8页/共177页#include using namespace std;int main() / Input line s parameterscout m;
4、cout b;第9页/共177页/ 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 = x endl;return 0;第10页/共177页第11页/共177页第12页/共177页the size of int is implementation dependent第13页/共177页Characters are encoded using some scheme where an in
5、teger 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 character types as well.A + 1 gives BJ + 3 results in M第14页/共177页第15页/共177页第16页/共177页“Hello World!”“Hello World!n” (“Hello Wor
6、ld!012”)“Hello World!”Memory allocation for a string literalH elloW orld!0010040010041010042010052第17页/共177页23 45 101 5523L 45L 101L 55L023 077L 045 010base 8 numbers038 093 0779not valid constants0 x2a 0 x45 0 xffL 0 xA1ebase 16 numbers第18页/共177页#include using namespace std;int main() cout Display
7、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;第19页/共177页第20页/共177页2.34 3.1415 .21L 45.e+23 2.3E-42310454103 . 2第21页/共177页#include using namespace std;int main() cout 230.e+3 endl;cout 230E3 endl
8、;cout 230000.0 endl;cout 2.3E5 endl;cout 0.23e6 endl;cout .23E+6 endl;return 0;第22页/共177页第23页/共177页asmelsefloatoperatorautoenumforprivateboolexplicitfriendthrowbreakexterngototruecasefalseinlinetypedef第24页/共177页WordCount Time NumberOfStudents第25页/共177页int x;int WordCnt, Radius, Height;float FlightTi
9、me, Mileage, Speed;第26页/共177页#include using namespace std;int main() float 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页always give objects an initial value!第28页/共177页COMPUTING AVERAGE VELOCITYeElapsedTimDi
10、stanceVelocity input: start and end milepost, elapsed time (h/m/s)output: average velocity (miles per hour).第29页/共177页Step 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 velocity.The steps to solvin
11、g 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 / 3600.0);int Distance = EndMilePost - StartMilePost;float Velo
12、city = 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页Modifying objectsextraction operationsconst declarationscompound assignment operationsinput with cinin
13、crement and decrement operation第33页/共177页int Score1 = 90;int Score2 = 75;int temp = Score2;Score2 = Score1;Score1 = temp;9075907575909075759075Score1Score2tempto swap the values of Score1 and Score2第34页/共177页int x = 0;x = 3.9;short s1 = 0;long i2 = 65535;s1 = i2;short m1 = 0;long n2 = 65536;m1 = n2;
14、cout x s1 m13-10第35页/共177页x = y = z + 2;x = (y = (z + 2);第36页/共177页i = i + 5;i += 5;i = i + 1;i += 1;+i;i = i - 1;i -= 1;-i;第37页/共177页int i = 4;int j = 5;int k = j * +i;cout k i;int i = 4;int j = 5;int k = j * i+;cout k i;25 520 5第38页/共177页string Message1 = “Enter your password:”;string Message2 = M
15、essage1;string FirstName = “Zach”;string LastName = “Davidson”;string FullName = FirstName + “ ” + LastName;FirstName += LastName;string Date = “March 7, 1994”;int length = Date.size();第39页/共177页converting dates from American format to international formatDecember 29, 195329 December 1953MonthDayYea
16、rDay Month Year第40页/共177页/ Prompt for and read the datecout “Enter the date in American format ” “(e.g., December 29, 1953): ”;char buffer100;cin.getline(buffer, 100);string Date = buffer;第41页/共177页to extract the month:int i = Date.find(“ ”);string Month = Date.substr(0, i);December 29, 1953第42页/共17
17、7页to locate and extract the day:int k = Date.find(“,”);string Day = Date.substr(i+1, k-i-1);December 29, 1953第43页/共177页December 29, 1953to extract the year:string Year = Date.substr( k+2, Date.size() );第44页/共177页to display the date in the new format:string NewDate = Day + “ ” + Month + “ ” + Year;co
18、ut “Original date: ” Date endl;cout “Converted date: ” NewDate endl;第45页/共177页第46页/共177页Y-coordinate:Distance from top of screenX-coordinate:Distance from left edge of screenHeight of windowWidth of window第47页/共177页/ Program 3.6: Api Demo#include #include #include using namespace std;int ApiMain() c
19、onst 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 Window(“Api Demo”, Width, Height); Window.Open(); RectangleShape Lawn(Window, Width/2.0, Height/2.0, Green, LawnLength, LawnWidth); Law
20、n.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 the window and exit AnyChar; Window.Close(); return 0;第49页/共177页Control constructsn bool typen Relational operatorsn short-circuit evalu
21、ationn if-else statementn switch statementn break statementn enum statementn for constructn while constructn do constructn infinite loopsn invariantsKey Concepts第50页/共177页bool P = true;bool Q = false;bool R = true;bool S = false;Boolean operators:P; / P has value trueP & R; / logical and is true whe
22、n both operands are trueP | Q; / logical or is true when at least one of the operands 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页int i = 1;int j = 0;int k = -1;int m = 0;i / i is nonzeroi & k / both ope
23、rands 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页int i = 1;int j = 2;int k = 2;char c = 2;char d = 3;char e = 2;The following expre
24、ssions are true.c = e i != k i e j = kThe following expressions are false.i = j c != e j k d =k第53页/共177页i + l j * 4 & ! P | QOperationUnary operatorsMultiplicative arithmeticAdditive arithmeticRelational orderingRelational equalityLogical andLogical orAssignmentPrecedence of selected operators arra
25、nged from highest to lowest(i+1) 5 )第55页/共177页ExpressionAction1Action2truefalseif ( Expression ) Action1 else Action2第56页/共177页cout Value1 Value2;int Larger;if ( Value1 Value2 ) Larger = Value2;else Larger = Value1;cout The larger of Value1 and Value2 is Larger endl;第57页/共177页switch (command) case u
26、:cout Move up endl;break;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页/共17
27、7页第66页/共177页A more complicated text processor:click to view sourceEcho input to standard output, converting uppercase to lowercase.第67页/共177页第68页/共177页for ( ForInit; ForExpression; PostExpression ) ActionInitialization step to prepare for the for loop evaluationPreparation for next iteration of the
28、for loopLogical expression that determines whether the action is to be executedAction to be performed for each iteration of the for loop第69页/共177页Compute n!:cout n;int nfactorial = 1;for (int i = 2; i = n; +i) nfactorial *= i;cout n ! = nfactorial endl;第70页/共177页Function basicsiomanip manipulatorsfo
29、rmatted outputfstream class ifstreamfstream class ofstreamfile manipulationstdlib libraryexit() functionassert librarytranslation unitcastingKey Concepts第71页/共177页consider the following quadratic expression:02cbxaxthe roots of the expression are given by:aacbb242click to view source第72页/共177页double
30、radical = sqrt(b*b - 4*a*c);function sqrt( )parameters (arguments)returns a value of type double第73页/共177页double sqrt(double number);#include math libraryfunction interfacefunction type or return typefunction nameheader fileparameter(formal parameter)第74页/共177页FunctionType FunctionName ( ParameterLi
31、st )Type of value 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页int PromptAndExtract();float CircleArea(float radius
32、);bool IsVowel(char CurrentCharacter);formal parameter第76页/共177页cout sqrt(14) sqrt(12);double QuarticRoot = sqrt(sqrt(5);double x = sqrt( );double y = sqrt(5, 3);invalid invocations of functionactual parameter第77页/共177页open a file to read dataopen a file to write datafile ioclick to view an example第
33、78页/共177页/ 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 rand() %100 0click to view source第88页/共177页The class construct and object-oriented design mutators facilitators const functions
34、 access specification: public and private object-oriented analysis and designKey Concepts第89页/共177页class ClassName public:/ Prototypes for constructors/ and public member functions/ and declarations for public/ data attributes go here. private:/ Prototypes for private data/ members and declarations
35、for/ private data attributes go here.;第90页/共177页class RectangleShape public:RectangleShape(SimpleWindow &Window, float XCoord, float YCoord, color &color, float Width, float Height);void Draw( );color GetColor( ) const;float GetWidth( ) const;void SetColor(const color &Color); private:float Width;co
36、lor Color;data members (attributes)inspectorsmutatorfacilitatormember functionsconstructoraccess specifier第91页/共177页/ program 7.1: user-defined class#include SimpleWindow W(MAIN WINDOW, 8.0, 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 S
37、ourceInstantiation第92页/共177页Click to view source第93页/共177页Implementing abstract data typesmutatorsfacilitatorsconst member functionsdestructorsauxiliary functions and operatorsoperator overloadingreference returnpseudorandom number sequenceKey Concepts第94页/共177页A rational number is the ratio of two
38、integersand is typically represented in the manner a/b.The basic arithmetic operations have the followingdefinitions:bdbcaddcbabdbcaddcbabcaddcbabdacdcba/第95页/共177页After 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) e
39、ndl;第96页/共177页What we need to do:第97页/共177页/ 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
40、 0;copy constructorextraction operationinsertion operationarithmetic operation第98页/共177页Click to view source第99页/共177页Click to view source第100页/共177页Listsvector subscriptingvector resizingstring subscriptingiteratorsiterator dereferencingvector of vectorstablematricesmember initialization listmultid
41、imensional arraysKey Concepts第101页/共177页BaseType ID SizeExp ;Type of Values in listName of listBracketed constant expression indicating number of elements in list第102页/共177页const int N = 20;const int M = 40;const int MaxStringSize = 80;const int MaxListSize = 1000;int A10;char BMaxStringSize;float C
42、M*N;int ValuesMaxListSize;see examples第103页/共177页-A0 A1 A2 A3 A4 A5 A6 A7 A8 A9A(uninitialized)one-dimensional array examples第104页/共177页int i = 7;int j = 2;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页int Frequency5 = 0, 0, 0, 0, 0;int
43、 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 R10;第106页/共177页Arrays defined in the global scope with fundamental base types have their array elements set to 0 unless there is explicit initialization. Array
44、s defined in a local scope with fundamental base types have uninitialized array elements unless there is explicit initialization.第107页/共177页char Letters = “abcdefghijklmnopqrstuvwxyz”;char G = “Hello”;Hello0G0G1G2G3G4G5G“null character”第108页/共177页Display inputs in reverse order. (Click here to view
45、source)第109页/共177页第110页/共177页Standard Template Library (STL)container adapters第111页/共177页The vector class template provides four constructors for defining a list of elements:第112页/共177页const int N = 20;const int M = 40;cout length;Rational r(1, 2);vector A(10);vector B(M);vector C(M*N);vector D(leng
46、th);vector E(N);vector F(N, r);vector G(10, 1);vector H(M, h);vector I(M*N, 0);vector J(length, 2);第113页/共177页vector R(E);vector S(G);vector T(J);vector U(10, 4);vector V(5, 1);第114页/共177页44444444444444444444111114444444444UVUVV = U;The assignment operator = is a member operator of the vector class第
47、115页/共177页The principal random access methods areoverloading of the subscript operator .A0 = 57;cout A5;第116页/共177页restrictions: bidirectional unidirectionalThe vector sequential access methods areimplemented using iterators.14723 512 621114sentinelvectorA(5);vector:iterator q+q;-q;第117页/共177页14723
48、512 621114AA.begin()A.end()A.rend()A.rbegin()第118页/共177页vector List(5);for (int i = 0; i List.size(); +i) Listi = 100 + i;100 101 102 103 104typedef vector:iterator iterator;typedef vector:reverse_iterator reverse_iterator;List第119页/共177页iterator p = List.begin();cout *p “ ”;+p;cout *p “ ”;+p;cout *
49、p “ ”;-p;cout *p “ ”;100 101 102 101100 101 102 103 104Listiterator q = List.rbegin();cout *q “ ”;+q;cout *q “ ”;+q;cout *q “ ”;-q;cout *q “ ”;104 103 102 103第120页/共177页int Sum = 0;for (iterator li = List.begin( ); li != List.end( ); +li) Sum = Sum + *li;a typical use of iterators:第121页/共177页Vector
50、objects can be used like objects of other types.void GetList(vector &A) int n = 0;while ( n An) +n;A.resize(n);第122页/共177页void PutList( vector &A ) for ( int i=0; iA.size( ); +i ) cout Ai endl;void GetValues(vector &A) A.resize(0);int Val;while (cin Val) A.push_back(Val);第123页/共177页void GetWords(vec
51、tor &List) List.resize(0);string s;while (cin s) List.push_back(s);第124页/共177页If standard input contained:a list of wordsto be read.thenvector A;GetWords(A);would set A in the manner:A0aA1listA2ofA3wordsA4toA5beA6read.The following would be also true:A00 = a;A32 = r;第125页/共177页int A33=1, 2, 3, 4, 5,
52、 6, 7, 8, 9;int B33=1, 2, 3, 4, 5, 6, 7, 8, 9;123456789A00 A01 A02 A10 A11 A12 A20 A21 A22第126页/共177页void GetWords(char ListMaxStringSize, int MaxSize, int &n) for (n=0; (nListn); +n) continue;const int MaxStringSize = 10;const int MaxListSize = 10;char AMaxListSizeMaxStringSize;int n;GetWords(A, Ma
53、xListSize, n);This would set A in the following manner:第127页/共177页A0a0A1list0A2of0A3 words0A4to0A5be0A6read.0A7a list of wordsto be read.Click here to view source第128页/共177页Pointers and dynamic memorypointers to constantsarrays and pointerscommand-line parameterspointers to functiondynamic objectsfr
54、ee storeoperators new and deleteexception handlingdangling pointersmemory leakdestructorscopy constructorthis pointerKey Concepts第129页/共177页int i = 100, *iPtr=0;char c = z, *s=0;Rational *rPtr=0;iPtr = &i;s = &c;iPtr = i;s = c;indirection(dereferencing)operatoraddress operatorillegal statementssciPt
55、riz100第130页/共177页int m =0;int n = 1;int* Ptr1 = &m;int *Ptr2 = Ptr1;int *Ptr3 = &n;*Ptr1 = *Ptr3;Ptr2 = Ptr3;Ptr3Ptr2Ptr11n0mPtr3Ptr2Ptr11n1m第131页/共177页Rational a(4, 3);Rational *aPtr = &a;(*aPtr).Insert(cout);aPtr-Insert(cout);A pointer object points to class-type objects(the selection operator has
56、 higher precedence)indirect member selector operator第132页/共177页int *PtrPtr;int i = 100;int *Ptr = &i;PtrPtr = &Ptr;PtrPtr = Ptr;illegal statement100PtrPtrPtri第133页/共177页Click to view source第134页/共177页suppose:char c1 = a;char c2 = b;const char *Ptr1 = &c1;char const *Ptr2 = &c1;char *const Ptr3 = &c2
57、;*Ptr1 and *Ptr2 are considered to be constants; Ptr1 and Ptr2 are not constant; Ptr3 is a constant; *Ptr3 is not constantread the declarations backwards第135页/共177页char Text9 = “Bryan”;for ( char *Ptr=Text; *Ptr!=0; +Ptr; ) cout Ptr endl;Bryanryanyanannint strlen(const char s) int i;for (i=0; si!=0;
58、 +i) continue;return i;第136页/共177页bcc32 SourceFileName ezWin.lib example:command-line parametersprogram nameClick to view source第137页/共177页int i = 100;int *ip;ip = new int(256);Rational *rp, *rlist;rp = new Rational(3, 4);rlist = new Rational5;delete ip;delete rp;delete rlist;memory leak第138页/共177页I
59、nheritancenpublic inheritanceprivate inheritancesingle inheritancemultiple inheritanceKey Concepts第139页/共177页Writing instrumentsLead pencilBallpoint Roller ball FountainCartridgeReservoirRetractableNonretractableRetractableMechanicalWoodenNonretractablePush buttonScrewClickerPen第140页/共177页第141页/共177
60、页C:WindowObjectDM:Window, LocationMF:GetPosition(), GetWindow(), SetPosition()C:ShapeDM:ColorMF:GetColor(), SetColor()C:RectangleShapeDM:Width, HeightMF:Draw(), GetWidth(), GetHeight(), SetSize()C:EllipseShapeDM:Width, HeightMF:Draw(), GetWidth(), GetHeight(), SetSize()C:LabelC: ClassDM: Data member
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 邢台学院《品牌设计》2022-2023学年第一学期期末试卷
- 邢台学院《过程控制系统实验》2023-2024学年期末试卷
- 教育机构安全生产信息管理制度
- 2024至2030年盐酸替罗非班氯化钠注射液项目投资价值分析报告
- 2024至2030年三维显示器项目投资价值分析报告
- 2024年渔用航海电脑项目可行性研究报告
- 2024年打蜡机转子模具项目可行性研究报告
- 2024至2030年中国高档笔记本数据监测研究报告
- 社区医疗设备管理制度
- 科技公司财务制度创新实践
- 生产现场作业十不干PPT课件
- 《孔乙己》公开课一等奖PPT优秀课件
- 美的中央空调故障代码H系列家庭中央空调(第一部分多联机)
- 物料承认管理办法
- 业主委员会成立流程图
- (完整版)全usedtodo,beusedtodoing,beusedtodo辨析练习(带答案)
- 小学综合实践活动方便筷子教案三年级上册精品
- 广联达办公大厦工程施工组织设计
- 疑难病例HELLP综合征
- Tiptop管理员手册
- 财务报告模版(向股东会、董事会)
评论
0/150
提交评论