0

Hello,
my question is how can I read decimal from txt file? For Example expression "2.0 1.3 +" - postfix to calculate it.

The one that I have right now work for integers but when it starts reading it chops of the decimal part. I can figure out what i need to implement to get it to read decimal.

TemplateStack <double> doubleStack;
	ifstream infile ("oop06in.txt",ios::in);
	if (! infile)
		cout << "error opening data file!\n";
	else // file ok
	{
		bool stackError = false; // true if stack empties too soon
		while (! infile.eof())
		{
			char token [10];
			infile >> token;
			// BODY OF LOOP GOES HERE
			if (token[0] >= '0' && token[0] <= '9')
			{
				doubleStack.push(atoi(token));
				
			}
			else
			{
				double temp1, temp2;
				switch (token[0])
				{
					case '+':
					{
						stackError = doubleStack.pop(temp1);
						stackError = doubleStack.pop(temp2);
						doubleStack.push(temp1 + temp2);
						break;
					}
					case '-':
					{
						stackError = doubleStack.pop(temp1);
						stackError = doubleStack.pop(temp2);
						doubleStack.push(temp1 - temp2);
						break;
					}
					case '*':
					{
						stackError = doubleStack.pop(temp1);
						stackError = doubleStack.pop(temp2);
						doubleStack.push(temp1 * temp2);
						break;
					}
					case '/':
					{
						stackError = doubleStack.pop(temp1);
						stackError = doubleStack.pop(temp2);
						doubleStack.push(temp1 / temp2);
						break;
					}
					default:
						cout << "Wrong input of arithmetic" << endl;
				}
			}
			cout << "stack is now " << doubleStack << "\n\n";
		} // end while not eof
	if (!stackError)
		cout << "stack emptied during expression evaluation!!\n";
	else // no stack error
	{
		double doubleResult;
		if (doubleStack.pop (doubleResult))
			cout << "expression evaluates to " << doubleResult << endl;
		else
			cout << "final pop of IntStack failed\n";
		if (doubleStack.isEmpty())
			cout << "empty stack => well-formed double expression!\n";
		else // stack is not empty
			cout << "non-empty stack => illegal double expression!\n";
		} // end else no stack error
	} // end else file ok*/
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by ArkM
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.