zoukankan      html  css  js  c++  java
  • 17:特殊类成员:函数指针5

    1, 函数首地址被赋值给了函数名,故函数名是函数内存地址的首地址。
    2, 一个指向函数的指针|必须确保该函数被定义 |
    |且分配了内存 |否则他将指向一个空地址,这是指针的大忌。
    3,long(*func)(int) : 声明了一个指针,该指针指向一个函数。
    long*func1(int) :声明了一个函数,该函数返回一个指针。

    4,定义:
    float (*fp)(float&, float&);
    fp=&rectangle;
    fp=rectangle; 都行
    #include <iostream>
    #include <string>
    using namespace std;
    
    float (*fp)(float&, float&);
    void (*p)(float&, float&);
    //函数调用列表,将极大的减少代码量
    float rectangle(float &x, float &y)
    {
        return x * y;
    }
    
    float triangle(float &x, float &y)
    {
        return x * y * 0.5;
    }
    
    void Swap(float &x, float &y)
    {
        float n = x;
        x = y;
        y = n;
    }
    
    void print(float &x, float &y)
    {
        cout << "长为:" << x << " 宽为:" << y << endl;
    }
    
    bool check(string str)
    {
        for(int i=0; i<str.length();i++)
        {
            if((str[i] > '9' || str[i] < '0')&&(str[i]!='.'))
            {
                return false;
            }
        }
        return true;
    }
    
    void get(float &x, float &y)
    {
        cout << "请输入x的新值: " << endl;
        std::string str1;
        cin >> str1;
        while(!check(str1))
        {
            cout << "输入的不是数字,请重新输入:x=" << endl;
            cin >> str1;
        }
    
        cout << "请输入y的新值: " << endl;
        std::string str2;
        cin >> str2;
        while(!check(str2))
        {
            cout << "输入的不是数字,请重新输入:y=" << endl;
            cin >> str2;
        }
    
        x = atof(str1.c_str());
        y = atof(str2.c_str());
    }
    
    int main()
    {
        bool quit=false;
        float a = 2, b=3;
        int choice;
        
        while(quit==false)
        {
            cout << " 0: quit 1: input x,y 2 triangle 3 rectangle 4 swap x y" << endl;
            cin>>choice;
            switch(choice)
            {
                case 1:
                    cout << "设定之前长和宽的值";
                    /*print(a, b);
                    get(a, b);
                    cout << "设定之后长和宽的值";
                    print(a, b);*/
                    p=get;
                    break;
                case 2:
                    //print(a, b);
                    cout << "三角形的面积为: ";/* << triangle(a,b) << endl;*/
                    fp=triangle;
                    break;
                case 3:
                    //print(a, b);
                    cout << "长方形的面积为: ";/* << rectangle(a,b) << endl;*/
                    fp=rectangle;
                    break;
                case 4:
                    cout << "交换之前长和宽的值";
                    /*print(a, b);
                    swap(a, b);
                    cout << "交换之后长和宽的值";
                    print(a, b);*/
                    p=Swap;
                    break;
                default:
                    quit=true;
                    break;
            }
    
            if(choice==1 || choice==4)
            {
                print(a, b);
                p(a, b);
                print(a, b);
            }
            if(choice==2 || choice==3)
            {
                print(a, b);
                cout << "面积为:"  << fp(a, b) << endl;
            }
    
        }
    }
  • 相关阅读:
    【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件
    二分查找
    MongoDB安装及启动
    Python文件IO(普通文件读写)
    Python面向对象--高级(二)
    Python面向对象--高级(一)
    Python类与对象--基础
    Python生成器、装饰器
    Python函数式编程
    Python函数及参数
  • 原文地址:https://www.cnblogs.com/i80386/p/4396017.html
Copyright © 2011-2022 走看看