exponents in VC++?

exponents in VC++?

Post by bird7727 on Sat Mar 07, 2009 4:05 am
([msg=19474]see exponents in VC++?[/msg])

I've been trying for a while to get this to work. I've been making a simple calculator program with some new things i learned from a book I'm reading (case/break, et cetera). I can't figure out exponents though. I tried putting in some safeguards but it still won't work properly, only for a few numbers.

The whole program:
Code: Select all
// CalculatorApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int _tmain(int argc, _TCHAR* argv[])
{
   int choice;
   int num1;
   int num2;
   int num3;
   int answer;

   cout<<"Do you want to add, subtract, or multiply?"<<endl
      <<"1: add"
      <<endl
      <<"2: subtract"
      <<endl
      <<"3: multiply"
      <<endl
      <<"4: divide"
      <<endl
      <<"5: exponent"
      <<endl;

   cin>>choice;

   if (choice < 1 || choice > 5)
   {
      cout<<"Enter 1, 2, 3, 4 or 5 next time!"<<endl;
      choice = 0;
   }
   
   if (choice == 1)
   {
      choice = 0;
      cout<<"How many numbers are there?"<<endl;
      cin>>choice;
      
   switch(choice)
      {
      case 1: cout<<"You need a calculator to add one number? Moron..."<<endl;
         break;

      case 2: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            answer = num1+num2;
            cout<<"The answer is: "<<answer<<endl;
            break;
         
      case 3: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            cout<<"Enter number 3: "<<endl;
            cin>>num3;
            answer = num1+num2+num3;
            cout<<"The answer is: "<<answer<<endl;
            break;
      default: cout<<"Sorry, but the calculator is unable to process your request at this time.\n"
                    <<"Please wait for an updated version or try again."
                    <<endl;
      }
   }

   if (choice == 2)
   {
      choice = 0;
      cout<<"How many numbers are there?"<<endl;
      cin>>choice;
      
   switch(choice)
      {
      case 1: cout<<"You need a calculator to subtract one number? Moron..."<<endl;
         break;

      case 2: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            answer = num1-num2;
            cout<<"The answer is: "<<answer<<endl;
            break;
         
      case 3: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            cout<<"Enter number 3: "<<endl;
            cin>>num3;
            answer = num1-num2-num3;
            cout<<"The answer is: "<<answer<<endl;
            break;
      default: cout<<"Sorry, but the calculator is unable to process your request at this time.\n"
                    <<"Please wait for an updated version or try again."
                    <<endl;
      }
   }

   if (choice == 3)
   {
      choice = 0;
      cout<<"How many numbers are there?"<<endl;
      cin>>choice;
      
   switch(choice)
      {
      case 1: cout<<"You need a calculator to multiply one number? Moron..."<<endl;
         break;

      case 2: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            answer = num1*num2;
            cout<<"The answer is: "<<answer<<endl;
            break;
         
      case 3: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            cout<<"Enter number 3: "<<endl;
            cin>>num3;
            answer = num1*num2*num3;
            cout<<"The answer is: "<<answer<<endl;
            break;
      default: cout<<"Sorry, but the calculator is unable to process your request at this time.\n"
                    <<"Please wait for an updated version or try again."
                    <<endl;
      }
   }

   if (choice == 4)
   {
      choice = 0;
      cout<<"How many numbers are there?"<<endl;
      cin>>choice;
      
   switch(choice)
      {
      case 1: cout<<"You need a calculator to divide one number? Moron..."<<endl;
         break;

      case 2: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            answer = num1/num2;
            cout<<"The answer is: "<<answer<<endl;
            break;
         
      case 3: cout<<"Enter number 1: "<<endl;
            cin>>num1;
            cout<<"Enter number 2: "<<endl;
            cin>>num2;
            cout<<"Enter number 3: "<<endl;
            cin>>num3;
            answer = num1/num2/num3;
            cout<<"The answer is: "<<answer<<endl;
            break;

      default: cout<<"Sorry, but the calculator is unable to process your request at this time.\n"
                    <<"Please wait for an updated version or try again."
                    <<endl;
      }
   }

   if (choice == 5)
   {
      
      cout<<"Enter the number: "<<endl;
      cin>>num1;
      cout<<"Enter the exponent: "<<endl;
      cin>>num2;
      
      if ((num1*num1)*num2 == num1*4)
      {
         answer = num1*2;
         cout<<"The answer is: "<<answer<<endl;
      }
      if ((num1*num1)*2)
      {
         answer = num1*num1;
         cout<<"The answer is: "<<answer<<endl;
      }
      else
      {
         answer = (num1*num1)*(num2*num1);
         cout<<"The answer is: "<<answer<<endl;
      }
   }

   system("PAUSE");
   return 0;
}


The snippet:
Code: Select all
   if (choice == 5)
   {
      
      cout<<"Enter the number: "<<endl;
      cin>>num1;
      cout<<"Enter the exponent: "<<endl;
      cin>>num2;
      
      if ((num1*num1)*num2 == num1*4)
      {
         answer = num1*2;
         cout<<"The answer is: "<<answer<<endl;
      }
      if ((num1*num1)*2)
      {
         answer = num1*num1;
         cout<<"The answer is: "<<answer<<endl;
      }
      else
      {
         answer = (num1*num1)*(num2*num1);
         cout<<"The answer is: "<<answer<<endl;
      }
   }
bird7727
New User
New User
 
Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)


Re: exponents in VC++?

Post by BhaaL on Sat Mar 07, 2009 6:19 am
([msg=19477]see Re: exponents in VC++?[/msg])

Interresting way of doing this.

You might want to look into the function "pow". For arbitrary numbers, this might be exactly what you need.
If you dont want to use it, or need something faster, you might aswell write a small func of that yourself:
Code: Select all
unsigned long pow(unsigned long number, unsigned long exponent) {
  unsigned long result = number;
  while (--exponent) result *= number;
  return result;
}

Just watch out for the data type, unsigned long will last for 32 bits, that is roughly 4 billion.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: exponents in VC++?

Post by ranko6 on Sat Mar 07, 2009 9:39 am
([msg=19489]see Re: exponents in VC++?[/msg])

BhaaL gave you perfect way to calculate exponents. This is not exactly for exponents but it might help you with your calculator. I made it in PHP before I found out there is a easier way (eval) but since it's not something that exists in c++ it could help you, with little changes to make it work in c++.
Code: Select all
function mask ($query)
{
   $i = 0;
   $j = 0;
   $m = 1;
   $bcounter = 0;
   $expression = array();

   do
   {
      $string = $query[$i];

      if ($string == "(")
      {
         $bcounter++;
         $expression[$j] = $string;
         $j++;
      }
      elseif ($string == ")")
      {
         if ($query[$i - 1] != ")")
         {
            $j++;
         }
         $bcounter--;
         $expression[$j] = $string;
         $j++;
      }
      elseif ($string == "+")
      {
         $j++;
         $expression[$j] = $string;
         $j++;
      }
      elseif ($string == "-")
      {
         $j++;
         $expression[$j] = $string;
         $j++;
      }
      elseif ($string == "*")
      {
         $j++;
         $expression[$j] = $string;
         $j++;
      }
      elseif ($string == "/")
      {
         $j++;
         $expression[$j] = $string;
         $j++;
      }
      else
      {
         $expression[$j] .= $string; //this needs changing
      }
      $i++;
   } while ($bcounter != 0);

   return math($expression, $m);
}

function math ($expression, &$i)//this one too, & should be passing by reference
{
   $result = $expression[$i++];

   while ($expression[$i] != ")")
   {
      $operator = $expression[$i];
      $value = $expression[++$i];

      if (($value == "(") || ($result == "("))
      {
         $value = math($expression, ++$i);
      }

      if ($operator == "+")
      {
         $result += $value;
         $i++;
      }
      elseif ($operator == "-")
      {
         $result -= $value;
         $i++;
      }
      elseif ($operator == "*")
      {
         $result *= $value;
         $i++;
      }
      elseif ($operator == "/")
      {
         $result /= $value;
         $i++;
      }
   }
   return $result;
}


what it can do? Well when you ask for users input they could write this:
"(1+(30*4/(5-1)))"
you pass it to mask function and it calculates it... Since it was for private use, there are some checks that I haven't made, for instance you have to write brackets, even if you have 2+3 you should write (2+3) or else it won't calculate right.
ranko6
New User
New User
 
Posts: 1
Joined: Thu Mar 05, 2009 8:11 pm
Blog: View Blog (0)


Re: exponents in VC++?

Post by bird7727 on Sun Mar 08, 2009 9:59 pm
([msg=19604]see Re: exponents in VC++?[/msg])

Thanks guys. I modified BhaaL's code (I'm not too good with functions) to work with the program. If you want it (I doubt it, it isn't a very good program) I'll look for a place to upload and post a link.
bird7727
New User
New User
 
Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests