結果

問題 No.708 (+ー)の式
ユーザー ohreitetsuohreitetsu
提出日時 2018-06-30 15:57:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,340 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 73,292 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-13 16:17:52
合計ジャッジ時間 1,639 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 RE -
testcase_10 WA -
testcase_11 WA -
testcase_12 RE -
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main(int argc, char* argv[])
{
	string S;
	cin>>S;
	char ch;
	char op;
	char data1,data2;
	stack<char> St;
	int i;
	for (i=0;i<S.length();i++){
		ch=S[i];
		if (ch=='+' || ch == '-'){
			St.push(ch);
			continue;
		}else if (ch=='('){
			St.push(ch);
			continue;
		}else if (ch==')'){
			ch=St.top();
			St.pop();
			St.pop();
			op=St.top();
			St.pop();
			data1=St.top();
			St.pop();
			switch(op){
				case '+':
					St.push(data1-'0'+ch-'0'+'0');
					break;
				case '-':
					St.push((data1-'0')-(ch-'0')+'0');
					break;
			}
			continue;
		}
		if ((ch>='0' && ch<='9')){
			if (St.empty()){
				St.push(ch);
				continue;
			}
			char op=St.top();
			if (op=='+' || op=='-'){
				St.pop();
				data1=St.top();
				St.pop();
				switch(op){
					case '+':
						St.push(data1-'0'+ch-'0'+'0');
						break;
					case '-':
						St.push((data1-'0')-(ch-'0')+'0');
						break;
				}
			}else{
				St.push(ch);
			}
		}
	}
	while (!St.empty()){
		data2=St.top();
		St.pop();
		if (St.empty()){
			cout<<data2<<endl;
			return 0;
		}
		op=St.top();
		St.pop();
		data1=St.top();
		St.pop();
		switch(op){
			case '+':
				St.push(data1-'0'+data2-'0'+'0');
				break;
			case '-':
				St.push((data1-'0')-(data2-'0')+'0');
				break;
		}
	}
	return 0;
}
0