我的代码很丑吗
  • 板块灌水区
  • 楼主封禁用户
  • 当前回复20
  • 已保存回复20
  • 发布时间2022/10/19 15:10
  • 上次更新2023/10/27 06:54:36
查看原帖
我的代码很丑吗
206814
封禁用户楼主2022/10/19 15:10

某次校内比赛,爆切一道入门题

刚想讲,结果被轰下台

大概就是计算字符串A+B=?,A-?=B之类的东西

想让大家评判一下,很丑吗?

#include <bits/stdc++.h>
#define int long long
#define title "nixiofyy"
using namespace std;
string input;
int Myatoi(string str){
	int ret = 0;
	for(int i = 0; i < str.length(); i++){
		ret += str[i] - '0';
		ret *= 10;
	}
	return ret / 10;
}
string Mysubstr(int l, int r){
	string here;
	for(int i = l; i < r; i++){
		here.push_back(input[i]);
	}
	return here;
}
int calc(int opt, bool flag, int x, int y){
	switch(opt){
		case 1: //?+B=C, x = B, y = C 
			if(flag) return y + x;
			return y - x;
		case 2: //A+?=C, x = A, y = C
			if(flag) return -(y - x);
			return y - x;
		case 3: //A+B=?, x = A, y = B
			if(flag) return x - y;
			return x + y;
	}
	return -1; //Never ends here.
}
signed main(){
#ifndef LOCAL
	freopen(title ".in", "r", stdin);
	freopen(title ".out", "w", stdout);
#endif
	bool flag = false;
	cin >> input;
	int sum = input.find_first_of('+'), equals = input.find_first_of('=');
	if(sum == string::npos) sum = input.find_first_of('-'), flag = true;
	if(input[sum - 1] == '?'){ //?+B=C
		cout << calc(1, flag,
				Myatoi(Mysubstr(sum + 1, equals)), 
				Myatoi(Mysubstr(equals + 1, input.length())));
	}else if(input[equals + 1] == '?'){ //A+B=?
		cout << calc(3, flag,
				Myatoi(Mysubstr(0, sum)), 
				Myatoi(Mysubstr(sum + 1, equals)));
	}else{ //A+?=C
		cout << calc(2, flag,
				Myatoi(Mysubstr(0, sum)), 
				Myatoi(Mysubstr(equals + 1, input.length())));
	}
#ifndef LOCAL
	fclose(stdin);
	fclose(stdout);
#endif
	return 0;
} 
2022/10/19 15:10
加载中...