Exponents in calculations

I'm in a c++ rpgramming class and this is what we have to do. We h

Page 1 of 1

2 Replies - 9332 Views - Last Post: 03 December 2006 - 04:55 PM Rate Topic: -----

#1 edub2015  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-December 06

Exponents in calculations

Posted 03 December 2006 - 12:45 PM

Is there anyway to code exponentiation in a program without using ^ ?

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//	The pupose of this program is to prompt user's to enter two 
//	numeric values and an operation to be preformed on those two 
//	values. The program will then accept the user's input and perform
//	the requested operation and display the original two values, as 
//	either sum, difference, product, quotient, exponentiation, or remainder.
//	It is assumed that the program could operate as a more advanced
//	four function calculator.

//	Function prototypes
	char mainMenu();
	double getUserValue(string);
	double addition(double, double);
	double subtraction(double, double);
	double multiplication(double, double);
	double division(double, double);
	void pauseForEnter();
	void exponetiation(double, double, double);
	void modulus(double, double, double);

	int main()
	{
		//	Declare variables
		char operation = ' ';
		double value1  = 0.0;
		double value2  = 0.0;
		double result  = 0.0;
		string keypress = "";

		//	Set numeric format and precisions for numericouput with
		// cout.
		cout << setiosflags(ios::fixed) << setprecision(2);

		//	Begin the main program loop
		do
		{
			//	Call the mainMenu() function to display the menu and
			//	get the user's menu choice.
			operation = mainMenu();

			//	Evaluate the user choice and determine what we need
			//	to do.
			if ((operation == 'A') || (operation == 'S') || (operation
				== 'M') || (operation == 'D')|| (operation == 'Q'))
			{
				//	Get the user values
				value1 = getUserValue("first");
				value2 = getUserValue("second");

				//	Perform the requested operation and display the results.
				switch (operation)
				{
				case 'A': result = addition(value1, value2);
					printf("Result: %.2f + %.2f = %.2f\n\n", value1, value2, result);
					break;

				case 'S': result = subtraction(value1, value2);
					printf("Result: %.2f - %.2f = %.2f\n\n", value1, value2, result);
					break;

				case 'M': result = multiplication(value1, value2);
					printf("Result: %.2f * %.2f = %.2f\n\n", value1, value2, result);
					break;

				case 'D': if (value2 != 0.0)
						  {
							  result = division(value1, value2);
							  printf("Result: %.2f / %.2f = %.2f\n\n", value1, value2, result);
						  }
						  else
						  cout << "\nError: Division by zero is not permitted.\n\n";
						//	end if/else
					break;

				case 'E': if (value != 0.0)
						  {
							  result = exponentiation(value1, value2);
							  printf("Result: %.2f < %.2f = %.2f\n\n", value1, value2, result);
							  break;
						  }

				case 'R': if (value != 0.0)
						  {
							  result = Modulus(value1, value2);
							  printf("Results: %.2f < %.2f = %.2f\n\n", value1, value2, result);
							  break;
						 
				}
				//	end switch

			}
			
			else if (operation != 'Q')
				printf("\nError: Invalid operation requested. Operation %c is not defined.\n\n",
				operation);

			else
				cout << "\nGoodbye!\n";
			//	end if/else

		   //	Pause the program for keystroke.
				pauseForEnter();
		}
		while (operation != 'Q');
		//	end do while

		//	Terminate the program
		return 0;
	}
	//	end function main()

	//	The function mainMenu() displays an explanation of the purpose of
	//	the program, as well as a menu of choices for the user. The user
	//	choice is obtained and returned to the caller.
	char mainMenu()
	{
		//	Declare variables
		char userChoice = ' ';

		//	Clear the screen, display the menu, and get the user menu
		//	choice.
		system("cls");
		cout << "This program will accept two numeric values and the perform\n"
			<< "The desired operation on those values.\n"
			<< "\nTo select the desired operation, enter the characters\n"
			<< "Associated with the operation as shown below.\n\n";
		cout << "\tA or a - Addition\n";
		cout << "\tS or s - Suntraction\n";
		cout << "\tM or m - Multiplication\n";
		cout << "\tD or d - Division\n";
		cout << "\tQ or q - Quit\n\n";
		cout << "Which operation would you like to perform? ";
		cin >> userChoice;
		cin.clear();

		//	Convert the user to uppercase and return to caller.
		return toupper(userChoice);
	}
	//	end function mainMenu()

	//	The function getUserValue() prompts the user to enter a value 
	//	to be used is the operation requested. The value is obtained 
	//	and returned to the caller.
	double getUserValue(string whichValue)
	{
		//	Declare variables
		double value = 0.0;

		//	Prompt the user to enter a value and return it to the caller.
		cout << "Enter the " << whichValue << " numeric value:";
		cin >> value;
		cin.clear();

		//	Return the value
		return value;
	}
	//	end function getUserValue()

	//	The function addition() calculates and return the sum of its
	//	parameter
	double addition(double num1, double num2)
	{
		return (num1 + num2);
	}
	//	end function addition

	//	The function subtraction() calculates and return the difference 
	//	of its parameter
	double subtraction(double num1, double num2)
	{
		return (num1 - num2);
	}
	//	end function subtraction

	//	The function multiplication() calculates and return the product 
	//	of its parameter
	double multiplication(double num1, double num2)
	{
		return (num1 * num2);
	}
	//	end function multiplication

	//	The function division() calculates and return the qoutient 
	//	of its parameter
	double division(double num1, double num2)
	{
		return (num1 / num2);
	}
	//	end function division

	//	The function pauseforEnter() waits for the user to press the 
	//	(Enter) key.
void pauseForEnter()
{
	//	Declare vaiables
		string keypress = "";

	//	Pause the program for a keystroke
		cout << "Please press (Enter)...";
		cin.ignore (1000,'\n');
		getline(cin, keypress);

	//	The function of exponentiation is to raise a numer x to the next power y
		void exponetiation(double, double, double);


}	//	end program


edit: modified title, added tags ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: Exponents in calculations

#2 msg555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 04-May 06

Re: Exponents in calculations

Posted 03 December 2006 - 01:26 PM

The ^ operator isn't exponentation, it is a bitwise xor.

You could always use the pow function.
Was This Post Helpful? 0
  • +
  • -

#3 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: Exponents in calculations

Posted 03 December 2006 - 04:55 PM

#include <math.h>

...

num=pow(base,exp);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1