我试图避免cout
声明。具体地,
cout<< "Enter integer 2: \n"
cin>> i
我尝试在第8个if-else语句的各个地方使用cin.ignore(j)
。也就是说,来自用户选择的选项8 。
我想用最后一个复杂的方法完成这个。我在&#34; C ++简介&#34;好吧,我对先进的方法不太熟悉。
这里的目标是在原型部分下创建一个具有给定功能的计算器。
#include <iostream>
#include <math.h>
using namespace std;
int menu();
int addition (int a, int b);
int subtract (int a, int b);
int multiply (int a, int b)
int division (int a, int b);
double realDivision (double a, double b);
double quotient (double a, double b);
double remainder (double a, double b);
int factorial (int a);
int main(int argc, const char * argv[])
{
int choice, i, j;
choice = menu();
if (choice == 0)
return 0;
cout << "Enter integer 1: \n";
cin >> i;
cout << "Enter integer 2: \n";
cin >> j;
if (choice == 1)
cout << addition(i, j) << endl;
else if (choice == 2)
cout << subtract(i, j) << endl;
else if (choice == 3)
cout << multiply(i, j) << endl;
else if (choice == 4)
cout << division(i, j) << endl;
else if (choice == 5)
cout << realDivision(i, j) << endl;
else if (choice == 6)
cout << quotient(i, j) << endl;
else if (choice == 7)
cout << remainder(i, j) << endl;
else if (choice == 8)
cout << i << ", factorial = " << factorial(i) << endl;
return 0;
}
int menu()
{
int choice = 10;
while ((choice > 9) || (choice < 0))
{
cout << "Enter 0 to quit\n";
cout << "Enter 1 for addition\n";
cout << "Enter 2 for subtraction\n";
cout << "Enter 3 for Multiplication\n";
cout << "Enter 4 for Division of two integers\n";
cout << "Enter 5 for Real Division of two integers\n"
cout << "Enter 6 for Quotient of a division\n";
cout << "Enter 7 for Remainder of a division\n";
cout << "Enter 8 for Factorial of an integer\n";
cout << "Enter 9 for Exponential of two integers\n";
cin >> choice;
}
return choice;
}
int addition(int a, int b)
{
return(a+b);
}
int subtract(int a, int b)
{
return(a-b);
}
int multiply(int a, int b)
{
return (a * b);
}
int division(int a, int b)
{
return a/b;
}
double realDivision(double a, double b)
{
return(a/b);
}
double quotient(double a, double b)
{
return(a/b);
}
double remainder(double a, double b)
{
return(a/b);
}
int factorial(int a)
{
int product = 1;
while (a > 1)
{
product *=a;
a--;
}
return product;
}
答案 0 :(得分:0)
您创建了一个单独的if-else
语句,该语句在除了阶乘条件之外的情况下接受第二个输入。因此,如果用户仅选择阶乘/平方根/平方根之类的操作,则您的计算器只会询问用户的一个输入。你可以这样做:
if (choice == 0)
return 0;
cout << "Enter integer 1: \n";
cin >> i;
if (choice == 8)
{
cout << i << ", factorial = 8" << factorial(i) << endl;
return 0;
}
else
{
cout << "Enter integer 2: \n";
cin >> j;
}
if (choice == 1)
cout << addition(i, j) << endl;
else if (choice == 2)
cout << subtract(i, j) << endl;
else if (choice == 3)
cout << multiply(i, j) << endl;
else if (choice == 4)
cout << division(i, j) << endl;
else if (choice == 5)
cout << realDivision(i, j) << endl;
else if (choice == 6)
cout << quotient(i, j) << endl;
else if (choice == 7)
cout << remainder(i, j) << endl;
return 0;
答案 1 :(得分:0)
如果因子选择总是8.你可以在读取第二个数字之前检查choice!=8
。如果选择是8,它将跳过第二个数字部分,然后继续它将读取第二个数字和然后继续。
#include <iostream>
#include <math.h>
using namespace std;
int menu();
int addition (int a, int b);
int subtract (int a, int b);
int multiply (int a, int b)
int division (int a, int b);
double realDivision (double a, double b);
double quotient (double a, double b);
double remainder (double a, double b);
int factorial (int a);
int main(int argc, const char * argv[])
{
int choice, i, j;
choice = menu();
if (choice == 0)
return 0;
cout << "Enter integer 1: \n";
cin >> i;
if(choice!=8)
cout << "Enter integer 2: \n";
cin >> j;
if (choice == 1)
cout << addition(i, j) << endl;
else if (choice == 2)
cout << subtract(i, j) << endl;
else if (choice == 3)
cout << multiply(i, j) << endl;
else if (choice == 4)
cout << division(i, j) << endl;
else if (choice == 5)
cout << realDivision(i, j) << endl;
else if (choice == 6)
cout << quotient(i, j) << endl;
else if (choice == 7)
cout << remainder(i, j) << endl;
else if (choice == 8)
cout << i << ", factorial = 8" << factorial(i) << endl;
return 0;
}
int menu()
{
int choice = 10;
while ((choice > 9) || (choice < 0))
{
cout << "Enter 0 to quit\n";
cout << "Enter 1 for addition\n";
cout << "Enter 2 for subtraction\n";
cout << "Enter 3 for Multiplication\n";
cout << "Enter 4 for Division of two integers\n";
cout << "Enter 5 for Real Division of two integers\n"
cout << "Enter 6 for Quotient of a division\n";
cout << "Enter 7 for Remainder of a division\n";
cout << "Enter 8 for Factorial of an integer\n";
cout << "Enter 9 for Exponential of two integers\n";
cin >> choice;
}
return choice;
}
int addition(int a, int b)
{
return(a+b);
}
int subtract(int a, int b)
{
return(a-b);
}
int multiply(int a, int b)
{
return (a * b);
}
int division(int a, int b)
{
return a/b;
}
double realDivision(double a, double b)
{
return(a/b);
}
double quotient(double a, double b)
{
return(a/b);
}
double remainder(double a, double b)
{
return(a/b);
}
int factorial(int a)
{
int product = 1;
while (a > 1)
{
product *=a;
a--;
}
return product;
}