• 函数名之后不要留空格,紧跟左括号‘(’,以与关键字区别


    函数名之后不要留空格,紧跟左括号‘(’,以与关键字区别。

     1 #include <iostream>
     2 
     3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     4 using namespace std;
     5 
     6 //参数为函数指针的函数
     7 int get_result(int a, int b, int (*sub)(int,int))
     8 {
     9     int r;
    10     r=sub(a,b);
    11     return r;
    12 }
    13  
    14 //计算最大值
    15 int max(int a, int b)
    16 {
    17     cout<<"In max"<<endl;
    18     return((a > b) ? a: b);
    19 }
    20 
    21 //计算最小值
    22 int min(int a, int b)
    23 {
    24     cout<<"In min"<<endl;
    25     return((a < b) ? a: b);
    26 }
    27 
    28 //求和
    29 int sum(int a, int b)
    30 {
    31     cout<<"In sum"<<endl;
    32     return(a+b);
    33 }
    34 
    35 
    36 int main(int argc, char** argv) {
    37     int a,b,result;
    38 
    39     //测试3次
    40     for (int i=1;i<=3;i++) {
    41         cout<<"Input a and b :";
    42         cin>>a>>b;
    43 
    44         cout<<i<<"	get_result("<<a<<","<<b<<", &max):"<<endl;
    45         result =get_result(a, b, &max);
    46         cout<<"Max of "<<a<<" and "<<b<<" is "<<result<<endl;
    47    
    48         result = get_result(a, b, &min);
    49         cout<<"Min of "<<a<<" and "<<b<<" is "<<result<<endl;
    50 
    51         result = get_result(a, b, &sum);
    52         cout<<"Sum of "<<a<<" and "<<b<<" is "<<result<<endl; 
    53     }
    54 
    55     return 0;
    56 }
  • 相关阅读:
    小埋的Dancing Line之旅:比赛题解&热身题题解
    洛谷P1311 选择客栈
    洛谷 P1039 侦探推理
    洛谷P1140 相似基因
    反⑨baka拖更大队:临时约法
    随机数据生成器
    洛谷P2285 【[HNOI2004]打鼹鼠】
    洛谷P3958 奶酪
    YII 关联查询
    Filter
  • 原文地址:https://www.cnblogs.com/borter/p/9413169.html
Copyright © 2020-2023  润新知