Arithmetic word number conversion problem

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Arithmetic word number conversion problem

Postby anonymous123 » Mon Apr 16, 2012 8:26 pm

Need help, trying to perform arithmetic operations using words, so far i've got to the stage where if the user enters a number in words with the operation type eg(one + six), the result is printed out as an int. But i am unable to convert this int back to a word. eg one + six = seven.
Also is there any way of putting the number to word conversion into the Calculator.h instead of main.cpp??

Please HELP!!

Code: Select all
#include <string>

#ifndef CALCULATOR_H
#define   CALCULATOR_H

class Calculator {
//    float a, b;
public:
    int add(int, int);
    int subtract(int, int);
    int multiply(int, int);
    int divide(int, int);
private:
    int n1;
    int n2;
};
#endif   /* CALCULATOR_H */


#include "Calculator.h"

int Calculator::add(int n1, int n2) {
    return (n1 + n2);
}
int Calculator::subtract(int n1, int n2) {
    return (n1 - n2);
}
int Calculator::divide(int n1, int n2) {
    return (n1 / n2);
}
int Calculator::multiply(int n1, int n2) {
    return (n1 * n2);


int main() {
    int length;
    Calculator calc;
    string word1, word2;
    char arithmetic;
    string One[10] = {"zero", "one", "two", "three", "four", "five", "six",
        "seven", "eight", "nine"};
   
   
    while (cin >> word1 >> arithmetic >> word2) {
       
        int n1 = 0, n2 = 0;
       
        if(word1.length()>word2.length()){    //Determine the longest length
           length = word1.length() ;
        }
        else
           length = word2.length();

        for (int x = 0; x < length; x++) {     //input to lower case
            word1[x] = std::tolower(word1[x]);
            word2[x] = std::tolower(word2[x]);
        }
        for (int i = 9; i > 0; i--) {        //checks to see if input matches
            if (word1.find(One[i]) == 0) {   //the array if so, prints the
                n1 = i;                      //position
                word1.erase(0, One[i].length());
                break;
            }
        }
        for (int i = 9; i > 0; i--) {
            if (word2.find(One[i]) == 0) {
                n2 = i;
                word2.erase(0, One[i].length());
                break;
            }
        }

        switch (arithmetic) {
            case '+':
                cout << calc.add(n1, n2) << endl;
                break;
            case '-':
                cout << calc.subtract(n1, n2) << endl;
                break;
            case '*':
                cout << calc.multiply(n1, n2) << endl;
                break;
            case '/':
                cout << calc.divide(n1, n2) << endl;
                break;
            default:
                cout << 0 << endl;
        }
    }
 
}

anonymous123
 
Posts: 1
Joined: Mon Apr 16, 2012 8:19 pm

Re: Arithmetic word number conversion problem

Postby exomo » Tue Apr 17, 2012 9:24 am

Finding the string for a number is the easier conversion. Just store your result in a variable (say result) and use One[result] to find the corresponding string.
Of course you can do the word<->number conversion in the Calculator class. You should not add the implementation to the .h file, but you can add a method prototype to the class definition and the implementation of it in the Calculator.cpp file. Pass one type as a parameter and return the other type.

Code: Select all
if(word1.length()>word2.length()){    //Determine the longest length
           length = word1.length() ;
        }
        else
           length = word2.length();

        for (int x = 0; x < length; x++) {     //input to lower case
            word1[x] = std::tolower(word1[x]);
            word2[x] = std::tolower(word2[x]);
        }
That is not a good idea. You can't use the length of the longer word for both words. Do the conversion for every word on its own.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 4 guests