0

我是 C++ 的初学者,几乎完全是编程(除了一点htmlcss)。

我决定开始我的第一个 C++ 项目。

一位朋友建议我尝试制作一个简单的计算器,所以这是我的第一枪。任何指针也会很棒!不确定我到底错过了什么,如果有的话,但我收到的错误是:

1>------ Build started: Project: CalculatorFinal, Configuration: Debug Win32 ------
1>  CalculatorFinal.cpp
1>c:\users\ramee\documents\visual studio 2010\projects\calculatorfinal
 \calculatorfinal\calculatorfinal.cpp(32): warning C4102: 'calc' : unreferenced label
1>  CalculatorFinal.vcxproj -> c:\users\ramee\documents\visual studio 2010
\Projects\CalculatorFinal\Debug\CalculatorFinal.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

我的代码在下面(如果这里的格式不正确,请道歉。这是我的第一篇文章:D

  // CalculatorFinal.cpp : Defines the entry point for the console application.
  //

  #include "stdafx.h"       // Including header
  #include <iostream>       // Including ioStream
  using namespace std;      // Namespace

  void calc (double x, double y);
  double result;
  double n1,n2;             // Declaring Variables
  char q,operation;


  int main()                
  { 
   cout<<"Welcome to My Calculator" <<endl; // Outputs welcome message
   cout<<""<<endl;                               // Blank Space
   cout<<"INSTRUCTIONS: Input a mathmatical equation" <<endl; // Outputs instruction            
   cout<<"              EX: 2 + 2" <<endl;       // Outputs instruction
   cout<<""<<endl;                               // Blank Space
   cout<<"Operators:"<<endl;                     // Outputs operation header
   cout<<"For Addition, select '+'"<<endl        // Outputs ADD instruction
   cout<<"For Subtraction, select '-'"<<endl;    // Outputs SUB instruction
   cout<<"For Multiplication, select '*'"<<endl; // Outputs MUL instruction
   cout<<"For Division, select '/'"<<endl;       // Outputs DIV instruction
   cout<<""<<endl;                               // Blank Space
   cout<<"To clear, select 'c'"<<endl;  // Outputs clear instruction
   cout<<"To quit, select 'q'"<<endl;   // Outputs QUIT instruction
   cout<<""<<endl;                                                                  // Blank Space
   cout<<"Input a mathmatical equation"<<endl;                                      // Input instructions
   cin>>n1>>operation>>n2;
   calc:(n1,n2);
   cout<<"The answer is:"<<result<<endl;
   std::cin>>q;             // Input "q" to "quit"
   return 0;}

void calc(double x, double y)                                                       // Operator function
    {            x=n1;
    y=n2;

    switch(operation)                                                           // Operator swtich statement
    {case '+':
        result = x + y;
        break;

    case '-':
        result = x - y;
        break;

    case '*':
        result = x * y;
        break;

    case '/':
        result = x / y;
        break;

    default:
        cout<<"Improper equation. Please input a valid mathmatical equation"<<endl;
        cin>>n1>>operation>>n2;
        calc (n1,n2);
    }

 }
4

6 回答 6

1

这是我根据你写的更好的计算器程序:

#include <iostream>
using namespace std;

//Function prototype
int solve(int, int, char);

int main()
 {
 //Declare variables
 int solution, num1, num2;
 char oper;

 //Output
 cout << "Calculator\n----------\n" << endl;
 cout << "Syntax:\n" << endl;
 cout << "1 + 3\n" << endl;
 cout << "Operators: +, -, *, /\n" << endl;
 cout << "Equation: ";

 //Input
 cin >> num1 >> oper >> num2;

 //Solve and output
 solution = solve(num1, num2, oper);
 cout << "Answer: " << solution << endl;

 //Pause [until enter key] and exit
 cin.ignore(); //Enter key from last cin may be passed, ignore it.
 cin.get();
 return 0;
 }

int solve(int num1, int num2, char oper)
 {
 //Switch oper
 switch(oper)
  {
  case '+':
   return num1 + num2;
  case '-':
   return num1 - num2;
  case '*':
   return num1 * num2;
  case '/':
   return num1 / num2;
  default:
   cout << "\nIncorrect operation!  Try again: ";
   cin >> num1 >> oper >> num2;
   solve(num1, num2, oper);
  }
 }

以下是您上一个程序中需要注意的一些事项:1) 函数原型没有函数名称 [即 void func(int)] 2) 使用返回值 [即返回结果;] 3) 确保您有半冒号。

.

.

.

.

[OLD POST: cout<<"For Addition, select '+'"<;* // 输出 ADD 指令

[没有结束分号]

供参考:

标准::cin>>q; // 输入“q”到“退出”

std:: 这里不需要。(使用命名空间标准;)

(删除 calc:(n1,n2) 中的冒号;)

--

你的程序现在可以运行了。]

于 2013-08-05T00:43:26.790 回答
0

我用 C++ 编写了这个简单的计算器,这样我们就可以加、减、除或乘以任意数量的数字。例如:2+3+4-2= 然后你就会得到你的答案。

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    char c;
    while(true){
        cout << "To solve your math problem you can use this syntex: number(+, -, / or *)number=" << endl;
        cout << "you can use as many number as you want." << endl << endl;

        int n, ans;
        char oper;

        cin >> n;
        ans = n;
        cin >> oper;

        while(oper!='='){

            cin >> n;

            if(oper=='+'){
                ans = ans + n;
            }
            if(oper=='-'){
                ans = ans - n;
            }
            if(oper=='/'){
                ans = ans/n;
            }
            if(oper=='*'){
                ans = ans*n;
            }
            cin >> oper;
        }
        cout << "answer: " << ans << endl << endl;

        cout << "Press esc to exit or press any key to continue." << endl << endl;
        c=getch();
        if(c==27){
            break;
        }
    }
    return 0;
}
于 2014-12-17T17:40:32.853 回答
0

这是我的代码,太长了但是支持更多的操作符

#include "stdafx.h"
#include"iostream"
#include"math.h"
#include"iomanip"
#include <string>
#include <sstream>

using namespace std;
double calc(string mystring);
double calc2(string mystring);
double factoriel(double number);
double root(double num1,double num2);
double dowork(int a,int b,string c);
int main(){
cout<<"***************************************************\n";
cout<<"*                                                 *\n";
cout<<"*                   calculator                    *\n";
cout<<"***************************************************\n\n\n";

string inpstring;
cin >> inpstring;



int length_string=inpstring.length();


double result;
if(abs(calc(inpstring))>abs(calc2(inpstring))){
result=calc(inpstring);
}
else if(abs(calc(inpstring))<=abs(calc2(inpstring))){
result=calc2(inpstring);
}

double s;
     s=3.14;
cout<<"\n"<<"\tresult    :     "<<result<<endl; 


system("pause");
}





double calc(string mystring){
int a=0;//just for switchings
int numberofop=0;
int length_string=mystring.length();

string ops;
string myop;
double param1=0;
double  param2=0;
double  result=0;
string first_inp;
string second_inp;
ops="+-*/^%!R";
    int length_ops=ops.length();

for (int i=0;i<=length_string-1;i++){
    if (i==0){
    if(mystring.substr (0,1)=="-"){
            continue;
        }
    }

    for (int j=0;j<=length_ops-1;j++){

        if (!(mystring.substr (i,1).compare(ops.substr(j,1)))){

            numberofop++;
            if (numberofop==1){
                    myop=ops.substr(j,1);

                    first_inp = mystring.substr (0,i);
                    second_inp = mystring.substr (i+1,length_string-1);

                    stringstream(first_inp) >> param1;
                    stringstream(second_inp) >> param2;


                    if (myop=="+"){
                        a=1;
                        }
                    else if(myop=="-"){
                        a=2;
                    }
                    else if(myop=="*"){
                        a=3;
                    }
                    else if(myop=="/"){
                        a=4;
                    }
                    else if(myop=="^"){
                        a=5;
                    }
                    else if(myop=="%"){
                        a=6;
                    }
                    else if(myop=="!"){
                        a=7;
                    }
                    else if(myop=="R"){
                        a=8;
                    }

            }
        }

    }
 }

switch (a){
            case 1:             
                result=param1+param2;
            break;

            case 2:             
                result=param1-param2;
            break;

            case 3:         
                result=param1*param2;
            break;

            case 4:             
                result=param1/param2;
            break;

            case 5:             
                result= pow(param1,param2);
            break;

            case 6:                     
                result= int(param1)% int(param2);
            break;
            case 7:                     
                result= factoriel(param1);
            break;
            case 8:                     
                result= root(param1,param2);
            break;
}




return result;
}





double factoriel(double a){
    cout<<"enter number      \n";


            double i=a;
            double d=1;
            while(i>1){

            d=d*i;
            i--;
            }
            return d;
}

double root(double num1,double num2){
    double result;
    double reverce;
        reverce=1/num2;
        result=pow(num1,reverce);
return result;
}


double calc2(string mystring){
int a=0;//just for switchings
int numberofop=0;
int length_string=mystring.length();
double pi=3.1415;
double teta;
string ops;
string myop;
double param1=0;
double  param2=0;
double  result=0;
string first_inp;
string second_inp;
ops="logsincostancot";
    int length_ops=ops.length();

for (int i=0;i<=length_string-1;i++){
    if (i==0){
    if(mystring.substr (0,1)=="-"){
            continue;
        }
    }

    for (int j=0;j<=length_ops-1;j++){

        if (!(mystring.substr (i,3).compare(ops.substr(j,3)))){

            numberofop++;
            if (numberofop==1){
                    myop=ops.substr(j,3);


                    second_inp = mystring.substr (i+3,length_string-1);


                    stringstream(second_inp) >> param2;


                    if (myop=="log"){
                        a=1;
                        }
                    else if(myop=="sin"){
                        a=2;
                    }
                    else if(myop=="cos"){
                        a=3;
                    }
                    else if(myop=="tan"){
                        a=4;
                    }
                    else if(myop=="cot"){
                        a=5;
                    }


            }
        }

    }
}

switch (a){
            case 1:             
                result=log(param2);
            break;

            case 2: 
                teta=(double(param2)*pi)/180;
                result=sin(teta);
            break;

            case 3: 
                    teta=(double(param2)*pi)/180;
                result=cos(teta);
            break;

            case 4: 
                teta=(double(param2)*pi)/180;
                result=tanf(teta);
            break;

            case 5:             
                teta=(double(param2)*pi)/180;
                result=1/tanf(teta);
            break;


}

return result;
}
double dowork(int a,int b,string c){
string cut;
cut=c.substr(a,b);
double result;
result=calc(cut);
cout<<"\nresult is    "<<result;

 return result;
}
于 2014-09-28T21:34:24.777 回答
0

我不会把它称为 C++ 程序。这是几乎所有业余 C++ 程序员都会犯的错误。我将其称为编写 C++ 程序的 C 风格。请不要误会我的意思,但您需要开始以面向对象的方式思考,以便您可以利用 C++ 的真正力量。

我建议您创建一个名为calculator 的C++ 类,并在开始编码之前考虑一下该类的设计。我会将 Add、Subtract、Divide 等方法保留为公共方法,而将其他方法保留为私有方法。这也将使您有机会在将来增强计算器类,例如为其添加内存支持,以便它记住最后一次操作或结果。开始以面向对象的方式思考,以避免以后难以管理的意大利面条式代码。

于 2013-08-05T03:43:13.867 回答
0

我评论说我已经改变了你原来的源代码。我已经检查过这段代码在 GCC ( G++ ) 编译器上工作

祝你好运!

#include "stdafx.h"       
#include <iostream>       
using namespace std;     

void calc (double _x, double _y); // CHANGED
double result;
double n1,n2;            
double x,y; // CHANGED
char q,operation;

int main()                
  { 
    cout<<"Welcome to My Calculator" <<endl; 
    cout<<""<<endl;
    cout<<"INSTRUCTIONS: Input a mathmatical equation" <<endl;
    cout<<"              EX: 2 + 2" <<endl;  
    cout<<""<<endl;                              
    cout<<"Operators:"<<endl;                 
    cout<<"For Addition, select '+'"<<endl;      
    cout<<"For Subtraction, select '-'"<<endl;    
    cout<<"For Multiplication, select '*'"<<endl; 
    cout<<"For Division, select '/'"<<endl;       
    cout<<""<<endl;                               
    cout<<"To clear, select 'c'"<<endl;  
    cout<<"To quit, select 'q'"<<endl;   
    cout<<""<<endl;                      
    cout<<"Input a mathmatical equation"<<endl;
    cin>>n1>>operation>>n2;
    calc(n1,n2); // CHANGED
    cout<<"The answer is:"<<result<<endl;
    std::cin>>q;      
    return 0;
 }

void calc(double _x, double _y) // CHANGED
    {            
    x=_x; // CHANGED
    y=_y; // CHANGED
    switch(operation)
    {case '+':
        result = x + y;
        break;
    case '-':
        result = x - y;
        break;
    case '*':
        result = x * y;
        break;
    case '/':
        result = x / y;
        break;
    default:
        cout<<"Improper equation. Please input a valid mathmatical equation"<<endl;
        cin>>x>>operation>>y; // CHANGED
        calc (x,y); // CHANGED
    }
}
于 2013-08-05T09:48:30.013 回答
0

这是我的看法,没有上课。你的全新。

#include <iostream>
#include <cstdlib>
using namespace std;

double a, b;
char operation;


int main()
{
    cout << "Welcome to my calculator program.\n";
    cout << "To make a calculation simply use the following operators -> (+           - * /).\n";
    cout << "To exit, simply write an operation but replace the operator     with 'q'. (eg. 2q3).\n";

    while (operation != 'q'){
        cin >> a >> operation >> b;
        if(std::cin.fail()){
            cout << "Input not numerical. Exiting...";
            exit(1);
        }

        switch (operation)
        {
        case '+':
            cout << "  = " << a + b << '\n';
            break;
        case '-':
            cout << "  = " << a - b << '\n';
            break;
        case '*':
            cout << "  = " << a * b << '\n';
            break;
        case ':':
        case '/':
            cout << "  = " << a / b << '\n';
        }
    }
    return 0;
}
于 2016-03-13T01:46:04.197 回答