Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <Windows.h>
- #include "Stack.h"
- #include "StackOnList.h"
- using namespace std;
- int preCalc(string line) {
- Stack stack(line.length()); // StackOnList stack; при использовании стека на однонаправленном списке
- for (int i = line.length() - 1; i >= 0; i--) {
- if (line[i] >= '0' and line[i] <= '9') {
- stack.push(line[i] - '0');
- }
- else {
- int x, y;
- switch (line[i])
- {
- case '+':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x + y);
- break;
- case '-':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x - y);
- break;
- case '*':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x * y);
- break;
- case '/':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x / y);
- break;
- default:
- break;
- }
- }
- }
- return stack.peek();
- }
- int postCalc(string line) {
- Stack stack(line.length()); // StackOnList stack; при использовании стека на однонаправленном списке
- for (int i = 0; i < line.length(); i++) {
- if (line[i] >= '0' and line[i] <= '9') {
- stack.push(line[i] - '0');
- }
- else {
- int x, y;
- switch (line[i])
- {
- case '+':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x + y);
- break;
- case '-':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(y - x);
- break;
- case '*':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x * y);
- break;
- case '/':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(y / x);
- break;
- default:
- break;
- }
- }
- }
- return stack.peek();
- }
- void sum() {
- char symbol;
- StackOnList first, second, result;
- int pr = 0;
- cin >> symbol;
- while (symbol != 'q') {
- first.push(symbol - '0');
- cin >> symbol;
- }
- cin >> symbol;
- while (symbol != 'q') {
- second.push(symbol - '0');
- cin >> symbol;
- }
- while (!first.isEmpty() or !second.isEmpty()) {
- int temp = (!first.isEmpty() ? first.peek() : 0) + (!second.isEmpty() ? second.peek() : 0) + pr;
- if (!first.isEmpty()) first.pop();
- if (!second.isEmpty()) second.pop();
- if (temp >= 10) {
- result.push(temp - 10);
- pr = 1;
- }
- else {
- result.push(temp);
- pr = 0;
- }
- }
- if (pr == 1) {
- result.push(1);
- }
- while (!result.isEmpty()) {
- cout << result.peek();
- result.pop();
- }
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- sum();
- /*string line;
- cout << "Введите выражение в префиксной форме:" << endl;
- cin >> line;
- cout << "Результат: " << preCalc(line) << endl;
- cout << "Введите выражение в постфиксной форме:" << endl;
- cin >> line;
- cout << "Результат: " << postCalc(line) << endl;*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement