Czy moglby ktos sprawdzic to co napisalem? Nie potrafie napisac metody zeStosu, pomozecie mi?
Teraz mam cos takiego:
Klasa Nawiasy:
public class Nawiasy {
boolean x=false;
StosJakoLista s = new StosJakoLista();
ElementStosu e = new ElementStosu();
boolean nawiasyZrownowazone(String wyrazenie){
if(s.pusty()==true && wyrazenie!=null){
x=true;
}
return x;
}
boolean nawiasOtwierajacy(char ch){
if(e.dane=='(' || e.dane=='[' || e.dane== '{'){
x=true;
}
return x;
}
boolean nawiasZamykajacy(char ch){
if(e.dane==')' || e.dane==']' || e.dane== '}'){
x=true;
}
return x;
}
}
Interfejs Stos:
import java.util.EmptyStackException;
public interface Stos {
boolean pusty();
char naStos(char znak);
char zeStosu() throws EmptyStackException;
}
Klasa ElementStosu:
public class ElementStosu {
ElementStosu nast;
char dane;
public ElementStosu(){
dane=0;
nast=null;
}
}
Klasa StosJakoLista:
import java.util.EmptyStackException;
public class StosJakoLista implements Stos {
boolean czyPusty=false;
private ElementStosu head;
private int length = 0;
public StosJakoLista(){
head=null;
length=0;
}
ElementStosu pomoc = new ElementStosu();
public char naStos(char nawias) {
length++;
if(head==null){
head = new ElementStosu();
nawias = head.dane;
head.nast=new ElementStosu();
}
else{
head.nast=pomoc;
pomoc.dane=nawias;
pomoc.nast=new ElementStosu();
}
return nawias;
}
public boolean pusty() {
if(head == null){
czyPusty=true;
}
return czyPusty;
}
public char zeStosu() throws EmptyStackException {
(tutaj nie wiem za bardzo co zrobic)
}
}
Klasa Main:
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException{
String wyrazenie=null;
StosJakoLista st=new StosJakoLista();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ElementStosu el = new ElementStosu();
Nawiasy n = new Nawiasy();
System.out.println("Wprowadz wyrazenie: ");
try{
wyrazenie=br.readLine();
}
catch(IOException e){
System.out.print("Blad podczas odczytu danych");
return;
}
char[] tab = wyrazenie.toCharArray();
for(int i=0; i<tab.length; i++){
if(tab[i]=='('){
st.naStos('(');
if(tab[i]==')'){
st.zeStosu();
}
}
if(tab[i]=='['){
st.naStos('[');
if(tab[i]==']'){
st.zeStosu();
}
}
if(tab[i]=='{'){
st.naStos('{');
if(tab[i]=='}'){
st.zeStosu();
}
}
}
if(wyrazenie==null){
System.out.println("Blad. ");
}
else{
if(n.nawiasyZrownowazone(wyrazenie)){
System.out.println("To wyrazenie jest zrownowazone. ");
}
else{
System.out.println("To wyrazenie nie jest zrownowazone. ");
}
}
}
}
PS ten "zaifowany" main tez nie wyglada chyba dobrze..