PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : java/ C++ was man damit machen kann



lucrative
27-01-2003, 20:25
ist vielleicht eine etwas dumme frage aber ich habe gerade angefangen java zu lernen.

soweit so gut.

aber letztens hab ich mit nem freunf gesprochen der schon ein paar c++ bücher durchgelesen hat.
der meinte er könne jetzt c++ so einigermaßen aber er wüsste nicht was er für programme schreiben könnte.

nu hab ich die frage:
hier sind ja ein paar programierer...
könntet ihr mir mal ein paar selbstgeschreibene programme von euch zeigen?
damit ich weis was man alles so anstellen kann....

am besten in java!

naja nur so e bitte, oder kennt ihr seiten zu dem thema?

grüße

wisi
27-01-2003, 20:46
was, du willst, das ich einen java-code poste?!!!?
die meisten codes haben bei mir an die 400 zeilen. danach macht mir das proggen keinen spaß mehr und ich beginne ein neues projekt.
aber gut, wenn du es unbedingt willst.
hier ist mein neuestes projekt: java brainfuck interpreter - der interpretiert in brainfuck geschriebene codes - funktioniert allerdings noch nicht:

///////////////////////////////////////////////////////////

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

///////////////////////////////////////////////////////////

public class JBFI extends JFrame
{
///////////////////////////////////////////////////////////

JMenuBar bar = new JMenuBar();
JMenu men1 = new JMenu("Datei");
JMenu men2 = new JMenu("Bearbeiten");
JMenu men3 = new JMenu("Ausführung");
JMenu men4 = new JMenu("Tools");
JMenu men5 = new JMenu("?");
JMenuItem itm1 = new JMenuItem("Neu");
JMenuItem itm2 = new JMenuItem("Öffnen...");
JMenuItem itm3 = new JMenuItem("Speichern");
JMenuItem itm4 = new JMenuItem("Speichern unter...");
JMenuItem itm5 = new JMenuItem("Drucken...");
JMenuItem itm6 = new JMenuItem("Beenden");
JMenuItem itm7 = new JMenuItem("Kopieren");
JMenuItem itm8 = new JMenuItem("Einfügen");
JMenuItem itm9 = new JMenuItem("Löschen");
JMenuItem itm10 = new JMenuItem("Alles markieren");
JMenuItem itm11 = new JMenuItem("Starten");
JMenuItem itm12 = new JMenuItem("Abbrechen");
JMenuItem itm13 = new JMenuItem("ASCII-Tabelle");
JMenuItem itm14 = new JMenuItem("ASCII2BF");
JMenuItem itm16 = new JMenuItem("CodeMader");
JMenuItem itm17 = new JMenuItem("CodeBeautyfulizer");
JMenuItem itm15 = new JMenuItem("Über JBFI...");

JEditorPane edi1 = new JEditorPane();
JScrollPane scr1 = new JScrollPane(edi1);
JOptionPane pan1 = new JOptionPane();
JFileChooser cho1 = new JFileChooser();

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
File openedFile = new File("");

///////////////////////////////////////////////////////////

public JBFI()
{
System.out.println("|------------------------------------------|");
System.out.println("| |");
System.out.println("| Java Brainfuck Interpreter 1.0 |");
System.out.println("| (c) by AWSoft 2003 |");
System.out.println("| |");
System.out.println("|------------------------------------------|\n");
System.out.println("Lade...");

this.setTitle("Java Brainfuck Interpreter - (c) by AWSoft 2003");
this.setSize(800, 600);
this.setLocation((int)(d.getWidth()/2-this.getWidth()/2), (int)(d.getHeight()/2-this.getHeight()/2));
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

men1.add(itm1);
men1.add(itm2);
men1.add(itm3);
men1.add(itm4);
men1.addSeparator();
men1.add(itm5);
men1.addSeparator();
men1.add(itm6);
men2.add(itm7);
men2.add(itm8);
men2.add(itm9);
men2.add(itm10);
men3.add(itm11);
men3.add(itm12);
men4.add(itm13);
men4.add(itm14);
men4.add(itm16);
men4.add(itm17);
men5.add(itm15);
bar.add(men1);
bar.add(men2);
bar.add(men3);
bar.add(men4);
bar.add(men5);

scr1.setHorizontalScrollBarPolicy(scr1.HORIZONTAL_ SCROLLBAR_NEVER);
scr1.setVerticalScrollBarPolicy(scr1.VERTICAL_SCRO LLBAR_ALWAYS);
edi1.setFont(new Font("Monospaced", 0, 12));

this.getContentPane().add(bar, BorderLayout.NORTH);
this.getContentPane().add(scr1, BorderLayout.CENTER);

itm1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm1_clicked(); } } );
itm2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm2_clicked(); } } );
itm3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm3_clicked(); } } );
itm4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm4_clicked(false); } } );
itm5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm5_clicked(); } } );
itm6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } } );
itm9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm9_clicked(); } } );
itm10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm10_clicked(); } } );
itm11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm11_clicked(); } } );
itm12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm12_clicked(); } } );
itm16.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm16_clicked(); } } );
itm17.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm17_clicked(); } } );
itm15.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { itm15_clicked(); } } );

System.out.println("Geladen...");
System.out.println("Speicherbereinigung...");
System.gc();
System.out.println("Speicherbereinigung abgeschlossen...");
System.out.println("Freier Speicher: " + Runtime.getRuntime().freeMemory() + " Bytes");
System.out.println("Java-Version: " + System.getProperty("java.version"));
}

public static void main(String[] args)
{
JBFI frame = new JBFI();
frame.setVisible(true);
}

public void itm1_clicked()
{
edi1.setText("");
openedFile = new File("");
}

public void itm2_clicked()
{
int ret = cho1.showOpenDialog(this);
if(ret==cho1.CANCEL_OPTION) { }
else
{
File f = cho1.getSelectedFile();
if(!f.exists()) { return; }
openedFile = f;
String temp = "";
try
{
DataInputStream dis1 = new DataInputStream(new FileInputStream(f));
while(true)
{
temp = temp + (char)(dis1.readByte());
}
}
catch(Exception e) { }
edi1.setText(temp);
edi1.setSelectionStart(0);
edi1.setSelectionEnd(0);
}
}

public void itm3_clicked()
{
itm4_clicked(true); return;
}

public void itm4_clicked(boolean save)
{
File f;
if(save==false || openedFile.toString().equals(""))
{
int ret = cho1.showSaveDialog(this);
if(ret==cho1.CANCEL_OPTION) { return; }
f = cho1.getSelectedFile();
openedFile = f;
}
else
{
f = openedFile;
}
try
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
dos.writeBytes(edi1.getText());
dos.flush();
dos.close();
}
catch(Exception e) { }
}

public void itm5_clicked()
{
PrintJob pjob = Toolkit.getDefaultToolkit().getPrintJob(this, "JBFI PrinterJob", null);
if(pjob==null) { return; }
Graphics g = pjob.getGraphics();
if(g==null) { return; }
g.setFont(new Font("Monospaced", 0, 12));
g.drawString(edi1.getText(), 100, 50);
g.dispose();
pjob.end();
}

public void itm9_clicked()
{
if(edi1.getSelectionStart()==edi1.getSelectionEnd( )) { return; }
edi1.setText(edi1.getText().substring(0, edi1.getSelectionStart()) +
edi1.getText().substring(edi1.getSelectionEnd(), edi1.getText().length()-edi1.getSelectionEnd()));
}

public void itm10_clicked()
{
edi1.setSelectionStart(0);
edi1.setSelectionEnd(edi1.getText().length());
}

public void itm11_clicked()
{
BFInterpreter bfi = new BFInterpreter();
bfi.setEinString(edi1.getText());
this.setExtendedState(this.ICONIFIED);
bfi.start();
}

public void itm12_clicked()
{
BFScreen bfs = new BFScreen();
bfs.setVisible(true);
}

public void itm16_clicked()
{
int ret = pan1.showConfirmDialog(this, "Alle Kommentare werden gelöscht!\nFortsetzen?", "Achtung!", pan1.YES_NO_OPTION);
if(ret==pan1.NO_OPTION) { return; }
BFInterpreter nix = new BFInterpreter();
edi1.setText(nix.codeMader(edi1.getText()));
edi1.setSelectionStart(0);
edi1.setSelectionEnd(0);
}

public void itm17_clicked()
{
int ret = pan1.showConfirmDialog(this, "Alle Kommentare werden gelöscht!\nFortsetzen?", "Achtung!", pan1.YES_NO_OPTION);
if(ret==pan1.NO_OPTION) { return; }
BFInterpreter nix = new BFInterpreter();
edi1.setText(nix.codeBeautyfulizer(edi1.getText()) );
edi1.setSelectionStart(0);
edi1.setSelectionEnd(0);
}

public void itm15_clicked()
{
InfoDLG dlg1 = new InfoDLG();
dlg1.setVisible(true);
}
}

///////////////////////////////////////////////////////////

class BFInterpreter extends Thread
{
long[] a = new long[60000];
int x = 30000;
boolean cancel = true;
boolean eingabe = false;
String inpStr = "";

public void setEinString(String EinString) { inpStr = EinString; }

public void run()
{
for(x=0; x<60000; x++) { a[x] = 0; }
x = 30000;
String temp = removeNotKnownSigns(inpStr);
cancel = false;
eingabe = false;
int ret = interpretString(temp);
}

public int interpretString(String EinString)
{
int i = 0;
for(i=0; i<EinString.length(); i++)
{
if(EinString.substring(i, i+1).equals("+")) a[x]++;
else if(EinString.substring(i, i+1).equals("-")) a[x]--;
else if(EinString.substring(i, i+1).equals(">")) x++;
else if(EinString.substring(i, i+1).equals("<")) x--;
else if(EinString.substring(i, i+1).equals(".")) SYNTAX_OUT();
else if(EinString.substring(i, i+1).equals(",")) SYNTAX_IN();
else if(EinString.substring(i, i+1).equals("]")) SYNTAX_ERROR();
else if(EinString.substring(i, i+1).equals("["))
{
int OK = 0; int i2 = 0;
for(i2=i; i2<EinString.length(); i2++)
{
if(EinString.substring(i2, i2+1).equals("[")) OK++;
else if(EinString.substring(i2, i2+1).equals("]")) OK--;
if(OK==0) break;
}
while(true)
{
if(a[x]==0) break;
int ret = interpretString(EinString.substring(i+1, i2));
}
i = i2;
}
if(cancel==true) break;
}

return 0;
}

public void SYNTAX_OUT() { if(a[x]<0 || a[x]>255) { return; } System.out.print((char)(a[x])); }
public void SYNTAX_IN()
{
try
{
java.io.DataInput quelle = new DataInputStream(System.in);
eingabe = true;
byte zeichen = 0;
while(eingabe)
{
zeichen = quelle.readByte();
eingabe = false;
}
a[x] = zeichen;
}
catch(Exception e)
{
SYNTAX_ERROR();
}
}
public void SYNTAX_ERROR() { System.out.println("\n\n+++ Fehler! +++"); cancel = true; }

public String removeNotKnownSigns(String EinString)
{
if(EinString.length()==0) { return EinString; }
String temp = "";
for(int i=0; i<EinString.length(); i++)
{
if(EinString.substring(i, i+1).equals("+")) temp+="+";
else if(EinString.substring(i, i+1).equals("-")) temp+="-";
else if(EinString.substring(i, i+1).equals("<")) temp+="<";
else if(EinString.substring(i, i+1).equals(">")) temp+=">";
else if(EinString.substring(i, i+1).equals("[")) temp+="[";
else if(EinString.substring(i, i+1).equals("]")) temp+="]";
else if(EinString.substring(i, i+1).equals(",")) temp+=",";
else if(EinString.substring(i, i+1).equals(".")) temp+=".";
}
return temp;
}

public String codeMader(String EinString)
{
String temp = removeNotKnownSigns(EinString);
String temp2 = "";
int tmp = 0;
for(int i=0; i<temp.length(); i++)
{
temp2+=temp.substring(i, i+1);
tmp++;
if(tmp==80) { temp2+="\r\n"; tmp = 0; }
}
return temp2;
}

public String codeBeautyfulizer(String EinString)
{
if(EinString.length()==0) { return EinString; }
int EINRUECKEN = 2;
String temp = removeNotKnownSigns(EinString);
String temp2 = "";
int OK = 0;
int GK = 0;
for(int i=0; i<temp.length(); i++)
{
if(temp.substring(i, i+1).equals("[")) OK++;
else if(temp.substring(i, i+1).equals("]")) GK++;
}
if(!(OK==GK)) return EinString;
int Einrueck = 0;
boolean FirstInLine = true;
int i2;
for(int i=0; i<temp.length(); i++)
{
if(temp.substring(i, i+1).equals("["))
{
temp2+="\n";
for(i2=0; i2<Einrueck; i2++) { temp2+=" "; }
temp2+="[\n";
Einrueck+=EINRUECKEN;
FirstInLine = true;
}
else if(temp.substring(i, i+1).equals("]"))
{
Einrueck-=EINRUECKEN;
temp2+="\n";
for(i2=0; i2<Einrueck; i2++) { temp2+=" "; }
temp2+="]\n";
FirstInLine = true;
}
else
{
if(FirstInLine==true)
{
for(i2=0; i2<Einrueck; i2++) { temp2+=" "; }
FirstInLine = false;
}
temp2+=temp.substring(i, i+1);
}
if(Einrueck<0) return EinString;
}
return temp2;
}
}

///////////////////////////////////////////////////////////

class BFScreen extends JFrame
{
JEditorPane edi1 = new JEditorPane();
JScrollPane scr1 = new JScrollPane(edi1);
JLabel lbl1 = new JLabel("Eingabe (false):");
JTextField fld1 = new JTextField();
JButton btn1 = new JButton("Abbrechen");
JButton btn2 = new JButton("Schließen");

public BFScreen()
{
this.setTitle("Java Brainfuck Interpreter - (c) by AWSoft 2003");
this.setSize(600, 535);
this.setResizable(false);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(d.getWidth()/2-this.getWidth()/2), (int)(d.getHeight()/2-this.getHeight()/2));
this.getContentPane().setLayout(null);

scr1.setBounds(5, 5, 585, 435);
scr1.setHorizontalScrollBarPolicy(scr1.HORIZONTAL_ SCROLLBAR_NEVER);
scr1.setVerticalScrollBarPolicy(scr1.VERTICAL_SCRO LLBAR_ALWAYS);
edi1.setFont(new Font("Monospaced", 0, 12));
edi1.setEditable(false);
lbl1.setBounds(5, 450, 90, 15);
fld1.setBounds(100, 445, 490, 25);
fld1.setFont(new Font("Monospaced", 0, 12));
btn1.setBounds(5, 475, 250, 30);
btn2.setBounds(340, 475, 250, 30);

this.getContentPane().add(scr1);
this.getContentPane().add(lbl1);
this.getContentPane().add(fld1);
this.getContentPane().add(btn1);
this.getContentPane().add(btn2);

}
}

viel spaß damit - hab allerdings etwas kürzen müssen.

lucrative
27-01-2003, 21:13
ich will nicht den code bei java meine ich
z.B.
ne tar.gz fin'le dich ich auspaken kann und dann ne ne class datei öffne kann mit "java name"
...

ReSeT
28-01-2003, 10:27
ber letztens hab ich mit nem freunf gesprochen der schon ein paar c++ bücher durchgelesen hat.
der meinte er könne jetzt c++ so einigermaßen aber er wüsste nicht was er für programme schreiben könnte.


mmh, wie hat er den C++ gelernt, ohne zu programmieren? Das ist so ähnlich, wie schwimmen zu wollen, wenn man alles über Wasser gelesen hat, aber noch nie drin war :D

Mal im Ernst : Es gibt so viele Möglichkeiten, Programme zu schreiben, gerade in Java.

Schreib nen Texteditor, einen Mail Client, einen FTP Client, ein Messageboard, TicTacToe, Tetris, lass der Phantasie freien Lauf. Und das Argument, daß habe alles schon einmal jemand programmiert zählt hier nicht.

Und ich werd hier bestimmt nicht hunderte von Zeilen Quelltext posten, die keinen weiterbringen, davon gibts ohne Ende im Netz.

Wichtig: Viel Quellcode lesen und selberschreiben.

lucrative
28-01-2003, 15:20
@ ReSeT

thx werd ich machen

Berufspenner
28-01-2003, 15:30
Hi@all

Wo wir schon mal beim Thema sind: Kennt wer gute C++ Seiten, auf denen man sich kommentierte Codeschnipsel angucken oder herunterladen kann? Ich bin zwar noch nicht so gut in C++ möchte aber mal ein kleines Projetk starten und da fehlt es mir einwenig an Inspiration.


Cu
André