如何使用 if else 和函数在 c 中制作计算器?

新手上路,请多包涵

我是一个新的贡献者,在这里我正在尝试制作一个简单的计算器,但我的代码中有错误。当我编译代码时,我得到:

 Error: C:\Users\IJLAL\Documents\collect2.exe    [Error] ld returned 1 exit status while compiling

如果有帮助,这里是我在 Dev C++ 中按下编译按钮或 F11 时的错误屏幕截图: 在此处输入图像描述

这是我的代码:

 #include<iostream>
using namespace std;
void fun(float a, float b);
int main()
{
    float a, b, sum, sub, mul, divide, mod;
    char op;

    //operands and operators are enterd by the user
    cout<<"Enter any two operands with operator=";
    cin>>a>>op>>b;
    fun(a, b);
    return 0;
}
void op(float a, float b)
{
    if(a+b)
    {
        float sum=a+b;
        cout<<"\nAddition of two numbers is="<<sum;
    }
    else if(a-b)
    {
        float sub=a-b;
        cout<<"\nSubtraction of two numbers is="<<sub;
    }
    else if(a*b)
    {
        float mul=a*b;
        cout<<"\nMultiplication of two numbers is="<<mul;
    }
    else if(a/b)
    {
        float divide=a/b;
        cout<<"\nDivision of two number is="<<divide;
    }
    else
    {
        cout<<"\nInvalid operator.......";
    }
}

请告诉我这个问题的解决方案,以便我可以成功编译代码。如果有任何更好的解决方案可以在 初学者级别 制作一个简单的计算器,请在回答中提及。

原文由 Ijlal Hussain 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 507
2 个回答

您可以使用附加功能来制作更好的计算器。您可以使用此代码。希望此代码对您有所帮助。

标头 <iomanip>C++ 标准库的输入/输出库的一部分,而 <math.h> 在我们执行数学运算时使用。

 #include<iostream>
#include<conio.h>
#include<math.h>
#include<iomanip>
  char op;
using namespace std;
void sum()
   {

    int sum = 0;
    int n;
    int numberitems;
    cout << "Enter number of items: \n";
    cin >> numberitems;

    for(int i=0;i<numberitems;i++)
    {
        cout<< "Enter number "<<i<<":\n\n" ;
        cin>>n;
        sum+=n;
    }
    cout<<"sum is: "<< sum<<endl<<endl;

    }
void diff()
    {
     int diff;
     int n1,n2;
     cout<<"enter two numbers to find their difference:\n\n";
     cout<<"enter first number:";
     cin>>n1;
     cout<<"\nenter second number:";
     cin>>n2;
     diff=n1-n2;
     cout<<"\ndifference is:"<<diff<<endl<<endl;
     }

void pro()

    {
     int pro=1;
     int n;
     int numberitems;
     cout<<"enter number of items:\n";
     cin>>numberitems;
     for(int i=0;i<=numberitems;i++)
     {
             cout<<"\nenter item "<<i<<":";
             cin>>n;
             pro*=n;
     }

     cout<<"product is:"<<pro<<endl<<endl;
     }

 void div()
     {
      int div;
      int n1;
      int n2;
      cout<<"enter 2 numbers to find their quotient\n\n";
      cout<<"enter numerator:";
      cin>>n1;
      cout<<"\nenter denominator:";
      cin>>n2;
      div=n1/n2;
      cout<<"\nquotient is:"<<div<<endl<<endl;
      }

void power()
     {
     long int p;
     int res=1,n;
     cout<<"enter number:";
     cin>>n;
     cout<<"\nenter power:";
     cin>>p;
     for(int i=1;i<=p;i++)
     {
      res=n*res;
     }
      cout<<n<<"\n power "<<p<<" is :"<<res<<endl;
     }

void sq()
     {
     float s;
     int n;
     cout<<"enter number to find its square root:";
     cin>>n;
     s=sqrt(n);
     cout<<"\nsquare root of "<<n<<" is :"<<s<<endl;
     }
 void fact()
     {
      long int f=1;
      int c=1,n;
      cout<<"enter number to find its factorial:";
      cin>>n;
      while(c<=n)
      {
                 f=f*c;
                 c+=1;
      }
                 cout<<"\nfactorial of "<<n<<" is :"<<f<<endl;
      }
void expo()
     {
          long double res=1,p;
     double e=2.718281828;
     cout<<"enter power of exponential function:";
     cin>>p;
     for(int i=1;i<=p;i++)
     {
      res=e*res;
     }
      cout<<" e^ "<<p<<" is :"<<res<<endl;

           }
int main()
{

    system("cls");
    do
    {

    system("pause");
    system("cls");
    cout<<"***which operation you want to perform***\n";
    cout<<"press 0 for exit\n";
    cout<<"press 1 for addition \n";
    cout<<"press 2 for subtraction\n";
    cout<<"press 3 for multiplication\n";
    cout<<"press 4 for division\n";
    cout<<"press 5 for power calculation\n";
    cout<<"press 6 for square root \n";
    cout<<"press 7 for factorial calculation\n";
    cout<<"press 8 for exponential calculation\n";
    cout<<"press option:";
    cin>>op;
    switch(op)
    {
              case '1':
              sum();

              break;
              case '2':
              diff();
              break;
              case '3':
              pro();
              break;
              case '4':
              div();
              break;
              case '5':
              power();
              break;
              case '6':
              sq();
              break;
              case '7':
              fact();
              break;
              case '8':
              expo();
              break;
              case '0':
              exit(0);
              default:
              cout<<"invalid input"  ;
              system("cls");
    }
    }

    while(op!='0');

                    getch();
                    }

原文由 Muhammad Usama 发布,翻译遵循 CC BY-SA 4.0 许可协议

你离结果不远了。问题是你没有定义函数 fun() 。此外,在您定义的函数 op() 中,您不使用输入运算符。

所以首先要做的是改变函数的签名:

 void fun(char op, float a, float b);

然后您需要在 main() 中调用您的函数,同时传递用户请求的操作:

 fun(op, a, b);

最后,您需要更改所有 if 以检查 op 是否是匹配运算符:

 void fun(char op, float a, float b)
{
    if(op=='+')
    {
        ...
    }
    else if(op=='-')
    {
...

然后你应该得到预期的结果。

在线演示

附加信息

  • if (a+b) 只是使用用户的两个值计算表达式,如果它不为零,则认为它是真的。
  • 一旦你让这个程序工作,你可以寻找 switch 语句

原文由 Christophe 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题