Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing Calculator
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aerii




PostPosted: Mon Apr 13, 2015 10:53 am   Post subject: Turing Calculator

What is it you are trying to achieve?
Make it so when he user types in an equation (Example: 2*2+3) then it will give the answer.


What is the problem you are having?
Outputs what I wrote (Example it shows 2*2+3 instead of 7)

Describe what you have tried to solve this problem
Tried searching for help but all I could find was doing real variables for num1 and num2 but not how I want it. Because you can do put 2 * (3+3-2) for example and it will give you the answer but it doesn't work the way im trying to do it.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:

% Practice printing words, drawing objects and more.
% Practice - Problem 2
% <My name here>
% April 10, 2015


% *Variables*

var space : string
space := ""

var name : string
var answer : string
var chars : array char of boolean
var mathAnswer : string

% Background Colour
% Draw.FillBox (0, 0, maxx, maxy, COLOURHERE)

% Generating a background TEXT colour
% Text.ColourBack (COLOURHERE)

% Practicing printing words and more.

% setscreen decides what will be shown when you click run. (example: nobuttonbar "takes off the button bar")
View.Set ("nobuttonbar, graphics, graphics:500;450, title: Variables Problem 2 | By: Lucas Winkler, position:center;center")


colour (9)
put "Press Enter to begin"

% loops the if statement to always check if enter is pressed unless its asking you to write an answer. If it is pressed then it starts the conversation with turing.
loop
    Input.KeyDown (chars)
    if chars (KEY_ENTER) then
        cls
        colour (9)
        put "Whats your name? " ..
        colour (5)
        get name : *
        % get name is the computer asking for your name. once entered it is stored as a variable string called name
        put space
        delay (750)
        % Clears Screen
        cls
        colour (9)
        put "Hello " ..
        colour (5)
        % writes name to the screen after "Hello" which is stored as a variable from when you wrote your name while the program is running
        put name ..
        colour (9)
        put "!"

        put space
        delay (1000)

        colour (9)
        put "This computer can calculate any math equation you want."

        put space
        delay (1750)

        colour (9)
        put "Would you like to get an answer to a question? " ..
        colour (5)
        get answer : *
        % If get answer is one of the following answers below then show answer to the question
        if answer = "Sure" or answer = "Yes" or answer = "Sure!" or answer = "Yes!" or answer = "sure" or answer = "yes" or answer = "sure!" or answer = "yes!" then
            colour (9)
            put "Alright! " ..
            delay (750)
            put "What would you like to be solved? " ..
            colour (5)
            get mathAnswer
            colour (9)
            put "The answer to your question is " ..
            colour (5)
            put mathAnswer

            put space
            delay (1000)

            put space

            colour (2)
            put "Press Enter to restart, or ESC to exit."
            colour (9)
            % Smiley Face :)
            % outer circle/head
            drawoval (50, 185, 50, 50, black)
            % left outer eye
            drawoval (23, 205, 12, 12, black)
            % right outer eye
            drawoval (78, 205, 12, 12, black)
            % left eye
            drawfilloval (23, 205, 7, 7, black)
            % right eye
            drawfilloval (78, 205, 7, 7, black)
            % mouth
            drawfillarc (51, 175, 30, 30, -180, 360, black)
            % if get answer is no then exit in 5 seconds
        elsif answer = "No" or answer = "no" or answer = "No!" or answer = "no!" then
            cls
            delay (500)
            put "Awwww.. Okay ;("
            delay (1250)
            put "Guess I'll have to leave"
            delay (1750)
            cls
            put "Turing as left the conversation. Exiting in 5 seconds."
            delay (1000)
            cls
            put "Turing as left the conversation. Exiting in 4 seconds.."
            delay (1000)
            cls
            put "Turing as left the conversation. Exiting in 3 seconds..."
            delay (1000)
            cls
            put "Turing as left the conversation. Exiting in 2 seconds.."
            delay (1000)
            cls
            put "Turing as left the conversation. Exiting in 1 second."
            delay (1000)
            cls
            Draw.FillBox (0, 0, maxx, maxy, black)
            quit
            % if get answer is anything other than listed (example: what?) then it says I couldn' understand
        else
            colour (9)
            put "Sorry I couldn't understand that. Please restart and try again."
            colour (4)
            put "(\"Hint Hint\": Sure, Yes, Sure!, Yes!, sure, yes, sure!, yes!, No, no, No!, no!)"
        end if
        % if escape is pressed the exit program in 5 seconds
    elsif chars (KEY_ESC) then
        cls
        put "Exiting program in 5 seconds."
        delay (1000)
        cls
        put "Exiting program in 4 seconds.."
        delay (1000)
        cls
        put "Exiting program in 3 seconds..."
        delay (1000)
        cls
        put "Exiting program in 2 seconds.."
        delay (1000)
        cls
        colour (4)
        put "Exiting program in 1 second."
        delay (1000)
        cls
        Draw.FillBox (0, 0, maxx, maxy, black)
        % ends the if statement and loop that was started at the beginning of the code
    end if
end loop
% exits the program
quit


% Practicing drawing objects to the screen.

%% Smiley Face :)
%% outer circle/head
% drawoval (42, 185, 50, 50, black)
%% left outer eye
% drawoval (15, 205, 12, 12, black)
%% right outer eye
% drawoval (70, 205, 12, 12, black)
%% left eye
% drawfilloval (15, 205, 7, 7, black)
%% right eye
% drawfilloval (70, 205, 7, 7, black)
%% mouth
% drawfillarc (43, 175, 30, 30, -180, 360, black)


Please specify what version of Turing you are using
4.11
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Apr 13, 2015 12:24 pm   Post subject: RE:Turing Calculator

You need to manually search through the input string and find the first expression to solve, then find the 2nd, then the third. Basically you need to manually implement BEDMAS. There are a number of ways to do this. A conceptually simple but tedious and inelegant method is to loop over the string looking for the first BEDMAS operation to solve, then replace that operation and its operands in the string with the solution. You could insert the string into an expression tree, which solves itself recursively. You could convert the string to post-fix or pre-fix notation, which eliminates BEDMAS entirely and iterate over it to solve, or insert it into a tree as well.

This is not an easy problem to solve. It will take a lot of work and you'll run into a lot of problems. That said, it is doable and if you put in an effort there is no reason you shouldn't get it to work.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: