fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <cstdlib>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. int main() {
  8. map<string,int> variables;
  9.  
  10. string line[2000];
  11. cin >> line[0];
  12. string line2;
  13. int i;
  14.  
  15. while(cin >> line2) {
  16. bool clear = false;
  17. if(line[0] == "clear") {
  18. clear = true;
  19. line[0] = line2;
  20. goto END1;
  21. }
  22. i = 1;
  23. line[1] = line2;
  24.  
  25. while(line[i] != "def" && line[i] != "clear" && line[i] != "calc") {
  26. i++;
  27. cin >> line[i];
  28. }
  29.  
  30. if(line[0] == "clear") {
  31. goto END1;
  32. } else if(line[0] == "def") {
  33. char* line1 = &(line[2][0]);
  34. variables[line[1]] = atoi(line1);
  35. } else {
  36. bool unknown = false;
  37. int sum = 0;
  38. int j = 1;
  39. sum += variables[line[j]];
  40. j++;
  41. A:
  42. if(line[j] == "+") {
  43. string temp = line[j + 1];
  44. char* temp1 = &temp[0];
  45. map<string,int>::iterator it = variables.find(temp1);
  46. if(it != variables.end())
  47. sum += variables[line[j + 1]];
  48. else {
  49. unknown = true;
  50. goto B;
  51. }
  52. } else if(line[j] == "-") {
  53. string temp = line[j+1];
  54. char* temp2 = &temp[0];
  55. map<string,int>::iterator it = variables.find(temp2);
  56. if(it != variables.end())
  57. sum -= variables[line[j + 1]];
  58. else {
  59. unknown = true;
  60. goto B;
  61. }
  62. } else if(line[j] == "=") {
  63. goto B;
  64. }
  65. j += 2;
  66. goto A;
  67. B:
  68. string h = "";
  69. for(int k = 1; k < i; k++) {
  70. h += line[k] + " ";
  71. }
  72. if(unknown == false) {
  73. string qwe;
  74. for(map<string,int>::iterator it = variables.begin(); it != variables.end(); it++) {
  75. int x = it->second;
  76. if(x == sum) {
  77. qwe = it->first;
  78. goto ASD;
  79. }
  80. }
  81. goto UNKNOWN;
  82. ASD:
  83. stringstream ss;
  84. ss << qwe;
  85. h += ss.str();
  86. } else {
  87. UNKNOWN:
  88. h += "unknown";
  89. }
  90. cout << h << endl;
  91. }
  92. line[0] = line[i];
  93. END1:
  94. if(clear)
  95. variables.clear();
  96. }
  97.  
  98. return 0;
  99. }
Success #stdin #stdout 0s 3480KB
stdin
def foo 3
calc foo + bar =
def bar 7
def programming 10
calc foo + bar =
def is 4
def fun 8
calc programming - is + fun =
def fun 1
calc programming - is + fun =
clear
calc programming - is + fun =
stdout
foo + bar = unknown
foo + bar = programming
programming - is + fun = unknown
programming - is + fun = bar
programming - is + fun = unknown