I am to create a project that evaluates math expressions and their derivatives. I have a Formula class with only virtual functions:

virtual void print()
virtual void print(int)
virtual Formula *derivative()
virtual int evaluate()

Then I have 7 other classes which inherent from Formula, they are:
Variable
Constant
Add
Multiply
Subtract
Divide
Exponent

These classes are to have constructors and destructors along with overriding the virtual functions.

I am reading the expression from a file, where are an expression such as 3*x looks like:

*
3
x

Can someone please help me with this?

Recommended Answers

All 3 Replies

>>Can someone please help me with this?
Can we do what to help you ? Post the code you have written, compiler's error messages, and ask questions. We are not going to write the program for you.

I have figured most of the program out. However I have a couple questions. First of all is it possible to have more than one function in Main.cpp, as in have main() and then another function that main can call. I need to call a function to read the input file recursively but for some reason will not find it. Heres the code:

#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include "Add.h"
#include "Subtract.h"
#include "Multiply.h"
#include "Divide.h"
#include "Formula.h"
#include "Exponent.h"
#include "Constant.h"
#include "Variable.h"

using namespace std;

void main(){

	ifstream inFile;	

	cout << "Enter file that contains the mathematical expression: " << endl;

	inFile.open("formula.txt");
	/*if (!inFile.is_open()){
	throw “File not opened properly”;		
	}*/

	Formula *expression = read(inFile);	

	inFile.close();	

}

Formula* read(ifstream inFile){
	char formula;	
	inFile >> formula;

	switch(formula){
			case 'x':
				return(Formula *var = new Variable());
				break;
			case '*':
				return(Formula *m = new Multiply(read(), read()));
				break;
			case '/':
				return(Formula *d = new Multiply(read(), read()));
				break;
			case '+':
				return(Formula *a = new Add(read(), read()));
				break;
			case '-':
				return(Formula *s = new Subtract(read(), read()));
				break;
			case '^':
				return(Formula *e = new Exponent(read(), read()));
				break;
			case '0':
				return(Formula *zero = new Constant(0));
				break;
			case '1':
				return(Formula *one = new Constant(1));
				break;
			case '2':
				return(Formula *two = new Constant(2));
				break;
			case '3':
				return(Formula *three = new Constant(3));
				break;
			case '4':
				return(Formula *four = new Constant(4));
				break;
			case '5':
				return(Formula *five = new Constant(5));
				break;
			case '6':
				return(Formula *six = new Constant(6));
				break;
			case '7':
				return(Formula *seven = new Constant(7));
				break;
			case '8':
				return(Formula *eight = new Constant(8));
				break;
			case '9':
				return(Formula *nine = new Constant(9));
				break;				
			default:				
				break;
	}	


}

Your text book should be able to answer that question. Yes, programs can have an unlimited number of functions which can be called from other functions such as main(). What your program is missing is the function prototype that needs to appear before main(). Just copy/paste line 33 somewhere above main() and replace the { with a semicolon ;

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.