Witam,
napisalem program ktory odczytuje wartosci z pliku i wykonuje dodawanie, odejmowanie itp
przykladowo jesli w pliku wejsciowym jest :
1 2 3
4 5 6
to w pliku wyjsciowym bedzie : 5 -3 9
chcialbym go teraz przerobic tak zebym nie musial pisac wszystkiego od nowa zeby (wiem ze kod nie jest piekny ale dopiero sie ucze;) dane byly w slupkach a nie w trzech wierszach czyli np:
1 2 +
2 5 -
3 6 +
w jaki sposob mozna to zrobic ?
ponizej kod programu :
import java.io.*;
public class program {
public static void main (String[] args) {
double[] tab_1 = new double[0];
double[] tab_3 = new double[0];
String[] tab_5 = new String[0];
BufferedReader brin = null;
String a1;
String b1;
String s1;
double wynik;
wynik=0;
File file = new File("wejscie");
File file2 = new File("wyjscie");
try {
brin = new BufferedReader(new FileReader(file));
String[] tab_2 = brin.readLine().split(" ");
tab_1 = new double[tab_2.length];
for (int i = 0; i < tab_2.length; i++) {
tab_1[i] = Double.parseDouble(tab_2[i]);
}
String[] tab_4 = brin.readLine().split(" ");
tab_3 = new double[tab_4.length];
for (int i = 0; i < tab_4.length; i++) {
tab_3[i] = Double.parseDouble(tab_4[i]);
}
String[] tab_6 = brin.readLine().split(" ");
tab_5 = new String[tab_6.length];
for (int i = 0; i < tab_6.length; i++) {
tab_5[i] = tab_6[i];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
brin.close();
} catch (IOException e) {
}
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileWriter fw = null;
try {
fw = new FileWriter(file2);
//// kalkulatror poczatek
for (int i = 0; i < tab_1.length; i++) {
if (tab_5[i].equals("+")) {
wynik=(tab_1[i]+tab_3[i]);
System.out.println("wynik a+b to: "+(wynik));
} else {
if (tab_5[i].equals("-")) {
wynik=(tab_1[i]-tab_3[i]);
System.out.println("wynik a-b to: "+(wynik));
} else {
if (tab_5[i].equals("mult")) {
wynik=(tab_1[i]*tab_3[i]);
System.out.println("wynik a*b to: "+(wynik));
}
else { if (tab_5[i].equals("dz")) {
wynik=(tab_1[i]/tab_3[i]);
System.out.println("wynik a/b to: "+(tab_1[i]/tab_3[i]));
} else { if (tab_5[i].equals("mod")) {
wynik=(tab_1[i]%tab_3[i]);
System.out.println("wynik a%b to: "+(tab_1[i]%tab_3[i]));
}}}}}
fw.write(wynik+" ");
}
/// kalkulator koniec
fw.write("\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}