Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.io.PrintWriter;
  8. import java.math.BigInteger;
  9. import java.util.*;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. class Fraction {
  14.     BigInteger licznik, mianownik;
  15.     static boolean bladGlobalny;
  16.    
  17.     Fraction(){
  18.         this.licznik.valueOf(0);
  19.         this.mianownik.valueOf(1);
  20.     }
  21.    
  22.     Fraction(BigInteger l, BigInteger m){
  23.         if(m.equals(0)) {m.valueOf(1); bladGlobalny=true;}
  24.         BigInteger d = nwd(l,m);
  25.         this.licznik = l.divide(d);
  26.         this.mianownik = m.divide(d);
  27.     }
  28.    
  29.     public static Fraction suma(Fraction f1, Fraction f2){
  30.         BigInteger gora = f1.licznik.multiply(f2.mianownik).add(f1.mianownik.multiply(f2.licznik));
  31.         BigInteger dol = f1.mianownik.multiply(f2.mianownik);
  32.         BigInteger d = nwd(gora,dol);
  33.         return (new Fraction(gora.divide(d),dol.divide(d)));
  34.     }
  35.    
  36.     public static Fraction iloczyn(Fraction f1, Fraction f2){
  37.         BigInteger gora = f1.licznik.multiply(f2.licznik);
  38.         BigInteger dol = f1.mianownik.multiply(f2.mianownik);
  39.         BigInteger d = nwd(gora,dol);
  40.         return (new Fraction(gora.divide(d),dol.divide(d)));
  41.     }
  42.    
  43.     public static Fraction iloraz(Fraction f1, Fraction f2){
  44.         BigInteger gora = f1.licznik.multiply(f2.mianownik);
  45.         BigInteger dol = f1.mianownik.multiply(f2.licznik);
  46.         if(dol.equals(0)) {dol.valueOf(1); bladGlobalny=true;}
  47.         BigInteger d = nwd(gora,dol);
  48.         return (new Fraction(gora.divide(d),dol.divide(d)));
  49.     }
  50.    
  51.     public static BigInteger nwd(BigInteger x, BigInteger y){
  52.         BigInteger a = new BigInteger(x.toString());
  53.         BigInteger b = new BigInteger(y.toString());;
  54.         while (b.compareTo(new BigInteger("0")) != 0)    {
  55.             BigInteger c  = a.mod(b);
  56.             a = new BigInteger(b.toByteArray());
  57.             b = new BigInteger(c.toByteArray());
  58.         }
  59.  
  60.         return a;
  61.     }
  62.    
  63.     public static Fraction convert(String wszystko) {
  64.         String licznik = wszystko;
  65.         int it1 = 0, it2 = 0;
  66.         while(wszystko.charAt(it1++) != '/')
  67.             it2++;
  68.        
  69.         licznik = licznik.substring(0, --it1);
  70.         wszystko = wszystko.substring(++it2, licznik.length() + 2);
  71.         return new Fraction(new BigInteger(licznik), new BigInteger(wszystko));
  72.     }
  73.    
  74.     public static Fraction oblicz(char op, Fraction f1, Fraction f2) {
  75.         switch (op){
  76.         case '+':
  77.             return suma(f1, f2);
  78.         case '*':
  79.             return iloczyn(f1, f2);
  80.         case '/':
  81.             return iloraz(f2, f1);
  82.         }
  83.         return f1;
  84.     }
  85.  
  86. }
  87.  
  88. public class Main {
  89.    
  90.     static Map<String, Fraction> M;
  91.     static BufferedReader br;
  92.     static BufferedWriter bw;
  93.    
  94.     public static void main(String[] args) throws IOException {
  95.         br = new BufferedReader(new InputStreamReader(System.in));
  96.         bw = new BufferedWriter(new OutputStreamWriter(System.out));
  97.         M = new HashMap<String, Fraction>();
  98.         boolean end;
  99.        
  100.         while(true) {
  101.             String testCase = br.readLine();
  102.             bw.write(testCase);
  103.             if (!odczytuj(M)) {
  104.                 bw.write(" N\n");
  105.             } else {
  106.                 bw.write(" Y\n");
  107.                 SortedSet<String> keys = new TreeSet<String>(M.keySet());
  108.                 for (String key : keys)
  109.                     bw.write(key + " " + M.get(key).licznik + " " + M.get(key).mianownik + '\n');
  110.                
  111.             }
  112.             M.clear();
  113.             bw.flush();
  114.         }
  115.     }
  116.    
  117.     public static boolean odczytuj(Map <String, Fraction> M) throws IOException {
  118.         String linia;
  119.         StringBuilder zmienna = new StringBuilder();
  120.         int it = 0;
  121.         linia = br.readLine();
  122.         if (linia == null) return false;
  123.         while (linia.charAt(it) != '=' && it != linia.length()) {
  124.             zmienna.append(linia.charAt(it));
  125.             it++;
  126.         }
  127.        
  128.         if(zmienna.equals("case") || zmienna.equals("cas")) return false;
  129.         if(it == linia.length()) Fraction.bladGlobalny = true;
  130.        
  131.         if(Fraction.bladGlobalny) return false;
  132.         linia = linia.substring(++it, linia.length());
  133.         if(zmienna.toString() != "" && !M.containsKey(zmienna)) {
  134.             M.put(zmienna.toString(), liczONP(M, linia));
  135.         }
  136.         else Fraction.bladGlobalny = true;
  137.        
  138.         return true;
  139.     }
  140.    
  141.     public static Fraction liczONP(Map <String, Fraction> M, String onp) throws IOException {
  142.         Stack<Fraction> S = new Stack<Fraction>();
  143.         Fraction f1, f2;
  144.         int o = 0;
  145.        
  146.         while(o != onp.length()) {
  147.             if (o < onp.length()) {
  148.                 String var = "";
  149.                 if (onp.charAt(o) == '_') {
  150.                     o++;
  151.                     continue;
  152.                 }
  153.    
  154.                 if (onp.charAt(o) >= 48 && onp.charAt(o) <= 57) {
  155.                     return Fraction.convert(onp);
  156.                 }
  157.    
  158.                 if (onp.charAt(o) == '+' || onp.charAt(o) == '*' || onp.charAt(o) == '/') {
  159.                     if (S.size()>1) {
  160.                         f1 = S.pop();
  161.                         f2 = S.pop();
  162.                         S.push(Fraction.oblicz(onp.charAt(o), f1, f2));
  163.                         o++;
  164.                         continue;
  165.                     }
  166.                     else { Fraction.bladGlobalny = true; return new Fraction(new BigInteger("1"), new BigInteger("1")); }
  167.                 }
  168.    
  169.                 if (onp.charAt(o) == 'a' || onp.charAt(o) == 'c' || onp.charAt(o) == 'g' || onp.charAt(o) == 't') {
  170.                     while (onp.charAt(o) == 'a' || onp.charAt(o) == 'c' || onp.charAt(o) == 'g' || onp.charAt(o) == 't')
  171.                         var += onp.charAt(o++);
  172.                     while (!M.containsKey(var))
  173.                         odczytuj(M);
  174.                     S.push(M.get(var));
  175.                 }
  176.                 else { Fraction.bladGlobalny = true; return new Fraction(new BigInteger("1"), new BigInteger("1")); }
  177.             }
  178.         }
  179.         if (S.size() != 1) Fraction.bladGlobalny = true;
  180.         return S.pop();
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement