Thread: calculator

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    calculator

    how can I program a caltulator which does process between 2,3,4,5,6,7,8,9,10 based numbers.

    for example asuume that these are two based numbers (111)+(101)=1000

    I want to build a program which can do these procces.Please help me.

  2. #2
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    first make a function that can convert a base 10 number to a base 2, 3, 4, 5 etc.

    and vice versa

    too late to remember the method ;0

    so basically you do the math w/ base 10
    so

    111 (base2) + 101 (base2) is really
    7 + 5
    so a computer can understand that

    do the math

    convert back to base2


    Making the function is the hard part. I forgot how to convert from anything other than base 2 and 8 =/

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You are asking about the algorithm or about the coding, I mean do you know how to convert between bases or you not?
    What exactly do you need to know.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    both algoritihm and coding

  5. #5
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    here

    ok i can give you algorithy for base 10 to base 2

    take a base 10 number ie 15
    module (% operator) by 2
    that is your remainer, which is the least significant digit of the base 2 number
    then divide by 2, which gives you 7 (least it should w/ int)
    module again by 2
    that remainer is your next least sig. digit
    divide by 2
    repeate till your division = 0

    *edit*
    click on the 'edit' button to visualize how things should line up properly
    *edit*
    *edit*
    mebe this helps
    *edit*
    3 edits' later, i give up
    *edit*

    so for 15:
    15%2 =_____1__| least sig. digit
    15/2 = 7 _______|
    7%2 = _____ 1__|
    7/2 = 3 ________|
    3%2 = _____1__|
    3/2 = 1 ________|
    1%2 = _____1__| most sig. digit
    1/2 = 0 end_____V
    =============
    1111 = 15 (base 2)


    btw, search this stuff on the web, i'm sure they're a zillion resources. Keyword = converting base or converting base 10, 2
    Last edited by Diamonds; 10-31-2002 at 07:31 AM.

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    If you don't know how to do it mathimaticaly, my advice to you is to learn that first then try to write the code.

    the example of converting from base 10 to base 2, will work on all conversion operations from a base to a smaller base.

    Try searching the web or writting the code for the base 10 to 2 converter and see what you will get.

  7. #7
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    Originally posted by ammar


    the example of converting from base 10 to base 2, will work on all conversion operations from a base to a smaller base.

    oh yeah haha, forgot

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Here's a function that converts a number of any base (1-36) in string form to an integer.
    Code:
            //String to integer
            int P__StrToNum(const char* str, int base)                
            {
                unsigned int digit, number = 0;
                while (*str) {
                    if (isdigit(*str)) digit = *str - '0';
                    else if (islower(*str)) digit = *str - 'a' + 10;
                    else digit = *str - 'A' + 10;
                    number = number * base + digit;
                    str++; }
                return(number);
            }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    This I found on this site a while ago............


    Should help
    Such is life.

  10. #10
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    Oh, and this.......


    Not my work, but look and you will find..
    Such is life.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    thank you very much.

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    but my problem is a little more complicated look at this;
    111+101=1000 in a 2 based additon process.
    how can I make a calculator which does a process like this.
    I want a calculator which makes 4 process between based numbers.

  13. #13
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    after the numbers (111, 102, etc..) are inputted and stored in an integer variable, just convert them to base 10 then do the calculation then convert then back to base 2 so its outputted the way you inputted them...

    did you get all that?

  14. #14
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    Dont think it was understood .





    But all the answers are in my two attachments. Use Baseconv.zip to change to base 10 (if you must), do the calculation, and change back- Then use the code in calc program.


    Sorted
    Such is life.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM