結果

問題 No.708 (+ー)の式
ユーザー GBGB
提出日時 2018-12-20 01:45:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,797 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 72,104 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-26 00:19:29
合計ジャッジ時間 1,301 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<string>
#include<deque>

using namespace std;

bool IsNum(char c) {
	if ('0' <= c && c <= '9') return true;
	else return false;
}
bool IsAdd(char c) {
	if (c=='+')return true;
	else return false;
}
bool IsSub(char c) {
	if (c=='-')return true;
	else return false;
}
int ctoi(char c) {
	if ('0' <= c && c <= '9') return (c - '0');
	return -1;
}
int pop(deque<int> &st) {
	int buff = st.front();
	st.pop_front();
	return buff;
}
void add(deque<int> &st,bool &flag) {
	int buff1 = pop(st);
	int buff2 = pop(st);
	int addAns = buff2 + buff1;
	st.push_front(addAns);
	flag = false;
}
void sub(deque<int> &st,bool &flag) {
	int buff1 = pop(st);
	int buff2 = pop(st);
	int subAns = buff2 - buff1;
	st.push_front(subAns);
	flag = false;
}

int main() {

	bool addFlag = false, subFlag = false;
	bool addFlag2 = false, subFlag2 = false;
	bool pareStartFlag = false;
	string str;
	deque<int> stack;
	cin >> str;

	for (int i = 0;i < str.size();i++) {
		char c = str[i];
		if (IsNum(c)) {
			stack.push_front(ctoi(c));
			if (!pareStartFlag) {
				if (addFlag) add(stack,addFlag);
				else if (subFlag) sub(stack,subFlag);
			}
			else {
				if (addFlag2) {
					add(stack,addFlag2);
					if (addFlag) add(stack,addFlag);
					else if (subFlag) sub(stack,subFlag);
					pareStartFlag = false;
				}
				else if (subFlag2) {
					sub(stack,subFlag2);
					if (addFlag) add(stack,addFlag);
					else if (subFlag) sub(stack, subFlag);
					pareStartFlag = false;
				}
			}
		}
		else if (pareStartFlag) {
			if (IsAdd(c)) addFlag2 = true;
			else if (IsSub(c))subFlag2 = true;
		}
		else if (IsAdd(c))addFlag=true;
		else if (IsSub(c))subFlag = true;
		else if (c=='(')pareStartFlag = true;
		else if (c==')')pareStartFlag = false;
	}
	
	cout << stack.front() << endl;

	return 0;
}
0