Make the program understand Inputted Arithmetic Code

hello guys,
i want to make a graphing calculator
i already have to code to determine the coordinates of a certain formula..
the problem now is, how can i make a certain arithmetic formula like for ex. *sqrt(x^2 PLUS 4x PLUS 4)* and make the program understand it.. (sorry, the forum just removes the PLUS SIGN wen i include it on the formula :3 )
it will be inputted through Java GUI like a scientific calculator..
i have tried studying using stacks and arrays for these but it just i can't implement the theory behind it to my program..
i already know the infix conversion to postfix or prefix but i just don't know how my program can interpret that
i need some enlightenment or tips on how can i do that with my current program..
right now, my program can only graph a certain formula when i edit it on the code and let the program run so it can be graphed [LIKE THIS|http://forums.sun.com/thread.jspa?messageID=11038067#11038067]
Edited by: kyzhuchan on Sep 3, 2010 7:14 AM
Edited by: kyzhuchan on Sep 3, 2010 7:15 AM

I would use a stack for that.
First you translate the equation into a postfix form and then you evaluate that.
Here is an example
import java.util.*;
public static double calc(List postFix, double x) {
    Stack<Double> stack=new Stack<Double>();
    for(Object o: postFix) {
        if(o instanceof Double) stack.push((Double)o);
        else if(o instanceof String) {
            String str=(String)o;
            if(str.equals("x")) {
                stack.push(x);
            } else if(str.equals("+")) {
                stack.push( stack.pop() + stack.pop() );
            } else if(str.equals("-")) {
                stack.push( -stack.pop() + stack.pop() );
            } else if(str.equals("*")) {
                stack.push( stack.pop() * stack.pop() );
            } else if(str.equals("/")) {
                stack.push( 1.0 / stack.pop() * stack.pop() );
    return stack.pop();
}Lets say the formula is (2.3 + 5.1) * (7 - 4) * xThe postfix form is 2.3 5.1 + 7 4 - * x *Then to calculate the value at x=4.3 you can do
List<Object> postFix=Arrays.<Object>asList(2.3, 5.1, "+", 7.0, 4.0, "-", "*", "x", "*");
System.out.println(calc(postFix, 4.3));

Similar Messages

  • Make the program run again, given the user's input?

    Hello,
    I need a little help with this program, you can see the entire thing below. I'm trying to make the program give the user the option to run again, using y or n. It's for a college project. Thanks.
    import java.util.*;
    class MyFifthProgram
         public static void main (String[] args)
              //char letterOnTelephone;
              do
                   System.out.print("Enter a letter from A - Z and I will output the corresponding number \non the telephone, then press enter:");
                   Scanner keyboard = new Scanner(System.in);
                   String letterOnTelephone = keyboard.next();
                   char letter = letterOnTelephone.charAt(0);
                   switch (letter)
                        case 'Q':
                        case 'q':
                        case 'Z':
                        case 'z':
                             System.out.println("Sorry, the letter " + letterOnTelephone + " cannot be found on the telephone....");
                             break;
                        default:
                             System.out.println("Sorry, the key " + letterOnTelephone + " cannot be found on the telephone......");
                             break;     
                        case 'A':
                        case 'a':
                        case 'B':
                        case 'b':
                        case 'C':
                        case 'c':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 2.");
                             break;
                        case 'D':
                        case 'd':
                        case 'E':
                        case 'e':
                        case 'F':
                        case 'f':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 3.");
                             break;
                        case 'G':
                        case 'g':
                        case 'H':
                        case 'h':
                        case 'I':
                        case 'i':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 4.");
                             break;
                        case 'J':
                        case 'j':
                        case 'K':
                        case 'k':
                        case 'L':
                        case 'l':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 5.");
                             break;
                        case 'M':
                        case 'm':
                        case 'N':
                        case 'n':
                        case 'O':
                        case 'o':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 6.");
                             break;
                        case 'P':
                        case 'p':
                        case 'R':
                        case 'r':
                        case 'S':
                        case 's':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 7.");
                             break;
                        case 'T':
                        case 't':
                        case 'U':
                        case 'u':
                        case 'V':
                        case 'v':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 8.");
                             break;
                        case 'W':
                        case 'w':
                        case 'X':
                        case 'x':
                        case 'Y':
                        case 'y':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 9.");
                             break;                                             
                   System.out.println("Would you like to run this program again? Enter Y for yes, or N for no:");
                   Scanner keyboard1 = new Scanner(System.in);
                   String runAgain = keyboard1.next();
                   char runIt = runAgain.charAt(0);
                   }while (runIt == 'y');
                        System.out.println("You can choosen " + runAgain + " therefor the program will run again.");
         }

    Thanks! That did the trick, however that doesn't solve my problem. How do I ask the user to run the program again at the end of the program? What am I missing?
    // Program takes a single letter and displays the corresponding digit on the telephone.
    import java.util.*;
    class MyFifthProgram
         public static void main (String[] args)
              System.out.println("Would you like to run this program? Enter Y for yes, or N for no:");
              Scanner keyboard1 = new Scanner(System.in);
              String runAgain = keyboard1.next();
              char runIt = runAgain.charAt(0);
              if (runIt == 'n')
                   System.exit(0);
              else if (runIt == 'N')
                   System.exit(0);
              do
                   System.out.print("Enter a letter from A - Z and I will output the corresponding number \non the telephone, then press enter:");
                   Scanner keyboard = new Scanner(System.in);
                   String letterOnTelephone = keyboard.next();
                   char letter = letterOnTelephone.charAt(0);
                   switch (letter)
                        case 'Q':
                        case 'q':
                        case 'Z':
                        case 'z':
                             System.out.println("Sorry, the letter " + letterOnTelephone + " cannot be found on the telephone....");
                             break;
                        default:
                             System.out.println("Sorry, the key " + letterOnTelephone + " cannot be found on the telephone......");
                             break;     
                        case 'A':
                        case 'a':
                        case 'B':
                        case 'b':
                        case 'C':
                        case 'c':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 2.");
                             break;
                        case 'D':
                        case 'd':
                        case 'E':
                        case 'e':
                        case 'F':
                        case 'f':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 3.");
                             break;
                        case 'G':
                        case 'g':
                        case 'H':
                        case 'h':
                        case 'I':
                        case 'i':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 4.");
                             break;
                        case 'J':
                        case 'j':
                        case 'K':
                        case 'k':
                        case 'L':
                        case 'l':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 5.");
                             break;
                        case 'M':
                        case 'm':
                        case 'N':
                        case 'n':
                        case 'O':
                        case 'o':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 6.");
                             break;
                        case 'P':
                        case 'p':
                        case 'R':
                        case 'r':
                        case 'S':
                        case 's':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 7.");
                             break;
                        case 'T':
                        case 't':
                        case 'U':
                        case 'u':
                        case 'V':
                        case 'v':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 8.");
                             break;
                        case 'W':
                        case 'w':
                        case 'X':
                        case 'x':
                        case 'Y':
                        case 'y':
                             System.out.println("The letter " + letterOnTelephone + " corresponds to the number 9.");
                             break;                                             
                   }while (runIt == 'n');
              //          System.out.println("Would you like to run this program? Enter Y for yes, or N for no:");
              //          Scanner keyboard2 = new Scanner(System.in);
              //          String runAgain1 = keyboard2.next();
              //          char runIt1 = runAgain1.charAt(0);
              //          if (runIt1 == 'n')
              //               System.exit(0);
              //          else if (runIt1 == 'N');
              //               System.exit(0);
         }

  • How do you make the program repeat itself?

    How would I make it so that when the if statement at the end of the code the program starts over from the begining?
    Any help appreciated. Here is the code if you wanna see it.
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    class SuperTester{
         public static void main ( String[] args ){
         // Create array that will hold deck
         Deck[] decko = new Deck[52];     
         // Fill the array with a deck
         decko = Deck.makeDeck();
         // Print new deck
         System.out.println( "PRINT NEW DECK" );
         System.out.println( "" );
         Deck.printDeck( decko );
         // Shuffle the deck ( simulated riffle shuffling )!
         decko = Deck.shuffleDeck( decko );
         // Print the shuffled deck
         System.out.println( "" );
         System.out.println( "PRINT SHUFFLED DECK" );
         System.out.println( "" );
         Deck.printDeck( decko );
         // Print first 5
         System.out.println( "" );
         System.out.println( "PRINT TOP 5" );
         System.out.println( "" );
         Deck.printFirstFive( decko );
         // Print single card
         System.out.println( "" );
         System.out.println( "PRINT SINGLE CARD" );
         System.out.println( "" );
         Deck.printCard ( decko [2] );
         System.out.println( "" );     
         BufferedReader in = new BufferedReader( new InputStreamReader (System.in));
         int input;
         System.out.println( " Do you want to re-run program? " );
         System.out.println( " Push 1 for yes " );
         System.out.println( " Push 2 for no " );
         System.out.println( "" );
    try
         input = Integer.parseInt( in.readLine() );
    catch( Exception e ){
    if ( input == 1 ){
    //WHAT DO I PUT HERE TO MAKE THE PROGRAM REPEAT ITSELF?
         } // end method
    } // end class

    So i should just do something like?
    int x = 0;
    while (x == 0){
    //code
    if (inpu == 1) {
        x == 0;
    }else{ x == 1; }                                                                                                                                                                                                                                                       

  • How do i make the program hold after it has created a JFrame

    I have created a JFrame with which the user shall login to a server. I call this JFrame from my main method (the JFrame-class is in a separate file). My problem is that after the login-window is created and set visible, my main method continues, i would like it to wait until the user have logged in before running the rest of the code.
    this i my main method:
    new LoginWindow();
    System.out.println("Logged in: "+Session.isLoggedin()+
                    "\nUsername: "+Session.getUsername());Session.isLoggedin() returns a boolean, true if user is logged in and otherwise false. class LoginWindow extends JFrame.
    i have tried to use a while-loop to solve my problem:
    while(!Session.isLoggedin()) { }and also
    whiel(true){
        if(Session.isLoggedin())
            break;
    }these while-loops never breaks, i have waited for about 3 min after successful login.It isn't because Session.isLoggedin() returns false, it does return true after a successful login (i've checked).
    the funny thing however is if i print something in the while-loop i does break when login is finished
    example:
    whiel(true){
        System.out.println(Session.isLoggedin());
        if(Session.isLoggedin())
            break;
    }this code makes the program wait until the login-phase is complete but i don't want it to print "text" a few thousand times while the user is logging in. (i've used "hej" as output aswell)
    Since i have no idea how long it will take for a user to log in i can't use the System.sleep(value) method.
    I would really appreciate any help
    Thanks in advance
    //Oscar

    SnuShogge wrote:
    I am still curious about why the while-loop only breaks if i print something using System.out.println(""), i didn't think that would make a difference since the break-statement remains the same.In general you should avoid trying to use while (true) or its variants in a Swing GUI as it often messes with the EDT, the main thread that renders the GUI graphics and interacts with the user.
    My problem i however partially solved by using a JDialog instead of a JFrame. I am planning on using JFrame later in the program so the problem might come back to haunt me if it isn't solved.This may not be a great idea. Usually an application has one single JFrame. Most other distinct windows a dialogs of one sort or another and should be JDialogs, not JFrames. Often better still is to use a CardLayout to swap JPanels or other components.

  • Are there any packages compatable with LabVIEW to make the program CFR21 part 11

    Hello All,
    Are there any packages that are compatable with a LabVIEW program, which creates a .csv file, that would make the program CFR 21 Part 11 compliant?

    A quick search of Google for " cfr 21part 11 labview" returns several links.
    http://www.ni.com/white-paper/4570/en
    http://www.ni.com/white-paper/8751/en
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness

  • Why do i have to keep clicking my left mouse button to make the program work it just stops and if left click it starts working

    Why do i have to keep clicking my left mouse button to make the program work it just stops and if left click it starts working. I'v only just started to use this program and It's no good at all if I go to a website and click a button I have to keep clicking it to finish what its doing.

    hello jeffsprig, can you try to replicate this behaviour when you launch firefox in safe mode once?
    [[Troubleshoot extensions, themes and hardware acceleration issues to solve common Firefox problems]]
    multiple other affected users have reported that this was an issue caused by malware on their pc. please run a scan of your system with [http://www.bleepingcomputer.com/download/adwcleaner/ adwcleaner] and the [http://www.malwarebytes.org/products/malwarebytes_free free version of malwarebytes].
    [[Troubleshoot Firefox issues caused by malware]]

  • Help, make the program start over

    Hi!
    I want to make this program to start over, an something is wrong, but I can not figure it out, this is new to me. the program is in swedish, but I will try to explaine the functions.
    If you type in zero (0) the program will halt and ask for a value greater then zero. However when you type in a greater value, it will never do the actual calculation. If you from the begining type in a greater value it will do the math. Can anyone help me to correct this.
    Plattform: mac and eclipse, with latest updates.
    Java code:
    import static javax.swing.JOptionPane.*;
    public class sf?r {
         public static void main (String[] arg) {
         String indata;
         indata = showInputDialog (null, "Ange radien i sf?ren du vill r?kna ut Volymen och Arean");
         double radien = Double.parseDouble (indata);
    int three = 3;
    int four = 4;
    double pi = 3.14;
    double volym = four*pi*radien*radien*radien/three;
    double area = four*pi*radien*radien;
    while (radien <=0) {
         indata = showInputDialog (null, "Felaktigt v?rde, Ange ett v?rde st?rre ?n 0");
         radien = Double.parseDouble (indata);
    showMessageDialog (null, "Volymen ?r " + volym + " Arean ?r " + area);
    System.exit (0);
    Martin

       while (radien <=0) {
         indata = showInputDialog (null, "Felaktigt v?rde, Ange ett v?rde st?rre ?n 0");
         radien = Double.parseDouble (indata);
    } this test for 0 is after you do the calculations:
      double volym = four*pi*radien*radien*radien/three;
    double area = four*pi*radien*radien; you need to test for 0 before the calculations.
    just FYI, when posting code, there are "CODE" tags (look at the top of the posting window). It'll make the code much easier to read
    Edited by: redfalconf35 on Nov 21, 2007 9:18 AM

  • How do you make the sentences you input in a drop down list wrap to the next line?

    How do you make the sentences that you input in a drop down list to wrap to the next line?

    You can't.
    see this thread for more info:  Can you set drop down list as multi-line???

  • Cant able to assign the program to a t-code

    hi there gurus,
    i cant assign the t-code to a program.
    i used se 93 and created the t-code over there,
    and i went to se43 to label it.
    but the t-code do not captures the program,
    is there any one can help me, urgent

    Hi ,
    GO to se80 give package name and click on display button,
    than selct a programm name from them and right click on it in that select the transaction.. there u can maintain  a t-code for that program.
    Regards,
    Kranthi

  • I need help in making the programe which updates our code into client machi

    I need help in making the programme which updates our code into client machine from the server.
    As yahoo messanger does if there is any updated version of yahoo messanger.
    It asks when we login into it do you want to update new version of yahoo messanger
    It automatically updates it
    Need Help??

    Sounds to me like you need Java Web Start.

  • How do you make the program start?

    I have installed PSE 11 twice, now; the second time after an uninstallation of the first installation.  Both times, the program started immediately FROM the installation.  After exiting from the program, I have not been able to restart the program.  I click on it; I double click on it (which should start 2 copies of the program!); I right click and click Open; I right click and click "open as administrator".  Absolutely NOTHING happens.  Is there some sort of glitch that means you have to leave the program open permanently?  Please, how do I open the program? 
    Thank you.
    Cherie Brumfield

    Thanks so much for responding.  As it happens, I had already tried that
    approach, with no results.  I also spent some time last evening with a tech
    on the Adobe Chat page, also with no results.  So I just reloaded my PSE 8
    program, which does still work!  And this morning I get a notice of an
    Adobe update to PSE 11 and, guess what?!--NOW PSE 11 works just fine!!  Go
    figure!
    All of which has nothing to do with you having responded to my plea for
    help.  I really appreciate someone responding; it's nice to know that
    people "out there" really do care.  Thank you.
    Cherie
    On Wed, Feb 6, 2013 at 4:55 AM, bridge_mastero

  • Everytime I try to open a creative cloud product, the program closes. I need to use both illustrator and indesign for school and I was wondering if anyone had any advice on how to actually make the program work?

    If anyone has any advice on how to make creative cloud programs work please let me know!
    Any help is appreciated.

    These questions below may be for a different product... but the KIND of information you need to supply is the same, for the products you use
    More information needed for someone to help... please click below and provide the requested information
    -Premiere Pro Video Editing Information FAQ http://forums.adobe.com/message/4200840

  • I Can't make the Geocoder handle Canadian Postal Code properly

    Hi !
    Does any one out there was able to approprietly setup Oracle Geocoder to properly return canadian postal code for a specific address in a dense urban area ?
    In Canada, a single street segment can have two different post codes when located in dense urban areas such as Montreal. One on the left and one the right... so one for odds number and one for even numbers.
    I don't see how to acheive this in the actual Oracle Geocoder table structure.
    I see two solutions to my problem. In order to appropriatly return a canadian post code for a specific address, Oracle would need to
    (1) have the GC_ROAD_SEGMENT_CA table have a L_POSTAL_CODE AND A R_POSTAL_CODE field. Thus returning the good postal code knowing if the house_number is odd or even.
    OR
    (2) have the GC_ADDRESS_POINT table have a POSTAL_CODE field allowing to directly return the postal code associated with an address.
    Any help would be greatly appreciated !!
    JB

    Hello,
    subscriber := sys.aq$_agent('sender', '[email protected]', NULL);
    should be
    subscriber := sys.aq$_agent('reciver', '[email protected]', NULL);
    With your enqueue code the message is being made available for any default subscribers. As it stands the message will be propagated to the target queue and made available for the consumer sender.
    So if you change your dequeue code to
    dequeue_options.consumer_name := 'SENDER';
    it will consume the message that has been propagated.
    Thanks
    Peter

  • Microsoft EXEL and Word users, is there any way to make the program fill

    the whole screen? Like in Windows? i really dont like all these menus flying everywhere...
    any thoughts?
    thanx!!

    You can try using this.
    It doesn't keep the menus from "flying everywhere" (that's just the way things work on the Mac; actually, some Linux environments too), but at least it makes it possible to maximize the apps to full screen.

  • Firefox is crashing or freezing excessively. I tried to upgrade to 3.6.12 but was told I lack necessary permissions. Is there a solution to make the program stable?

    I had a pop up that suggested updating Adobe flash (which I did) but then I lost the chance to take the next suggested step to stabilize Firefox. I'm using a Mac Mini G4 that I bought from a friend. I get Apple updates, but Mozilla says I lack permission for the Firefox upgrade.

    You can try to repair the disk permissions for the Firefox application files and folders, but it is easier to do trash the current version and download and install a full copy of the new version.
    * Download a new copy of the Firefox program: http://www.mozilla.com/firefox/all.html
    * Trash the current Firefox application to do a clean (re)install.
    * Install the new version that you have downloaded.
    Your profile data is stored elsewhere in the [http://kb.mozillazine.org/Profile_folder_-_Firefox Firefox Profile Folder], so you won't lose your bookmarks and other personal data.

Maybe you are looking for

  • Startup key combinations with bluetooth keyboard?

    In Lion, accessing the recovery partition entails holding down the command-R key combination at startup. Selecting a startup disk and starting up in Safe Boot mode also require holding key combinations at startup. But under Lion and a bluetooth keybo

  • New York 18 font?

    I am looking for New York 18, i think its a font, for Oregon Trail 5. Apparently it is supposed to come with OS9, but from what I can tell, Leopard does not have OS9 in it. any ideas on how to get this? I know it is copyrighted, but maybe its hidden

  • Checking for existing Rollback Segments

    Hello you all, Is there a possibility to check for existing rollback segments within procedures? Thanks Hans

  • NOCLassDefFoundError: javax/jdo/InstanceCallbacks

    Hey, We are trying to run the JDO enhancer through ant. When we run the task, we get the following error: However, we dont get this error when we run through Eclipse. Can someone tell me why we might be getting this error? And how can we get rid of t

  • How do i fix my backup assistant

    My backup assistant won't work