Coder Social home page Coder Social logo

infix_calculator's Introduction

infix_calculator


for homework


我不知道能說甚麼,反正就上課教的,但這是我的寫法
下面是輸入後序求值。

#include <iostream>
#include <string>
#include <stack>

using namespace std;

int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	int buffer1, buffer2;
	stack <int> numbers;
	string expn;
	while (cin >> expn)
		if (isdigit(expn[0]) || (expn.size() > 1))
			numbers.push(stoi(expn));
		else {
			buffer2 = numbers.top(); numbers.pop();
			buffer1 = numbers.top(); numbers.pop();
			switch (expn[0]) {
			case '+':
				numbers.push(buffer1 + buffer2);
				break;
			case '-':
				numbers.push(buffer1 - buffer2);
				break;
			case '*':
				numbers.push(buffer1 * buffer2);
				break;
			case '/':
				numbers.push(buffer1 / buffer2);
				break;
			}
		}
	cout << numbers.top() << '\n';
}

這是中序轉後序

#include <iostream>
#include <string>

using namespace std;

#define Priority(x) (x == '+' || x == '-' ? 1 : x == '%' ? 2 : (x == '*' || x == '/' ? 3 : 0))

int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	char operators[1000];
	string expn;
	int ptr = 0;
	getline(cin, expn);
	for(int i = 0; i != expn.size(); ++i)
		if(expn[i] != ' ') {
			if(isalpha(expn[i]))
				cout << expn[i] << ' ';
			else if(expn[i] == '(')
				operators[ptr] = '(', ++ptr;
			else if(expn[i] == ')') {
				while(operators[ptr - 1] != '(')
					--ptr, cout << operators[ptr] << ' ';
				--ptr;
			}
			else{
				while(ptr && Priority(operators[ptr - 1]) >= Priority(expn[i]))
					--ptr, cout << operators[ptr] << ' ';
				operators[ptr] = expn[i]; ++ptr;
			}
		}
	while(ptr)
		--ptr, cout << operators[ptr] << ' ';
	cout << '\n';
	return 0;
}

infix_calculator's People

Contributors

hsiungyuu avatar

Watchers

 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.