Hi...
I'm a new user here. I've stumbled across this place on a couple occasions in the past, and it worked out to my advantage when I needed a little help with some coding assignments.
My Data Structures class is working on stacks right now, and that is our current assignment.
This assignment is 3 parts, 1 of which is optional for extra credit.
The 2 parts I am working on, before hopefully also doing the third for extra points, are as follows:
1) verify whether or not equations are balanced, according to parentheses, brackets, and curly braces.
2) InFix to PostFix conversions.
example: (a+b)*c => ab+c*
All of the parts need to evaluate from a .txt file that is read in.
Here is what I have thrown together so far:
class stackOb {
int top;
char stack[];
int maxLen;
public stackOb(int maxLen) {
stack = new char[maxLen];
top = -1;
this.maxLen = maxLen;
}
public void push(char item) {
}
public char pop() {
}
public boolean empty() {
if (top == -1) {
return true;
}
else {
return false;
}
}
public void reset() {
top = -1;
}
public void showStack() {
int j;
System.out.println(" ");
System.out.println("Stack contents ...");
for(j=top;j>-1;j--) {
System.out.println(stack[j]);
}
System.out.println(" ");
}
public void showStack0toTop() {
int j;
System.out.println(" ");
System.out.println("Stack contents ...");
for(j=0;j<=top;j++) {
System.out.print(stack[j]);
}
System.out.println(" ");
}
}
import java.util.Scanner;
import java.io.*;
public class fileIn {
String fname;
public fileIn() {
System.out.println("starting fileIn()...");
getFileName();
readFileContents();
}
public void getFileName() {
Scanner in = new Scanner(System.in);
System.out.println("Enter file name please.");
fname = in.nextLine();
System.out.println("You entered "+fname);
}
public void readFileContents() {
boolean looping;
DataInputStream in;
String line;
int j, len;
char ch;
try {
in = new DataInputStream(new FileInputStream(fname));
looping = true;
while(looping) {
if (null == (line = in.readLine())) {
looping = false;
in.close();
}
else {
System.out.println("line = "+line);
len = line.length();
for(j=0;j<len;j++){
System.out.println("line["+j+"] = "+line.charAt(j));
}
}
}
}
catch(IOException e) {
System.out.println("Error " + e);
}
}
public static void main(String[] args) {
System.out.println("START main method...");
fileIn f = new fileIn();
System.out.println("END main method...");
}
}
This code I've provided will read in the .txt file and analyze each line at a character level, which I'm sure I need to do for this assignment. I've also included the structure for my stack class.
I've been staring at it for a while now, and I'm a little stumped with where to go from here. Perhaps I need to go back and reread some of the material on stacks and stack operations in order to get a clearer picture of what exactly I need to do.
Any tips or pointers of which direction to go from here would be greatly appreciated! :cool: