includeiostream using namespace std void printmessage cout How

  • Slides: 70
Download presentation

#include<iostream> using namespace std; void printmessage () { cout << "How do you do!"

#include<iostream> using namespace std; void printmessage () { cout << "How do you do!" << endl ; } 函数调用语句 void main() { printmessage() ; } 台州学院·计算机系·面向对象程序设计(C++) 9

#include<iostream> using namespace std; double max ( double x , double y ) {

#include<iostream> using namespace std; double max ( double x , double y ) { if ( x > y ) return x ; else return y ; } void main() { double a, b; cin >> a >> b ; double m = max( a, b ); cout << max( m, 3. 5 ) << endl ; } 函数调用表达式 台州学院·计算机系·面向对象程序设计(C++) 10

#include<iostream> using namespace std; double max ( double x , double y ) {

#include<iostream> using namespace std; double max ( double x , double y ) { if ( x > y ) return x ; else return y ; 实际参数是函数参数 } void main() { double a, b; cin >> a >> b ; double m = max( a, b ); cout << << max( m, max( a, b))<< , a+3. 5 cout a+3. 5 endl ; ) << endl ; } 台州学院·计算机系·面向对象程序设计(C++) 11

math. h中几个常用的数学函数 函数原型 说明 int abs( int n ); n的绝对值 double cos( double x

math. h中几个常用的数学函数 函数原型 说明 int abs( int n ); n的绝对值 double cos( double x ); x(弧度)的余弦 double exp( double x ); 指数函数ex double fabs( double x ); x的绝对值 double fmod( double x, double y ); x/y的浮点余数 double log( double x ); x的自然对数(以e为底) double log 10( double x ); x的对数(以 10为底) double pow( double x, double y ); x的y次方(xy) double sin( double x ); x(弧度)的正弦 double sqrt( double x ); x的平方根 double tan( double x ); x(弧度)的正切 台州学院·计算机系·面向对象程序设计(C++) 15

// 用库函数求正弦和余弦值 #include <iostream> 包含头文件 #include <math. h> using namespace std; void main() {

// 用库函数求正弦和余弦值 #include <iostream> 包含头文件 #include <math. h> using namespace std; void main() { double pi = 3. 1415926535; double x, y; 调用库函数 x = pi / 2; y = sin( x ); cout << "sin( " << x << " ) = " << y << endl ; y = cos( x ); cout << "cos( " << x << " ) = " << y << endl ; } 台州学院·计算机系·面向对象程序设计(C++) 16

//包含局部和全局变量的程序 #include <iostream> using namespace std; void func(); int i=0; void main() { float

//包含局部和全局变量的程序 #include <iostream> using namespace std; void func(); int i=0; void main() { float p=9. 0; cout<<i<<‘t‘<<p<<endl; func(); } float z=9. 0; void func() { int j=5; cout<<j <<‘t ‘<<z <<‘t‘ <<i<<endl; return; } 台州学院·计算机系·面向对象程序设计(C++) 20

// 例 #include <iostream> using namespace std; int func(); void main() C++不对自动变量初始化 { cout

// 例 #include <iostream> using namespace std; int func(); void main() C++不对自动变量初始化 { cout << func() << endl ; 静态变量默认初始化值为 0 } int func() auto a = 1 { int a = 0 ; static b = 2 static int b = 1 ; 3 a ++ ; auto a = 1 b ++ ; static b = 3 cout << "auto a = " << a << endl ; 4 cout << "static b = " << b << endl ; return a + b ; } 台州学院·计算机系·面向对象程序设计(C++) 22

// 求阶乘 #include<iostream> using namespace std; long Factorial ( int ) ; void main

// 求阶乘 #include<iostream> using namespace std; long Factorial ( int ) ; void main () 非递归调用 { int k ; cout << "Compute Factorial(k) , Please input k: " ; cin >> k ; cout << k << "! = " << Factorial(k) << endl ; } long Factorial ( int n ) { if ( n == 1 ) 递归调用 return 1 ; else return n * Factorial ( n - 1 ) ; } long Factorial ( int n ) { int i; long f=1; for(i=1; i<=n; i++) f=f*i return f; } • 递归结构是更强的循环结构 • 所有的循环结构都可以写成 递归结构,反之不一定行 台州学院·计算机系·面向对象程序设计(C++) 37

# include <iostream. h> int abs ( int a ) ; 重载示例: double abs

# include <iostream. h> int abs ( int a ) ; 重载示例: double abs ( double f ) ; 参数个数相同 void main () { cout << abs ( -5 ) << endl ; 参数类型不同 cout << abs ( -7. 8 ) << endl ; } int abs ( int a ) { return a < 0 ? -a : a ; } double abs ( double f ) { return f < 0 ? -f : f ; } 台州学院·计算机系·面向对象程序设计(C++) 44

重载示例: 参数个数不同 # include <iostream. h> int max ( int a , int b

重载示例: 参数个数不同 # include <iostream. h> int max ( int a , int b ) ; int max ( int a , int b, int c ) ; void main () { cout << max ( 5, 3 ) << endl ; cout << max (4, 8, 2 ) << endl ; } int max ( int a , int b ) { return a > b ? a : b ; } int max ( int a , int b, int c ) { int t ; t = max ( a , b ) ; return max ( t , c ) ; } 台州学院·计算机系·面向对象程序设计(C++) 45

my. Area. h my. Circle. cpp double circle( double radius ) ; double rect(

my. Area. h my. Circle. cpp double circle( double radius ) ; double rect( double width, double length ) ; const double pi = 3. 14 ; double circle ( double radius ) { return pi * radius ; } my. Rect. cpp double rect ( double with, double length ) { return with * length ; } 台州学院·计算机系·面向对象程序设计(C++) 53

my. Main. cpp #include<iostream. h> #include "my. Area. h" void main() { double width,

my. Main. cpp #include<iostream. h> #include "my. Area. h" void main() { double width, length ; cout << "Please enter two numbers: n" ; cin >> width >> length ; cout << "Area of recttangle is: " << rect( width, length ) << endl ; double radius ; cout << "Please enter a radius: n" ; cin >> radius ; cout << "Area of circle is: " << circle( radius ) << endl ; } 台州学院·计算机系·面向对象程序设计(C++) 54

100 200 300 1100 500 600 #include <iostream. h> 100 int n=100; 500 200

100 200 300 1100 500 600 #include <iostream. h> 100 int n=100; 500 200 300 int main(){ int i=200, j=300; cout<< n<<'t'<<i<<'t'<<j<<endl; { //内层 内n=500+600 int i=500, j=600, n; =1100 n=i+j; 内 j= 600 cout<< n<<'t'<<i<<'t'<<j<< endl; //输出局部变量n 内 i= 500 cout<<: : n<<endl; //输出全局变量n 外 j=300 } n=i+j; //修改全局变量 外 i=200 cout<< n<<'t'<<i<<'t'<<j<< endl; 全局n= 100 500 200+300=500 return 0; } 台州学院·计算机系·面向对象程序设计(C++) 57

//改错练习 #include<iostream> using namespace std; int main() { int a, b; cout<<"输入两整数:"<<endl; cin>>a>>b; cout<<“a="<<a<<'t'<<"b="<<b<<endl;

//改错练习 #include<iostream> using namespace std; int main() { int a, b; cout<<"输入两整数:"<<endl; cin>>a>>b; cout<<“a="<<a<<'t'<<"b="<<b<<endl; if(b>=a) { int t; t=a; a=b; b=t; } cout<<"a="<<a<<'t'<<"b="<<b<<endl; cout<<t<<endl; return 0; } 台州学院·计算机系·面向对象程序设计(C++) 58