• 函数巩固


      

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int add(int num)//按值传递
     4 {
     5     num++;
     6 }
     7 int add1(int& num)//参数传递(引用传递)
     8 {
     9     num++;
    10 }
    11 
    12 int main()
    13 {
    14     int num = 10;
    15     add(num);//按值传递,传递的num是一个副本,地址空间相与之前发生了变化
    16     cout << num << endl;//10
    17     add1(num);//引用传递,num是原数据的别名,地址相同
    18     cout << num << endl;//11
    19     return 0;
    20 }

    使用const关键字防止不希望修改的数组

     

     

      

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 void show(int array[], int len)
     4 {
     5     for(int i = 0; i < len ;i++)
     6     {
     7         cout << array[i]++ << " , ";
     8     }
     9     cout << endl;
    10 }
    11 void show1(const int array[], int len)//当我们不希望数组内容在函数体里面被修改的时候,我们可以对数组参数加const,变成一个常量
    12 {
    13     for(int i = 0; i < len ;i++)
    14     {
    15         //报错
    16         //cout << array[i]++ << " , ";
    17     }
    18     cout << endl;
    19 }
    20 void show2(const int* begin,const int* end)//使用数组区间的函数
    21 {
    22      for(const int* ptr = begin; ptr <= end; ptr++){
    23         cout <<*ptr << ",";
    24      }
    25 }
    26 
    27 int main()
    28 {
    29     int array[5] = {1,2,3,4,5};
    30     int array1[5] = {1,2,3,4,5};
    31     show(array,5);//1 , 2 , 3 , 4 , 5 ,
    32     show(array,5);//2 , 3 , 4 , 5 , 6 ,
    33     const int* ptr_begin = array;
    34     show2(ptr_begin, ptr_begin - 1 + sizeof(array)/sizeof(int));
    35     return 0;
    36 }

     

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 void show(double (*arr)[5],int len){//二维数组做参数,行数大小要做参数传入
     4    for(int i = 0; i < len; i++)
     5    {
     6        for(int j = 0; j < 5; j++){
     7         cout << arr[i][j] << " || " << *(*(arr + i)+j) << " , " ;
     8        }
     9        cout <<endl;
    10    }
    11 }
    12 int main()
    13 {
    14     double  array[2][5] = {
    15         { 1, 2, 3, 4, 5},
    16         { 1.1, 2.1, 33, 3.2,1.9}
    17     };
    18     show(array,2);
    19     return 0;
    20 }

    函数指针是指向函数的一种指针

     

     

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int isZero = 0;
     4 double add(double a, double b)
     5 {
     6     return  a + b;
     7 }
     8 double subtraction(double a,double b)
     9 {
    10     return a - b;
    11 }
    12 double mul(double a, double b)
    13 {
    14     return a * b;
    15 }
    16 double division(double a, double b)
    17 {
    18     if(b == 0){
    19         cout << "分母不可为0";
    20         isZero = 1;
    21         return 0;
    22     }else{
    23         return a/b;
    24     }
    25 }
    26 void print(double (*ptr)(double a,double b),double c, double d)//把函数指针做为参数传入
    27 {
    28     double c1=ptr(c, d);
    29     if(isZero == 0){
    30             cout << c1;
    31     }else{
    32       isZero = 0;
    33     }
    34 }
    35 int main()
    36 {
    37     double a, b;
    38     char ch = ' ';
    39     int flag = 0;
    40     double (*ptr)(double a, double b);
    41     while(true){
    42     cout << "-----请输入要计算的两个数------"  << endl;
    43     cin >> a >> b;
    44     cout << "请输入要执行何种计算(+,-,*,/)" << endl;
    45     cin >> ch;
    46     switch(ch)
    47     {
    48 
    49         case '+': ptr = add; break;//函数指针作为各个函数的载体执行相应的运算,即该指针指向指定函数所在内存首地址
    50         case '-': ptr = subtraction; break;
    51         case '*': ptr = mul; break;
    52         case '/': ptr = division; break;
    53         default: cout << "输入错误,重新开始"  << endl; flag = 1;
    54     }
    55     if(flag == 1) { flag = 0;continue;};
    56     cout << "结果是:";
    57     print(ptr,a,b);
    58     cout << endl;
    59     cout << "是否跳出循环(y/n)" << endl;
    60     char ch1 = ' ';
    61     while(cin >> ch1)
    62     {
    63         if(ch1 == 'y'){
    64             break;
    65         }else if(ch1 == 'n'){
    66             break;
    67         }else{
    68            cout << "请输入y or n" << endl;
    69         }
    70     }
    71     if(ch1 == 'n')  continue;
    72     else  break;
    73  }
    74     return 0;
    75 }
  • 相关阅读:
    快慢指针
    成绩排序
    简单排序
    个人作业——软件工程实践总结作业
    团队作业——项目验收与总结博客(麻瓜制造者)
    Beta阶段总结博客(麻瓜制造者)
    “福大易宝”宣传文案
    Beta冲刺(5/5)(麻瓜制造者)
    Beta冲刺(4/5)(麻瓜制造者)
    个人作业——软件工程实践总结作业(待续)
  • 原文地址:https://www.cnblogs.com/henuliulei/p/11678309.html
Copyright © 2020-2023  润新知