C++课件(精华版)_第1页
C++课件(精华版)_第2页
C++课件(精华版)_第3页
C++课件(精华版)_第4页
C++课件(精华版)_第5页
已阅读5页,还剩49页未读 继续免费阅读

下载本文档

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

文档简介

1、2、c+ programming,a simple c+ program types, variables, expressions,how to write a program,find the whole steps in the real model use some graph or nature language to describe realize with computer language,workflow,pseudo code,a first idea: int main() variables while (condition) analyze the expressi

2、on evaluate the expression print the result,4,main function,int main(void) return 0;,int x=5; int y=8; int z=x+y; coutx“+”y“=“zendl,5,2.1.cpp,include using namespace std; /* * a simple program for demonstrating the basics of a c+ project. * * it does a good job of demonstrating c+ fundamentals, but

3、a * terrible job with the poetry. */ int main() cout dont you just feel like a louse; cout endl; cout to learn that your new theorem was proved by gauss?; cout endl; return 0;,6,cout,cout,7,c+ tokens,a token is the smallest element of a c+ program that is meaningful to the compiler. kinds of tokens:

4、 identifiers, keywords, literals, operators, punctuators, and other separators. tokens are usually separated by white space. white space can be one or more blanks, horizontal or vertical tabs, new lines, form feeds or comments,8,c+ keywords,auto const double float int short struct unsigned unsigned

5、break break continue elsefor long switch void case sizeof typedef char do if return static union while ,etc,9,commenting,* *name of program *information of author *function of program * */ /a sample int main() / /*this is in the comment this is also in the comment */ .,10,constants,1,2,3 1.2, 4.50 “

6、name”, “your_phonenumber” ture,false 0 x12 1,a,$,xhh,ddd #define pi 3.141592 #define price 100 const int pi= 3.141592,11,2.2.cpp,include using namespace std; int main() int x; int y; x = 3; y = 4; cout x+y endl; return 0;,12,variables types,built-in types boolean type bool 1byte character types char

7、 1byte integer types int 2-4bytes(2) -3276832767 short (2) true, false character literals: char c; a, x, 4, n, $ integer literals: int x; 0, 1, 123, -6, 0 x34, 0 xa3 floating point literals: double d; float f; 1.2, 13.345, .3, -0.54, 1.2e3, . 3f, .3f string literals: string s; asdf, howdy, all yall,

8、14,variables names,choose meaningful names confuse mtbf, tla, myw, nbv short names can be meaningful x is a local variable i is a loop index dont use long names ok: partial_sum, element_count, staple_partition too long: the_number_of_elementsremaining_free_slots_in_the_symbol_table,15,not variables

9、names,a name in a c+ program starts with a letter, contains letters, digits, and underscores (only) x, number_of_elements, fourier_transform, z2 not names: 12x, time$to$market, main line not start names with underscores: _foo not use keywords int if while,16,declaration and initialization,int a = 7;

10、 int b = 9; char c = a; double x = 1.2; string s1 = hello, world; string s2 = 1.2,17,9,a,1.2,13 hello, world,4 | 1.2,b,c,x,s1,s2,7,a,constant variables,const int i=5; i=6; /error,18,think about,int a,b,c=2; int x,y,z,10; int m=2; int n=3; long int sum=0,add; long hello; char a=m; char b,c,d; char m=

11、65,n=a+1; float a,b,ccc=3.1415; float sum=0.0; double f1, f2=1.414e12,19,assignment and increment,int a = 7; a = 9; a = a+a; a += 2; +a,20,7,9,18,20,21,a,think about,int a=10, b; b=a; float x; int k=300; x=k; float x=3.14; int n; n=x+6; float x=3.14; int n; n=3; cout x+n; 3.0/9 or (float)3/9,2.3.cpp

12、 a program to illustrate integer overflow,include using namespace std; /* * a program to illustrate what happens when large integers * are multiplied together. */ int main() int million = 1000000; int trillion = million * million; cout according to this computer, million squared is trillion . endl;,

13、22,a type-safety violation(“implicit narrowing”,int main() int a = 20000; char c = a; int b = c; if (a != b) cout oops!: a != b n; else cout wow! we have large charactersn;,23,c+ character set,0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r

14、 s t u v w x y z _ $ # ( ) % : ; . ? * + / char let = 97; cycle through the ascii table with math! char bee = a + 1,26,operators, + -, - * / ,27,arithmetic assignment operators,28,think about: int a=12; a += a -= a * a ; cout a,29,expressions,boolean type: bool (true and false) equality operators:=

15、= (equal), != (not equal) logical operators: / a program to investigate the behavior of the mod (%) operation. int main() int a, b; cout ; cin a; cout ; cin b; cout a % b = a%b endl; return 0;,30,think about,17/3=5? 5/9=0? int n; n%2+(n+1)%2=? n=2; n+; n=n+1 n=2: n+; n-; +n;-n; r=2; m=-n; p=r+; m=10

16、,n=100; p=(n+n,n*n,n-2); p=n+n,n*n,n-2,31,think about,int i=2; (i+)+(i+)+(i+) ; couti; i=2; (-i)+(-i) ; couti; i=2; i=(i+i+i) ; couti; i=2; i=(i-i) ; couti,2.6.cpp string input,include #include using namespace std; int main() cout first second; string name = first + + second; cout hello, name n;,33,

17、think about: the output,int x,y; x = 10; y = x+; cout y endl; int a,b; a = 10; b = +a; cout b endl,34,boolean expressions,a b a a=! k,37,38,statements,a = b; double d2 = 2.5; if (x = 2) y = 4; while (true) cout“hello”; for(int i=0;i8;i+) cout i; int average = (length+width)/2; return x,floor,int flo

18、or(float x) return (int) x;,39,ceiling,int ceiling(float x) if (x (int) x 0.0) return (int)(x + 1.0); else return (int) x;,40,to be continued,3、c+ programming,control statements,conditions1,if (condition) /do this,void main() int x; cin x; if (x0)x=-x; cout x; coutsqrt(x);,bool leapyear(int y,*any y

19、ear divisible by 4 except centenary years not divisible by 400*/ bool leapyear(int y) / any year divisible by 4 except centenary years not divisible by 400 if (y%4) return false; if (y%100=0,conditions2,if (condition) /do this else /do that,if (x0) cout-x; else coutx,void main() int a=5,b=8; if (ab)

20、 cout a; else cout b;,example,void main() char a; couta; if(a=a,conditions3,if (condition) /do this else if (condition) /do that else /do other thing,void main() int a=3,b=17,c=5; if (ab) if (bc) coutc) coutc) coutc) coutbca; else coutcba;,1 x0 y=f(x)= 0 x=0 -1 x0) k=1; else if (x=0) k=0; else k=-1; coutx k;,example,xy)? x:y,get the max one of a and b int max(int a, int b) int m = a b ? a : b ; return m;,if (condition) /do this else

温馨提示

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

评论

0/150

提交评论