Jump to content

Get pseudo code into my calculator

- - - - -

  • Please log in to reply
4 replies to this topic

#1
RikardE

RikardE

    Newbie

  • Members
  • Pip
  • 2 posts
I was provided with this pseudo code and was told it would make my calculator able to calculate sqrt. The problem is that I don't really know how to get this into my original code :-/ Would be totally awesome if someone could get this working! (I'm writing in C++ btw)
Here is the pseudo code:
// pseudocode

cin >> token;

if token.begins_with('#')

   cin >> right

   result = sqrt(right)

else

   left = token.convert_to_numeric_value()

   cin >> op >> right

   switch (op) {

      // as you had originally

   }

}

I want to get that into my existing code which is:
#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

int main() {

    cout<<"Welcome to the Rikard Eskilsson calculator!\n";

    cout<<"Please enter the mathematical problem you wish to solve\n";

    double  left, right;

    char oper;

    double  result;

    while (cin >> left >> oper >> right) {

        switch (oper) {

            case '+': result = left + right;

                      break;

            case '-': result = left - right;

                      break;

            case '*': result = left * right;

                      break;

            case '/': result = left / right;

                      break;

            case '^': result = pow (left, right);

                      break;

            default : cout << "Incorrect value '" << oper << "'" << endl;

                      continue;

        }

        cout << result << endl << endl;

    }


    return 0;

}

The help is urgent and much appreciatet!:c-smile:

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

int main() {

    cout<<"Welcome to the Rikard Eskilsson calculator!\n";

    cout<<"Please enter the mathematical problem you wish to solve\n";

    double  left, right;

    char oper;

    double  result;

    while (cin >> left >> oper >> right) {

        switch (oper) {

            case '+': result = left + right;

                      break;

            case '-': result = left - right;

                      break;

            case '*': result = left * right;

                      break;

            case '/': result = left / right;

                      break;

            case '^': result = pow (left, right);

                      break;

[B][COLOR="red"]            case '#':

                      // your code in the if statement goes here

                      break;[/COLOR][/B]

            default : cout << "Incorrect value '" << oper << "'" << endl;

                      continue;

        }

        cout << result << endl << endl;

    }


    return 0;

}


sudo rm -rf /

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Do another case check for character you want to use for sqrt, and then call sqrt from <cmath> library.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

You would have to add another case in switch statement to handle sqrt operation. Now its upto you what character you use it as sqrt operator probably #.

Munir

#5
RikardE

RikardE

    Newbie

  • Members
  • Pip
  • 2 posts
You are all giving very good soloutions to my problem but my real problem is that i don't have a clue how to write this in code! I'm a very very new beginner and I can't really write any code freehanded, so can anyone be an angel and make my calculator able to count sqrt? :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users