GithubHelp home page GithubHelp logo

dz_sem4's Introduction

// Задача№1.Пусть дан LinkedList с несколькими элементами. // Напишите класс LLTasks с методом revert, который принимал бы на вход LinkedList и возвращает “перевернутый” список. // Пример // // Дан // [1, One, 2, Two] // // Вывод // [1, One, 2, Two] // [Two, 2, One, 1]

import java.util.LinkedList;

class LLTasks {

public static LinkedList revert(LinkedList ll) { // Напишите свое решение ниже

LinkedList<Object> newLl = new LinkedList<Object>();

for (Object item : ll) {
  newLl.addFirst(item);
}

return newLl;

} }

// Не удаляйте этот класс - он нужен для вывода результатов на экран и проверки class Printer {

public static void main(String[] args) { LinkedList ll = new LinkedList<>();

if (args.length == 0 || args.length != 4) {
  // При отправке кода на Выполнение, вы можете варьировать эти параметры
  ll.add(1);
  ll.add("One");
  ll.add(2);
  ll.add("Two");
} else {
  ll.add(Integer.parseInt(args[0]));
  ll.add(args[1]);
  ll.add(Integer.parseInt(args[2]));
  ll.add(args[3]);
}

LLTasks answer = new LLTasks();
System.out.println(ll);
LinkedList<Object> reversedList = answer.revert(ll);
System.out.println(reversedList);

} } // Задача№2.В классе MyQueue реализуйте очередь с помощью LinkedList со следующими методами: // enqueue() - помещает элемент в конец очереди // dequeue() - возвращает первый элемент из очереди и удаляет его // first() - возвращает первый элемент из очереди, не удаляя // getElements() - возвращает все элементы в очереди // Пример // queue.enqueue(1); // queue.enqueue(10); // queue.enqueue(15); // queue.enqueue(5); // System.out.println(queue.getElements()); // // [1, 10, 15, 5] // queue.dequeue(); // queue.dequeue(); // System.out.println(queue.getElements()); // // [15, 5] // System.out.println(queue.first()); // // 15

import java.util.LinkedList;

class MyQueue { private LinkedList elements = new LinkedList<>();

public MyQueue() {
}

public MyQueue(LinkedList<T> elements) {
    this.elements = elements;
}

public void enqueue(T element){
    elements.add(element);
}

public T dequeue(){
    T first = elements.getFirst();
    elements.removeFirst();
    return first;
}

public T first(){
    return elements.getFirst();
}

public LinkedList<T> getElements() {
    return elements;
}

public void setElements(LinkedList<T> elements) {
    this.elements = elements;
}

}

class Printer { public static void main(String[] args) { MyQueue queue; queue = new MyQueue<>();

    if (args.length == 0) {
        queue.enqueue(1);
        queue.enqueue(10);
        queue.enqueue(15);
        queue.enqueue(5);
    } else {
        queue.enqueue(Integer.parseInt(args[0]));
        queue.enqueue(Integer.parseInt(args[1]));
        queue.enqueue(Integer.parseInt(args[2]));
        queue.enqueue(Integer.parseInt(args[3]));
    }

    System.out.println(queue.getElements());

    queue.dequeue();
    queue.dequeue();
    System.out.println(queue.getElements());

    System.out.println(queue.first());
}

}

// Задача№3.В обычный калькулятор без логирования добавьте возможность отменить последнюю операцию. // Отмена последней операции должна быть реализована следующим образом: // если передан оператор '<' калькулятор должен вывести результат предпоследней операции.

// calculate('+', 3, 7) // calculate('+', 4, 7) // calculate('<', 0, 0)

// // 10 // // 11 // // 10

// calculate('*', 3, 2) // calculate('-', 7, 4) // calculate('<', 0, 0)

// // 6 // // 3 // // 6

import java.util.ArrayDeque; import java.util.Deque;

class Calculator {

private ArrayDeque<Integer> results;

public Calculator() {
    results = new ArrayDeque<Integer>();
}

public int calculate(char op, int a, int b) {

    int result = 0;
    if (op == '+')
        result = a + b;
    else if (op == '-')
        result =  a - b;
    else if (op == '*')
        result =  a * b;
    else if (op == '/')
        result =  a / b;

    if (op == '<') {
        results.removeLast();
        result = results.removeLast();

        /* Вариант без удаления элементов
        Integer beforeLast = null;
        Integer last = null;
        for (Integer item : results) {
            beforeLast = last;
            last = item;
        }
        result = beforeLast;
         */
    } else
        results.add(result);


    return result;
}

}

class Printer { public static void main(String[] args) { int a, b, c, d; char op, op2, undo;

    if (args.length == 0) {
   
        a = 3;
        op = '+';
        b = 7;
        c = 4;
        op2 = '+';
        d = 7;
        undo = '<';
    } else {
        a = Integer.parseInt(args[0]);
        op = args[1].charAt(0);
        b = Integer.parseInt(args[2]);
        c = Integer.parseInt(args[3]);
        op2 = args[4].charAt(0);
        d = Integer.parseInt(args[5]);
        undo = args[6].charAt(0);
    }

    Calculator calculator = new Calculator();
    int result = calculator.calculate(op, a, b);
    System.out.println(result);
    int result2 = calculator.calculate(op2, c, d);
    System.out.println(result2);
    int prevResult = calculator.calculate(undo, 0, 0);
    System.out.println(prevResult);
}

}

// Эталонное решение от автора

import java.util.ArrayDeque; import java.util.Deque;

class Calculator { public Deque resultsStack = new ArrayDeque<>(); public Deque operationsStack = new ArrayDeque<>(); public int prevResult;

public int calculate(char op, int a, int b) {
    if (op == '<') {
        if (resultsStack.size() >= 2) {
            resultsStack.pop();
            prevResult = resultsStack.peek();
        }
        return prevResult;
    } else {
        int result = performOperation(op, a, b);
        resultsStack.push(result);
        operationsStack.push(op);
        prevResult = result;
        return result;
    }
}


private int performOperation(char op, int a, int b) {
    switch (op) {
        case '+':
            return add(a, b);
        case '-':
            return minus(a, b);
        case '*':
            return mult(a, b);
        case '/':
            return divide(a, b);
        default:
            throw new IllegalArgumentException("Некорректный оператор: " + op);
    }
}

private int divide(int a, int b) {
    if (b != 0) {
        return a / b;
    } else {
        throw new ArithmeticException("Деление на ноль");
    }
}

private int mult(int a, int b) {
    return a * b;
}

private int minus(int a, int b) {
    return a - b;
}

private int add(int a, int b) {
    return a + b;
}

}

class Printer { public static void main(String[] args) { int a, b, c, d; char op, op2, undo;

    if (args.length == 0) {
        a = 3;
        op = '+';
        b = 7;
        c = 4;
        op2 = '+';
        d = 7;
        undo = '<';
    } else {
        a = Integer.parseInt(args[0]);
        op = args[1].charAt(0);
        b = Integer.parseInt(args[2]);
        c = Integer.parseInt(args[3]);
        op2 = args[4].charAt(0);
        d = Integer.parseInt(args[5]);
        undo = args[6].charAt(0);
    }

    Calculator calculator = new Calculator();
    int result = calculator.calculate(op, a, b);
    System.out.println(result);
    int result2 = calculator.calculate(op2, c, d);
    System.out.println(result2);
    int prevResult = calculator.calculate(undo, 0, 0);
    System.out.println(prevResult);
}

}

class Printer { public static void main(String[] args) { int a, b, c, d; char op, op2, undo;

    if (args.length == 0) {
    
        a = 3;
        op = '+';
        b = 7;
        c = 4;
        op2 = '+';
        d = 7;
        undo = '<';
    } else {
        a = Integer.parseInt(args[0]);
        op = args[1].charAt(0);
        b = Integer.parseInt(args[2]);
        c = Integer.parseInt(args[3]);
        op2 = args[4].charAt(0);
        d = Integer.parseInt(args[5]);
        undo = args[6].charAt(0);
    }

    Calculator calculator = new Calculator();
    int result = calculator.calculate(op, a, b);
    System.out.println(result);
    int result2 = calculator.calculate(op2, c, d);
    System.out.println(result2);
    int prevResult = calculator.calculate(undo, 0, 0);
    System.out.println(prevResult);
}

}

dz_sem4's People

Contributors

artemtsmyg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.