不知道对不对的交互库
查看原帖
不知道对不对的交互库
674631
qianruiyi楼主2024/5/31 22:18

checker.cpp:

#include "testlib.h"
#include <iostream>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
int n;
int cntstd[301], cntasw[301];
int priority[301];
stack<char> opr;
stack<double> num;
void init()
{
	priority['+'] = 1;
	priority['-'] = 1;
	priority['*'] = 2;
	priority['/'] = 2;
}
double calculate(string str)
{
	int nownum = 0;
	for (int i = 0; i < 6; i++)
	{
		if (isdigit(str[i]))
		{
			nownum = nownum * 10 + str[i] - '0';
			if (i == 5 || !isdigit(str[i + 1]))
			{
				num.push(nownum);
				nownum = 0;
			}
		}
		else
		{
			if (opr.empty() || priority[(int)str[i]] > priority[(int)opr.top()]) opr.push(str[i]);
			else
			{
				char oper = opr.top();
				opr.pop();
				double b = num.top();
				num.pop();
				double a = num.top();
				num.pop();
				if (oper == '+') num.push(a + b);
				if (oper == '-') num.push(a - b);
				if (oper == '*') num.push(a * b);
				if (oper == '/')
				{
					if (b == 0) return 0x3f3f3f3f;
					num.push(a / b);
				}
				opr.push(str[i]);
			}
		}
	}
	while (!opr.empty())
	{
		char oper = opr.top();
		opr.pop();
		double b = num.top();
		num.pop();
		double a = num.top();
		num.pop();
		if (oper == '+') num.push(a + b);
		if (oper == '-') num.push(a - b);
		if (oper == '*') num.push(a * b);
		if (oper == '/')
		{
			if (b == 0) return 0x3f3f3f3f;
			num.push(a / b);
		}
	}
	double ret = num.top();
	num.pop();
	return ret;
}
string check(string &answer, string &standard)
{
	memset(cntstd, 0, sizeof cntstd);
	memset(cntasw, 0, sizeof cntasw);
	string ret;
	ret.resize(6);
	if (answer.length() != 6)
	{
		ret = "------";
		return ret;
	}
	for (int i = 0; i < 6; i++)
	{
		if (!isdigit(answer[i])&&answer[i]!='+'&&answer[i]!='-'&&answer[i]!='*'&&answer[i]!='/')
		{
			ret = "------";
			return ret;
		}
		if (i!=0&&answer[i-1]=='0'&&isdigit(answer[i]))
		{
			ret = "------";
			return ret;
		}
		if ((i==0||i==5||!isdigit(answer[i-1]))&&!isdigit(answer[i]))
		{
			ret = "------";
			return ret;
		}
	}
	if (calculate(answer) != n)
	{
		ret = "------";
		return ret;
	}
	for (int i = 0; i < 6; i++)
	{
		if (answer[i] == standard[i]) ret[i] = 'A';
		else cntstd[(int)standard[i]]++;
	}
	for (int i = 0; i < 6; i++)
	{
		if (answer[i] == standard[i]) continue;
		else
		{
			if (++cntasw[(int)answer[i]] <= cntstd[(int)answer[i]]) ret[i] = 'B';
			else ret[i] = 'X';
		}
	}
	return ret;
}
int main(int argc, char* argv[])
{
	setName("Interactor Mathler");
	registerInteraction(argc, argv);
	init();
	n = inf.readInt();
	cout << n << endl;
	string asw = ans.readString();
	int cnt = 0;
	bool flag = false;
	vector<string> vec1, vec2;
	while (true)
	{
		string guess = ouf.readString();
		string feedback = check(guess, asw);
		cout << feedback << endl;
		vec1.push_back(guess);
		vec2.push_back(feedback);
		cnt++;
		if (cnt > 12) break;
		if (feedback == "AAAAAA")
		{
			flag = true;
			break;
		}
	}
	string sta;
	if (flag) sta = "You succeeded.";
	else sta = "You asked over 12 times.";
	if (flag) quitf(_ok, (const char *)sta.data());
	else quitf(_wa, (const char *)sta.data());
	return 0;
}
2024/5/31 22:18
加载中...