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

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++ を使用してコーディングしたので、必要な数だけ足し算、引き算、割り算、または掛け算を行うことができます。例: 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

これを C++ プログラムとは呼びません。これは、ほとんどすべてのアマチュア C++ プログラマーが犯す間違いです。私はこれを C++ プログラムを書く C スタイルと呼んでいます。誤解しないでいただきたいのですが、C++ の真の力を活用できるようにするには、オブジェクト指向の方法で考え始める必要があります。

計算機と呼ばれる C++ クラスを作成し、コーディングを開始する前にクラスの設計について少し考えることをお勧めします。Add、Subtract、Divide などのメソッドを public として保持し、その他のメソッドを private として保持します。これにより、最後の操作または結果を記憶するようにメモリサポートを追加するなど、将来的に電卓クラスを拡張する機会も得られます。後で管理するのが難しいスパゲッティ コードを回避するために、オブジェクト指向の方法で考え始めます。

于 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 に答える