Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: need C# advice

  1. #1

    Default need C# advice


    guyz d activity is to let the user input a number and the program should output a string version of it...
    i already got it but wondering if this can be shorter...cause its a very long code...tnx for those who would reply

    string[] ones = new string[]{"one","two","three","four","five","six","seven"," eight","nine"};
    string[] tens = new string[]{"ten","twenty","thirty","fourty","fifty","sixty", "seventy","eighty","ninety"};

    string h,o,t;
    Console.Write("Enter Number: ");
    string num = Console.ReadLine;
    if (num.length==3)
    {

    for(y=0;y<=9;y++)
    {
    if(num[0]==y)
    {
    h= ones[y] + " " + "hundred";
    }

    if(num[1]==y)
    {
    t = tens [y] + " ";
    }
    else
    {
    t= "";
    }
    if(num[2]==y)
    {
    o= ones[y];
    }

    }

    }
    if (num.length==2)
    {

    for(y=0;y<=9;y++)
    {


    if(num[0]==y)
    {
    t = tens [y] + " ";
    }
    else
    {
    t= "";
    }
    if(num[1] == y)
    {
    o= ones[y];
    }

    }

    }
    else
    {

    for(y=0;y<=9;y++)
    {



    if(num[0] == y)
    {
    o= ones[y];
    }

    }

    }
    Console.Write(h + " " + t + " " + o);
    Console.ReadLine();

    P.S. Before receiving all the flames i would just like to say i did my best..its the shortest way i can think of...but just incase there is a better way it would be nice to know so ill learn more

  2. #2
    kalas kaau ka ug loop brad.. usba na na one loop ra ug switch lng gamita para dali masabtan imong code.
    use prepend pud para dili na ka 3 variables anang h o t nimo

  3. #3
    string numstring = "";
    int tempnum;

    if (num.length == 3)
    {
    tempnum = (int) num[0];
    if (tempnum != 0)
    {
    numstring = ones[tempnum - 1] + " " + "hundred ";
    }
    tempnum = (int) num[1];
    if (tempnum != 0)
    {
    numstring += tens[tempnum - 1] + " ";
    }
    tempnum = (int) num[2];
    if (tempnum != 0)
    {
    numstring = ones[tempnum - 1];
    }
    }
    else if (num.length == 2)
    {
    tempnum = (int) num[0];
    if (tempnum != 0)
    {
    numstring += tens[tempnum - 1] + " ";
    }
    tempnum = (int) num[1];
    if (tempnum != 0)
    {
    numstring = ones[tempnum - 1];
    }
    }
    else if (num.length == 1)
    {
    tempnum = (int) num[0];
    if (tempnum != 0)
    {
    numstring = ones[tempnum - 1];
    }
    }

    double check lng kung naa sayop..

  4. #4
    bosing pang 3 digit number ra ni xa??

  5. #5
    Got this one from google a while back ... much longer but much more complete in providing english translation of a number:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace test1
    {
        class Program
        {
            public static string IntegerToWords(long inputNum)
            {
                int dig1, dig2, dig3, level = 0, lasttwo, threeDigits;
    
                string retval = "";
                string x = "";
                string[] ones = {
                                    "zero",
                                    "one",
                                    "two",
                                    "three",
                                    "four",
                                    "five",
                                    "six",
                                    "seven",
                                    "eight",
                                    "nine",
                                    "ten",
                                    "eleven",
                                    "twelve",
                                    "thirteen",
                                    "fourteen",
                                    "fifteen",
                                    "sixteen",
                                    "seventeen",
                                    "eighteen",
                                    "nineteen"
                                };
                string[] tens = {
                                    "zero",
                                    "ten",
                                    "twenty",
                                    "thirty",
                                    "forty",
                                    "fifty",
                                    "sixty",
                                    "seventy",
                                    "eighty",
                                    "ninety"
                                };
                string[] thou = {
                                    "",
                                    "thousand",
                                    "million",
                                    "billion",
                                    "trillion",
                                    "quadrillion",
                                    "quintillion"
                                };
    
                bool isNegative = false;
                if (inputNum < 0)
                {
                    isNegative = true;
                    inputNum *= -1;
                }
    
                if (inputNum == 0)
                    return ("zero");
    
                string s = inputNum.ToString();
    
                while (s.Length > 0)
                {
                    // Get the three rightmost characters
                    x = (s.Length < 3) ? s : s.Substring(s.Length - 3, 3);
    
                    // Separate the three digits
                    threeDigits = int.Parse(x);
                    lasttwo = threeDigits%100;
                    dig1 = threeDigits/100;
                    dig2 = lasttwo/10;
                    dig3 = (threeDigits%10);
    
                    // append a "thousand" where appropriate
                    if (level > 0 && dig1 + dig2 + dig3 > 0)
                    {
                        retval = thou[level] + " " + retval;
                        retval = retval.Trim();
                    }
    
                    // check that the last two digits is not a zero
                    if (lasttwo > 0)
                    {
                        if (lasttwo < 20) // if less than 20, use "ones" only
                            retval = ones[lasttwo] + " " + retval;
                        else // otherwise, use both "tens" and "ones" array
                            retval = tens[dig2] + " " + ones[dig3] + " " + retval;
                    }
    
                    // if a hundreds part is there, translate it
                    if (dig1 > 0)
                        retval = ones[dig1] + " hundred " + retval;
    
                    s = (s.Length - 3) > 0 ? s.Substring(0, s.Length - 3) : "";
                    level++;
                }
    
                while (retval.IndexOf("  ") > 0)
                    retval = retval.Replace("  ", " ");
    
                retval = retval.Trim();
    
                if (isNegative)
                    retval = "negative " + retval;
    
                return (retval);
            }
    
            static void Main(string[] args)
            {
                int inputNum;
    
                Console.WriteLine("Enter a number: ");
                string inputstr = Console.ReadLine();
                inputNum = Convert.ToInt32(inputstr);
                string englishTranslation = IntegerToWords(inputNum);
                Console.WriteLine("You entered: [{0}]",englishTranslation);
    
    
                Console.ReadLine();
            }
        }
    }
    Able to give sample run like:

    Code:
    Enter a number:
    1234
    You entered: [one thousand two hundred thirty four]
    Code:
    Enter a number:
    602
    You entered: [six hundred two]
    Code:
    Enter a number:
    -12678902
    You entered: [negative twelve million six hundred seventy eight thousand nine hundred two]
    Last edited by kolz; 07-02-2009 at 01:34 AM.

  6. #6
    yup 3 digits ra bai...nindota ana imo sir kolz oi...libog pa lang for me kay dont know what indexof() method is and some of the other codes...but studyuan nako na unya

    sir bluedes sakto bitaw na imo..damn nag lisod lisod man ko for loop...reallly need to learn alot pa

    question lang sir...is "(int)num[0]" similar to "int.parse(num[0])"?

  7. #7
    string[] ones = new string[]{"one","two","three","four","five","six","seven "," eight","nine"};
    string[] tens = new string[]{"ten","twenty","thirty","fourty","fifty","sixt y", "seventy","eighty","ninety"};
    imo nalimtan ang eleven to nineteen

  8. #8
    Try kuno gamit ug hashtable.

  9. #9
    Quote Originally Posted by moz_k2 View Post
    imo nalimtan ang eleven to nineteen
    ahh bitaw noh...damn ni samot ug libog...hehehe

    unsa nang has table bai? search sa nako kay bestfriend google

  10. #10
    para ma shorten ang program bai, then dynamic ang number of digits ma support, maayo gyud mag gamit ug % bai.

  11.    Advertisement

Page 1 of 3 123 LastLast

Similar Threads

 
  1. Need your advice for a wireless router
    By jhnpl in forum Computer Hardware
    Replies: 20
    Last Post: 08-09-2012, 09:40 PM
  2. Why do we have to experience pain? NEED YOUR ADVICE
    By IamTheWind- in forum "Love is..."
    Replies: 88
    Last Post: 04-04-2012, 11:03 PM
  3. Hard Disk Probs..need an advice of what to do..
    By benz_jie2002 in forum Computer Hardware
    Replies: 17
    Last Post: 01-23-2007, 10:24 PM
  4. dvd r/w : unsa-on pag burn ug dvd's : need ur advice
    By rasta_dubbed in forum Computer Hardware
    Replies: 14
    Last Post: 12-09-2005, 06:31 PM
  5. need your advice on lcd monitors...
    By for_real in forum Computer Hardware
    Replies: 9
    Last Post: 09-12-2005, 02:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top