MiniComputer.cpp 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "MiniComputer.hpp"
#include <string>
#include <iostream>
#include <unistd.h>

MiniComputer::MiniComputer(std::string name)
{
    setComputerName(name);
}

std::string
MiniComputer::getComputerName()
{
    return computerName_;
}

void
MiniComputer::setComputerName(std::string name)
{
    if (name.length() > 25) {
        computerName_ = name.substr(0, 25);
        return;
23
    }
24 25 26 27 28 29 30 31

    computerName_ = name;
}

int
MiniComputer::run()
{
    std::cout << getComputerName() << std::endl; /// getter
32
start:
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 125 126 127 128 129 130
    showMainMenu();
    int command = getMainCommand();
    if (0 == command) { /// exit command
        return 0;
    }
    int returnValue = executeCommand(command);
    if (0 == returnValue) { /// everything is ok
        goto start;
    }
    return returnValue;
}

void
MiniComputer::showMainMenu()
{
    if(::isatty(STDIN_FILENO)) {
        std::cout << "Command Set\n"
                  << "0 – exit\n"
                  << "1 – load\n"
                  << "2 – store\n"
                  << "3 – print\n"
                  << "4 – add\n\n"
                  << "> Command: ";
    }
}

int
MiniComputer::getMainCommand()
{
    int command;
    std::cin >> command;
    return command;
}

int
MiniComputer::executeCommand(int command)
{
    if (1 == command) { /// load command
        return executeLoadCommand();
    }
    if (2 == command) { /// store command
        return executeStoreCommand();
    }
    if (3 == command) { /// print command
        return executePrintCommand();
    }
    if (4 == command) { /// add command
        return executeAddCommand();
    }
    /// error case
    std::cerr << "Error 1: Command not found!" << std::endl;
    return 1;
}

int
MiniComputer::loadRegister(std::string registerName)
{
    if (::isatty(STDIN_FILENO)) {
        std::cout << "Load into " << registerName << "\n"
                  << "Input the value to load into register " << registerName << ".\n"
                  << registerName << ": ";
    }
    int number;
    std::cin >> number;
    std::cout << registerName << " = " << number << "\n" << std::endl;
    return number;
}

bool
MiniComputer::isRegisterError(int registerNumber)
{
    if (registerNumber > 3) {
        std::cerr << "Error 2: Register not found" << std::endl;
        return true;
    }
    if (registerNumber < 0) {
        std::cerr << "Error 2: Register not found" << std::endl;
        return true;
    }
    return false;
}

int
MiniComputer::executeLoadCommand()
{
    if (::isatty(STDIN_FILENO)) {
        std::cout << "Load (into one of the following registers)\n"
                  << "0 - a\n"
                  << "1 - b\n"
                  << "2 - c\n"
                  << "3 - d\n\n"
                  << "> Register: ";
    }
    int registerNumber;
    std::cin >> registerNumber;
    if (isRegisterError(registerNumber)) {
        return 2;
    }
131
    std::string registerName = getRegisterName(registerNumber);
132 133
    int registerValue = loadRegister(registerName);
    setRegisterValue(registerNumber, registerValue);
134 135 136 137 138 139 140 141 142 143 144
    return 0;
}

int
MiniComputer::executeStoreCommand()
{
    std::cerr << "Error 127: The store command is not implemented yet" << std::endl;
    return 127;
}

int
145
MiniComputer::getRegisterValue(int registerNumber)
146 147
{
    if (0 == registerNumber) {
148
        return registerA_;
149 150
    }
    if (1 == registerNumber) {
151
        return registerB_;
152 153
    }
    if (2 == registerNumber) {
154
        return registerC_;
155 156
    }
    if (3 == registerNumber) {
157
        return registerD_;
158
    }
159 160
    std::cerr << "Assert 1: Invalid register number" << std::endl;
    return 0;
161 162 163
}

std::string
164
MiniComputer::getRegisterName(int registerNumber)
165 166
{
    if (0 == registerNumber) {
167
        return "a";
168 169
    }
    if (1 == registerNumber) {
170
        return "b";
171 172
    }
    if (2 == registerNumber) {
173
        return "c";
174 175
    }
    if (3 == registerNumber) {
176
        return "d";
177
    }
178 179
    std::cerr << "Assert 1: Invalid register number" << std::endl;
    return "";
180 181
}

182 183
void
MiniComputer::setRegisterValue(int registerNumber, int registerValue)
184 185
{
    if (0 == registerNumber) {
186
        registerA_ = registerValue;
187
    } else if (1 == registerNumber) {
188
        registerB_ = registerValue;
189
    } else if (2 == registerNumber) {
190
        registerC_ = registerValue;
191
    } else if (3 == registerNumber) {
192
        registerD_ = registerValue;
193 194
    } else {
        std::cerr << "Assert 1: Invalid register number" << std::endl;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }
}

int
MiniComputer::executeAddCommand()
{
    if (::isatty(STDIN_FILENO)) {
        std::cout << "Add (Register 1 + Register 2 = Register 3)\n"
                  << "0 - a\n"
                  << "1 - b\n"
                  << "2 - c\n"
                  << "3 - d\n\n"
                  << "> Register1: ";
    }

    int registerNumber1;
    std::cin >> registerNumber1;
    if (isRegisterError(registerNumber1)) {
        return 2;
    }
215 216
    int inputRegister1 = getRegisterValue(registerNumber1);
    std::string registerLetter1 = getRegisterName(registerNumber1);
217 218 219 220 221 222 223 224 225

    int registerNumber2;
    if (::isatty(STDIN_FILENO)) {
        std::cout << "> Register2: ";
    }
    std::cin >> registerNumber2;
    if (isRegisterError(registerNumber2)) {
        return 2;
    }
226 227
    int inputRegister2 = getRegisterValue(registerNumber2);
    std::string registerLetter2 = getRegisterName(registerNumber2);
228 229 230 231 232 233 234 235 236

    int registerNumber3;
    if (::isatty(STDIN_FILENO)) {
        std::cout << "> Register3: ";
    }
    std::cin >> registerNumber3;
    if (isRegisterError(registerNumber3)) {
        return 2;
    }
237 238 239
    int inputRegister3 = inputRegister1 + inputRegister2;
    setRegisterValue(registerNumber3, inputRegister3);
    std::string registerLetter3 = getRegisterName(registerNumber3);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    std::cout << registerLetter3 << " = " << registerLetter1 << " + " << registerLetter2 << " = " << inputRegister1 << " + " << inputRegister2 << " = " << inputRegister3 << "\n" << std::endl;
    return 0;
}

int
MiniComputer::executePrintCommand()
{
    int commandRegister;
    if (::isatty(STDIN_FILENO)) {
        std::cout << "Print (Register or String)\n"
                  << "0 – Register\n"
                  << "N – String Length\n\n"
                  << "> Print: ";
    }
    std::cin >> commandRegister;
    if (0 == commandRegister) {
        if (::isatty(STDIN_FILENO)) {
            std::cout << "Registers\n"
                      << "0 - a\n"
                      << "1 - b\n"
                      << "2 - c\n"
                      << "3 - d\n\n"
                      << "> Register: ";
        }
        int registerNumber;
        std::cin >> registerNumber;
        if (isRegisterError(registerNumber)) {
            return 2;
        }
269 270 271 272
        std::cout << getRegisterValue(registerNumber);
        if (::isatty(STDIN_FILENO)) {
            std::cout << "\n\n";
        }
273
        return 0;
274 275 276 277 278
    }
    if (commandRegister > 0) {
        std::string line;
        if (::isatty(STDIN_FILENO)) {
            std::cout << "> String: ";
279
            std::cin.ignore(commandRegister, '\n');
280 281 282 283 284
        }
        std::getline(std::cin, line);
        if (static_cast<int>(line.length()) > commandRegister) {
            line = line.substr(0, commandRegister);
        }
285
start:
286 287 288 289
        if (static_cast<int>(line.length()) < commandRegister) {
            line += " ";
            goto start;
        }
290 291
        std::cout << line;
        if (::isatty(STDIN_FILENO)) {
Laura's avatar
Laura committed
292
            std::cout << "\n\n";
293
        }
294 295
        return 0;
    }
Laura's avatar
Laura committed
296
    std::cerr << "Error 2: Register not found!" << std::endl;
297 298
    return 2;
}