Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package derivata;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Lars
- */
- public class Derivata {
- // static ArrayList<Integer> pos = new ArrayList<>();
- static ArrayList<String> exp = new ArrayList<>();
- static ArrayList<Integer> ix = new ArrayList<>();
- static ArrayList<Integer> posx = new ArrayList<>();
- static ArrayList<Integer> posi = new ArrayList<>();
- static String nameOfFunction = "F" + "'" + "() = ";
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try {
- String ekvation;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- 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"
- + "Ex. x^(51)*8x^(5)");
- ekvation = br.readLine();
- int size = ekvation.length() + 1;
- int relativ = 0;
- for (int i = 0; i < ekvation.length(); i++) {
- String pos = "";
- if (ekvation.charAt(i) == '(') {
- int x = i + 1;
- posx.add(x - 1);
- while (ekvation.charAt(x) != ')') {
- System.out.println("First cordinate of exp: " + x);
- pos += ekvation.charAt(x);
- posi.add(x);
- x++;
- }
- exp.add(pos);
- for (String s : exp) {
- int ref = Integer.parseInt(s);
- ref += - 1;
- ix.add(ref);
- }
- }
- }
- for (int i = 0; i < posi.size(); i++) {
- System.out.println(posi.get(i));
- ekvation = removeCharAt(ekvation, posi.get(i) - i);
- }
- for (int x = 0; x < posx.size(); x++) {
- System.out.println(posx.size() + ' ' + ix.size());
- ekvation = new StringBuffer(ekvation).insert(posx.get(x) + 1, ix.get(x + 1).toString()).toString();
- }
- for (int x = 0; x < posx.size(); x++) {
- relativ = ekvation.length() - size;
- ekvation = new StringBuffer(ekvation).insert(posx.get(x) + relativ, "*" + exp.get(x).toString()).toString();
- System.out.println("l: " + ekvation.length() + " planned offset: " + relativ);
- }
- System.out.println(nameOfFunction + ekvation);
- } catch (IOException ex) {
- Logger.getLogger(Derivata.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static String removeCharAt(String s, int pos) {
- return s.substring(0, pos) + s.substring(pos + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment