Advertisement
Guest User

APS 3.NALOGA

a guest
Nov 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. //
  2. //
  3. //
  4. // ***ITS WORKING***
  5. //
  6. //
  7. //
  8. import java.io.*;
  9. import java.util.Scanner;
  10. class LinkedList{
  11. protected LinkedListElement first;
  12. protected LinkedListElement last;
  13. LinkedList()
  14. {
  15. makenull();
  16. }
  17. public void makenull()
  18. {
  19. first = new LinkedListElement(null, null);
  20. last = null;
  21. }
  22. public String write()
  23. {
  24. String izhod = "";
  25. LinkedListElement el;
  26. el = first.next;
  27. while(el != null) {
  28. izhod += el.element;
  29. if(el.next != null) { izhod += ","; }
  30. el = el.next;
  31. }
  32. return izhod;
  33. }
  34. int length()
  35. {
  36. LinkedListElement el = first.next;
  37. int i = 0;
  38. while(el != null) {
  39. ++i;
  40. el = el.next;
  41. }
  42. return i;
  43. }
  44. boolean deleteNth(int n)
  45. {
  46. LinkedListElement el = first;
  47. LinkedListElement el2 = el;
  48. LinkedListElement el1;
  49.  
  50. while(n > 0) {
  51. if(el == null) { return false; }
  52. el = el.next;
  53. if(n != 1) { el2 = el; }
  54. --n;
  55. }
  56. el1 = el.next;
  57. if(el1.next != null) {
  58. el.next = el1.next;
  59. } else {
  60. el.next = null;
  61. last = el2;
  62. }
  63. return true;
  64. }
  65. public void addLast(Object obj)
  66. {
  67. LinkedListElement el = new LinkedListElement(obj, null);
  68. LinkedListElement elZadnji;
  69.  
  70. if(last != null) {
  71. elZadnji = last.next;
  72. elZadnji.next = el;
  73. last = elZadnji;
  74. } else {
  75. first.next = el;
  76. last = first;
  77. last.next = el;
  78. }
  79. }
  80. void addFirst(Object obj)
  81. {
  82. LinkedListElement el = new LinkedListElement(obj, null);
  83. LinkedListElement elPrvi;
  84. if(first.next != null) {
  85. elPrvi = first.next;
  86. el.next = elPrvi;
  87. first.next = el;
  88. } else {
  89. first.next = el;
  90. last = first;
  91. last.next = el;
  92. }
  93. }
  94. }
  95. class LinkedListElement{
  96. Object element;
  97. LinkedListElement next;
  98.  
  99. LinkedListElement(Object obj)
  100. {
  101. element = obj;
  102. next = null;
  103. }
  104.  
  105. LinkedListElement(Object obj, LinkedListElement nxt)
  106. {
  107. element = obj;
  108. next = nxt;
  109. }
  110. }
  111. public class Naloga3 {
  112. //public static LinkedList list;
  113. public static PrintWriter out;
  114. public static void main(String[] args) throws IOException{
  115. Scanner vhod = new Scanner(new File(args[0]));
  116. String a = "";
  117. out = new PrintWriter(args[1]);
  118. if(vhod.hasNext()) { a = vhod.next(); }
  119. int dol = a.length();
  120. String [] tab = a.split(",");
  121. LinkedList list = new LinkedList();
  122. list.addFirst(Integer.parseInt(tab[0]));
  123.  
  124. for(int i = 1; i < tab.length; ++i) {
  125. list.addLast(Integer.parseInt(tab[i]));
  126. }
  127. //out.write(list.write());
  128. dol = vhod.nextInt();
  129. for(int i = 0; i < dol; i++) {
  130. a = vhod.next();
  131. tab = a.split(",");
  132. switch(tab[0]) {
  133. case "o":
  134. ohrani(tab[1], Integer.parseInt(tab[2]), list);
  135. break;
  136. case "z":
  137. zdruzi(tab[1], list);
  138. break;
  139. case "p":
  140. preslikaj(tab[1], Integer.parseInt(tab[2]), list);
  141. break;
  142. }
  143. //list.write();
  144. //izpisi(list);
  145.  
  146. }
  147. //out.write("\n");
  148. vhod.close();
  149. out.close();
  150. }
  151. public static void preslikaj(String op, int val, LinkedList list) throws IOException {
  152. LinkedListElement el = list.first.next;
  153. if(op.contentEquals("*")) {
  154. while(el != null) {
  155. el.element = (Integer) (el.element) * val;
  156. el = el.next;
  157. }
  158. } else {
  159. while(el != null) {
  160. el.element = (Integer) (el.element) + val;
  161. el = el.next;
  162. }
  163. }
  164. izpisi(list);
  165. }
  166. public static void ohrani(String op, int val, LinkedList list) throws IOException {
  167. LinkedListElement el = list.first.next;
  168. int i = 0;
  169. if(op.contentEquals(">")) {
  170. while(el != null) {
  171. int vrednost = (Integer) (el.element);
  172. if(vrednost <= val) { list.deleteNth(i); --i; }
  173. ++i;
  174. el = el.next;
  175. //list.write();
  176. }
  177. } else if(op.contentEquals("<")) {
  178. while(el != null) {
  179. int vrednost = (Integer) (el.element);
  180. if(vrednost >= val) { list.deleteNth(i); --i; }
  181. ++i;
  182. el = el.next;
  183. //list.write();
  184. }
  185. } else if(op.contentEquals("=")){
  186. while(el != null) {
  187. int vrednost = (Integer) (el.element);
  188. if(vrednost != val) { list.deleteNth(i); --i; }
  189. ++i;
  190. el = el.next;
  191. //list.write();
  192. }
  193. }
  194. izpisi(list);
  195. }
  196. public static void zdruzi(String op, LinkedList list) throws IOException {
  197. int vrednost = -1;
  198. LinkedListElement el = list.first.next;
  199. if(el != null) {
  200. if(op.contentEquals("*")) {
  201. vrednost = 1;
  202. while(el != null) {
  203. vrednost *= (Integer) (el.element);
  204. el = el.next;
  205. }
  206. } else {
  207. vrednost = 0;
  208. while(el != null) {
  209. vrednost += (Integer) (el.element);
  210. el = el.next;
  211. }
  212. }
  213. }
  214. list.first.next = new LinkedListElement(vrednost);
  215. list.last = list.first;
  216. list.last.next = list.first.next;
  217. izpisi(list);
  218. }
  219. public static void izpisi(LinkedList list) throws IOException{
  220. LinkedListElement el;
  221. el = list.first.next;
  222. while(el != null) {
  223. out.write(""+(el.element));
  224. if(el.next != null) { out.write(","); }
  225. el = el.next;
  226. }
  227. out.write("\n");
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement