美文网首页
c++快速入门3:控制结构和函数及递归

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

作者: python测试开发 | 来源:发表于2021-07-28 09:25 被阅读0次

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 ;
}

小结

  • if语句将给定的测试表达式评估为布尔值的真或假。
  • 在if语句后面的大括号中包含的语句只有在评估结果为真时才会被执行。
  • if和else关键字用于根据测试表达式的结果执行条件性分支。
  • witch语句是条件性分支的另一种形式,它将case语句与给定值相匹配。
  • for 循环结构有参数声明初始化器、测试表达式和增量器或减量器。
  • while循环和do-while循环必须在其循环体中始终有一个递增器或递减器。
  • 任何类型的循环都可以通过在循环体中加入break语句来立即终止。
  • 任何类型的循环的单次迭代都可以通过在循环体中加入continue语句而被跳过。
  • 函数通常在程序开始时被声明为原型,并在主函数之后定义。
  • 在一个函数中声明的变量只能从该函数中访问,因为它们只有局部范围。
  • 如果在函数原型和定义中声明了参数,那么值可以被传入函数。
  • 重载函数具有相同的名称,但声明的参数数量或类型不同。
  • 递归函数重复调用自己,直到满足测试条件。
  • 只有一个或两个语句的简短的函数定义可以用inline关键字代替原型来声明。

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/hqxomltx.html