public
Created

[08/13/13] Challenge #135 [Easy] Arithmetic Equations (2nd Attempt)

  • Download Gist
gistfile1.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 
#include <stdlib.h>
#include <stdio.h>
#include <vector>
using namespace std;
 
struct ExpressionElement {
bool isValue;
union {
int value;
char op;
} data;
};
 
int main()
{
int min, max;
char opChars[3] = { '+', '-', 'x' };
scanf("%d %d", &min, &max);
 
int consts[4];
char ops[3];
 
// Infinite loop unless user exists...
bool correct = true;
int correctResult = 0;
while(true)
{
// If correct, generate new challenge
if(correct)
{
for(int i = 0; i < 4; i++)
consts[i] = min + rand() % (max - min + 1); // +1 since we're inclusive
for(int i = 0; i < 3; i++)
ops[i] = opChars[rand() % 3];
// Place equation into ExpressionElement list, so we can compute a solution
vector< ExpressionElement > expressionList;
for(int i = 0; i < 4; i++)
{
ExpressionElement element;
element.isValue = true;
element.data.value = consts[i];
expressionList.push_back(element);
if(i < 3)
{
element.isValue = false;
element.data.op = ops[i];
expressionList.push_back(element);
}
}
 
// Do all multiplication first, storing the result to the right of the op
for(int i = 0; i < expressionList.size() / 2; i++)
{
int index = i * 2;
ExpressionElement arg0 = expressionList.at(index + 0);
ExpressionElement op = expressionList.at(index + 1);
ExpressionElement arg1 = expressionList.at(index + 2);
 
if(arg0.isValue && !op.isValue && arg1.isValue && op.data.op == 'x')
{
expressionList.erase(expressionList.begin() + index);
expressionList.erase(expressionList.begin() + index);
expressionList.at(index).data.value = arg0.data.value * arg1.data.value;
i--;
}
}
 
// Add/sub
for(int i = 0; i < expressionList.size() / 2; i++)
{
int index = i * 2;
ExpressionElement arg0 = expressionList.at(index + 0);
ExpressionElement op = expressionList.at(index + 1);
ExpressionElement arg1 = expressionList.at(index + 2);
 
if(arg0.isValue && !op.isValue && arg1.isValue)
{
expressionList.erase(expressionList.begin() + index);
expressionList.erase(expressionList.begin() + index);
if(op.data.op == '+')
expressionList.at(index).data.value = arg0.data.value + arg1.data.value;
else
expressionList.at(index).data.value = arg0.data.value - arg1.data.value;
i--;
}
}
correctResult = expressionList.at(0).data.value;
correct = false;
}
 
printf("%d %c %d %c %d %c %d\n", consts[0], ops[0], consts[1], ops[1], consts[2], ops[2], consts[3]);
 
char input[512];
fgets(input, 511, stdin);
 
if(input[0] == 'q' || input[0] == 'Q')
{
break;
}
else
{
int value = -1;
int scanned = sscanf(input, "%d", &value);
if(scanned != 1)
printf("Incorrect input format, try again\n");
else if(value != correctResult)
printf("Incorrect...\n");
else if(value == correctResult)
{
printf("Correct!\n");
correct = true;
}
}
}
 
// All done
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.