我可以升级它还是应该使用其他方法制作一个新的?
它只能用于2个数字。我希望通过同时键入所有操作来使其适用于更多变量(不需要单击Enter)。
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
float a;
float b;
char c;
cout<< "Enter operation ";
cin>> a;
cin>> c;
cin>> b;
switch (c)
{
case '+':
cout<<a<<"+"<<b<<"="<<a+b;
break;
case '-':
cout<<a<<"-"<<b<<"="<<a-b;
break;
case'/':
cout<<a<<"/"<<b<<"="<<a/b;
break;
case'*':
cout<<a<<"*"<<b<<"="<<a*b;
break;
}
return 0;
}
答案 0 :(得分:1)
这不是跨平台的,但我希望你明白这一点。
#include <iostream>
#include <iomanip>
void Sum()
{
int number;
std::cout << "Enter number of operands: ";
std::cin >> number;
double * s = new double[number];
double answer = 0;
for (int i = 0; i < number; i++)
{
std::cout << "Enter " << i + 1 << " a number: ";
std::cin >> s[i];
answer += s[i];
}
for (int i = 0; i < number - 1; i++)
std::cout << std::setprecision(8) << s[i] << " + ";
std::cout << std::setprecision(8) << s[number - 1] << " = " << std::setprecision(8) << answer << std::endl;
delete[]s;
}
int main()
{
Sum();
std::cin.get();
return 0;
}