Pagini recente » Cod sursa (job #504208) | Cod sursa (job #2661352) | Cod sursa (job #1449315) | Cod sursa (job #1268762) | Cod sursa (job #2797646)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("input.in");
ofstream fout ("output.out");
bool hasToPrint = false;
bool ifCondition = false;
bool functionEnded = false;
bool firstFunction = true;
stack <int> processes, ifStatements;
map <string, string> variables;
string printString;
void printFunction(string s) {
hasToPrint = false;
if (variables.find(s) != variables.end()) {
printString += variables[s];
}
else {
printString += s;
}
}
void throwError() {
printString = "ERROR";
while (!processes.empty()) {
processes.pop();
}
}
void interpretInstruction(string instruction) {
if (instruction == "start") {
if (!firstFunction) {
fout << "\n";
}
else {
firstFunction = false;
}
processes.push(0);
while (!ifStatements.empty()) {
ifStatements.pop();
}
printString.clear();
functionEnded = false;
}
if (processes.empty()) {
return;
}
if (instruction == "print" && processes.top() != -1) {
hasToPrint = true;
}
if (instruction == "if" && processes.top() != -1) {
string cond;
fin >> cond;
string condition;
if (variables.find(cond) != variables.end()) {
condition = variables[cond];
}
else {
condition = cond;
}
if (condition == "true") {
ifCondition = true;
processes.push(1);
}
else if (condition == "false") {
ifCondition = false;
processes.push(-1);
}
else {
throwError();
}
}
else if (instruction == "if") {
processes.push(-1);
}
if (instruction == "else" && ifCondition == false && processes.top() != -1) {
processes.push(1);
}
else if (instruction == "else") {
processes.push(-1);
// ifStatements.pop();
}
if (instruction == "var" && processes.top() != -1) {
string name, value;
fin >> name >> value;
if (variables.find(name) == variables.end()) {
variables[name] = value;
}
else {
throwError();
}
}
if (instruction == "set" && processes.top() != -1) {
string name, value;
fin >> name >> value;
if (variables.find(name) != variables.end()) {
if (variables.find(value) != variables.end()) {
variables[name] = variables[value];
}
else {
variables[name] = value;
}
}
else {
throwError();
}
}
if (instruction == "return" && processes.top() != -1) {
string returnvalue;
fin >> returnvalue;
while (processes.top() != 0) {
processes.pop();
}
if (!processes.empty()) {
processes.pop();
}
}
if (instruction == "end") {
if (!processes.empty()) {
processes.pop();
}
}
}
void readInput() {
int n;
fin >> n;
string instructions = " ";
printString.clear();
while (fin >> instructions) {
interpretInstruction(instructions);
if (hasToPrint && processes.top() != -1) {
fin >> instructions;
printFunction(instructions);
}
if (processes.empty() && !functionEnded) {
functionEnded = true;
fout << printString;
printString.clear();
variables.clear();
while (!ifStatements.empty()) {
ifStatements.pop();
}
}
}
}
int main()
{
readInput();
return 0;
}