In: Computer Science
In the above code sample, tokens.h is not provided and intVariableEntry definition is also not provided. Therefore, to get the code working, created my own intVariableTable.h which defines whatever is necessary. You will need to change / discard it depending on what is already defined in Tokens.h.
///////// intVariableTable.h /////////////////////////////////////////
#include <iostream>
using std::string;
class IntVariableEntry {
public:
string name;
int value;
};
class IntVariableTable {
IntVariableEntry *int_variable_table;
int num_int_variables;
public:
IntVariableTable();
~IntVariableTable();
int numVariables() const;
int lookupVariable(const string& token) const;
int defineVariable(const string& token1, const string& token2);
string getName(int index) const;
int getValue(int index) const;
void setValue(int index, int value);
bool validIndex(int index) const;
void display() const;
};
/// IntVariableTable.cpp /////// code is mostly fine, added a missing initialization for num_int_variables ///////////
#define MAX_INT_VARIABLES 100
#include "IntVariableTable.h"
#include <assert.h>
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::left;
using std::right;
using std::setw;
using std::string;
// The IntVariableTable constructor dynamically allocates the fixed size array of integer variables.
IntVariableTable::IntVariableTable()
{
int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES];
num_int_variables = 0;
}
// The IntVariableTable destructor deallocates the integer variable array.
IntVariableTable::~IntVariableTable()
{
delete[] int_variable_table;
}
// Returns the number of variables added to the integer variable table.
int IntVariableTable::numVariables() const
{
return num_int_variables;
}
// Returns the index of the string token argument in the integer variable table,
// or -1 if the variable name is not found in the integer variable table.
int IntVariableTable::lookupVariable(const string& token) const
{
int index = -1;
for (int i = 0; i < num_int_variables; i++) {
if (int_variable_table[i].name.compare(token)==0) {
index = i;
break;
}
}
return index;
}
// Adds integer variable name and initial value string tokens to the integer variable
// table, and returns the index of the table entry used to store the variable.
// If the variable name is already present in the integer variable table, a message is
// generated and the table entry index is returned.
// If there is no available entry in the integer variable table, a message is generated
// and -1 is returned.
int IntVariableTable::defineVariable(const string& token1, const string& token2)
{
int index = lookupVariable(token1);
if (index == -1) {
if (num_int_variables == MAX_INT_VARIABLES) {
cout << "no available entry available" << endl;
return -1;
}
int_variable_table[num_int_variables].name = token1;
int_variable_table[num_int_variables].value = 0;
num_int_variables++;
}
else {
cout << "entry already exists" << endl;
}
return index;
}
// Returns the name string present in the integer variable table entry specified
// by the index argument.
// An assertion is triggered if the index argument is out of bounds.
string IntVariableTable::getName(int index) const
{
assert(validIndex(index));
return int_variable_table[index].name;
}
// Returns the numeric value present in the integer variable table entry specified
// by the index argument.
// An assertion is triggered if the index argument is out of bounds.
int IntVariableTable::getValue(int index) const
{
assert(validIndex(index));
return int_variable_table[index].value;
}
// Sets the numeric value in the integer variable table entry specified by the index argument.
// An assertion is triggered if the index argument is out of bounds.
void IntVariableTable::setValue(int index, int value)
{
assert(validIndex(index));
int_variable_table[index].value = value;
}
// Returns whether the argument is a valid integer variable table index.
bool IntVariableTable::validIndex(int index) const
{
return ((index >= 0) && (index < num_int_variables));
}
// Displays the contents of the integer variable table on the console.
void IntVariableTable::display() const
{
if (num_int_variables == 0) {
cout << endl << "The integer variable table is empty" << endl;
}
else {
cout << endl << "Integer variable table: [index | variable name | initial value]" << endl << endl;
for (int i = 0; i < num_int_variables; i++) {
cout << right << setw(8) << i << " "
<< left << setw(24) << int_variable_table[i].name
<< right << setw(8) << int_variable_table[i].value << endl;
}
}
}
/////// Main function to test: Please note: Not clear what the second string parameter in defineVariable is to be used for - no clarity in TODO comments. Have ignored it for now.
#include <iostream>
#include "IntVariableTable.h"
using namespace std;
int main()
{
IntVariableTable it;
cout << it.lookupVariable ("zonko") << endl;
it.defineVariable ("first", "junk");
it.defineVariable ("second", "junk");
it.defineVariable ("third", "junk");
it.defineVariable ("fourth", "junk");
it.defineVariable ("fifth", "junk");
cout << it.getName (1) << endl;
it.setValue (1, 100);
it.setValue (0, 10);
it.setValue (2, 200);
it.display ();
return 0;
}
Sample output:
-1
second
Integer variable table: [index | variable name | initial value]
0 first 10
1 second 100
2 third 200
3 fourth 0
4 fifth 0
...Program finished with exit code 0