I am currently applying to many Software Engineer positions, and have been asked to complete several coding challenges. The most recent was an equation evaluator, written in C++, that was supposed to evaluate equations.
The criteria given were as follows:
- Each equation is specified on a separate line
- LHS is the left hand side of the equation and is always a variable name
- A variable name is composed of letters from the alphabet for which
isalpha(c)
is always true - RHS is the right hand side of the equation and can be composed of variables, unsigned integers, and the
+
operator
The program needed to take a filename as input. That file contained a set of equations, such as:
- left = 5
- right = 6
- total = left + right
It needed to evaluate the set of equations and print the value of each variable, sorted in ascending order by variable name. If a variable didn't have a well-defined and unique solution, the program should output the exact contents of the file (no error or debug output).
After completing the challenge, I submitted my code and ended up being told I didn't make it to the next stage and that my code was... less than stellar. I'm only now getting out of college, and I'm not entirely sure where I can improve, so I'm hoping that's where you can help me. The code below is what I submitted, and I would appreciate any suggestions or advice on things I could have done better or other ways of tackling some of the problems. Only constructive criticism, please! I know the code is not the best, but it was my best at the time. I want to improve.
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
bool isNum(char test)
{
return (test == '0' || test == '1' || test == '2' || test == '3' || test == '4' || test == '5' || test == '6' || test == '7' || test == '8' || test == '9');
}
int main()
{
string fileName;
char fileChar;
unordered_map<string, int> myVariables;
unordered_map<std::string, int>::const_iterator result;
string tempNumStr = "", tempVar = "", leftSide = "", line = "";
int tempNum;
bool leftSideComplete = false, additionTime = false, justHadEquals = false, errorExists = false;
vector<string> mapKeys;
cout << "For what file would you like to evaluate equations? Verify that this file is located in the directory from which you are running this program." << "\n";
cin >> fileName;
ifstream inFile;
inFile.open(fileName);
if (!inFile) {
cerr << "Unable to open the file " << fileName << "\n";
system("pause");
return 0;
}
while (getline(inFile, line)) {
if (errorExists)
break;
leftSide = "";
tempVar = "";
tempNumStr = "";
tempNum = 0;
leftSideComplete = false;
additionTime = false;
justHadEquals = false;
for (int i = 0; i < line.length(); i++) {
if (isNum(line[i])) {
tempNumStr += line[i];
}
else if (isalpha(line[i])) {
tempVar += line[i];
}
else if (line[i] == ' ') {
if (leftSideComplete && (additionTime || justHadEquals)) {
if (tempVar.length() > 0) {
result = myVariables.find(tempVar);
if (result == myVariables.end()) {
errorExists = true;
tempVar = "";
}
else {
tempNum = myVariables.at(tempVar);
myVariables.at(leftSide) += tempNum;
tempNum = 0;
additionTime = false;
justHadEquals = false;
}
}
else if (tempNumStr.length() > 0) {
stringstream convert(tempNumStr);
convert >> tempNum;
myVariables.at(leftSide) += tempNum;
tempNum = 0;
tempNumStr = "";
additionTime = false;
justHadEquals = false;
}
}
}
else if (line[i] == '=') {
if (justHadEquals || leftSideComplete)
errorExists = true;
result = myVariables.find(tempVar);
if (result == myVariables.end()) {
myVariables.insert(make_pair(tempVar, 0));
}
leftSide = tempVar;
tempVar = "";
leftSideComplete = true;
justHadEquals = true;
}
else if (line[i] == '+') {
if ((additionTime && (tempVar.length() == 0 && tempNumStr.length() == 0)) || !leftSideComplete)
errorExists = true;
additionTime = true;
tempVar = "";
tempNumStr = "";
tempNum = 0;
}
if(i == line.length() - 1){
if (leftSideComplete && (additionTime || justHadEquals)) {
if (tempVar.length() > 0) {
result = myVariables.find(tempVar);
if (result == myVariables.end()) {
errorExists = true;
tempVar = "";
}
else {
tempNum = myVariables.at(tempVar);
myVariables.at(leftSide) += tempNum;
tempNum = 0;
additionTime = false;
justHadEquals = false;
leftSideComplete = false;
}
}
else if (tempNumStr.length() > 0) {
stringstream convert(tempNumStr);
convert >> tempNum;
myVariables.at(leftSide) += tempNum;
tempNum = 0;
tempNumStr = "";
additionTime = false;
justHadEquals = false;
leftSideComplete = false;
}
}
break;
}
}
}
inFile.close();
if (errorExists) {
inFile.open(fileName);
while (getline(inFile, line)) {
cout << line << endl;
}
}
else {
for (auto& x : myVariables) {
mapKeys.push_back(x.first);
sort(mapKeys.begin(), mapKeys.end());
}
for (int i = 0; i < mapKeys.size(); i++) {
std::cout << mapKeys[i] << " = " << myVariables.at(mapKeys[i]) << std::endl;
}
}
system("pause");
return 0;
}
using namespace std
, indent your code properly, declare the variables right where you need them. -- As long as there are no answers, you can still edit your code so that the reviewers can focus on more interesting details. \$\endgroup\$isNum
, thereturn
should be indented. And inmain
, all lines between the opening brace and the very last line in the file should be indented also. \$\endgroup\$