Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.math.BigInteger;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- class Fraction {
- BigInteger licznik, mianownik;
- static boolean bladGlobalny;
- Fraction(){
- this.licznik.valueOf(0);
- this.mianownik.valueOf(1);
- }
- Fraction(BigInteger l, BigInteger m){
- if(m.equals(0)) {m.valueOf(1); bladGlobalny=true;}
- BigInteger d = nwd(l,m);
- this.licznik = l.divide(d);
- this.mianownik = m.divide(d);
- }
- public static Fraction suma(Fraction f1, Fraction f2){
- BigInteger gora = f1.licznik.multiply(f2.mianownik).add(f1.mianownik.multiply(f2.licznik));
- BigInteger dol = f1.mianownik.multiply(f2.mianownik);
- BigInteger d = nwd(gora,dol);
- return (new Fraction(gora.divide(d),dol.divide(d)));
- }
- public static Fraction iloczyn(Fraction f1, Fraction f2){
- BigInteger gora = f1.licznik.multiply(f2.licznik);
- BigInteger dol = f1.mianownik.multiply(f2.mianownik);
- BigInteger d = nwd(gora,dol);
- return (new Fraction(gora.divide(d),dol.divide(d)));
- }
- public static Fraction iloraz(Fraction f1, Fraction f2){
- BigInteger gora = f1.licznik.multiply(f2.mianownik);
- BigInteger dol = f1.mianownik.multiply(f2.licznik);
- if(dol.equals(0)) {dol.valueOf(1); bladGlobalny=true;}
- BigInteger d = nwd(gora,dol);
- return (new Fraction(gora.divide(d),dol.divide(d)));
- }
- public static BigInteger nwd(BigInteger x, BigInteger y){
- BigInteger a = new BigInteger(x.toString());
- BigInteger b = new BigInteger(y.toString());;
- while (b.compareTo(new BigInteger("0")) != 0) {
- BigInteger c = a.mod(b);
- a = new BigInteger(b.toByteArray());
- b = new BigInteger(c.toByteArray());
- }
- return a;
- }
- public static Fraction convert(String wszystko) {
- String licznik = wszystko;
- int it1 = 0, it2 = 0;
- while(wszystko.charAt(it1++) != '/')
- it2++;
- licznik = licznik.substring(0, --it1);
- wszystko = wszystko.substring(++it2, licznik.length() + 2);
- return new Fraction(new BigInteger(licznik), new BigInteger(wszystko));
- }
- public static Fraction oblicz(char op, Fraction f1, Fraction f2) {
- switch (op){
- case '+':
- return suma(f1, f2);
- case '*':
- return iloczyn(f1, f2);
- case '/':
- return iloraz(f2, f1);
- }
- return f1;
- }
- }
- public class Main {
- static Map<String, Fraction> M;
- static BufferedReader br;
- static BufferedWriter bw;
- public static void main(String[] args) throws IOException {
- br = new BufferedReader(new InputStreamReader(System.in));
- bw = new BufferedWriter(new OutputStreamWriter(System.out));
- M = new HashMap<String, Fraction>();
- boolean end;
- while(true) {
- String testCase = br.readLine();
- bw.write(testCase);
- if (!odczytuj(M)) {
- bw.write(" N\n");
- } else {
- bw.write(" Y\n");
- SortedSet<String> keys = new TreeSet<String>(M.keySet());
- for (String key : keys)
- bw.write(key + " " + M.get(key).licznik + " " + M.get(key).mianownik + '\n');
- }
- M.clear();
- bw.flush();
- }
- }
- public static boolean odczytuj(Map <String, Fraction> M) throws IOException {
- String linia;
- StringBuilder zmienna = new StringBuilder();
- int it = 0;
- linia = br.readLine();
- if (linia == null) return false;
- while (linia.charAt(it) != '=' && it != linia.length()) {
- zmienna.append(linia.charAt(it));
- it++;
- }
- if(zmienna.equals("case") || zmienna.equals("cas")) return false;
- if(it == linia.length()) Fraction.bladGlobalny = true;
- if(Fraction.bladGlobalny) return false;
- linia = linia.substring(++it, linia.length());
- if(zmienna.toString() != "" && !M.containsKey(zmienna)) {
- M.put(zmienna.toString(), liczONP(M, linia));
- }
- else Fraction.bladGlobalny = true;
- return true;
- }
- public static Fraction liczONP(Map <String, Fraction> M, String onp) throws IOException {
- Stack<Fraction> S = new Stack<Fraction>();
- Fraction f1, f2;
- int o = 0;
- while(o != onp.length()) {
- if (o < onp.length()) {
- String var = "";
- if (onp.charAt(o) == '_') {
- o++;
- continue;
- }
- if (onp.charAt(o) >= 48 && onp.charAt(o) <= 57) {
- return Fraction.convert(onp);
- }
- if (onp.charAt(o) == '+' || onp.charAt(o) == '*' || onp.charAt(o) == '/') {
- if (S.size()>1) {
- f1 = S.pop();
- f2 = S.pop();
- S.push(Fraction.oblicz(onp.charAt(o), f1, f2));
- o++;
- continue;
- }
- else { Fraction.bladGlobalny = true; return new Fraction(new BigInteger("1"), new BigInteger("1")); }
- }
- if (onp.charAt(o) == 'a' || onp.charAt(o) == 'c' || onp.charAt(o) == 'g' || onp.charAt(o) == 't') {
- while (onp.charAt(o) == 'a' || onp.charAt(o) == 'c' || onp.charAt(o) == 'g' || onp.charAt(o) == 't')
- var += onp.charAt(o++);
- while (!M.containsKey(var))
- odczytuj(M);
- S.push(M.get(var));
- }
- else { Fraction.bladGlobalny = true; return new Fraction(new BigInteger("1"), new BigInteger("1")); }
- }
- }
- if (S.size() != 1) Fraction.bladGlobalny = true;
- return S.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement