Apr 21, 2012 at 5:00am UTC
Hey everyone I'm having some trouble with my c++ program I'm trying to write a program that will use a calculator to calculate numerals but instead of inputing numerals you input words as in one + eight not 1 + 8. So far i have got it working except for when i try to add numbers like twenty_two + sixty_one
It will except twenty + two or thirty + six. The calculator works fine when i debug i get the right numerals except its not changing over to the right word number.
If someone could help me finish this it would be greatly greatly appreciated because I'm going bald in my early twenties
-
-
Wordnum.h
/*
File: WordNum.h header file
Title: Number Words Assignment
Author: Benjamin s Wells
Fan: 2101447
Email: well0143@flinders.edu.au
*/
#ifndef WORDNUM_H
#define WORDNUM_H
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;
class Wordnum {
private:
int value_;
public:
Wordnum():value_(0){};
Wordnum(int n):value_(n) {};
Wordnum(const string& in){}
bool is_a_number(const string& str);
void set_value(const string& str);
int convert_string_to_number(const string& str);
string convert_number_to_word_thousands(int number) const;
string convert_to_string() const;
friend Wordnum operator+ (const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ + n2.value_);
}
friend Wordnum operator- (const Wordnum& ns, const Wordnum& ns2){
return Wordnum(ns.value_ - ns2.value_);
}
friend Wordnum operator* (const Wordnum& nm, const Wordnum& nm2){
return Wordnum(nm.value_ * nm2.value_);
}
friend Wordnum operator/ (const Wordnum& nd, const Wordnum& nd2) {
return Wordnum(nd.value_ / nd2.value_);
}
friend ostream& operator<< (ostream& o, const Wordnum& n){
o << n.convert_to_string();
return o;
}
friend istream& operator>> (istream& i, Wordnum& n){
string str_value;
i>>str_value; n.set_value(str_value);
return i;
}
};
#endif
WordNum.cpp
//
// WordNum.cpp
// Assignment Number Words
//
// Created by Stuart Wells on 20/04/12.
// Copyright (c) 2012 Scotch College. All rights reserved.
#include <iostream>
#include <stdio.h>
#include <cctype>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include "WordNum.h"
bool Wordnum::is_a_number(const string &str){
string::const_iterator it;
bool is_a_number = true;
for (it = str.begin(); it < str.end(); it++)
if (*it < '0' or *it > '9') {
is_a_number = false;
break;
}
return is_a_number;
}
void Wordnum::set_value(const string& str){
if (is_a_number(str)){
istringstream iss (str, istringstream::in);
iss >> value_;
}
else {
value_ = convert_string_to_number(str);
}
}
int Wordnum::convert_string_to_number(const string& str){
if (str == "one"){
return 1;}
else if (str == "two"){
return 2;}
else if (str == "three"){
return 3;}
else if (str == "four"){
return 4;}
else if (str == "five"){
return 5;}
else if (str == "six"){
return 6;}
else if (str == "seven"){
return 7;}
else if (str == "eight"){
return 8;}
else if (str == "nine"){
return 9;}
else if (str == "ten"){
return 10;}
else if (str == "eleven"){
return 11;}
else if (str == "twelve"){
return 12;}
else if (str == "thirteen"){
return 13;}
else if (str == "fourteen"){
return 14;}
else if (str == "fifteen"){
return 15;}
else if (str == "sixteen"){
return 16;}
else if (str == "seventeen"){
return 17;}
else if (str == "eighteen"){
return 18;}
else if (str == "nineteen"){
return 19;}
else if (str == "twenty"){
return 20;}
else if (str == "thirty"){
return 30;}
else if (str == "fourty"){
return 40;}
else if (str == "fifty"){
return 50;}
else if (str == "sixty"){
return 60;}
else if (str == "seventy"){
return 70;}
else if (str == "eighty"){
return 80;}
else if (str == "ninty"){
return 90;}
else if (str == "one_hundred"){
return 100;}
else if (str == "two_hundred"){
return 200;}
else if (str == "three_hundred"){
return 300;}
else if (str == "four_hundred"){
return 400;}
else if (str == "five_hundred"){
return 500;}
else if (str == "six_hundred"){
return 600;}
else if (str == "seven_hundred"){
return 700;}
else if (str == "eight_hundred"){
return 800;}
else if (str == "nine_hundred"){
return 900;}
else if (str == "one_thousand"){
return 1000;}
}
string Wordnum::convert_number_to_word_thousands(int number) const {
//number must be < 1000
string base[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string tenth[] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty"};
string hundreth[] = {"", "one_hundred", "two_hundred", "three_hundred", "four_hundred", "five_hundred", "six_hundred", "seven_hundred", "eight_hundred", "nine_hundred"};
div_t divresult;
divresult = div (number,100);
string hundred_str;
//div(125,100) -> quot = 1, rem 25
if (divresult.quot > 0) {
hundred_str = hundreth[divresult.quot];
if (divresult.rem == 0) {
return hundred_str;
}
}
if (divresult.rem < 20) {
if (hundred_str.length() > 0) {
return hundred_str + "_and_" + base[divresult.rem];
} else {
return base[divresult.rem];
}
//rem >= 20
} else {
//div(45,10) -> quot=4, rem 5
div_t divresult2 = div(divresult.rem, 10);
cout << "proof of div " "2 q " << divresult2.quot << ", r " << divresult2.rem << endl;
if (divresult2.rem == 0) { // div(40,10) -> quot = 4, rem = 0
if (hundred_str.length() > 0) {
return hundred_str + "_and_" + tenth[divresult2.rem];
} else {
return tenth[divresult2.rem];
}
} else {
if (hundred_str.length() > 0) {
return hundred_str + "_and_" + tenth[divresult2.quot] + "_" + base[divresult2.rem];
} else {
return tenth[divresult2.quot] + "_" + base[divresult2.rem];
}
}
}
}
string Wordnum::convert_to_string() const {
if (value_ < 1000) {
return convert_number_to_word_thousands(value_);
} else {
"fault";
}
}
main.cpp
/* File: main.cpp main file
Title: Number Words Assignment
Author: Benjamin s Wells
Fan: 2101447
Email: well0143@flinders.edu.au
*/
#include <iostream>
#include "WordNum.h"
using namespace std;
int main () {
Wordnum n1;
Wordnum n2;
char op;
while (cin >> n1 >> op >> n2){
switch (op) {
case '+': cout << n1 + n2 << endl; break;
case '-': cout << n1 - n2 << endl; break;
case '*': cout << n1 * n2 << endl; break;
case '/': cout << n1 / n2 << endl; break;
}
}
}
and this is the function i want to replace the if and else if statements int WordNum.cpp
:)