diff options
42 files changed, 3032 insertions, 0 deletions
diff --git a/CS2452/DictionarySearch.java b/CS2452/DictionarySearch.java new file mode 100644 index 0000000..5d86a91 --- /dev/null +++ b/CS2452/DictionarySearch.java @@ -0,0 +1,91 @@ +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class DictionarySearch +{ + public static String[] dict; + + public static void main(String[] args) + { + BufferedReader ir = new BufferedReader(new InputStreamReader(System.in)); + BufferedReader fr = null; + try + { + fr = new BufferedReader(new FileReader("/home/tje/workspace/Recursion/dictionary.wwf")); + } catch (FileNotFoundException e1) + { + e1.printStackTrace(); + } + + String str = null; + dict = new String[0]; + System.out.println("Dictionary loading..."); + + do + { + try + { + str = fr.readLine(); + } catch (IOException e) + { + e.printStackTrace(); + } + + String[] tmp = new String[dict.length + 1]; + int i; + for (i = 0; i < dict.length; i++ ) + { + tmp[i] = dict[i]; + } + tmp[i] = str; + dict = tmp; + + } while (str != null); + System.out.println("Dictionary loaded."); + + while (true) + { + System.out.print("What word would you like to look up? "); + try + { + System.out.println("The closest word is " + check(ir.readLine().toLowerCase(), 0, dict.length)); + } catch (IOException e) + { + e.printStackTrace(); + } + + + + } + } + + public static String check(String word, int a, int b) + { + if (a == b -1) + { + return dict[a]; + } + int mid = (a+b)/2; + if(word.compareTo(dict[mid]) > 0) + { + return check(word, mid, b); + } + + if (word.compareTo(dict[mid]) < 0) + { + return check(word, a, mid); + } + + if (word.compareTo(dict[mid]) == 0) + { + return dict[mid]; + } + + + return word; + + } +}
\ No newline at end of file diff --git a/CS2452/GUI_network/Gui.java b/CS2452/GUI_network/Gui.java new file mode 100644 index 0000000..2187030 --- /dev/null +++ b/CS2452/GUI_network/Gui.java @@ -0,0 +1,108 @@ +package assign6; + +import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; +import java.io.*; +import java.util.concurrent.Semaphore; + +import javax.imageio.ImageIO; +import javax.swing.*; + +public class Gui extends JFrame implements ActionListener, MouseListener +{ + JPanel panel; + JLabel label; + BufferedImage image; + Color color; + Semaphore write; + boolean sc; + + + public Gui(String filename, Color color, Semaphore write, boolean sc) + { + this.color = color; + this.sc = sc; + + panel = new JPanel(new GridLayout(1,1)); + panel.setBackground(color); + try + { + image = ImageIO.read(new File(filename)); + } catch (IOException e) + { + e.printStackTrace(); + } + label = new JLabel(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT))); + + panel.add(label); + panel.addMouseListener(this); + this.add(panel); + + this.write = write; + + } + + public void actionPerformed(ActionEvent e) + { + + } + + public void mouseClicked(MouseEvent e) + { + write.release(); + changeImage(); + } + + + public void mouseEntered(MouseEvent e) + { + + } + + public void mouseExited(MouseEvent e) + { + + } + + public void mousePressed(MouseEvent e) + { + + } + + public void mouseReleased(MouseEvent e) + { + + } + + public void changeImage() + { + + String filename; + Color color; + if(sc) + { + filename = "img2.jpg"; + color = Color.GREEN; + } + else + { + filename = "img1.jpg"; + color = Color.BLUE; + } + + try + { + image = ImageIO.read(new File(filename)); + } catch (IOException e) + { + e.printStackTrace(); + } + label.setIcon(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT))); + + panel.setBackground(color); + panel.repaint(); + + sc = !sc; + } +}
\ No newline at end of file diff --git a/CS2452/GUI_network/MGui.java b/CS2452/GUI_network/MGui.java new file mode 100644 index 0000000..a0879c6 --- /dev/null +++ b/CS2452/GUI_network/MGui.java @@ -0,0 +1,92 @@ +package assign6; + +import java.net.*; +import java.util.concurrent.Semaphore; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +import java.io.*; + +public class MGui +{ + + public static void main(String[] args) + { + Gui g = null; + Semaphore write = new Semaphore(1); + try + { + write.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + ServerSocket ss; + Socket s = null; + + if (args[0].compareTo("server") == 0) + { + try + { + ss = new ServerSocket(Integer.parseInt(args[1])); + s = ss.accept(); + } catch (NumberFormatException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + g = new Gui("img1.jpg",Color.BLUE, write,true); + g.setSize(1000, 1000); + g.setLocation(32, 32); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + SocketWriter sw = new SocketWriter(s,write); + + + } + else if (args[0].compareTo("client") == 0) + { + + InetAddress address = null; + int port = 0; + + try + { + address = InetAddress.getByName(args[1]); + port = Integer.parseInt(args[2]); + + } catch (UnknownHostException e) + { + e.printStackTrace(); + } + + try + { + s = new Socket(address, port); + } catch (IOException e) + { + e.printStackTrace(); + } + + g = new Gui("img2.jpg",Color.GREEN,write,false); + g.setSize(1000, 1000); + g.setLocation(500, 500); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + SocketWriter sw = new SocketWriter(s,write); + + } + + SocketReader sr = new SocketReader(s,g); + + } + +} diff --git a/CS2452/GUI_network/SocketReader.java b/CS2452/GUI_network/SocketReader.java new file mode 100644 index 0000000..dacdaf1 --- /dev/null +++ b/CS2452/GUI_network/SocketReader.java @@ -0,0 +1,49 @@ +package assign6; + +import java.net.*; + +import javax.swing.JFrame; + +import java.io.*; + +public class SocketReader extends Thread +{ + BufferedReader br; + Gui g; + Thread t; + + public SocketReader(Socket s, Gui g) + { + try + { + br = new BufferedReader(new InputStreamReader(s.getInputStream())); + } catch (IOException e) + { + e.printStackTrace(); + } + + this.g = g; + + t = new Thread(this); + t.start(); + } + + public void run() + { + String str = null; + while (true) + { + try + { + str = br.readLine(); + } catch (IOException e) + { + e.printStackTrace(); + } + if (str != null) + g.changeImage(); + } + + } + +}
\ No newline at end of file diff --git a/CS2452/GUI_network/SocketWriter.java b/CS2452/GUI_network/SocketWriter.java new file mode 100644 index 0000000..1d882ab --- /dev/null +++ b/CS2452/GUI_network/SocketWriter.java @@ -0,0 +1,56 @@ +package assign6; + +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; + +public class SocketWriter extends Thread +{ + Thread t; + BufferedWriter bw; + Semaphore write; + boolean sc; + + public SocketWriter(Socket s, Semaphore write) + { + try{ + this.bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + } + catch(IOException e) + { + e.printStackTrace(); + } + + this.write = write; + + + + t = new Thread(this); + t.start(); + } + + public void run() + { + + while(true) + { + try + { + write.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + try + { + bw.newLine(); + bw.flush(); + } catch (IOException e) + { + e.printStackTrace(); + } + } + + } + +} diff --git a/CS2452/Q8.java b/CS2452/Q8.java new file mode 100644 index 0000000..76c0bcb --- /dev/null +++ b/CS2452/Q8.java @@ -0,0 +1,33 @@ + +public class Q8 +{ + + public static void main(String[] args) + { + int[] str= new int[(int) Math.ceil(args[0].length()/6.00)]; + + args[0] = args[0].toLowerCase(); + + while (args[0].length() % 6 != 0) + { + args[0] = args[0].concat(String.format("%c",'a' + 31)); + } + + for(int i = 0; i < str.length; i++) + { + str[i] = 0; + for(int j = 0; j < 6; j++) + { + str[i] <<= 5; + str[i] += args[0].charAt(j + (i*6)) - 'a' ; + } + } + + for(int i = 0; i < str.length; i++) + { + System.out.printf("%x\n", str[i]); + } + + } + +} diff --git a/CS2452/Skype/src/Skype/Client.java b/CS2452/Skype/src/Skype/Client.java new file mode 100644 index 0000000..f1716fe --- /dev/null +++ b/CS2452/Skype/src/Skype/Client.java @@ -0,0 +1,63 @@ +package Skype; + +import java.io.IOException; +import java.net.*; +import java.util.concurrent.Semaphore; + +public class Client extends Thread +{ + Socket s; + InetAddress ip; + int port; + audio aud; + Gui g; + Semaphore audflg; + Semaphore guiflg; + + public Client(Gui g, String ip, String port, audio aud, Semaphore audflg,Semaphore guiflg) + { + this.aud = aud; + this.audflg = audflg; + this.guiflg = guiflg; + this.g = g; + + try + { + this.ip = InetAddress.getByName(ip); + } catch (UnknownHostException e) + { + e.printStackTrace(); + } + this.port = Integer.parseInt(port); + this.start(); + } + + public void run() + { + //System.out.printf("Client Started\n"); + try + { + s = new Socket(ip, port); + } catch (IOException e) + { + e.printStackTrace(); + } + try + { + audflg.acquire(); + } catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + aud.setOpen(true); + audflg.release(); + + speaker t1 = new speaker(s, audio.getFormat(), aud, audflg,g); + mic t2 = new mic(s, audio.getFormat(), g, guiflg); + + t1.start(); + t2.start(); + } + +} diff --git a/CS2452/Skype/src/Skype/Gui.java b/CS2452/Skype/src/Skype/Gui.java new file mode 100644 index 0000000..6e0f0a0 --- /dev/null +++ b/CS2452/Skype/src/Skype/Gui.java @@ -0,0 +1,222 @@ +package Skype; + +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.concurrent.Semaphore; + +import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.text.AbstractDocument; + +public class Gui extends JFrame implements ActionListener, ChangeListener +{ + JButton call; + JCheckBox mute; + JSlider volume; + JLabel time, ipL, portL, volumeL,ipLH,portLH; + JTextField ipT, portT; + JPanel panel, ip, volumeP, slider; + GridBagConstraints c; + Timer timer; + int sec; + + boolean incall, servercall; + + Semaphore audflg; + Semaphore svrflg; + Semaphore guiflg; + audio aud; + + public Gui(audio aud, Semaphore svrflg, Semaphore audflg, Semaphore guiflg,int port) + { + this.guiflg = guiflg; + timer = new Timer(1000, this); + incall = false; + this.svrflg = svrflg; + this.audflg = audflg; + this.aud = aud; + + c = new GridBagConstraints(); + c.ipadx = 10; + c.ipady = 10; + + panel = new JPanel(new GridBagLayout()); + + call = new JButton("Call"); + call.addActionListener(this); + + //Volume Panel setup + volume = new JSlider(SwingConstants.VERTICAL); + volume.addChangeListener(this); + volume.setPaintTicks(true); + volume.setMajorTickSpacing(25); + volume.setMinorTickSpacing(5); + volume.setPaintTicks(true); + volume.setPaintLabels(true); + + volumeL = new JLabel("Volume"); + + volumeP = new JPanel(); + volumeP.setLayout(new GridBagLayout()); + + volumeP.add(volume, c); + c.gridx = 0; + c.gridy = 1; + volumeP.add(volumeL, c); + + mute = new JCheckBox("Mute"); + mute.addActionListener(this); + c.gridx = 1; + c.gridy = 1; + volumeP.add(mute, c); + volumeP.setVisible(true); + + //Address setup + ipL = new JLabel("IP:"); + ipT = new JTextField(15); + ipT.setText("127.0.0.1"); + ((AbstractDocument) ipT.getDocument()).setDocumentFilter(new numberFilter()); + ip = new JPanel(new GridBagLayout()); + c.gridx = 0; + c.gridy = 0; + c.anchor = GridBagConstraints.EAST; + ip.add(ipL, c); + c.gridx = 1; + + c.anchor = GridBagConstraints.WEST; + ip.add(ipT, c); + + portL = new JLabel("Port:"); + portT = new JTextField(5); + portT.setText("7777"); + ((AbstractDocument) portT.getDocument()).setDocumentFilter(new numberFilter()); + c.gridy = 1; + c.anchor = GridBagConstraints.WEST; + ip.add(portT, c); + c.gridx = 0; + c.anchor = GridBagConstraints.EAST; + ip.add(portL, c); + + try + { + ipLH = new JLabel("Your IP: ".concat(InetAddress.getLocalHost().getHostAddress().toString())); + } catch (UnknownHostException e) + { + e.printStackTrace(); + } + portLH = new JLabel("Your Port: ".concat(Integer.toString(port))); + + + c.anchor = GridBagConstraints.WEST; + c.gridy = 2; + c.gridx = 1; + ip.add(ipLH,c); + c.gridy++; + ip.add(portLH,c); + + time = new JLabel("TIME"); + + c.gridx = 0; + c.gridy = 0; + c.anchor = GridBagConstraints.NORTHWEST; + panel.add(ip, c); + c.gridx = 1; + panel.add(volumeP, c); + c.gridy = 1; + c.anchor = GridBagConstraints.CENTER; + panel.add(call, c); + c.gridx = 0; + panel.add(time, c); + panel.setVisible(true); + + this.add(panel); + + } + + public void actionPerformed(ActionEvent e) + { + try + { + guiflg.acquire(); + + if (e.getSource() == mute) + { + if (mute.isSelected()) + { + volume.setEnabled(false); + aud.setVolume(0); + } + else + { + volume.setEnabled(true); + aud.setVolume(volume.getValue()); + } + } + else if (e.getSource() == call) + { + if (incall) + { + call.setText("Call"); + timer.stop(); + //System.out.println("Hung up"); + incall = false; + try + { + audflg.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + //System.out.println("False"); + aud.setOpen(false); + audflg.release(); + svrflg.release(); //allow server to accept connections again. + } + else + { + call.setText("Hang up"); + sec = 0; + timer.start(); + Client c = new Client(this, ipT.getText(), portT.getText(), aud, audflg,guiflg); + //System.out.printf("IP: %s, Port: %s\n", ipT.getText(), portT.getText()); + incall = true; + + } + } + else if ((e.getSource() == timer) && incall) + { + sec++ ; + if (sec < 60 * 60) time.setText(String.format("%02d:%02d", sec / 60, sec % 60)); + else time.setText(String.format("%d:%02d:%02d", sec / (60 * 60), (sec / 60) % 60, sec % 60)); + } + } catch (InterruptedException e2) + { + // TODO Auto-generated catch block + e2.printStackTrace(); + } + guiflg.release(); + + } + + public void stateChanged(ChangeEvent e) + { + if (e.getSource() == volume) + { + if (audflg.tryAcquire()) + { + aud.setVolume(volume.getValue()); + //change volume .getValue(); + audflg.release(); + } + + } + } + +} diff --git a/CS2452/Skype/src/Skype/MGui.java b/CS2452/Skype/src/Skype/MGui.java new file mode 100644 index 0000000..c545ffe --- /dev/null +++ b/CS2452/Skype/src/Skype/MGui.java @@ -0,0 +1,27 @@ +package Skype; + +import java.util.concurrent.Semaphore; + +import javax.swing.JFrame; + +public class MGui +{ + public static void main(String[] args) + { + audio aud = new audio(50); + Semaphore volflg = new Semaphore(1); + Semaphore svrflg = new Semaphore(1); + Semaphore guiflg = new Semaphore(1); + + int port = 7777; + + Gui g = new Gui(aud,svrflg,volflg,guiflg,port); + g.setSize(500, 500); + g.setLocation(32, 32); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + Server server = new Server(g, port,aud,svrflg,volflg,guiflg); + } + +} diff --git a/CS2452/Skype/src/Skype/Server.java b/CS2452/Skype/src/Skype/Server.java new file mode 100644 index 0000000..e8b8a0b --- /dev/null +++ b/CS2452/Skype/src/Skype/Server.java @@ -0,0 +1,88 @@ +package Skype; + +import java.io.IOException; +import java.net.*; +import java.util.concurrent.Semaphore; + +public class Server extends Thread +{ + ServerSocket ss; + Socket s; + int port; + Gui g; + audio aud; + Semaphore svrflg; + Semaphore audflg; + Semaphore guiflg; + + public Server(Gui g, int port, audio aud, Semaphore svrflg, Semaphore audflg, Semaphore guiflg) + { + this.guiflg = guiflg; + this.audflg = audflg; + this.svrflg = svrflg; + this.aud = aud; + this.port = port; + this.g = g; + try + { + this.ss = new ServerSocket(port); + } catch (IOException e) + { + e.printStackTrace(); + } + + this.start(); + + } + + public void run() + { + while (true) + { + //System.out.printf("Server is waiting\n"); + try + { + svrflg.acquire(); + //System.out.println(ss); + s = ss.accept(); + } catch (IOException e) + { + e.printStackTrace(); + } catch (InterruptedException e) + { + e.printStackTrace(); + } + + try + { + audflg.acquire(); + } catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + aud.setOpen(true); + audflg.release(); + //System.out.println("ServerSpeaker"); + speaker t1 = new speaker(s, audio.getFormat(), aud, audflg,g); + mic t2 = new mic(s, audio.getFormat(),g,guiflg); + try + { + guiflg.acquire(); + g.call.setText("Hang up"); + g.sec = 0; + g.timer.start(); + g.incall = true; + guiflg.release(); + } catch (InterruptedException e) + { + e.printStackTrace(); + } + + t1.start(); + t2.start(); + + } + } + +}
\ No newline at end of file diff --git a/CS2452/Skype/src/Skype/audio.java b/CS2452/Skype/src/Skype/audio.java new file mode 100644 index 0000000..d9181fd --- /dev/null +++ b/CS2452/Skype/src/Skype/audio.java @@ -0,0 +1,36 @@ +package Skype; + +import java.net.Socket; + +import javax.sound.sampled.AudioFormat; + +public class audio +{ + double vol; + boolean open; + + audio(int vol) + { + this.vol = vol / 100.00; + } + + public static AudioFormat getFormat() + { + float sampleRate = 8000; + int sampleSizeInBits = 8; + int channels = 1; + boolean signed = true; + boolean bigEndian = true; + return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); + } + + public void setVolume(int volume) + { + vol = volume / 100.00; + } + + public void setOpen(boolean a){ + open = a; + } + +}
\ No newline at end of file diff --git a/CS2452/Skype/src/Skype/mic.java b/CS2452/Skype/src/Skype/mic.java new file mode 100644 index 0000000..01cadfd --- /dev/null +++ b/CS2452/Skype/src/Skype/mic.java @@ -0,0 +1,77 @@ +package Skype; + +import java.util.*; +import java.util.concurrent.Semaphore; + +import javax.sound.sampled.*; + +import java.io.*; +import java.net.*; + +class mic extends Thread +{ + Socket s; + AudioFormat format; + Gui g; + Semaphore guiflg; + + public mic(Socket socket, AudioFormat format, Gui g, Semaphore guiflg) + { + s = socket; + this.format = format; + this.g = g; + this.guiflg = guiflg; + } + + public void run() + { + BufferedOutputStream bout = null; + try + { + DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); + final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info); + + line.open(format, 1024); + line.start(); + int bufferSize = 32; + byte buffer[] = new byte[bufferSize]; + try + { + bout = new BufferedOutputStream(s.getOutputStream()); + + //System.out.printf("mic started\n"); + while (true) + { + int count = line.read(buffer, 0, buffer.length); + + if (count > 0) bout.write(buffer, 0, count); + + bout.flush(); + } + }catch (SocketException se) + { + try + { + guiflg.acquire(); + } catch (InterruptedException e) + { + e.printStackTrace(); + } + + g.call.setText("Call"); + g.incall = false; + guiflg.release(); + + + } + catch (Exception e) + { + e.printStackTrace(); + } + } catch (LineUnavailableException e1) + { + e1.printStackTrace(); + } + } + +} diff --git a/CS2452/Skype/src/Skype/numberFilter.java b/CS2452/Skype/src/Skype/numberFilter.java new file mode 100644 index 0000000..def1b3e --- /dev/null +++ b/CS2452/Skype/src/Skype/numberFilter.java @@ -0,0 +1,48 @@ +package Skype; + +import java.awt.Toolkit; + +import javax.swing.text.AttributeSet; +import javax.swing.text.BadLocationException; +import javax.swing.text.DocumentFilter; + +class numberFilter extends DocumentFilter +{ + @Override + public void insertString(DocumentFilter.FilterBypass fp, int offset, String string,AttributeSet aset) throws BadLocationException + { + int len = string.length(); + boolean isValidInteger = true; + + for (int i = 0; i < len; i++ ) + { + if (!Character.isDigit(string.charAt(i)) && string.charAt(i) != '.') + { + isValidInteger = false; + break; + } + } + if (isValidInteger) super.insertString(fp, offset, string, aset); + else + Toolkit.getDefaultToolkit().beep(); + } + + public void replace(DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset) throws BadLocationException + { + int len = string.length(); + boolean isValidInteger = true; + + for (int i = 0; i < len; i++) + { + if (!Character.isDigit(string.charAt(i)) && string.charAt(i) != '.') + { + isValidInteger = false; + break; + } + } + if (isValidInteger) + super.replace(fp, offset, length, string, aset); + else + Toolkit.getDefaultToolkit().beep(); + } +}
\ No newline at end of file diff --git a/CS2452/Skype/src/Skype/speaker.java b/CS2452/Skype/src/Skype/speaker.java new file mode 100644 index 0000000..596986f --- /dev/null +++ b/CS2452/Skype/src/Skype/speaker.java @@ -0,0 +1,90 @@ +package Skype; + +import java.util.*; +import java.util.concurrent.Semaphore; +import java.io.*; +import java.net.*; +import javax.sound.sampled.*; + +class speaker extends Thread +{ + Socket s; + AudioFormat format; + audio aud; + Semaphore flg; + Gui g; + + public speaker(Socket socket, AudioFormat format, audio aud, Semaphore audflg, Gui g) + { + this.g = g; + this.aud = aud; + this.flg = audflg; + s = socket; + this.format = format; + } + + public void run() + { + try + { + DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); + final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); + line.open(format, 1024); + line.start(); + + final AudioInputStream ais = new AudioInputStream(s.getInputStream(), format, 2000000); + int count; + int bufferSize = (int) format.getSampleRate() * format.getFrameSize(); + byte buffer[] = new byte[bufferSize]; + + //System.out.printf("speakers started\n"); + while (((count = ais.read(buffer, 0, buffer.length)) != -1) && aud.open) + { + flg.release(); + if (count > 0) + { + if (flg.tryAcquire()) + { + + for (int i = 0; i < buffer.length; i++ ) + { + buffer[i] *= aud.vol; + } + flg.release(); + } + + line.write(buffer, 0, count); + } + + } + line.drain(); + line.close(); + } catch (IOException e) + { + System.err.println("I/O problems: " + e); + + } catch (LineUnavailableException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + try + { + flg.acquire(); + } catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + //System.out.println("out of while"); + try + { + s.close(); + //System.out.println("S closed"); + } catch (IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/CS2452/assign2/src/P1.java b/CS2452/assign2/src/P1.java new file mode 100644 index 0000000..3580cf9 --- /dev/null +++ b/CS2452/assign2/src/P1.java @@ -0,0 +1,39 @@ +import java.io.*; + +class P1 +{ + + public static void main(String[] args) + { + String str = null; + BufferedReader br; + br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = null; + + try + { + File file = new File(args[0]); + + if (! file.exists()) + { + file.createNewFile(); + } + + bw = new BufferedWriter(new FileWriter(file)); + while(true) + { + str = br.readLine(); + bw.write(str); + bw.newLine(); + bw.flush(); + } + } catch(Exception e) + { + System.out.printf("Unable to write to file %s\n", args[0]); + } + + + + + } +} diff --git a/CS2452/assign5/src/Client.java b/CS2452/assign5/src/Client.java new file mode 100755 index 0000000..9d7cb19 --- /dev/null +++ b/CS2452/assign5/src/Client.java @@ -0,0 +1,32 @@ +import java.net.*; +import java.io.*; + +class Client +{ + public static void main(String[] args) + { + try + { + byte b = 0; + int cnt = 0; + Socket s = new Socket(args[0], Integer.parseInt(args[1])); + DataInputStream di = new DataInputStream(s.getInputStream()); + while (true) + { + b = di.readByte(); + System.out.printf("%d - %d\t%x\t%c\n", ++cnt, b, b, b); + if ((cnt % (1024 * 1024)) == 0) + { + System.out.println("------MILLION------\n\n"); + cnt = 0; + Thread.sleep(1000); + di.close(); + return; + } + } + } catch (Exception e) + { + e.printStackTrace(); + } + } +}
\ No newline at end of file diff --git a/CS2452/assign5/src/ResourceReader.java b/CS2452/assign5/src/ResourceReader.java new file mode 100755 index 0000000..f9f0f71 --- /dev/null +++ b/CS2452/assign5/src/ResourceReader.java @@ -0,0 +1,89 @@ +import java.io.*; +import java.util.concurrent.Semaphore; + +public class ResourceReader implements Runnable +{ + public static double cor = 0; + public static Semaphore flg1 = new Semaphore(1); + + Thread t; + byte[] res = null, sentinel = null; + DataOutputStream dout = null; + Semaphore flg = null; + Semaphore flg2 = null; + + ResourceReader(DataOutputStream dot, byte[] r, Semaphore s, Semaphore s2) + { + res = r; + dout = dot; + flg = s; + flg2 = s2; + sentinel = new byte[32768]; + for (int i = 0; i < sentinel.length; i++ ) + sentinel[i] = 65; + t = new Thread(this, "ResourceReader"); + t.start(); + } + + public void run() + { + int i; + byte value = 33; + boolean running = true; + + System.out.println("ResourceReader: starts"); + while (running) + { + try + { + + flg2.acquire(); + flg2.release(); + flg1.acquire(); + if (cor == 0) + { + flg.acquire(); + cor++ ; + flg1.release(); + for (int j = 0; j < 50; j++ ) + dout.write(res, 0, res.length); + dout.write(sentinel, 0, sentinel.length); + flg.release(); + } + else + { + cor++ ; + flg1.release(); + for (int j = 0; j < 50; j++ ) + dout.write(res, 0, res.length); + dout.write(sentinel, 0, sentinel.length); + } + flg1.acquire(); + cor-- ; + flg1.release(); + + System.out.println("ResourceReader: sent a bunch"); + Thread.sleep(100); + running = false; + } catch (Exception e) + { + e.printStackTrace(); + try + { + dout.close(); + } catch (Exception e2) + { + } + return; + } + } + System.out.println("ResourceReader: ends"); + try + { + dout.close(); + } catch (Exception e2) + { + } + + } +} diff --git a/CS2452/assign5/src/ResourceWriter.java b/CS2452/assign5/src/ResourceWriter.java new file mode 100755 index 0000000..2fb0967 --- /dev/null +++ b/CS2452/assign5/src/ResourceWriter.java @@ -0,0 +1,56 @@ +import java.util.concurrent.Semaphore; + +public class ResourceWriter implements Runnable +{ + Thread t; + byte[] res = null; + Semaphore flg = null; + Semaphore flg2 = null; + + ResourceWriter(byte[] r, Semaphore s, Semaphore s2) + { + res = r; + flg = s; + flg2 = s2; + t = new Thread(this, "ResourceWriter"); + t.start(); + } + + public void run() + { + int i; + byte value = 33; + boolean running = true; + + System.out.println("ResourceWriter: starts"); + while (running) + { + System.out.println("RW in while"); + try + { + flg2.acquire(); + flg.acquire(); + + for (i = 0; i < res.length; i++ ) + res[i] = value; + flg.release(); + flg2.release(); + } catch (Exception e0) + { + e0.printStackTrace(); + } + + value++ ; + if (value == 100) value = 33; + System.out.println("ResourceWriter: updated, waiting 15, 14, ..."); + try + { + Thread.sleep(15000); + } catch (Exception e) + { + } + } + System.out.println("ResourceWriter: ends"); + + } +} diff --git a/CS2452/assign5/src/Test.java b/CS2452/assign5/src/Test.java new file mode 100755 index 0000000..ed6234c --- /dev/null +++ b/CS2452/assign5/src/Test.java @@ -0,0 +1,23 @@ +class Test +{ + public static void main(String[] args) + { + try + { + if (args.length != 1) + { + System.out.println("usage: java Test listeningPort"); + System.exit(1); + } + System.out.println("Test progam starts"); + + WRServer server = new WRServer(Integer.parseInt(args[0])); + server.t.join(); + + System.out.println("Test progam ends"); + } catch (Exception e) + { + e.printStackTrace(); + } + } +}
\ No newline at end of file diff --git a/CS2452/assign5/src/WRServer.java b/CS2452/assign5/src/WRServer.java new file mode 100755 index 0000000..eadddc3 --- /dev/null +++ b/CS2452/assign5/src/WRServer.java @@ -0,0 +1,59 @@ +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; + +class WRServer implements Runnable +{ + int port; + Thread t; + byte[] theSharedResource = null; + Semaphore flg = null; + Semaphore flg2 = null; + + WRServer(int p) + { + port = p; + theSharedResource = new byte[1024 * 1024]; + flg = new Semaphore(1); + flg2 = new Semaphore(2); + t = new Thread(this, "WRServer"); + t.start(); + } + + public void run() + { + ServerSocket ss = null; + Socket s = null; + DataOutputStream dout = null; + boolean running = true; + System.out.println("WRServer is alive"); + + ResourceWriter rw = new ResourceWriter(theSharedResource, flg, flg2); + + try + { + ss = new ServerSocket(port); + } catch (Exception e0) + { + e0.printStackTrace(); + } + + while (running) + { + try + { + System.out.println("WRServer: waiting for incoming connection"); + s = ss.accept(); + System.out.println("WRServer: received incoming connection"); + + dout = new DataOutputStream(s.getOutputStream()); + ResourceReader rr = new ResourceReader(dout, theSharedResource, flg, flg2); + } catch (Exception e) + { + e.printStackTrace(); + } + } + + System.out.println("WRServer is finished"); + } +} diff --git a/CS2452/chat/ChatClient.java b/CS2452/chat/ChatClient.java new file mode 100644 index 0000000..816d097 --- /dev/null +++ b/CS2452/chat/ChatClient.java @@ -0,0 +1,47 @@ +import java.util.*; +import java.io.*; +import java.net.*; + +class ChatClient +{ + public static void main(String[] args) + { + Socket s; + BufferedReader br, tr; + BufferedWriter bw; + InetAddress address = null; + int port = 0; + + tr = new BufferedReader(new InputStreamReader(System.in)); + + try{ + address = InetAddress.getByName(args[0]); + port = Integer.parseInt(args[1]); + }catch (UnknownHostException ex) + { + ex.printStackTrace(); + } + + try + { + System.out.printf("Connecting...\n"); + s = new Socket(address, port); + + br = new BufferedReader(new InputStreamReader(s.getInputStream())); + + bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + System.out.printf("Client>"); + + while(true) + { + bw.write(tr.readLine()); + bw.newLine(); + bw.flush(); + System.out.printf("%s: %s\n%s>", "Server", br.readLine(), "Client"); + } + } catch (Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chat/ChatServer.java b/CS2452/chat/ChatServer.java new file mode 100644 index 0000000..3259611 --- /dev/null +++ b/CS2452/chat/ChatServer.java @@ -0,0 +1,44 @@ +import java.io.*; +import java.util.*; +import java.net.*; + +class ChatServer +{ + public static void main(String[] args) + { + ServerSocket ss; + Socket s; + BufferedReader br, tr; + BufferedWriter bw; + + tr = new BufferedReader(new InputStreamReader(System.in)); + + try + { + ss = new ServerSocket(2452); + + s = ss.accept(); + System.out.printf("Client connected...\n"); + + + br = new BufferedReader(new InputStreamReader(s.getInputStream())); + bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + + //String[] address = new String[2]; + //address[0] = InetAddress.toString(s.getAddress()); + //address[1] = InetAddress.toString(s.getLocalAddress()); + + while(true) + { + System.out.printf("%s: %s\n%s>", "Client", br.readLine(), "Server"); + bw.write(tr.readLine()); + bw.newLine(); + bw.flush(); + } + + }catch (Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chatThreaded/ChatClient.java b/CS2452/chatThreaded/ChatClient.java new file mode 100644 index 0000000..1822240 --- /dev/null +++ b/CS2452/chatThreaded/ChatClient.java @@ -0,0 +1,40 @@ +import java.util.*; +import java.io.*; +import java.net.*; + + + +class ChatClient +{ + public static void main(String[] args) + { + Socket s; + InetAddress address = null; + int port = 0; + + + + try + { + address = InetAddress.getByName(args[0]); + port = Integer.parseInt(args[1]); + }catch (UnknownHostException ex){ + ex.printStackTrace(); + } + + try + { + s = new Socket(address, port); + + T1 screenWriter = new T1(s); + T2 socketWriter = new T2(s); + screenWriter.start(); + socketWriter.start(); + + } + catch(Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chatThreaded/ChatServer.java b/CS2452/chatThreaded/ChatServer.java new file mode 100644 index 0000000..67608d1 --- /dev/null +++ b/CS2452/chatThreaded/ChatServer.java @@ -0,0 +1,27 @@ +import java.util.*; +import java.io.*; +import java.net.*; + +class ChatServer +{ + public static void main(String[] args) + { + ServerSocket ss; + Socket s; + + try + { + ss = new ServerSocket(2452); + s = ss.accept(); + + T1 screenWriter = new T1(s); + T2 socketWriter = new T2(s); + screenWriter.start(); + socketWriter.start(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chatThreaded/T1.java b/CS2452/chatThreaded/T1.java new file mode 100644 index 0000000..6fd8e06 --- /dev/null +++ b/CS2452/chatThreaded/T1.java @@ -0,0 +1,33 @@ +import java.util.*; +import java.io.*; +import java.net.*; + + +class T1 extends Thread +{ + Socket s; + + public T1(Socket socket) + { + s = socket; + } + public void run() + { + BufferedReader tr = new BufferedReader(new InputStreamReader(System.in)); + + try + { + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + + while(true) + { + bw.write(tr.readLine()); + bw.newLine(); + bw.flush(); + } + }catch(Exception e){ + e.printStackTrace(); + } + } +} + diff --git a/CS2452/chatThreaded/T2.java b/CS2452/chatThreaded/T2.java new file mode 100644 index 0000000..470c917 --- /dev/null +++ b/CS2452/chatThreaded/T2.java @@ -0,0 +1,32 @@ + +import java.util.*; +import java.io.*; +import java.net.*; + + +class T2 extends Thread +{ + Socket s; + public T2(Socket socket) + { + s = socket; + } + public void run() + { + + try + { + + BufferedReader bw = new BufferedReader( new InputStreamReader(s.getInputStream())); + String str = "Connected"; + do + { + System.out.printf("%s\n", str); + str = bw.readLine(); + }while (str != null); + }catch(Exception e){ + e.printStackTrace(); + } + } +} + diff --git a/CS2452/sumofdigits.java b/CS2452/sumofdigits.java new file mode 100644 index 0000000..24d4b69 --- /dev/null +++ b/CS2452/sumofdigits.java @@ -0,0 +1,18 @@ + +public class sumofdigits +{ + public static void main(String[] args) + { + System.out.print(sum(23657)); + } + + public static int sum(int num) + { + if(num == 0) + { + return 0; + } + return (num % 10) + sum(num/10); + } + +} diff --git a/CS2501/bstAssignment.c b/CS2501/bstAssignment.c new file mode 100644 index 0000000..6fff4a7 --- /dev/null +++ b/CS2501/bstAssignment.c @@ -0,0 +1,215 @@ +#include<stdlib.h> +#include<stdio.h> +#include <string.h> + +typedef struct anode { + char *name; + struct anode *r, *l; +} node; + + + +node* contains(tree, q) +node **tree; +char *q; +{ + node *tmp; + if(!(*tree)) return 0; + + if ( strcmp((*tree)->name, q) == 0){ + return *tree; + } + tmp = contains(&(*tree)->l, q); + return tmp ? tmp : contains(&(*tree)->r, q); +} + + + +insert(tree, item) +node **tree; +node *item; +{ + if(!(*tree)) { + *tree = item; + return 1; + } + + if(strcmp(item->name, (*tree)->name) < 0) + insert(&(*tree)->l, item); + + else if(strcmp(item->name, (*tree)->name) > 0) + insert(&(*tree)->r, item); +} + +node* getParent(tree,item) +node **tree; +node *item; +{ + node *tmp; + if(!(*tree)) return 0; + if(*tree == item) + return -1; + if((*tree)->l == item || (*tree)->r == item) + return *tree; + tmp = getParent(&(*tree)->l, item); + return tmp ? tmp : getParent(&(*tree)->r, item); +} + +delete(tree, item) +node **tree; +node *item; +{ + node *tmp, *parent, *tmp_parent; + int cnt; + if(!(*tree)) return 1; + tmp = contains(tree, item->name); + if(!tmp){ + printf("%s dose not exist\n", item->name); + return 1; + } + parent = getParent(tree, tmp); + if(!tmp->l && !tmp->r) + { + if (parent == -1){ + *tree = 0; + return 1; + free(tmp); + } + if(parent->l == tmp){ + parent->l = 0; + free(tmp); + return 1; + } + parent->r = 0; + free(tmp); + return 1; + } + if(tmp->l && tmp->r){ + item = tmp->r; + while(item->l) + item = item->l; + + tmp_parent = getParent(tree, item); + if(parent == -1){ + *tree = item; + item->l = tmp->l; + item->r = tmp->r; + + if(tmp_parent == tmp) + item->r = 0; + else + tmp_parent->l = 0; + + tree = &item; + free(tmp); + return 1; + + } + if(parent->l == tmp){ + parent->l = item; + item->l = tmp->l; + item->r = tmp->r; + + if(tmp_parent == tmp) + item->r = 0; + else + tmp_parent->l = 0; + + tree = &item; + free(tmp); + return 1; + } + parent->r = item; + item->l = tmp->l; + item->r = tmp->r; + + + if(tmp_parent == tmp) + item->r = 0; + else + tmp_parent->l = 0; + + tree = &item; + free(tmp); + return 1; + + } + + if (parent == -1) { + *tree = tmp->l ? tmp->l : tmp->r; + free(tmp); + return 1; + } + if(parent->l == tmp){ + parent->l = tmp->l ? tmp->l : tmp->r; + free(tmp); + return 1; + } + + parent->r = tmp->l ? tmp->l : tmp->r; + return 1; + + +} + + + + +void printTree(tree) +node **tree; +{ + if (!(* tree)) return; + printTree(&(*tree)->l); + printTree(&(*tree)->r); + printf("\t%s\n",(*tree)->name); + return; +} + + +/* +The loop below reads from stdin until EOF, then searches +to see if the BST contains the word found in argv[1]. + +Modify the loop to read a series of lines of one of two types: + +i value +d value + +Lines starting with the ASCII i are to be inserted into the binary search tree. +Those beginning with 'd' are to be deleted if they exist. If they do not exist, +output a line of text saying so and do nothing else. + +Stop your read loop at EOF (like it is now). Instead of +calling contains(), print out the tree; make it a preorder print */ + + + +void main(argc, argv, envp) +int argc; +char **argv, **envp; +{ + int i; + char buf[1024]; + node *curr, *root; + + root = 0; + + while (gets(buf)) { + curr = (node *)malloc(sizeof(node)); + curr->l = curr->r = NULL; + curr->name = strdup(buf + 2); + if('i' == *buf){ + printf("insert: %s %s\n", buf, curr->name); + insert(&root, curr); + } + else if('d' == *buf){ + printf("delete:%s %s\n", buf, curr->name); + delete(&root, curr); + } + else if ('p' == *buf) + printTree(&root); + + } + printTree(&root); + +} diff --git a/CS2501/graph.c b/CS2501/graph.c new file mode 100644 index 0000000..75d2353 --- /dev/null +++ b/CS2501/graph.c @@ -0,0 +1,116 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct agraph{ + int matrix[100][100]; +} graph; + +extern graph *g = 0; + +void init_graph() +{ + int i; + g = malloc(sizeof(graph)); + for (i = 0; i < 100; i++){ + g->matrix[i][i] = -1; + } +} + +int adjacent(x,y) +int x,y; +{ + return g->matrix[x][y]; +} + +int* neighbors(x) +int x; +{ + int i, j; + j = 1; + for(i = 0; i < 100; i++) + { + if(g->matrix[x][i]){ + j++; + } + } + int *y; + y = calloc(j * sizeof(int)); + j = 0; + for(i = 0; i < 100; i++) { + if (g->matrix[x][i]){ + y[j++] = i; + } + } + return y; +} + +int add_vertex(x) +int x; +{ + if(!g){ + init_graph(); + } + if (g->matrix[x][x] == -1){ + g->matrix[x][x] = 0; + return 1; + } + return 0; +} + +int remove_vertex(x) +int x; +{ + if(g->matrix[x][x] == -1){ + return 0; + } + int i; + for(i = 0; i < 100; i++){ + g->matrix[x][i] = 0; + g->matrix[i][x] = 0; + } + return 1; +} + +int add_edge(x,y) +int x,y; +{ + if (g->matrix[x][y]){ + return 0; + } + + g->matrix[x][y] = 1; + return 1; +} + +int remove_edge(x,y) +int x,y; +{ + if(g->matrix[x][y]){ + g->matrix[x][y] = 0; + return 1; + } + return 0; + +} + +void printEdges() +{ + printf("Edges: \n" ); + int i,j; + for (i = 0; i < 100; i++){ + for(j = 0; j < 100; j++){ + if(g->matrix[i][j] == 1) + printf("\t%d -> %d\n", i, j); + } + } +} + +void printVertices() +{ + printf("Vertices:\n"); + int i; + for(i = 0; i <100; i++) + if(g->matrix[i][i] != -1) + printf("\t%d\n", i); + +} diff --git a/CS2501/sorting/sortAssignment1.c b/CS2501/sorting/sortAssignment1.c new file mode 100644 index 0000000..1ff5662 --- /dev/null +++ b/CS2501/sorting/sortAssignment1.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + + +printData(d,n) +int *d,n; +{ + int i; + for (i=0; i<n; i++) + printf("data[%d] = %d\n", i, d[i]); +} + + +selectionSortAscending(d, n) +int *d, n; +{ + int i, j, tmp; + + for (i = 0; i < n; i++) { + tmp = d[i]; + for (j = i + 1; j < n; j++) { + if(tmp > d[j]){ + d[i] = d[j]; + d[j] = tmp; + tmp = d[i]; + } + } + + } +} + +insertionSortDescending(d, n) +int *d, n; +{ + int i,j,tmp; + + for (i = 1; i < n; i++){ + for (j = i; d[j] > d[j-1] && j != 0; j--){ + tmp = d[j]; + d[j] = d[j-1]; + d[j-1] = tmp; + } + } +} + + + + +main(argc, argv, envp) +int argc; +char **argv, **envp; +{ + char buf[1024]; + int n, *data, i; + if (argc !=2 ) { + printf("usage: sortAssignment1 #elements\n"); + exit(1); + } + + n = atoi(argv[1]); + data = calloc(n, sizeof(int)); + for (i=0; i<n; i++) { + gets(buf); + data[i] = atoi(buf); + } + + + printf("unsorted data:\n"); sleep(1); + printData(data, n); + + + selectionSortAscending(data, n); + printf("After selection sort\n"); sleep(1); + printData(data, n); + + insertionSortDescending(data, n); + printf("After insertion sort\n"); sleep(1); + printData(data, n); +} diff --git a/CS2501/sorting/sortAssignment2.c b/CS2501/sorting/sortAssignment2.c new file mode 100644 index 0000000..508690d --- /dev/null +++ b/CS2501/sorting/sortAssignment2.c @@ -0,0 +1,94 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + + +printData(d,n) +int *d,n; +{ + int i; + for (i=0; i<n; i++) + printf("data[%d] = %d\n", i, d[i]); +} + + +/* implement (only) one of your choosing */ +quickSort(d, n) +int *d, n; +{ +} + +mergeSort(d, n) +int *d, n; +{ + if (n == 1) + return; + + + int i, n1, n2, *tmp1, *tmp2; + + n1 = n/2; + n2 = (n%2 == 0) ? n1 : (n1 + 1); + + /* + printf("recursion!%d %d %d %x\n", n1, n2, n, d ); + */ + mergeSort(d, n1); + mergeSort(&d[n1], n2); + + tmp1 = malloc(sizeof(int) * n1); + tmp2 = malloc(sizeof(int) * n2); + + memcpy(tmp1, d, sizeof(int) * n1); + memcpy(tmp2, &d[n1], sizeof(int) * n2); + + + for (i = n-1; i >= 0; i--) { + if(n2 == 0) + d[i] = tmp1[--n1]; + + else if (n1 == 0) + d[i] = tmp2[--n2]; + else + d[i] = (tmp1[n1-1] < tmp2[n2-1]) ? tmp1[--n1] : tmp2[--n2]; + } + printf("\n"); + free(tmp1); + free(tmp2); + return; +} + +heapSort(d, n) +int *d, n; +{ +} + + + + +main(argc, argv, envp) +int argc; +char **argv, **envp; +{ + char buf[1024]; + int n, *data, i; + if (argc !=2 ) { + printf("usage: sortAssignment1 #elements\n"); + exit(1); + } + + n = atoi(argv[1]); + data = calloc(n, sizeof(int)); + for (i=0; i<n; i++) { + gets(buf); + data[i] = atoi(buf); + } + + + printf("unsorted data:\n"); sleep(1); + printData(data, n); + + mergeSort(data, n); + printf("After sort\n"); + printData(data, n); +} diff --git a/CS2501/trees/tree0.c b/CS2501/trees/tree0.c new file mode 100755 index 0000000..f9d6585 --- /dev/null +++ b/CS2501/trees/tree0.c @@ -0,0 +1,54 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + + treenode *a, *b, *c, *d, *z; + + z = malloc(sizeof(treenode)); + z->id = 'z'; + z->left = 0; + z->right= 0; + + d = malloc(sizeof(treenode)); + d->id = 'd'; + d->left = 0; + d->right= 0; + + c = malloc(sizeof(treenode)); + c->id = 'c'; + c->left = d; + c->right= z; + + b = malloc(sizeof(treenode)); + b->id = 'b'; + b->left = 0; + b->right= 0; + + a = malloc(sizeof(treenode)); + a->id = 'a'; + a->left = b; + a->right= c; + +/* manually create nodes to build this tree: + + a + / \ + b c + / \ + d z + +*/ +} diff --git a/CS2501/trees/tree1.c b/CS2501/trees/tree1.c new file mode 100755 index 0000000..0615377 --- /dev/null +++ b/CS2501/trees/tree1.c @@ -0,0 +1,106 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +/* FILL ME in */ +inorder(t) +treenode *t; +{ +if(!t) return 1; +inorder(t->left); +printf("%c\n", t->id); +inorder(t->right); +} + + + +/* FILL ME in */ +postorder(t) +treenode *t; +{ +if(!t) return 1; + postorder(t->left); + postorder(t->right); +printf("%c\n",t->id); +} + + + +preorder(t) +treenode *t; +{ +if(!t) return 1; +printf("%c",t->id); +printf("--%x %x\n",t->left,t->right); +preorder(t->left); +preorder(t->right); +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + +/* print out the tree from the first assignment: + + a + / \ + b c + / \ + d z + + print the nodes out three times: + preorder + inorder + postorder + +you will have to create the inorder() and postorder() +print functions to do this +*/ +treenode *a, *b, *c, *d, *z; + +z = malloc(sizeof(treenode)); +z->id = 'z'; +z->left = 0; +z->right= 0; + +d = malloc(sizeof(treenode)); +d->id = 'd'; +d->left = 0; +d->right= 0; + +c = malloc(sizeof(treenode)); +c->id = 'c'; +c->left = d; +c->right= z; + +b = malloc(sizeof(treenode)); +b->id = 'b'; +b->left = 0; +b->right= 0; + +a = malloc(sizeof(treenode)); +a->id = 'a'; +a->left = b; +a->right= c; + +printf("INORDER\n"); +inorder(a); +printf("PREORDER\n"); +preorder(a); +printf("POSTORDER\n"); +postorder(a); + +return 1; +} diff --git a/CS2501/trees/tree2.c b/CS2501/trees/tree2.c new file mode 100755 index 0000000..25bf50b --- /dev/null +++ b/CS2501/trees/tree2.c @@ -0,0 +1,50 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +int countNodes(t) +treenode *t; +{ + int i = 0; + if(!t) return 0; + return 1 + countNodes(t->left) + countNodes(t->right); + +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + + treenode *t; + t = malloc(sizeof(treenode)); + /* build a binary tree with at least 6 nodes */ + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + t->left->left->left = 0; + + printf("the tree has %d nodes\n", countNodes(t)); +} diff --git a/CS2501/trees/tree3.c b/CS2501/trees/tree3.c new file mode 100755 index 0000000..89e68c7 --- /dev/null +++ b/CS2501/trees/tree3.c @@ -0,0 +1,60 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + + +int countLevels(t) +treenode *t; +{ + if(!t) return 0; + return 1 + ((countLevels(t->left) > countLevels(t->right)) ? countLevels(t->left) : countLevels(t->right)); +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + treenode *t; + t = malloc(sizeof(treenode)); + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + t->left->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + + + t->left->left->left->left = 0; + t->left->left->left->right = 0; + + t->id = 1; + t->right->id = 3; + t->left->id = 2; + + t->right->right->id = 7; + t->right->left->id = 6; + t->left->right->id = 5; + t->left->left->id = 4; + + printf("the tree has %d levels\n", countLevels(t)); +} diff --git a/CS2501/trees/tree4.c b/CS2501/trees/tree4.c new file mode 100755 index 0000000..77b64d9 --- /dev/null +++ b/CS2501/trees/tree4.c @@ -0,0 +1,87 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + + +/* WRITE A RECURSIVE routine which swaps the + left and right child for every node in a given tree */ +/* will this operate in pre, in, or post order fashion? +does it matter? if so, why does it matter? */ + +rotate(t) +treenode *t; +{ + treenode *tmp; + if(!t) return; + rotate(t->left); + rotate(t->right); + tmp = t->left; + t->left = t->right; + t->right = tmp; +} + + +preorder(t) +treenode *t; +{ + if (!t) return; + + printf("%d\n", t->id); + preorder(t->left); + preorder(t->right); +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + treenode *t; + t = malloc(sizeof(treenode)); + /* build a binary tree with at least 6 nodes */ + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + t->left->left->left = 0; + + /* build a binary tree with at least 6 nodes */ + t->id = 1; + t->right->id = 3; + t->left->id = 2; + + t->right->right->id = 7; + t->right->left->id = 6; + t->left->right->id = 5; + t->left->left->id = 4; + + + /* build a binary tree with at least 6 nodes */ + + /* now show your rotate function works */ + preorder(t); + rotate(t); + printf("\n"); + preorder(t); + +} diff --git a/CS2501/trees/tree5.c b/CS2501/trees/tree5.c new file mode 100755 index 0000000..f2482f5 --- /dev/null +++ b/CS2501/trees/tree5.c @@ -0,0 +1,65 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +/* FILL ME in */ +breadthFirst(t) +treenode *t; +{ + treenode **q; + int front, back; + front = 0; + back = 0; + q = malloc(100 * sizeof(treenode*)); + *q = t; + while(q[back]){ + printf("%d\n", q[back]->id); + q[++front] = q[back]->left; + q[++front] = q[back++]->right; + } +} + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + + treenode *t; + t = malloc(sizeof(treenode)); + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + t->left->left->left = 0; + + t->id = 1; + t->right->id = 3; + t->left->id = 2; + + t->right->right->id = 7; + t->right->left->id = 6; + t->left->right->id = 5; + t->left->left->id = 4; + /* build a tree any way you like with at least 5 nodes */ + /* print it out breadth first, use a queue (so write one first) */ + breadthFirst(t); +} diff --git a/CS2501/trees/tree6.c b/CS2501/trees/tree6.c new file mode 100755 index 0000000..cbe95c2 --- /dev/null +++ b/CS2501/trees/tree6.c @@ -0,0 +1,111 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + char *name; + struct node *left; + struct node *right; +} treenode; + +typedef struct queue { + int front,back; + treenode **data; +} queue; + + + + +inorder(t) +treenode *t; +{ + if (!t) return; + inorder(t->left); + printf("%s\n",t->name); + inorder(t->right); +} + + + +addNode(n,v,q) +treenode **n; +char *v; +queue *q; +{ + treenode *nxt; + nxt = malloc(sizeof(treenode)); + memset(nxt, 0, sizeof(nxt)); + + nxt->name = v; /* safe - caller creates new storage for each */ + enqueue(nxt,q); + +} + +enqueue(t,q) +treenode *t; +queue *q; +{ + q->data[(q->front)++] = t; +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + +/* + +prompt the user for a series of names and add them +in the order given to create a complete binary tree + + +the user enters + giselle + magda + aviva + ahnk + havi + minna + monique + ^d + + +you build: + + + giselle + / \ + magda aviva + / \ / \ + ahnk havi minna monique + + +then print it out inorder +this should work for any number of name entered. +(consider using a queue) +*/ + + treenode *t = 0; + char buf[1024]; + queue q; + q.back = 0; + q.front = 0; + q.data = calloc(100,sizeof(treenode*)); + + while (gets(buf)) + addNode(&t, strdup(buf),&q); + + printf("q.back %d q.front %d", q.back, q.front); + while(q.back < q.front){ + printf("bulid tree pass %d %x\n", q.back,q.data[q.back]); + q.data[q.back]->left = q.data[((2 * q.back) +1)]; + q.data[q.back]->right = q.data[((2 * q.back) + 2)]; + q.back++; + } + t = q.data[0]; + inorder(t); +} diff --git a/CS2771/Assmbly1.s b/CS2771/Assmbly1.s new file mode 100644 index 0000000..4719280 --- /dev/null +++ b/CS2771/Assmbly1.s @@ -0,0 +1,84 @@ +;Tucker Evans +;CS3871 +;A collection of Assembly programs written from c code + + +;1 +; char i,j; +; +; i=3; +; j = i+i; + +start: + MOV A, #0 + MOV R0, #3 + MOV A, R0 + ADD A, R0 + MOV R1, A + JMP start + + +;2 +; char a,b; +; +; a=64; +; while (a > 0) { +; b = a; +; a--; +; } + +start: + MOV A, #64 +test: + JNZ loop + JMP start + + +loop: + MOV R0, A + DEC A + JMP test + +;3 +; char i,j,k; +; i=3; +; j=5; +; if (i == j) +; k=8; +; else +; k=9; + +start: + + MOV R0, #3 + MOV R1, #5 + + MOV A, R0 + MOV #40h, A + CJNE R1, #40h, false + MOV R3, #8 + JMP start + +false: + MOV R3, #9 + JMP start + +;4 +; char buf[64]; +; char i; +; +; for (i=0; i<64; i++) +; buf[i]=i; + +start: + MOV R0, #0 + MOV R1, #20h + MOV A, R0 +l1: + CJNE A, #64, l2 + JMP start +l2: + MOV @R1, A + INC A + INC R1 + JMP l1 diff --git a/CS3871/os/boot.s b/CS3871/os/boot.s new file mode 100644 index 0000000..6dc9464 --- /dev/null +++ b/CS3871/os/boot.s @@ -0,0 +1,37 @@ + bits 16 + org 7c00h + mov si, loading + call print + + mov ax, 7d00h + mov es, ax + mov bx, $0 + + mov dl, 0 + mov dh, 0 + mov ch, 0 + mov cl, 2 + mov al, 1 + mov ah, 2h + + int 13h + mov si, loaded + call print + + jmp 7d00h + + + +loading: db 'Loading OS into RAM...', 10, 13, 0 +loaded: db 'Jumping to OS...', 10, 13, 10, 13, 0 + +print: + mov ah, 0eh +.loop: + lodsb + cmp al, 0 + je .done + int 10h + jmp .loop +.done: + ret diff --git a/CS3871/os/copy.c b/CS3871/os/copy.c new file mode 100644 index 0000000..6e21083 --- /dev/null +++ b/CS3871/os/copy.c @@ -0,0 +1,95 @@ +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> + +int main(argc, argv) +int argc; +char ** argv; +{ + int val; + char buf[512], *dev; + int file, floppy; + + if (argc < 3 ) { + perror("usage: copy path_to_bootloader path_to_os [path_to_device]\n"); + exit(1); + } + if (argc >= 4) { + dev = argv[3]; + } else { + dev = "/dev/fd0"; + } + + memset(buf, 0, 512); + + floppy = open(dev, O_RDWR); + if (file < 0) { + perror("Could not open floppy: "); + exit(1); + } + + /****************************** + * Write Bootloader to Floppy * + ******************************/ + + file = open(argv[1], O_RDONLY); + if (file < 0) { + perror("Could not open bootloader: "); + exit(1); + } + + val = read(file, buf, 510); + close(file); + if (val < 0) { + perror("Could not read bootloader: "); + exit(1); + } + + buf[510] = 0x55; + buf[511] = 0xaa; + + lseek(floppy, 0, SEEK_CUR); + + val = write(floppy, buf, 512); + if (val < 0) { + perror("Error writing to floppy: "); + close(floppy); + exit(1); + } + + + /********************** + * Write OS to Floppy * + **********************/ + + file = open(argv[2], O_RDONLY); + if (file < 0) { + perror("Could not open os: "); + exit(1); + } + + val = read(file, buf, 512); + close(file); + if (val < 0) { + perror("Could not read os: "); + exit(1); + } + + val = write(floppy, buf, 512); + if (val < 0) { + perror("Error writing to floppy: "); + close(floppy); + exit(1); + } + + syncfs(floppy); + close(floppy); + + printf("Floppy successfully copied!\n"); + return 0; +} diff --git a/CS3871/os/os.s b/CS3871/os/os.s new file mode 100644 index 0000000..c77c583 --- /dev/null +++ b/CS3871/os/os.s @@ -0,0 +1,159 @@ + mov ax, base + add ax, 120h + mov ss, ax + mov sp, 1000h + + mov ax, data ; Set data segment to safe? area of memory + mov ds, ax + + call clear + + call load_strings + mov si, 0h ;promt + call print + mov di, 30h ;Q + + +start: + mov ah, 0 + int 16h + cmp al, 8 + je back + cmp al, 13 + je deal_nl + call print_char + call save_char + jmp start +deal_nl: + call print_string + mov di, 30h + mov si, 10h ;newline + call print + mov si, 0h ;prompt + call print + jmp start +back: + dec di + call print_char + jmp start + +data: db 00h, 20h +base: db 6h, 66h + +print: + mov ah, 0eh +.loop: + lodsb + cmp al, 0 + je .done + int 10h + jmp .loop +.done: + ret + +print_char: + mov ah, 0eh + int 10h + ret + +save_char: + mov [di], al + inc di + mov al, 0 + mov [di], al + ret + +print_string: + mov si, 10h ;newline + call print + mov si, 20h ;Prog: + call print + mov si, 30h ;Q +ps_loop: + mov al, [si] + cmp al, 0 + je ps_done + cmp al, 32 + je space + call print_char + jmp ps_next +space: + mov di, si + mov si, 10h ;newline + call print + mov si, 27h ;Args: + call print + mov si, di + jmp ps_next +ps_next: + inc si + jmp ps_loop +ps_done: + ret + +clear: + mov ah, 0h + int 10h + ret + +load_strings: + mov di, 0h + mov byte [di], 'F' + inc di + mov byte [di], 'l' + inc di + mov byte [di], 'o' + inc di + mov byte [di], 'p' + inc di + mov byte [di], 'p' + inc di + mov byte [di], 'y' + inc di + mov byte [di], 'O' + inc di + mov byte [di], 'S' + inc di + mov byte [di], '#' + inc di + mov byte [di], 0 + +;NEWLINE + mov di, 10h + mov byte [di], 10 + inc di + mov byte [di], 13 + inc di + mov byte [di], 0 +;PROG: + mov di, 20h + mov byte [di], 'P' + inc di + mov byte [di], 'r' + inc di + mov byte [di], 'o' + inc di + mov byte [di], 'g' + inc di + mov byte [di], ':' + inc di + mov byte [di], ' ' + inc di + mov byte [di], 0 + inc di + +;ARGS + mov byte [di], 'A' + inc di + mov byte [di], 'r' + inc di + mov byte [di], 'g' + inc di + mov byte [di], 's' + inc di + mov byte [di], ':' + inc di + mov byte [di], ' ' + inc di + mov byte [di], 0 +ret |