c++快速入门3:控制结构和函数及递归

2021-07-28  本文已影响0人  python测试开发

if

C++的if关键字执行基本的条件测试。

ifelse.cpp

#include <iostream>
using namespace std ;

int main()
{
  int num = 8 ;
  char letter = 'A' ;

  if( num > 5 )
  {
    cout << "Number exceeds five" << endl ;

    if( letter == 'A' )
    {
      cout << "Letter is A" << endl ;
    }

  }
  else { cout << "Number is five or less" << endl ; }

  return 0 ;
}

测试布尔值时可以使用速记法--if ( flag == true ) 可以写成if ( flag )。
避免嵌套超过三层的if语句--以避免混乱和错误。

switch

#include <iostream>
using namespace std ;

int main()
{
  int num = 3 ;

  switch ( num )
  {
    case 1 : cout << num << " : Monday" ; break ;
    case 2 : cout << num << " : Tuesday" ; break ;
    case 3 : cout << num << " : Wednesday"  ; break ;
    case 4 : cout << num << " : Thursday" ; break ;
    case 5 : cout << num << " : Friday" ; break ;

    default : cout << num << " : Weekend day" ;
  }
  cout << endl ;
  return 0 ;
}

for循环

forloop.cpp

#include <iostream>
using namespace std ;

int main()
{
  int i , j ;

  for ( i = 1 ; i < 4 ; i++ )
  {  
    cout <<  "Loop iteration: " << i << endl ;

    for ( j = 1 ; j < 4 ; j++ ){
      cout << "    Inner loop iteration: " << j << endl ;
    }
  }

  return 0 ;
}

while循环

while.cpp

#include <vector>
#include <iostream>
using namespace std ;

int main()
{
  int i = 0 ; 
  vector <int> vec(10) ;

  while ( i < vec.size() )
  {
    i++ ; // 1 - 10

    if ( i== 3 ) { cout << " | Skipped" ; continue ; }

    if ( i == 8 ) { cout << endl << "Done" ; break ; }

    vec[ i-1 ] = i ;    // vec[0] = 1, vect[1] = 2, etc.

    cout << " | " << vec.at( i-1 ) ;
  }
  cout << endl;

  return 0 ;
}

函数

使用函数有三个主要好处。

函数中声明的变量只能在该函数中局部使用,而不能在其他函数中全局使用。这种限制被称为 "变量范围"。

scope.cpp
图像通过指定要包括的C++库类和要使用的命名空间前缀来启动一个新程序

#include <iostream>
using namespace std ;

float bodyTempC();
float bodyTempF();

int main()
{
  cout << "Centigrade: " << bodyTempC() << endl ;
  cout << "Fahrenheit: " << bodyTempF() << endl ;
  return 0 ;
}

float bodyTempC()
{
  float temperature = 37.0 ;
  return temperature ;
}

float bodyTempF()
{
  float temperature = 98.6 ;
  return temperature ;
}

参数传递

函数调用经常向函数提供参数值。这些参数可以是任何数量和数据类型,但它们必须与函数原型声明中指定的参数一致。"按值传递 "的过程中,传递给函数的参数只提供原始值的副本。

args.cpp

#include <iostream>
using namespace std ;

float fToC( float degreesF = 32.0 ) ;

int main()
{
  float fahrenheit, centigrade ;

  cout << "Enter a Fahrenheit temperature: " ;
  cin >> fahrenheit ;

  centigrade = fToC( fahrenheit ) ;

  cout << fahrenheit << "F is " << centigrade << "C" ;
  cout << endl << "Freezing point: " << fToC() << "C" ;

  cout << endl ;
  return 0 ;
}

float fToC( float degreesF )
{
  float degreesC = ( (5.0 / 9.0 ) * ( degreesF - 32.0 ) ) ;
  return degreesC ;
}

函数重载

函数 "重载 "允许参数在数字、数据类型或数字和数据类型上不同的同名函数。编译器通过识别其参数数量和数据类型,将函数调用与对应函数相匹配--这一过程被称为 "函数解析"。

#include <iostream>
using namespace std ;

float computeArea( float ) ;
float computeArea( float, float ) ;
float computeArea( char, float, float ) ;

int main()
{
  float num, area ;

  cout << "Enter dimension in feet: " ;
  cin >> num ;

  area = computeArea( num ) ;
  cout << "Circle: Area = " << area << " sq.ft." << endl ;

  area = computeArea( num, num ) ;
  cout << "Square: Area = " << area << " sq.ft." << endl ;

  area = computeArea( 'T', num, num ) ;
  cout << "Triangle: Area = " << area << "sq.ft." << endl ;

  return 0 ;
}

float computeArea( float diameter )
{
  float radius = ( diameter / 2 ) ;
  return ( 3.141593 * (radius * radius) ) ;
}

float computeArea( float width, float height )
{
  return ( width * height) ;
}

float computeArea( char letter, float width , float height )
{
  return ( width / 2 ) * height ;
}

递归

#include <iostream>
using namespace std ;

long factorial( long n )
{
  long result ;
  if( n == 1 ) result = 1 ;
  else
  result = ( factorial( n - 1 ) * n );
  return result ;
}

long computeFactorials( long num, long max )
{
  cout << "Factorial of " << num << ": " ;
  cout << factorial( num ) << endl ;
  num++ ;
  if( num > max ) return EXIT_SUCCESS;
  else computeFactorials( num , max ) ;
  return EXIT_SUCCESS;
}

// Replace the factorial declaration and definition with the inline declaration below.
// inline long factorial( long n )
// { return ( n == 1 ) ? 1 : ( factorial( n - 1 ) * n ); }

// BONUS - Uncomment the function below and insert "verbose(n);"
// at the start of the factorial function to see what is happening in this program.
// inline void verbose(long n)
// {cout << n ; ( n == 1 ) ? cout << " = " : cout << "x" ; }

int main()
{
  computeFactorials( 1, 80 ) ;
  return 0 ;
}

小结

上一篇下一篇

猜你喜欢

热点阅读