結果

問題 No.49 算数の宿題
ユーザー komori3komori3
提出日時 2017-09-14 16:48:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,945 bytes
コンパイル時間 728 ms
コンパイル使用メモリ 76,724 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 17:38:24
合計ジャッジ時間 1,133 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function ‘calc::op calc::parse_operator()’:
main.cpp:82:16: warning: control reaches end of non-void function [-Wreturn-type]
    syntax_error("parse_operator");
    ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <functional>
#include <set>
#include <numeric>
#include <stack>
#include <utility>
#include <time.h>
//#include "util.h"

using namespace std;
typedef long long lint;
typedef unsigned long long ull;
typedef pair<lint, lint> Pii;

#define PI 3.14159265358979323846
#define EPS 1e-6
#define MOD ((lint)1000000007)
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define CHAR_BIT 8

template <typename _Ty>
ostream& operator << (ostream& ostr, const vector<_Ty>& v) {
	if (v.empty()) {
		cout << "{ }";
		return ostr;
	}
	cout << "{" << v.front();
	for (auto itr = ++v.begin(); itr != v.end(); itr++) {
		cout << ", " << *itr;
	}
	cout << "}";
	return ostr;
}

class calc {
private:
	string S;
	int pos;
	enum op { MUL, ADD };
public:
	calc(string S) {
		this->S = S;
		pos = 0;
	}
	calc(string S, int pos) {
		this->S = S; this->pos = pos;
	}
	void syntax_error(string S) {
		cout << S << endl;
		exit(EXIT_FAILURE);
	}
	int parse_int() {
		if (S[pos] < '0' || '9' < S[pos])
			syntax_error("parse_int");

		int res = 0;
		while (pos < S.size() && '0' <= S[pos] && S[pos] <= '9') {
			res *= 10;
			res += S[pos] - '0';
			pos++;
		}
		return res;
	}
	op parse_operator() {
		if (S[pos] == '*') {
			pos++;
			return ADD;
		}
		else if (S[pos] == '+') {
			pos++;
			return MUL;
		}
		else
			syntax_error("parse_operator");
	}
	int calculate() {
		int res = parse_int();
		while (pos != S.size()) {
			op o = parse_operator();
			if (o == ADD)
				res += parse_int();
			else
				res *= parse_int();
		}
		return res;
	}
};

int yuki0044()
{
	string S;
	cin >> S;
	cout << calc(S).calculate() << endl;

	return 0;
}

int main()
{
	//clock_t start, end;
	//start = clock();

	yuki0044();

	//end = clock();
	//printf("%d msec.\n", end - start);

	return 0;
}
0