Welcome to Dream.In.Code
Become a Java Expert!

Join 149,606 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,905 people online right now. Registration is fast and FREE... Join Now!




Complicated Expressions

 
Reply to this topicStart new topic

Complicated Expressions

futura91
18 Aug, 2007 - 07:35 PM
Post #1

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
i've got one last assignment:

Machine Problem 4
Complicated Expressions
Filename: MP4FamilyName.java
Deadline: August 20, 2007 12:00 NN
Input: inMP4.txt
Output: console
The most important activity of ACM is the GSM network. As the mobile phone operator, ACM must build its own transmitting stations. It is very important to compute the exact behavior of electro-magnetic waves. Unfortunately, prediction of electro-magnetic fields is a very complex task and the formulas describing them are very long and hard-to-read. For example, below are the Maxwell's Equations describing the basic laws of electrical engineering.
ACM has designed its own computer system that can make some field computations and produce results in the form of mathematic expressions. Unfortunately, by generating the expression in several steps, there are always some unneeded parentheses inside the expression. Your task is to take these partial results and make them "nice" by removing all unnecessary parentheses.
Input
There is a single positive integer T on the first line of input. It stands for the number of expressions to follow. Each expression consists of a single line containing only lowercase letters, operators (+, -, *, /) and parentheses (( and )). The letters are variables that can have any value, operators and parentheses have their usual meaning. Multiplication and division have higher priority then subtraction and addition. All operations with the same priority are computed from left to right (operators are leftassociative). There are no spaces inside the expressions. No input line contains more
than 250 characters.
Output
Print a single line for every expression. The line must contain the same expression with unneeded parentheses removed. You must remove as many parentheses as possible without changing the semantics of the expression. The semantics of the expression is considered the same if and only if any of the following conditions hold:
o The ordering of operations remains the same. That means "(a+B)+c" is the same
as "a+b+c", and "a+(b/c)" is the same as "a+b/c".
o The order of some operations is swapped but the result remains unchanged with respect to the addition and multiplication associativity. That means "a+(b+c)" and "(a+B)+c" are the same. We can also combine addition with subtraction and multiplication with division, if the subtraction or division is the second operation. For example, "a+(b-c)" is the same as "a+b-c".
o You cannot use any other laws, namely you cannot swap left and right operands and you cannot replace "a-(b-c)" with "a-b+c".
Sample Input
8
(a+(b*c))
((a+B)*c)
(a*(b*c))
(a*(b/c)*d)
((a/(b/c))/d)
((x))
(a+B)-(c-d)-(e/f)
(a+B)+(c-d)-(e+f)
Sample Output
a+b*c
(a+B)*c
a*b*c
a*b/c*d
a/(b/c)/d
x
a+b-(c-d)-e/f
a+b+c-d-(e+f)

i have no idea how to do this. could someone give me an algorithm? then i'll go from there and create my code and post it here for checking...

please...

This post has been edited by futura91: 18 Aug, 2007 - 07:39 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Complicated Expressions
18 Aug, 2007 - 07:49 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
QUOTE(futura91 @ 18 Aug, 2007 - 08:35 PM) *

i have no idea how to do this. could someone give me an algorithm? then i'll go from there and create my code and post it here for checking...

please...


I'm sorry, but I truly doubt, in fact I'm almost 100% positive, no one is just going to hand you an algorithm, thats not what we're here for.
User is offlineProfile CardPM
+Quote Post

futura91
RE: Complicated Expressions
18 Aug, 2007 - 08:31 PM
Post #3

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
i'm sorry... here, i've got my code. could anyone please check it???

CODE
import java.io.*;
import javax.swing.*;
import java.util.*;

public class MP4{
    public static void main(String[] args)
    throws IOException{
        try{
            String fileName = "inMP4.txt";
            String str="";
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            Boolean repeat = true;
            StringTokenizer st = new StringTokenizer(str);
            String a;
            int x=0;
            char tD;
            char[] b=new char[x];
            System.out.println("Output: ");
            while(repeat){
                str=br.readLine();
                if(str==null) {
                    repeat=false;
                    break;
                }
                else{
                    while(st.hasMoreTokens()){
                        x = st.countTokens();
                        a = st.nextToken();
                        b = a.toCharArray();
                        for(int i=0;i<x;i++){
                            tD=str.charAt(i);
                            if((tD=='(') || (tD==')')){
                                // this is where i want to delete the parentheses, what method can i use???
                            }
                            System.out.println(b);    
                        }
                    }    
                }      
            fr.close();
        }
    }
        
        catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Invalid!", "ERROR MESSAGE", JOptionPane.ERROR_MESSAGE);
        }
    }
}


This post has been edited by futura91: 18 Aug, 2007 - 08:34 PM
User is offlineProfile CardPM
+Quote Post

futura91
RE: Complicated Expressions
19 Aug, 2007 - 06:03 PM
Post #4

New D.I.C Head
*

Joined: 27 Jul, 2007
Posts: 12


My Contributions
i've made revisions to my previous code. but the problem is i can only delete the first parenthesis in a line. how can i make my program delete all the parentheses in every line?

CODE
import java.io.*;
import javax.swing.*;
import java.util.*;

public class MP4Dizon{
    public static void main(String[] args)
    throws IOException{
        try{
            String fileName = "d:\\inMP4.txt";
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            String str="";
            Boolean repeat = true;
            StringTokenizer st;
            String a;
            int x=0;
            char tD;
            System.out.println("Output: ");
            StringBuffer bufnew;
            while(repeat){
                str=br.readLine();
                if(str==null) {
                    repeat=false;
                    break;
                }
                else{
                    st = new StringTokenizer(str);
                    while(st.hasMoreTokens()){
                        x = st.countTokens();
                        a = st.nextToken();
                        bufnew = new StringBuffer(a);
                        for(int i=0;i<x;i++){
                            tD=str.charAt(i);
                                            if((tD=='(') || (tD==')')){
                                                bufnew.deleteCharAt(i);
                                               }
                                    System.out.println(bufnew);    
                                        }
                    }    
                }      
            }
            fr.close();
        }
        
        catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Invalid!", "ERROR MESSAGE", JOptionPane.ERROR_MESSAGE);
        }
    }
}


it's so close... please help me...
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month