I'm working on a bonus project for my C++ class. The original problem was to add two large numbers given by the professor, or more specifically, write a program that would add these two numbers. After a while I got it working, relitively easy bit of code with some pointers from a classmate.

This bonus project will help my grade, but aas the name implies, is not required. So I'm modifying my code to do subtraction, multiplication and division of these numbers. The problem is that I'm subtracting in columns but don't know how to add 10 to the current number on the stack and then subtract 1 from the next number since I'm not yeat dealing with the number that has yet to come.

Any help with the (probably) simple change to my syntax would be greatly appreciated.

if (choice == 'S' || choice == 's')
	{
		cout << " " << numberOne << "\n-" << numberTwo << 
			"\n----------------------------------------------------\n ";
		//while loop that converts the numbers on the top of each stack to ints
		//as long as the first stack is not empty then pops them off the stack

		while(!(first.empty()))
		{
			//convert the number on top of the first stack to an int, store in 'one'
			one = strConverter(first.top());
			//convert the number on top of the second stack to an int, store in 'two'
			two = strConverter(second.top());
			
			//pop both numbers
			first.pop();
			second.pop();
			
			//add both numbers and store in 'total'
			total += one - two;

			//equate 'onesCol' to modulus of ten of 'total'
			onesCol = total %10;

			//push 'onesCol' onto result stack
			result.push(onesCol);

			//get the new total each loop
			total = (total / 10) %10;
		}

		//while loop that displays the intermediate totals of each loop as long
		//as the result stack is not empty

		while(!(result.empty()))
		{
			//store the top number of result stack in integer form into 'holder'
			holder = (int)result.top();
			//display value of 'holder'
			cout << holder;
			//pop the top number from the result stack.
			result.pop();
		}
		cout << "\n\n ";

		system ("PAUSE");
	}

Recommended Answers

All 6 Replies

Hi there,

Why not just create 2 functions, one for adding and one for subtracting?

That way, you can add/subtract the relevant number once you do have it. Something like:

// Assuming you want to return an int...
int add(int &value, int addition)
{
           value = value + addition;
           return value;
}

but wouldn't that leave me in the same place? like I make a subtraction function, and call it after popping the top number from each stack. So let's say the first number is 5 the second is 8. Wouldn't the subtraction function return a value of -3?

right now my output for the subtraction (should the user choose subtraction) looks like thiss:

78956
-69908
-------
1-105-2

So what I need is for the program to know to add 10 to that 6 on the end, and then subtract 1 from the adjacent 5, but without having access to the 5 yet. I'm completely blank on how to do this. But I have this sinking feeling it's something ridiculously simple. >.<

To be clear, the numbers this program needs to be able to add/subtract (EDIT) are 51 digits long. Otherwise this would be painfully simple. lol

I'm still a little unclear but I think a solution may lie in storing values in variables that can act as buffers (temporary stores)

ok I'll just give the whole program. It may be easier to see what i'm doing that way.

//Chris Lopes
//15 December, 2009
//Adding Ginormous Numbers V2
//Adding the capability of user input numbers
//Adding subtraction, multiplication, and division
//Allowing user to pick which operation to perform
//Can he do it? Let's have a look-see!

//included libraries

#include <iostream>
#include <string>
#include <stack>

//using the standard sections of included libraries

using namespace std;

//declare some stack variables that hold chars and/or ints

stack<char> first; //holds the first number to be added
stack<char> second; //holds the second number to be added
stack <int> result; //holds the sum of the two numbers added

//function to convert strings to integers since these numbers 
//are too large to  be held in int type variables initially

int strConverter(char one)
{
	int two = 0;
	switch(one)
	{
		case '0':
			two = 0;
			break;

		case '1':
			two = 1;
			break;

		case '2':
			two = 2;
			break;

		case '3':
			two = 3;
			break;

		case '4':
			two = 4;
			break;

		case '5':
			two = 5;
			break;

		case '6':
			two = 6;
			break;

		case '7':
			two = 7;
			break;

		case '8':
			two = 8;
			break;

		case '9':
			two = 9;
			break;

	};
	return two;
}

//main function that holds the large numbers as string variables,
//and controls the pushing and popping of the stacks

void main()
{
	/*string numberOne = "192831928479182391284192839128391283891239127391293";
	string numberTwo = "001298319283985247568373658736458739187231001230812";*/
	
	int total = 0, one = 0, two = 0, holder = 0, tensCol = 0, onesCol = 0;
	string numberOne, numberTwo;
	char choice = 'A';

	cout << " Adding Ginormous Numbers Is Fun!\n Just Look!\n\n";
	cout << "Please enter the first number. ";
	cin >> numberOne;
	cout << "Please enter the second number. ";
	cin >> numberTwo;
	cout << "Which operation would you like to perform on these numbers?\n"
		<< "A/a to Add, S/s to Subtract, M/m to Multiply, or D/d to Divide.";
	cin >> choice;
	cout << "\n\n";

	//for loop that continuously pushes numbers to the first stack depending 
	//on the length of the number in question.

	for(int count = 0; count <= numberOne.length() -1; count++)
	{
		first.push(numberOne[count]);
	}

	//for loop that continuously pushes numbers to the second stack depending 
	//on the length of the number in question.

	for(int count = 0; count <= numberTwo.length() -1; count++)
	{
		second.push(numberTwo[count]);
	}

	if (!(choice == 'A' || choice == 'a' || choice ==  'S' || choice == 's'))
	{
		cout << "That is not one of the acceptable operations. \n Use A/a for Addition, or S/s for Subtraction.\n";
		cin >> choice;
	}

	if (choice == 'A' || choice == 'a')
	{
		cout << " " << numberOne << "\n+" << numberTwo << 
			"\n----------------------------------------------------\n ";
		//while loop that converts the numbers on the top of each stack to ints
		//as long as the first stack is not empty then pops them off the stack

		while(!(first.empty()))
		{
			//convert the number on top of the first stack to an int, store in 'one'
			one = strConverter(first.top());
			//convert the number on top of the second stack to an int, store in 'two'
			two = strConverter(second.top());
			
			//pop both numbers
			first.pop();
			second.pop();
			
			//add both numbers and store in 'total'
			total += one + two;

			//equate 'onesCol' to modulus of ten of 'total'
			onesCol = total %10;

			//push 'onesCol' onto result stack
			result.push(onesCol);

			//get the new total each loop
			total = (total / 10) %10;
		}

		//while loop that displays the intermediate totals of each loop as long
		//as the result stack is not empty

		while(!(result.empty()))
		{
			//store the top number of result stack in integer form into 'holder'
			holder = (int)result.top();
			//display value of 'holder'
			cout << holder;
			//pop the top number from the result stack.
			result.pop();
		}
		cout << "\n\n ";

		system ("PAUSE");
	}

	if (choice == 'S' || choice == 's')
	{
		cout << " " << numberOne << "\n-" << numberTwo << 
			"\n----------------------------------------------------\n ";
		//while loop that converts the numbers on the top of each stack to ints
		//as long as the first stack is not empty then pops them off the stack

		while(!(first.empty()))
		{
			//convert the number on top of the first stack to an int, store in 'one'
			one = strConverter(first.top());
			//convert the number on top of the second stack to an int, store in 'two'
			two = strConverter(second.top());
			
			//pop both numbers
			first.pop();
			second.pop();
			
			//add both numbers and store in 'total'
			total += one - two;

			//equate 'onesCol' to modulus of ten of 'total'
			onesCol = total %10;

			//push 'onesCol' onto result stack
			result.push(onesCol);

			//get the new total each loop
			total = (total / 10) %10;
		}

		//while loop that displays the intermediate totals of each loop as long
		//as the result stack is not empty

		while(!(result.empty()))
		{
			//store the top number of result stack in integer form into 'holder'
			holder = (int)result.top();
			//display value of 'holder'
			cout << holder;
			//pop the top number from the result stack.
			result.pop();
		}
		cout << "\n\n ";

		system ("PAUSE");
	}
}

the Adding works exactly like it should, but the subtraction isn't. If you run it, you'll see what's wrong better than I can explain.

hi!
I don't know if you solved this issue -> a small hint :)

try this for your substraction

total += one - two;
if (total >0)
{
  onesCol = total;
  total =0;
}
else
   onesCol= total + 10;

//push 'onesCol' onto result stack
result.push(onesCol);

Daniel

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.