結果

問題 No.222 引き算と足し算
ユーザー hanorver
提出日時 2015-10-08 19:29:11
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 954 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 93,876 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-15 22:05:24
合計ジャッジ時間 1,947 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<regex>

int main() {
	std::string str;
	std::cin >> str;

	for (int i = 1; i < str.length(); i++) {
		if (str[i] < '0') {
			if(str[i] == '-'){
				if(str[i + 1] == '-'){
					str.replace(i, 2, "-");
				} else if (str[i + 1] == '+') {
					str.replace(i, 2, "+");
				} else {
					str.replace(i, 1, "+");
				}
			} else {
				if (str[i + 1] == '-') {
					str.replace(i, 2, "+");
				} else if (str[i + 1] == '+') {
					str.replace(i, 2, "-");
				} else {
					str.replace(i, 1, "-");
				}
			}
		}
	}

	int i = 0, a = 0;
	if (str[0] == '+' || str[0] == '-')i++;
	for (; str[i] != '+' && str[i] != '-'; i++) {
		a *= 10;
		a += str[i] - '0';
	}

	if (str[0] == '-') {
		a *= -1;
	}

	char op = str[i];
	i++;
	int b = 0;
	for (;i < str.length();i++) {
		b *= 10;
		b += str[i] - '0';
	}

	int ans;
	if (op == '+') {
		ans = a + b;
	} else {
		ans = a - b;
	}

	std::cout << ans << std::endl;
	return 0;
}
0