daslaller

Untitled

May 31st, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package derivata;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.util.ArrayList;
  11. import java.util.Iterator;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16.  *
  17.  * @author Lars
  18.  */
  19. public class Derivata {
  20.  
  21.     // static ArrayList<Integer> pos = new ArrayList<>();
  22.     static ArrayList<String> exp = new ArrayList<>();
  23.     static ArrayList<Integer> ix = new ArrayList<>();
  24.     static ArrayList<Integer> posx = new ArrayList<>();
  25.     static ArrayList<Integer> posi = new ArrayList<>();
  26.     static String nameOfFunction = "F" + "'" + "() = ";
  27.  
  28.     /**
  29.      * @param args the command line arguments
  30.      */
  31.     public static void main(String[] args) {
  32.         try {
  33.             String ekvation;
  34.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  35.             System.out.println("skriv in giltig ekvation, för upphöjt använd ^, logaritmiska lösningar eventuella naturliga logaritmer går ej att använda eg. E^Kx or lg^X or x^Kx \n"
  36.                     + "Ex. x^(51)*8x^(5)");
  37.             ekvation = br.readLine();
  38.             int size = ekvation.length() + 1;
  39.             int relativ = 0;
  40.             for (int i = 0; i < ekvation.length(); i++) {
  41.                 String pos = "";
  42.                 if (ekvation.charAt(i) == '(') {
  43.                     int x = i + 1;
  44.                     posx.add(x - 1);
  45.                     while (ekvation.charAt(x) != ')') {
  46.                         System.out.println("First cordinate of exp: " + x);
  47.                         pos += ekvation.charAt(x);
  48.                         posi.add(x);
  49.                         x++;
  50.                     }
  51.                     exp.add(pos);
  52.                     for (String s : exp) {
  53.                         int ref = Integer.parseInt(s);
  54.                         ref += - 1;
  55.                         ix.add(ref);
  56.                     }
  57.                 }
  58.             }
  59.             for (int i = 0; i < posi.size(); i++) {
  60.                 System.out.println(posi.get(i));
  61.                 ekvation = removeCharAt(ekvation, posi.get(i) - i);
  62.             }
  63.             for (int x = 0; x < posx.size(); x++) {
  64.                 System.out.println(posx.size() + ' ' + ix.size());
  65.                 ekvation = new StringBuffer(ekvation).insert(posx.get(x) + 1, ix.get(x + 1).toString()).toString();
  66.             }
  67.             for (int x = 0; x < posx.size(); x++) {
  68.                 relativ = ekvation.length() - size;
  69.                 ekvation = new StringBuffer(ekvation).insert(posx.get(x) + relativ, "*" + exp.get(x).toString()).toString();
  70.                 System.out.println("l: " + ekvation.length() + " planned offset: " + relativ);
  71.             }
  72.             System.out.println(nameOfFunction + ekvation);
  73.         } catch (IOException ex) {
  74.             Logger.getLogger(Derivata.class.getName()).log(Level.SEVERE, null, ex);
  75.         }
  76.     }
  77.  
  78.     public static String removeCharAt(String s, int pos) {
  79.         return s.substring(0, pos) + s.substring(pos + 1);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment