I have a working calculator , token and a balanced parentheses file.

**this is my calc.cc file**

#include <cstdlib>

#include <iostream>

#include <math.h>

using namespace std;

int main()

{

double num;

double num2;

char choice;

for (;;){

do {

cout<<"Please choose an option by entering the number, press q to quit\n";

cout<<"1 - Addition\n";

cout<<"2 - Subtraction\n";

cout<<"3 - Division\n";

cout<<"4 - Multiplication\n";

cout<<"5 - Raise to power\n";

cout<<"6 - Unary Minus\n";

cin>>choice;

} while ( choice < '1' || choice > '6' && choice != 'q');

if (choice == 'q') break;

switch (choice) {

case '1':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another number to be added\n";

cin>>num2;

cout<<"The answer is:";

cout<<num + num2;

cout<<"\n";

break;

case '2':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another number to be subtracted\n";

cin>>num2;

cout<<"The answer is:";

cout<<num - num2;

cout<<"\n";

break;

case '3':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another one to be divided\n";

cin>>num2;

cout<<"The answer is:";

cout<<num / num2;

cout<<"\n";

break;

case '4':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another one to be multiplied\n";

cin>>num2;

cout<<"The answer is:";

cout<<num * num2;

cout<<"\n";

break;

case '5':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another number to be its power\n";

cin>>num2;

cout<<"The answer is:";

cout<<pow(num,num2);

cout<<"\n";

break;

case '6':

cout<<"Please enter number to be negated\n";

cin>>num;

cout<<"The answer is:";

cout<<num*-1;

cout<<"\n";

break;

default:

cout<<"That is not an option";

}

}

return 0;

}

**this is my token.cc file**

//#include <cmath>

//#include "calc.h"

//#include <stack>

#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main(){

string s;

string sa[20];

cout<<"Enter Text: " <<endl;

getline(cin,s);

cout<<"Entered Text is: " << s <<endl;

istringstream iss(s);

int count = 0;

while(iss){

string sub;

iss>>sub;

if(sub == "") // breaking input string into tokens

break;

cout <<"token " << count << ": " <<sub <<endl;

sa[count]= sub; // putting into array

count = count + 1;

}

cout<<"Number of tokens "<<count << endl<<endl;

for(int i = 0; i<4; i++){ // print first and 2nd number tokens

cout<< i + 1 <<" Element of user input is: "<<sa[i]<<endl;

}

return 0;

}

**this is my balanced parentheses file.**

#include <iostream>

#include <stack>

#include <string>

using namespace std;

int main(void) {

string s;

int i;

stack<char> myStack;

cout << "enter parentheses: ";

cin >> s;

for (i=0;i<s.length();i++)

if (s[i] == '(' || s[i] == '[' || s[i] == '{')

myStack.push('(' || '[' || '{');

else if (s[i] == ')' || ']' || '}')

if (myStack.size() == 0) {

cout << "Error: ) with no matching (\n";

return 1;

} else

myStack.pop();

else

cout << "Invalid character [" << s[i] << "]\n";

if (myStack.size() == 0)

cout << "Parentheses balanced.\n";

else

cout << "Error: ( with no matching )\n";

return 0;

}

My token.cc file is suppose to identify the type of item. which i am unsure how to do...???

My question is how to combine these programs to make one .cc file, as well as what to put in the .h file??

**my calc program is suppose to do the following :**

arithmetic operations : +,-,*,/ and ^

Unary minus

Parenthesis

Storing values in variables using assignment operator =

using variable in calculations

#include <cstdlib>

#include <iostream>

#include <math.h>

using namespace std;

int main()

{

double num;

double num2;

char choice;

for (;;){

do {

cout<<"Please choose an option by entering the number, press q to quit\n";

cout<<"1 - Addition\n";

cout<<"2 - Subtraction\n";

cout<<"3 - Division\n";

cout<<"4 - Multiplication\n";

cout<<"5 - Raise to power\n";

cout<<"6 - Unary Minus\n";

cin>>choice;

} while ( choice < '1' || choice > '6' && choice != 'q');

if (choice == 'q') break;

switch (choice) {

case '1':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another number to be added\n";

cin>>num2;

cout<<"The answer is:";

cout<<num + num2;

cout<<"\n";

break;

case '2':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another number to be subtracted\n";

cin>>num2;

cout<<"The answer is:";

cout<<num - num2;

cout<<"\n";

break;

case '3':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another one to be divided\n";

cin>>num2;

cout<<"The answer is:";

cout<<num / num2;

cout<<"\n";

break;

case '4':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another one to be multiplied\n";

cin>>num2;

cout<<"The answer is:";

cout<<num * num2;

cout<<"\n";

break;

case '5':

cout<<"Please enter a number\n";

cin>>num;

cout<<"Another number to be its power\n";

cin>>num2;

cout<<"The answer is:";

cout<<pow(num,num2);

cout<<"\n";

break;

case '6':

cout<<"Please enter number to be negated\n";

cin>>num;

cout<<"The answer is:";

cout<<num*-1;

cout<<"\n";

break;

default:

cout<<"That is not an option";

}

}

return 0;

}

//#include <cmath>

//#include "calc.h"

//#include <stack>

#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main(){

string s;

string sa[20];

cout<<"Enter Text: " <<endl;

getline(cin,s);

cout<<"Entered Text is: " << s <<endl;

istringstream iss(s);

int count = 0;

while(iss){

string sub;

iss>>sub;

if(sub == "") // breaking input string into tokens

break;

cout <<"token " << count << ": " <<sub <<endl;

sa[count]= sub; // putting into array

count = count + 1;

}

cout<<"Number of tokens "<<count << endl<<endl;

for(int i = 0; i<4; i++){ // print first and 2nd number tokens

cout<< i + 1 <<" Element of user input is: "<<sa[i]<<endl;

}

return 0;

}

#include <iostream>

#include <stack>

#include <string>

using namespace std;

int main(void) {

string s;

int i;

stack<char> myStack;

cout << "enter parentheses: ";

cin >> s;

for (i=0;i<s.length();i++)

if (s[i] == '(' || s[i] == '[' || s[i] == '{')

myStack.push('(' || '[' || '{');

else if (s[i] == ')' || ']' || '}')

if (myStack.size() == 0) {

cout << "Error: ) with no matching (\n";

return 1;

} else

myStack.pop();

else

cout << "Invalid character [" << s[i] << "]\n";

if (myStack.size() == 0)

cout << "Parentheses balanced.\n";

else

cout << "Error: ( with no matching )\n";

return 0;

}

My token.cc file is suppose to identify the type of item. which i am unsure how to do...???

My question is how to combine these programs to make one .cc file, as well as what to put in the .h file??

arithmetic operations : +,-,*,/ and ^

Unary minus

Parenthesis

Storing values in variables using assignment operator =

using variable in calculations

closed account (*9y8C5Di1*)

You need to have the input as a string. Create a function that scans strings for numbers(0-9) and saves the numbers in different dynamically assigned variables. Create the same concept again, but now only with mathematical operators. With these simple functions you can draw out numbers from a string and then determine with the mathematical operator function what properties they will have. Then just convert the string into a float or a numeric type.

Topic archived. No new replies allowed.