//任意输入两个操作数和一个加、减运算符,完成对应的加减运算,并给出结果 //示例:输入3+5,输出3+5=8 #include <iostream> using namespace std; float add(float x, float y) { cout << x << "+" << y << "="; return x + y; } float sub(float x, float y) { cout << x << "-" << y << "="; return x - y; } int main() { float a, b; char c; float (*p) (float, float);//此处说明了一个函数指针,能指向具有两个float形参并返回float的函数。 while (1) { cout << "a op b" << endl; cin >> a >> c >> b; switch (c) { case '+': p = add; break; case '-': p = sub; break; default: cout << "input error!" << endl;return 1; } cout << p(a, b) << endl; cout << "continue ? (y/n):"; cin >> c; if (c != 'y' && c != 'Y') break; } return 0; }