C++课件(精华版)

上传人:仙*** 文档编号:243040735 上传时间:2024-09-14 格式:PPT 页数:54 大小:743KB
返回 下载 相关 举报
C++课件(精华版)_第1页
第1页 / 共54页
C++课件(精华版)_第2页
第2页 / 共54页
C++课件(精华版)_第3页
第3页 / 共54页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,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 expression,evaluate the expression,print the result,4,main Function,int main(void),return 0;,cout“Hello, Tom!”;,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 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 expressions,cout “hello, world!”;,cout 5;,cout x;,cout x+5;,cout x“+5=”x+5;,”,n”, “t”, “ “, endl ,setw(n),setw(n): #include ,cout,7,C+ Tokens,A token is the,smallest element,of a C+ program that is meaningful to the compiler.,Kinds of tokens:,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,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,“name”, “your_phonenumber”,ture,false,0x12,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,1byte,Integer types,int,2-4bytes(2) -3276832767,short,(2),&,long(4),-21474836482147483647,Floating-point types,double,8bytes,float,4bytes,13,1 byte,(8 bits),2 bytes,(16 bits),4 bytes,(32 bits),8 bytes,(64 bits),16 bytes,(128 bits),short,char,bool,int,float,long,double,long double,Variables Literals,Boolean literals: bool t;,true, false,Character literals: char c;,a, x, 4, n, $,Integer literals: int x;,0, 1, 123, -6, 0x34, 0xa3,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!”,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 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;,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=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,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;,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,20000,a,?,c,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 s t u v w x y z,_ $ # ( ) % : ; . ? * + / & | ! = , ,24,ASCII code,25,Chars to ints,Convert between a char and its ASCII int,int num = a;,char let = 97;,Cycle through the ASCII table with math!,char bee = a + 1;,26,Operators,+, +,-, -,*,/,%,27,Arithmetic Assignment Operators,Symbol,Example,Equivalent,+=,x += 1;,x = x + 1;,*,=,doub *= 2;,doub = doub * 2;,-=,n -= 5;,n = n - 5;,/=,third /= 3;,third = third / 3;,%=,odd %= 2;,odd = odd % 2;,28,Think about:,int a=12;,a += a -= a * a ;,cout a;,29,Expressions,Boolean type:,bool,(,true,and,false,),Equality operators:,= =,(equal),!=,(not equal),Logical operators:,&,(and),|,(or),!,(not),Relational operators:,(greater than),=,Character type:,char,(,a,7, and,),Integer types:,short, int, long,arithmetic operators:,+, -, *, /, %,Floating-point types:,float, double,(,12.45,and,1.234e3,),arithmetic operators:,+, -, *, /,/2.5.cpp input and output,#include ,using namespace std;,/ 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,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,Think about: the output?,int x,y;,x = 10;,y = x+;,cout y endl;,int a,b;,a = 10;,b = +a;,cout b 2) false,(x y) & (y 0) true,(x 0) false,(x 0) true,36,Think about,int a,b,c,d,k=1,h=0;,a=! k,b=kh,c=h,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 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 year 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 ,return true;,Conditions2,if (condition),/do this,else,/do that,if (x0),cout-x;,else,coutb) cout a;,else cout b;,Example,void main(),char a;,couta;,if(a=a &a=A &a96),a-=32;,else,a+=32;,coutb),if (bc) coutabc) coutacb;,else coutcac) coutbac) coutbca;,else coutcb0,y=f(x)= 0 x=0,-1 x0) k=1;,else if (x=0) k=0;,else k=-1;,coutx y)? 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,if (condition),/do this,conditions4,Switches,switch (expression),case i:,/do this,break;,case j:,/do that,break;,default:,/do other thing,Switch example,#include ,using namespace std;,int main(),int x = 6;,switch(x),case 1:,cout x is 1n;break;,case 2:,case 3:,cout x is 2 or 3; break;,default:,cout x is not 1, 2, or 3;,return 0;,/weekday,void weekday(int d),switch (d) ,case 7:,cout Sunday;break;,case 1:,cout Monday;break;,case 2:,cout Tuesday;break;,case 3:,cout Wednesday;break;,case 4:,cout Thursday;break;,case 5:,cout Friday;break;,case 6:,cout Saturday;break;,return;,For Loops,for (initializations; condition; updates),/do this again and again,for(int i = 0; i 10; i+),cout i;,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!