| | |
how do perform multiple math operations in one single output?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
imtrying to do a math calculation that involves multiple math operation ex((a+b)*(sin(a)/sin(b)) something like this but whenever i execute the code its giving me 3 answers this is the code hoping you could help me it very urgent though thanks!!
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <math.h> #include <string.h> int nVar = 0; char varName[50]; double varValue[50]; double getValue(char ch); void setValue(char ch, double d); int main() { cout << "This program enables the user to perform Math Functions such as fabs, sqrt, pow, log, log10, sin cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, ceil, floor, mod. Just input a character(a - z ) followed by an equal sign and the numerical value then press enter. Then in the preceding line input the function you want to use followed by a parenthesis and the alpha character then press enter. To quit, type quit then press enter:\n\n\n"; for(int i = 0; i < 50; i++) { varName[i] = '('; } char ch; char input[6]; double operand1; double operand2; double res; while(1) { cout << ">>"; cin >> ch; if(ch =='q'|| ch =='Q') { break; } if(ch =='quit') { break; } input[0] = ch; operand1 = getValue(ch); cin >> ch; input[1] = ch; if(ch == '=') { cin >> operand1; setValue(input[0], operand1); cout << input[0] << "=\n\n\n\t" <<operand1 << "\n"; } else { switch(ch) { case'+': cin >> ch; operand2 = getValue(ch); res = operand1 + operand2; cout << "ans=\n\n\n\t" << res << "\n"; break; case'-': cin >> ch; operand2 = getValue(ch); res = operand1 - operand2; cout << "ans=\n\n\n\t" << res << "\n"; break; case'*': cin >> ch; operand2 = getValue(ch); res = operand1 * operand2; cout << "ans=\n\n\n\t" << res << "\n"; break; case'/': cin >> ch; operand2 = getValue(ch); res = operand1 / operand2; cout << "ans=\n\n\n\t" << res << "\n"; break; default: cin >> input[2]; int i = 3; while (ch != '(') { cin >> ch; input [i] = ch; i++; } input[i-1] = '\0'; if(strcmp(input, "sin") == 0) { cin >> ch; operand1 = getValue(ch); res = sin(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "cos") == 0) { cin >> ch; operand1 = getValue(ch); res = cos(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "tan") == 0) { cin >> ch; operand1 = getValue(ch); res = tan(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "exp") == 0) { cin >> ch; operand1 = getValue(ch); res = exp(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "log") == 0) { cin >> ch; operand1 = getValue(ch); res = log(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "pow") == 0) { cin >> ch; operand1 = getValue(ch); cin >> ch; cin >> ch; operand2 = getValue(ch); res = pow(operand1, operand2); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "asin") == 0) { cin >> ch; operand1 = getValue(ch); res = asin(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "acos") == 0) { cin >> ch; operand1 = getValue(ch); res = acos(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "atan") == 0) { cin >> ch; operand1 = getValue(ch); res = atan(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "fabs") == 0) { cin >> ch; operand1 = getValue(ch); res = fabs(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "sqrt") == 0) { cin >> ch; operand1 = getValue(ch); res = sqrt(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "ceil") == 0) { cin >> ch; operand1 = getValue(ch); res = ceil(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "sinh") == 0) { cin >> ch; operand1 = getValue(ch); res = sinh(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "cosh") == 0) { cin >> ch; operand1 = getValue(ch); res = cosh(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "tanh") == 0) { cin >> ch; operand1 = getValue(ch); res = tanh(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "fmod") == 0) { cin >> ch; operand1 = getValue(ch); res = fmod(operand1, operand2); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "floor") == 0) { cin >> ch; operand1 = getValue(ch); res = floor(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "log10") == 0) { cin >> ch; //cin >> ch; operand1 = getValue(ch); res = log10(operand1); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } if(strcmp(input, "atan2") == 0) { cin >> ch; operand1 = getValue(ch); cin >> ch; cin >> ch; res = atan2(operand1, operand2); cout << "ans=\n\n\n\t" << res << "\n"; cin >> ch; } break; } } } return 0; } double getValue(char ch) { for(int i = 0; i < nVar; i++) { if(ch == varName[i]) { return varValue[i]; } } varName[nVar] = ch; varValue[nVar] = 0.0; nVar++; return 0.0; } void setValue(char ch, double d) { for(int i = 0; i <nVar; i++) { if(ch == varName[i]) { varValue[i] = d; } } }
![]() |
Similar Threads
- Calculator (value of an arithmetic expression) (Java)
- stack/queue program for long math operation - need help (C++)
- Send data on a serial port (C++)
- SAS datasets to Excel multiple sheets (Computer Science)
- Multiple operations from 2 inputs. (Java)
- MySQL Multiple Table Search (MySQL)
- Need help with integers in c++ (C++)
- Multiple Adsense publishers on single site (Advertising Sales Strategies)
- Data Abstraction (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Help with WndProc Mapping
- Next Thread: How to set maximum length for EditControl (textBox)
Views: 155 | Replies: 0
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





