What's new

Closed Help mga ka ph...

Status
Not open for further replies.

Jbl00

Addict
Joined
Jun 21, 2018
Posts
30
Reaction
2
Points
71
paano po ba gumawa ng program sa c++ na magconvert ng number to number word. Hanggang 5,000

Example:
input: 505
Output: five hundred five

Hindi pa kase ako marunong gumamit ng loop.. Please po patulong...
 
hindi ka matututo kung aasa ka lang "kung" ano lang ang tinuturo sayo ng teacher mo. advance advance dahil hindi lahat ng about sa programming ay tinuturo sa school.
 
Ganito yung concept na ibibigay ko.

Diba kunyare 4890

May method kasi na ginagamit (nakalimutan ko na) paramakuha si 4000
Then si 800
Then si 90

Tapos gawa ka ng txt file at gamit ang parsing eh dun ka kumuha nung kumparison sa for every tens hundreds at thousands mo.

So

If thouVar == 4000
thouWord = Four Thousand

If hundVar == 800
HundWord = etc etc

Tapos concat na lang ng strings .
Sana nakatulong po
 
Ganito yung concept na ibibigay ko.

Diba kunyare 4890

May method kasi na ginagamit (nakalimutan ko na) paramakuha si 4000
Then si 800
Then si 90

Tapos gawa ka ng txt file at gamit ang parsing eh dun ka kumuha nung kumparison sa for every tens hundreds at thousands mo.

So

If thouVar == 4000
thouWord = Four Thousand

If hundVar == 800
HundWord = etc etc

Tapos concat na lang ng strings .
Sana nakatulong po
salamat po sa idea...
 
  • Like
Reactions: Cyy
relatively easy lang ito... may konting trabaho lang ito tapos ang logic ay relatively easy.

- gamit ka ng KEY-VALUE pair array (or MAP)...or parang ganito (1,"one")
- pero hindi mo gagawin lahat from 1 to 5000..
- gagawa ka ng MAP ARRAY based on DIGIT places (like ones, tens, hundreds and thousands)
**parang ganito**
[1,"one", 2,"two" up 9] and call it ONES_ARRAY
- take note, merong mga "unsual" like ten, 11, 12, 13, up to 19...all the "teens"... you can use LOGIC sa mga ito or gawa ka ng MAP array din dahil konti lang naman sila. Konti lang ang mga un-usual. I think nasa range lang ng tens up to 99.

- pagdating sa HUNDREDS and THOUSANDS... puwedeng wala ng MAP array ito... gamitin mo lang LOGIC

***halimbawa***

501 = "five HUNDRED one"
- yung "five" get it from your ONES_ARRAY
- yung HUNDRED...ay logic
- yung "one" get it from your ONES_ARRAY

4302 = "four THOUSAND three HUNDRED two
- yung "four" get it from your ONES_ARRAY
- yung THOUSAND..ay logic
- yung "three" get it from your ONES_ARRAY
- yung HUNDRED..ay logic
- yung "two" get it from your ONES_ARRAY

33 = thirty three
- yung "thirty" is from your TENS_ARRAY (like 10, 20, 30, 40...up to 90)
- yung "three" is from your ONES_ARRAY

233 = two HUNDRED thirty three
- yung two is from your ONES_ARRAY
- HUDRED is logic
- thirty is from your TENS_ARRAY
- three is from your ONES_ARRAY

Kuha mo na? It's array + logic + string concatenation

Parang ganito na yung whole logic niya.
GIVEN: 699
- find out if GIVEN is in the ONES, TENS, HUNDREDS, THOUSANDS
- read FIRST DIGIT ("6"), get word from ONES_ARRAY, concatenate word HUNDRED
- read SECOND DIGIT, skip if zero, get word from ONES-ARRAY, concatenate TY
- read THIRD DIGIT, skip if zero, get word from ONES_ARRAY
- exit


and just to prove my point (na it simplier that you might think). Heto ang initial code ko pang 20 to 99.
TAKE NOTE: initial lang ito at puwedeng may mga errors pa....and in JAVA.

Code:
import java.util.HashMap;
import java.util.Map;

public class MainApp {
    static String oneString = "1,one,2,two,3,three,4,four,5,five,6,six,7,seven,8,eight,9,nine";
    static String tenString = "10,ten,20,twenty,30,thirty,40,forty,50,fifty,60,sixty,70,seventy,80,eighty,90,ninety";
    static Map<Integer,String> onesMap;
    static Map<Integer,String> tensMap;
   
    public static void main(String[] args) {
        createMaps();
        //test number
        //assuming TENS lang muna numbers 20 and up to 99
        //if you try 19 or 15 or 12 - katawa tawa ang lalabas (error)
        int num = 29;
        System.out.println(convertNumberToWords(num));
    }
   
    static String convertNumberToWords(int number) {
        return readFirstDigit(number) + readLastDigit(number);
    }

//READ the leftmost digit
    static String readFirstDigit(int number) {
        String word = ""+number;
        int digit = Integer.parseInt(""+word.charAt(0)) * 10;
        word = tensMap.get(digit);
        return word;
    }
//READ the rightmost digit
    static String readLastDigit(int number) {
        String word = " - ";
        if(number != 0) {
            String numString = ""+ number;
            int digit = Integer.parseInt(""+numString.charAt(1));
            String mapWord = onesMap.get(digit);
            if(mapWord==null) {
                word="";
            } else {
                word = " " + mapWord;
            }
        } 
        return word;
    }
   


  
//mga initialization methods lang mga mga ito
//pang prepare lang ng mga MAP arrays
    static void createMaps() {
        //ones
        onesMap = splitToMap(oneString);
        //tens
        tensMap = splitToMap(tenString);
    }
   
    static String getWord(Map<Integer,String> map, int number) {
        String theWord = "";
        theWord = map.get(number);
        return theWord;
    }
   
    static HashMap<Integer,String> splitToMap(String string) {
        HashMap<Integer,String> theMap = new HashMap<>();
        String[] stringArray = string.split(",");
        for(int i=0; i<stringArray.length; i=i+2) {
            String key = stringArray[i];
            String value = stringArray[i+1];
            theMap.put(Integer.parseInt(key), value);
        }
        return theMap;
    }
   
}
 
Last edited:
relatively easy lang ito... may konting trabaho lang ito tapos ang logic ay relatively easy.

- gamit ka ng KEY-VALUE pair array (or MAP)...or parang ganito (1,"one")
- pero hindi mo gagawin lahat from 1 to 5000..
- gagawa ka ng MAP ARRAY based on DIGIT places (like ones, tens, hundreds and thousands)
**parang ganito**
[1,"one", 2,"two" up 9] and call it ONES_ARRAY
- take note, merong mga "unsual" like ten, 11, 12, 13, up to 19...all the "teens"... you can use LOGIC sa mga ito or gawa ka ng MAP array din dahil konti lang naman sila. Konti lang ang mga un-usual. I think nasa range lang ng tens up to 99.

- pagdating sa HUNDREDS and THOUSANDS... puwedeng wala ng MAP array ito... gamitin mo lang LOGIC

***halimbawa***

501 = "five HUNDRED one"
- yung "five" get it from your ONES_ARRAY
- yung HUNDRED...ay logic
- yung "one" get it from your ONES_ARRAY

4302 = "four THOUSAND three HUNDRED two
- yung "four" get it from your ONES_ARRAY
- yung THOUSAND..ay logic
- yung "three" get it from your ONES_ARRAY
- yung HUNDRED..ay logic
- yung "two" get it from your ONES_ARRAY

33 = thirty three
- yung "thirty" is from your TENS_ARRAY (like 10, 20, 30, 40...up to 90)
- yung "three" is from your ONES_ARRAY

233 = two HUNDRED thirty three
- yung two is from your ONES_ARRAY
- HUDRED is logic
- thirty is from your TENS_ARRAY
- three is from your ONES_ARRAY

Kuha mo na? It's array + logic + string concatenation

Parang ganito na yung whole logic niya.
GIVEN: 699
- find out if GIVEN is in the ONES, TENS, HUNDREDS, THOUSANDS
- read FIRST DIGIT ("6"), get word from ONES_ARRAY, concatenate word HUNDRED
- read SECOND DIGIT, skip if zero, get word from ONES-ARRAY, concatenate TY
- read THIRD DIGIT, skip if zero, get word from ONES_ARRAY
- exit


and just to prove my point (na it simplier that you might think). Heto ang initial code ko pang 20 to 99.
TAKE NOTE: initial lang ito at puwedeng may mga errors pa....and in JAVA.

Code:
import java.util.HashMap;
import java.util.Map;

public class MainApp {
    static String oneString = "1,one,2,two,3,three,4,four,5,five,6,six,7,seven,8,eight,9,nine";
    static String tenString = "10,ten,20,twenty,30,thirty,40,forty,50,fifty,60,sixty,70,seventy,80,eighty,90,ninety";
    static Map<Integer,String> onesMap;
    static Map<Integer,String> tensMap;
 
    public static void main(String[] args) {
        createMaps();
        //test number
        //assuming TENS lang muna numbers 20 and up to 99
        //if you try 19 or 15 or 12 - katawa tawa ang lalabas (error)
        int num = 29;
        System.out.println(convertNumberToWords(num));
    }
 
    static String convertNumberToWords(int number) {
        return readFirstDigit(number) + readLastDigit(number);
    }

//READ the leftmost digit
    static String readFirstDigit(int number) {
        String word = ""+number;
        int digit = Integer.parseInt(""+word.charAt(0)) * 10;
        word = tensMap.get(digit);
        return word;
    }
//READ the rightmost digit
    static String readLastDigit(int number) {
        String word = " - ";
        if(number != 0) {
            String numString = ""+ number;
            int digit = Integer.parseInt(""+numString.charAt(1));
            String mapWord = onesMap.get(digit);
            if(mapWord==null) {
                word="";
            } else {
                word = " " + mapWord;
            }
        }
        return word;
    }
 


 
//mga initialization methods lang mga mga ito
//pang prepare lang ng mga MAP arrays
    static void createMaps() {
        //ones
        onesMap = splitToMap(oneString);
        //tens
        tensMap = splitToMap(tenString);
    }
 
    static String getWord(Map<Integer,String> map, int number) {
        String theWord = "";
        theWord = map.get(number);
        return theWord;
    }
 
    static HashMap<Integer,String> splitToMap(String string) {
        HashMap<Integer,String> theMap = new HashMap<>();
        String[] stringArray = string.split(",");
        for(int i=0; i<stringArray.length; i=i+2) {
            String key = stringArray[i];
            String value = stringArray[i+1];
            theMap.put(Integer.parseInt(key), value);
        }
        return theMap;
    }
 
}
relatively easy lang ito... may konting trabaho lang ito tapos ang logic ay relatively easy.

- gamit ka ng KEY-VALUE pair array (or MAP)...or parang ganito (1,"one")
- pero hindi mo gagawin lahat from 1 to 5000..
- gagawa ka ng MAP ARRAY based on DIGIT places (like ones, tens, hundreds and thousands)
**parang ganito**
[1,"one", 2,"two" up 9] and call it ONES_ARRAY
- take note, merong mga "unsual" like ten, 11, 12, 13, up to 19...all the "teens"... you can use LOGIC sa mga ito or gawa ka ng MAP array din dahil konti lang naman sila. Konti lang ang mga un-usual. I think nasa range lang ng tens up to 99.

- pagdating sa HUNDREDS and THOUSANDS... puwedeng wala ng MAP array ito... gamitin mo lang LOGIC

***halimbawa***

501 = "five HUNDRED one"
- yung "five" get it from your ONES_ARRAY
- yung HUNDRED...ay logic
- yung "one" get it from your ONES_ARRAY

4302 = "four THOUSAND three HUNDRED two
- yung "four" get it from your ONES_ARRAY
- yung THOUSAND..ay logic
- yung "three" get it from your ONES_ARRAY
- yung HUNDRED..ay logic
- yung "two" get it from your ONES_ARRAY

33 = thirty three
- yung "thirty" is from your TENS_ARRAY (like 10, 20, 30, 40...up to 90)
- yung "three" is from your ONES_ARRAY

233 = two HUNDRED thirty three
- yung two is from your ONES_ARRAY
- HUDRED is logic
- thirty is from your TENS_ARRAY
- three is from your ONES_ARRAY

Kuha mo na? It's array + logic + string concatenation

Parang ganito na yung whole logic niya.
GIVEN: 699
- find out if GIVEN is in the ONES, TENS, HUNDREDS, THOUSANDS
- read FIRST DIGIT ("6"), get word from ONES_ARRAY, concatenate word HUNDRED
- read SECOND DIGIT, skip if zero, get word from ONES-ARRAY, concatenate TY
- read THIRD DIGIT, skip if zero, get word from ONES_ARRAY
- exit


and just to prove my point (na it simplier that you might think). Heto ang initial code ko pang 20 to 99.
TAKE NOTE: initial lang ito at puwedeng may mga errors pa....and in JAVA.

Code:
import java.util.HashMap;
import java.util.Map;

public class MainApp {
    static String oneString = "1,one,2,two,3,three,4,four,5,five,6,six,7,seven,8,eight,9,nine";
    static String tenString = "10,ten,20,twenty,30,thirty,40,forty,50,fifty,60,sixty,70,seventy,80,eighty,90,ninety";
    static Map<Integer,String> onesMap;
    static Map<Integer,String> tensMap;
  
    public static void main(String[] args) {
        createMaps();
        //test number
        //assuming TENS lang muna numbers 20 and up to 99
        //if you try 19 or 15 or 12 - katawa tawa ang lalabas (error)
        int num = 29;
        System.out.println(convertNumberToWords(num));
    }
  
    static String convertNumberToWords(int number) {
        return readFirstDigit(number) + readLastDigit(number);
    }

//READ the leftmost digit
    static String readFirstDigit(int number) {
        String word = ""+number;
        int digit = Integer.parseInt(""+word.charAt(0)) * 10;
        word = tensMap.get(digit);
        return word;
    }
//READ the rightmost digit
    static String readLastDigit(int number) {
        String word = " - ";
        if(number != 0) {
            String numString = ""+ number;
            int digit = Integer.parseInt(""+numString.charAt(1));
            String mapWord = onesMap.get(digit);
            if(mapWord==null) {
                word="";
            } else {
                word = " " + mapWord;
            }
        }
        return word;
    }
  


 
//mga initialization methods lang mga mga ito
//pang prepare lang ng mga MAP arrays
    static void createMaps() {
        //ones
        onesMap = splitToMap(oneString);
        //tens
        tensMap = splitToMap(tenString);
    }
  
    static String getWord(Map<Integer,String> map, int number) {
        String theWord = "";
        theWord = map.get(number);
        return theWord;
    }
  
    static HashMap<Integer,String> splitToMap(String string) {
        HashMap<Integer,String> theMap = new HashMap<>();
        String[] stringArray = string.split(",");
        for(int i=0; i<stringArray.length; i=i+2) {
            String key = stringArray[i];
            String value = stringArray[i+1];
            theMap.put(Integer.parseInt(key), value);
        }
        return theMap;
    }
  
}
salamat lodi... Nadagdagan na naman ang idea ko.. Hahanapin ko muna sinasabe mong map ray.. Wla pa kse kami dyan na topic.. Hehhee
 
Status
Not open for further replies.

Similar threads

Back
Top