~learax/csci112-2021-william-culhane

200eb1c46d53a84e7aa222225a64048f1eced711 — William Culhane 3 years ago 20d1686 tonic lab5
lab5: Finish assignment
1 files changed, 205 insertions(+), 7 deletions(-)

M labs/lab5/lab5.cpp
M labs/lab5/lab5.cpp => labs/lab5/lab5.cpp +205 -7
@@ 9,24 9,222 @@ using namespace std;
 * Fri Jun  4 10:46:06 AM MDT 2021
 */

// These are a collection of helper functions that generate strings
// for each location in the number

void handle_thousand(vector<string> *out_vec, char digit) {
  switch (digit) {
  case '1': {
    out_vec->push_back("one thousand");
    break;
  }
  case '2': {
    out_vec->push_back("two thousand");
    break;
  }
  case '3': {
    out_vec->push_back("three thousand");
    break;
  }
  case '4': {
    out_vec->push_back("four thousand");
    break;
  }
  case '5': {
    out_vec->push_back("five thousand");
    break;
  }
  case '6': {
    out_vec->push_back("six thousand");
    break;
  }
  case '7': {
    out_vec->push_back("seven thousand");
    break;
  }
  case '8': {
    out_vec->push_back("eight thousand");
    break;
  }
  case '9': {
    out_vec->push_back("nine thousand");
    break;
  }
  }
}

void handle_hundred(vector<string> *out_vec, char digit) {
  switch (digit) {
  case '1': {
    out_vec->push_back("one hundred");
    break;
  }
  case '2': {
    out_vec->push_back("two hundred");
    break;
  }
  case '3': {
    out_vec->push_back("three hundred");
    break;
  }
  case '4': {
    out_vec->push_back("four hundred");
    break;
  }
  case '5': {
    out_vec->push_back("five hundred");
    break;
  }
  case '6': {
    out_vec->push_back("six hundred");
    break;
  }
  case '7': {
    out_vec->push_back("seven hundred");
    break;
  }
  case '8': {
    out_vec->push_back("eight hundred");
    break;
  }
  case '9': {
    out_vec->push_back("nine hundred");
    break;
  }
  }
}

void handle_ten(vector<string> *out_vec, char digit) {
  switch (digit) {
  case '2': {
    out_vec->push_back("twenty");
    break;
  }
  case '3': {
    out_vec->push_back("thirty");
    break;
  }
  case '4': {
    out_vec->push_back("fourty");
    break;
  }
  case '5': {
    out_vec->push_back("fifty");
    break;
  }
  case '6': {
    out_vec->push_back("sixty");
    break;
  }
  case '7': {
    out_vec->push_back("seventy");
    break;
  }
  case '8': {
    out_vec->push_back("eighty");
    break;
  }
  case '9': {
    out_vec->push_back("ninety");
    break;
  }
  }
}

void handle_single(vector<string> *out_vec, char digit) {
  switch (digit) {
  case '1': {
    out_vec->push_back("one");
    break;
  }
  case '2': {
    out_vec->push_back("two");
    break;
  }
  case '3': {
    out_vec->push_back("three");
    break;
  }
  case '4': {
    out_vec->push_back("four");
    break;
  }
  case '5': {
    out_vec->push_back("five");
    break;
  }
  case '6': {
    out_vec->push_back("six");
    break;
  }
  case '7': {
    out_vec->push_back("seven");
    break;
  }
  case '8': {
    out_vec->push_back("eight");
    break;
  }
  case '9': {
    out_vec->push_back("nine");
    break;
  }
  }
}

int main(int argc, char **argv) {
  // Check for appropriate number of arguments
  if (argc != 2) {
    cerr << "Invalid number of argument\n";
    return 1;
  }

  char* raw_num = argv[1];

  unsigned int raw_num_length;
  char *raw_num = argv[1];
  unsigned int raw_num_length = 0; // Digits in the number

  for (unsigned int i = 0; i < 4; i++) {
    if (raw_num[i] == 0) {
      raw_num_length = i + 1;
  // Find the number of digits in the input number
  for (unsigned int i = 0; i < 5; i++) {
    if (raw_num[i] == '\0') {
      raw_num_length = i;
      break;
    } else if (raw_num[i] == '0') {
      // Handle if a 0 is found
      cerr << "Oops! Entered a 0 in the number\n";
      return 2;
    }
  }

  vector<string> out_vec;

  // Create the output vector, going from the MSD to the LSD
  switch (raw_num_length) {
  case 4:
    handle_thousand(&out_vec, raw_num[raw_num_length - 4]);
  case 3:
    handle_hundred(&out_vec, raw_num[raw_num_length - 3]);
  case 2:
    if (raw_num[raw_num_length - 2] == '1') {
      // Handle if the 10^1 digit is a 1
      cerr << "Oops! Entered a 1 in the tens place\n";
      return 3;
    }
    handle_ten(&out_vec, raw_num[raw_num_length - 2]);
  case 1:
    handle_single(&out_vec, raw_num[raw_num_length - 1]);
    break;
  default:
    cerr << "Unexpected input length\n";
    return 1;
  }

  // Print output and exit with success
  cout << "Number " << raw_num << " is written as";

  for (unsigned int i = 0; i < out_vec.size(); i++) {
    cout << ' ' << out_vec[i];
  }

  cout << raw_num_length;
  cout << '\n';

  return 0;
}