1 / 182

第一章 基本 C++ 程序结构

第一章 基本 C++ 程序结构. C++ 程序结构 C++ 的数据类型 常量和变量 表达式与操作符 输入、输出. C++ 的程序结构. 例: /* 求一个表达式的值* / 注释 #include &lt;iostream.h&gt; 编译预处理 void main( ) 主函数 {int a,b,result; cout&lt;&lt; “ please input two number:<br> ” ;

Télécharger la présentation

第一章 基本 C++ 程序结构

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第一章 基本C++程序结构 • C++程序结构 • C++的数据类型 • 常量和变量 • 表达式与操作符 • 输入、输出

  2. C++的程序结构 例:/*求一个表达式的值*/ 注释 #include <iostream.h> 编译预处理 void main( ) 主函数 {int a,b,result; cout<<“please input two number:\n”; cin>>a>>b; result=3*a-2*b+1; cout>>“result is:”>>result;}

  3. 数据类型 • 数据类型 Int : 表示整数 Float : 表示不能用整数衡量的数 Char: 表示字符,经常用来构成字串,表示一 串文字 例: ‘a’‘#’“English” Double: 和Float类似,都是我们通常理解的小 数,和Float区别在于存储方式 Void: 表示没有值,通常用于表示函数没有返 回值

  4. 基本数据类型的修饰符号 • long可以修饰int和double类型,long int 为长整型,占4个字节,long double 为长双精度型占10个字节 • short只能修饰int,short int 占2个字节 • signed和unsigned可以修饰int和char • Signed、unsigned可以和short、long一起修饰int,例:unsigned short int

  5. 数据在计算机中的存储 参本书附录

  6. 常量和变量 • 标识符 定义:给程序中的实体----变量、常量、函 数、数组、结 构体以及文件所起的名字 命名规则:由字母、数字、下划线组成,且必须以字母 或下划线开头 下面是合法的标识符: Max_value sum TATAL y2k 下面是不合法的标识符 2END 1998 student%

  7. 注意几点: • c++中有一些如main、int、while、void是保留字,定义标识符时不能用这些保留字 • c++区分字符大小写 例:MAX Max • 标识符尽量接近所表示含义 • 标识符中间不能有空格

  8. 常量 • 常数 • 整型常数: 10进制整数:123,-345,123L 8进制整数:0123 16进制整数:0x123 8进制和16进制的数没有负数,只能表示无符号整数

  9. 实型常数 定点数形式:0.123 指数形式:123e5,4.5E-2 实型常数分为float,double,long double三 类,一个实 数若没有说明,表示double型 34.5f表示float型 123e5L表示long double 型

  10. 字符常数 例 ‘A’‘?’‘d’ 特殊字符:以‘\’开头的字符序列 例 : cout<<“please input two number:\n”; 见书P41表3.2 在内存中,字符数据以ASCII码存储,即以整数表示 例: ‘A’ ASCII码为65

  11. 所以C++中字符数据和整型数据之间可 相互赋值,只要注意其表示的范围合理 例:int i=97; int a=‘b’; char c=i; char c=97; cout<<i<<end1; cout<<a<<end1; cout<<c<<end1; cout<<c<<end1; char c=1234

  12. 字符串常数 是由一对双引号括起来的字符序列 “how do you do?” “abc345” 在C++中,字符串总是以‘\0’结束 例:“HELLO” 注意: char c=“ab“ ‘A’和“A”区别?输出时有无区别?

  13. 常量 用一些符号来表示这些常数,在程序中自初始化后值 保持不变 常量名的取名必须符合标识符定义规则 常量定义语法: const 常量类型 常量名=初始化值 例:const float PI=3.14; const int max=100; 注意:常量只能在定义时初始化,不能被重新赋值 const PI=3.14; …… PI=0;

  14. 变量 在程序中值可变的量,变量名符合标识符定义规则 变量定义语法:变量类型 变量名; unsigned int age,myweight; float distance; char grade; int number=0;(在定义时可以初始化) float a=98.7;

  15. 赋值语句:变量名=表达式; number=10; grade=‘r’; a=56.9; 注意类型匹配

  16. 输入、输出 • 终端输出 语法:cout<<要输出的内容 cout代表计算机系统的标准输出设备,通过向cout输出内 容来在显示器上显示信息 例:cout<<“I am a student”; I am a student cout<< “my name is”<<“jone”; my name is jone 例:float aver=12.34; aver=78.9; cout<<“the aver is”<<aver;

  17. 可换行显示 • 输出一个换行控制符end1 cout <<“my name is”<<end1<<“jone”; my name is jone • 输出换行字符‘\n’ cout<<“my name is \n”<<“jone”;

  18. 终端输入 float aver; cin>>aver; Int line,col; cin>>line>>col; 45 56

  19. 表达式与操作符

  20. 第二章 程序流程控制 • 复合语句 • 分支流程 • 循环流程

  21. 复合语句 简单语句: char a,b; ; 将多个语句组合在一起,用一对花括号将 这些语句组合在一起,称为复合语句 {int i=4; cout<<i;} 不需要分号结尾

  22. 分支流程 • If 语句 语法:If (条件表达式) 语句1 [else 语句2] If (a<b) cout<<a; else cout<<b;

  23. If语句嵌套 语法:If (条件表达式1) If (条件表达式2) 语句1 else 语句2

  24. if(grade>=90) cout<<“优” else if(grade>=80) cout<<“良” else if(grade>=60) cout<<“及格” else cout<<“不及格” if(grade>=60) if(grade>=90) cout<<“优” else if (grade>=80) cout<<“良” else cout<<“及格” else cout<<“不及格“

  25. 条件运算符 表达式1?表达式2:表达式3 If(a>b) max=a; Else max=b; max=(a>b)?a:b

  26. Switch语句 语法:switch(表达式) { case 值1: 语句组1; case 值2: 语句组2; …… [default: 语句组0;] };

  27. Swith(day){ Case 0:cout<<“Sunday”;break; Case 1:cout<<“monday”;break; Case 2:cout<<“tuseday”;break; Case 3:cout<<“wednesday”;break; Case 4:cout<<“thursday”;break; Case 5:cout<<“friday”;break; Case 6:cout<<“saturday”;break; Default:cout<<“unknown week day”;break;};

  28. 例:char grade=‘B’; switch(grade) {case ‘A’:cout <<“85~100\n”;break; case ‘B’:cout <<“70~84\n”;break; case ‘C’:cout <<“60~69\n”;break; case ‘D’:cout <<“<60\n”;break; default:cout <<“error\n”;}

  29. While语句 语法: While (条件表达式) 循环体 防止出现死循环 i=1; While(i<=10) {sum=sum+i; i--;} 循环流程

  30. #include <iostream.h> void main( ) { int sum=0; int i=1; while(i<=100) {sum+=i; i++;} cout<<sum;}

  31. While(条件表达式1) { 语句1; if(条件表达式2) break; 语句2; } 语句3; break语句使用:用在while,do…while,for和switch语句中, 在循环语句中,break用来从最近的封闭循环体内跳出, 在switch语句中,break用来跳出switch语句。

  32. #include <iostream.h> void main( ) {int sum=0; int val; int n=0; cout <<“input some integers:\n”; while (1) {cin>>val; if (val==0) break; sum=+val; n++;} cout<<sum/n; }

  33. While(条件表达式1) { 语句1; if(条件表达式2) continue; 语句2; } 语句3; Continue语句使用:用在循环语句中,作用为结束本次循 环,即跳过循环体中尚未执行的语句,继续进行下一次 是否执行循环的判定。

  34. #include <iostream.h> void main( ) {int sum=0; int val; int n=0; cout <<“input some integers:\n”; while (1) {cin>>val; if (val==0) break; if (val<0) continue; sum=+val; n++;} cout<<sum/n; }

  35. do …while语句 语法:do 循环体 while(条件表达式); 注意:与while语句的区别 循环体中也可以使用continue和break语句

  36. #include <iostream.h> void main( ) {int sum=0; int val; int n=0; cout <<“input some integers:\n”; do {cin>>val; if (val==0) break; if (val<0) continue; sum=+val; n++;} while(1); cout<<sum/n; }

  37. For循环 语法:for(初始化表达式;条件表达式;循环表达式) 循环体语句 初始化表达式; while(条件表达式) {循环体语句; 循环表达式;}

  38. int sum=0; int i=1; while(i<=100) {sum+=i; i++;} int sum=0; for(int i=1;i<=100;i++) sum+=i;

  39. #include <iostream.h> void main( ) {int score; for(int i=0;i<10;i++) {cout<<“input a student’s score:\n”; cin>>score; score=1.05*score; if(score>100) score=100; cout<<score<<end1;}

  40. 第三章 函数 • 函数机制 • 函数定义 • 函数调用 • 函数参数的传递方式

  41. 函数机制 把相关的语句组织在一起,并给它们注明相应的名称, 利用这种方法把程序分块,这种形式的组合就称为函数 函数的使用通过函数调用开始启动 程序总是从 main( )函数开始启动 例:main( ) {function1( ); function2( );} void function1( ) {…… } void function2( ) {…… }

  42. 函数定义 语法:返回类型 函数名(函数参数表) {函数体 } • 函数名 • 函数参数:函数完成功能所需要的输入信息,也叫形参格式如下: int max(int a,int b) 与形参对应的是实参,实参是在调用函数的地方传给函数 例: int max(int a,int b) {…… } void main( ) { int val1=3,val2=4; cout<<max(val1,val2);}

  43. 返回类型:就是函数返回结果值的数据类型,若为返回类型:就是函数返回结果值的数据类型,若为 void就表示函数不返回值 函数通过在函数体中用return语句来返回函数值 main( ) f1( ) {…… {…… u=f1(a,t); return; ……} } 几种形式:return; 没有返回值 return(表达式);

  44. 函数体:是函数功能的实现,由一些语句组成,在函数体中可以定义变量和常量,叫局部量,在函数体外定义的变量或常量是全局量函数体:是函数功能的实现,由一些语句组成,在函数体中可以定义变量和常量,叫局部量,在函数体外定义的变量或常量是全局量 例:int glo; void main( ) {int loc1; loc1=3; loc1glo glo=4; } void func( ) {int loc2; loc2=3; loc2 glo=5;}

  45. 注意:C++不允许函数定义嵌 void main( ) void main( ) {…… {…… void func( ) } {…… void func( ) } {…… ……. } }

  46. 函数调用 • 调用一个函数就是暂时中断现有程序的运行,转去执行被调用函数,当被调用函数执行结束以后,再返回到中断处继续执行的过程 main( ) f1( ) f2( ) {…… {…… {…… u=f1(I,t); c=f2(a,b); retrun; ……} ……} } 语法:函数名(实参表)

  47. 函数参数的传递方式 • 指针参数 指针的值是一个地址,因而可以通过指针来间接访问另外一个内存地址,当函数的参数是指针时,它的实在参数的值必须是一个地址。

  48. 例:#include <iostream.h> void swap(int a,int b) {int temp=a; a=b; b=temp;} void main( ) {int val1=10;val2=20; cout<<val<<val2<<end1; swap(val1,val2); cout<< val1<<val2<<end1;}

  49. #include <iostream.h> void swap(int *a,int * b) {int temp=*a; *a=*b; *b=temp;} void main( ) {int val1=10;val2=20; cout<<val<<val2<<end1; swap(&val1,&val2); cout<< val1<<val2<<end1;}

  50. 引用参数 当函数的形式参数是引用类型时,它实际上是对实在参数所代表的变量或常量的引用,它自已没有独立的内存空间 注意:引用参数前面加‘&’,但不要在实参前面加‘&‘ 引用参数对应的实参必须有对应的内存空间, 实参不能是表达式 例:swap(12,34); swap(val1+4,val2*6);

More Related