Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  calculator
Language: C/C++
Code:
#include <stdio.h>

int main()
{
     float num1, ans;
     char opr;
     char Exit = 'a';
     while (Exit != 'e')
     {
          printf("Enter the math problem: (Enter opr = '#' for root)");
          scanf("%f", &num1);
          scanf(" %c", &opr, 1);
          if (opr == '#')
          {
               float algorithm1, algorithm2, algorithm3;
               int loop = 0;

               //algorithm

               algorithm1 = num1 / 2; // algorithm1(6) = num1(12) / 2

               algorithm2 = (algorithm1 + 2) / 2; // algorithm2(4) = (algorithm1(6) + 2) / 2
               while (loop < 10)
               {
                    algorithm1 = num1 / algorithm2; // algorithm1(3) num1(12) / algorithm2(4)
                    algorithm3 = (algorithm1 + algorithm2) / 2; // algorithm3(3.5) = (algorithm1(3) + algorithm2(4)) / 2
                    algorithm1 = num1 / algorithm3; // algorithm1(3.43) = num1(12) / algorithm3(3.5)
                    algorithm2 = (algorithm1 + algorithm3) / 2; // algorithm2(3.465) = (algorithm1(3.43) + algorithm3(3.5)) / 2

                    loop++;
               }
               printf("The root for %f is: %fnif you want to exit press 'E' to continue press any button ecept 'E'", num1, algorithm2);
          }

          else
          {
               float num2;

               scanf("%f", &num2);

               switch (opr)
               {
               case '+':
                    ans = num1 + num2;
                    break;

               case '-':
                    ans = num1 - num2;
                    break;

               case '*':
                    ans = num1 * num2;
                    break;

               case '/':
                    ans = num1 / num2;
                    break;

               case '^':
               {
                    int powerCount = 0;
                    ans = 1;

                    if (num2 > 2)
                    {
                         while (powerCount < num2)
                         {
                              ans = ans * num1;
                              powerCount++;
                         }
                    }

                    else if (num2 < 0)
                    {                         num2 = num2 * -1;                         while (powerCount < num2)
                         {                              ans = ans * num1;
                              powerCount++;
                         }

                         ans = 1 / ans;
                    }
                    break;
               }
               default:
               {
                    printf("invalid operation");
                    break;
               }

          } // switch(opr)
               printf("The answer is: %fnif you want to exit press 'E' to continue press any button except 'E'", ans);
          } // opr != #
          scanf(" %c", &Exit, 1);
     } // Exit != 'e'
     printf("Thank you for using Tal's calculator");
} // main
          
          
Comments: