結果

問題 No.222 引き算と足し算
ユーザー springroll
提出日時 2018-11-18 17:44:29
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 1,134 bytes
コンパイル時間 1,906 ms
コンパイル使用メモリ 167,884 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-24 14:57:13
合計ジャッジ時間 3,067 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:71:9: warning: 'op' may be used uninitialized [-Wmaybe-uninitialized]
   71 |         if (op == '+') {
      |         ^~
main.cpp:9:14: note: 'op' was declared here
    9 |         char op;
      |              ^~

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;


int main() {
	string s;
	cin >> s;
	int x,y;
	char op;
	int i=0;

	bool fx=false, fy=false, f=false;
	
	/* x と op を求める処理 */
	if (s[0] == '+' || s[0] == '-' ) {
		string sx;
		for (i = 1; i < s.length(); i++) {
			if (s[i] == '+' || s[i] == '-' ) {
				op = s[i];
				break;
			}
			else {
				sx.push_back(s[i]);
			}
		}
		if (s[0] == '+') {
			x = stoi(sx);
		}
		else {
			x = stoi(sx);
			x *= -1;
		}
	}
	else {
		string sx;
		for (i = 0; i < s.length(); i++) {
			if (s[i] == '+' || s[i] == '-') {
				op = s[i];
				break;
			}
			else {
				sx.push_back(s[i]);
			}
		}
		x = stoi(sx);
	}

	/* y を求める処理 */
	if (s[i+1] == '+' || s[i+1] == '-') {
		string sy;
		for (int j = i+2; j < s.length(); j++) {
			sy.push_back(s[j]);
		}
		if (s[i+1] == '+') {
			y = stoi(sy);
		}
		else {
			y = stoi(sy);
			y *= -1;
		}
	}
	else {
		string sy;
		for (int j = i + 1; j < s.length(); j++) {
			sy.push_back(s[j]);
		}
		y = stoi(sy);
	}

	//cout << x <<" "<< op <<" "<< y << endl;
	if (op == '+') {
		cout << x-y << endl;
	}
	else {
		cout << x+y << endl;
	}
}
0