Hi i have a calculator progran which is shown below...i need to make this into a class calculator....i need some ideas to get started....like what should be the member functions, private data members etc.....

#include <iostream>
#include <string>
#include <map>
#include <cctype>

using namespace std;


double number_value;
string string_value;
int no_of_errors;
map<string,double> table;

enum Token_value
{
	NAME,      NUMBER,     END,
	PLUS='+',  MINUS='-',  MUL='*',   DIV='/',
	PRINT=';', ASSIGN='=', LP='(',    RP=')'
};
Token_value cur_tok=PRINT;

double error(const string& s);
Token_value get_token();
double prim(bool get);
double term(bool get);
double expr(bool get);


double expr(bool get)
{
	double left=term(get);

	for(;;)
		switch(cur_tok)
	{
		case PLUS:
			left+=term(true);
			break;
		case MINUS:
			left-=term(true);
			break;
		default:
			return left;
	}
}

double term(bool get)
{
	double left=prim(get);
	for(;;)
		switch(cur_tok)
	{
		case MUL:
			left*=prim(true);
			break;
		case DIV:
			if(double d=prim(true))
			{
				left/=d;
				break;
			}
			return error("divide by 0");
		default:
			return left;
	}
}

double prim(bool get)
{
	if(get)get_token();

	switch (cur_tok)
	{
	case NUMBER:
		{
		double v=number_value;
		get_token();
		return v;
		}
	case NAME:
		{
			double &v=table[string_value];
			if(get_token()==ASSIGN) v=expr(true);
			return v;
		}
	case MINUS:
		return -prim(true);
	case LP:
		{
		double e=expr(true);
		if(cur_tok!=RP) return error(") expected");
		get_token();
		return e;
		}
	default:
		return error("primary expected");
	}
}

Token_value get_token()
{
	char ch=0;
	cin>>ch;
	switch(ch)
	{
	case 0:
		return cur_tok=END;
	case ';':
	case '*':
	case '/':
	case '+':
	case '-':
	case '(':
	case ')':
	case '=':
		return cur_tok=Token_value(ch);
	case '0':case '1':case '2':case '3':case '4':
	case '5':case '6':case '7':case '8':case '9':
	case '.':
		cin.putback(ch);
		cin>>number_value;
		return cur_tok=NUMBER;
	default:
		if(isalpha(ch))
		{
			cin.putback(ch);
			cin>>string_value;
			return cur_tok=NAME;
		}
		error("bad token");
		return cur_tok=PRINT;
	}
}


double error(const string& s)
{
	no_of_errors++;
	cerr<<"error:"<<s<<'\n';
	return 1;
}

int main()
{
	
	table["pi"]=3.1415926535897932385;
	table["e"]=2.7182818284590452354;

	while(cin)
	{
		get_token();
		if(cur_tok==END)
			break;
		if(cur_tok==PRINT) 
			continue;
		cout<<expr(false)<<'\n';
	}
	return no_of_errors;
}

Recommended Answers

All 3 Replies

Well, making a class is like anything else and to really develop a solid class takes time to get used to. However, there are a few steps you can follow to figure this out.

1. Setup your function structure. In this case, you can keep all your functions as they are and simply move them to a "class." Generally, the class is contained within a header file with simply the function definitions and then the corresponding .cpp file contains the full definition of the functions.
2. Determine recursive variables. If you find yourself continuously redefining a certain variable type for the same purpose, it's a good idea to probably define that within your class one time and use it appropriately. This is not to say you won't have to say var = 0; sometimes, but it says you the time and (as little as it may be) memory of int var = 0; multiple times.
3. Figure out permissions for your class objects. Determine whether they should be public (you can call these items outside of the class) and what needs to be private or protected (items that should never be called outside of the class. protected values can be inherited)

Now in those three steps we've condensed much of the class writing process. Just to give you some direction on where I would start in your own code, here is a little sample below which may turn out to help.

#include <iostream>
#include <string>
#include <map>
#include <cctype>

using namespace std;

class My_Calculator
{
public:
    My_Calculator(); // The constructor - this function runs when you init the class
    ~My_Calculator(); // The destructor - runs when you kill the class

    /**
     * Give your enum to the class
     */
    enum Token_value
    {
	NAME,      NUMBER,     END,
	PLUS='+',  MINUS='-',  MUL='*',   DIV='/',
	PRINT=';', ASSIGN='=', LP='(',    RP=')'
    };

    // A public variable
    Token_value cur_tok=PRINT;

    // Your wonderful functions
    double error(const string& s);
    Token_value get_token();
    double prim(bool get);
    double term(bool get);
    double expr(bool get);
private:
    double left; // The only one that sticks out that seems to only be needed in the class
};

/**
 * Define each of your class members with the blocks
 */
My_Calculator::My_Calculator(){} // Constructor doesn't really need to do anything
My_Calculator::~My_Calculator(){} // Destructor doesn't really need to do anything

double My_Calculator::error(const string& s)
{
    // Your definition
}

int main()
{
    My_Calculator calc;
    // Run your program
    /**
     * To access class members as we have done above,
     * use calc.FUNCTION_OR_VARIABLE_HERE; in the proper context
     * of its pre-assigned output type. Not the "." instead of the "->"
     * because we did not init with My_Calculator *calc;
     */
    return 0;
}

Hope that helps and is a pretty through explanation!

Regards,
Dennis M.

Remember, when defining a class, you should make that class objective minimal as possible. So don't dump all functions and variable into the class if not necessary.

Thank you very much... :) it helped me a lot!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.