a C++ question

This is a discussion on a C++ question within the C++ Programming forums, part of the General Programming Boards category; I wanna make a program which like..... the user keys in 12345 and when user gives some commands... e.g. left ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    a C++ question

    I wanna make a program which like.....
    the user keys in 12345 and when user gives some commands...
    e.g.
    left shift 1digit -> 23451
    right shift 1digit -> 51234
    left shift 2digit -> 34512
    But doing that without using array...
    So, how can I do that?
    my friend told me to use fflush and use file IO...
    if it's true.... how can I do?? thx.........

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    store the digits in an array as a string. Use a loop and loop through the array swopping the elements however you need.If you need a numeric value rather than a string then atoi() it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    Using a list seems like a possible solution if you can't use an array. Store user input in a string. Create a list using nodes that contain a pointer to the next node and a single char for the data portion of the node. You'll still need to use loops to place the char from the string in the list and to perform the shifting.

  4. #4
    Unregistered
    Guest
    To do it with file I/O you would need to output the data as single char, then use fseek() and associated functions to position the file pointer wherever you want it before reading to or writing from the file. Since each char will take up the same amount of space in the file, it is a strategy that could work, if you are familiar with fseek() and associated functions. Given that working with files is typically slower than reassigning pointes, it seems less appealing than using a list (or an array), however.

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    long rollLeft (long num);
    long rollRight (long num);
    int digits (int num);
    
    void main (void)
    {
       cout << rollRight (12345);
       getch ();
    }
    
    long rollLeft (long num)
    {
       int i;
       long subtractor = 1;
       int size = digits (num);
       int x = 0;
    
       for (i = 0; i < size; i++)
          subtractor *= 10;
    
       while (num > subtractor)
       {
          num -= subtractor;
          x++;
       }
    
       return (num * 10) + x;
    }
    
    long rollRight (long num)
    {
       int i;
       long lastDigit = 0;
       int size = digits (num);
    
       while (num % 10)
       {
          num--;
          lastDigit++;
       }
    
       for (i = 0; i < size; i++)
          lastDigit *= 10;
    
       cout << lastDigit;
    
       return ((num / 10) + lastDigit);
    }
    
    int digits (int num)
    {
       int digits = 0;
    
       while (num > 1)
       {
           digits++;
           num /= 10;
       }
    
       return digits;
    }
    MSVC++ 6.0

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by samGwilliam
    [code]
    #include <iostream.h>
    #include <conio.h>

    long rollLeft (long num);
    ......
    Umm... How does it work?
    Can you tell me? thx!!

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Can anyone just tells me how to shift the numbers??
    Thx a lot!!

  8. #8
    Señor Member
    Join Date
    Jan 2002
    Posts
    561
    Looks to me like Sam just gave you all the help you need.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by tim545666
    Looks to me like Sam just gave you all the help you need.
    Oh... sorry... But I just can't understand it... and I don't know how it works.....
    I wanna learn and understand how it works...

    sorry... and thx!!

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Oh! I got something!

    I know how it works in some ways...
    but.... the following code I can't understand....

    int digits (int num); // Why it has to declare in main?

    int i;
    long subtractor = 1; //why it needs a subtractor?
    int size = digits (num); //How does digits work?


    sub *= 10; //What does *= mean?

    num -= subtractor; //What does -= mean?

    x++; //it means add 1 after right?

    num--; //What does it mean? mean minus after?

    num /= 10; // What does /= mean?




    would anyone tell me what they mean?? thx a bunch!!

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    x+=a means x=x+a. you can replace the plus sign with almost any other operator
    x++ means x=x+1. it adds one to x
    x-- subtracts one from x, just like x++ except the opposite of it.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Thx ygfperson!!

    it can left/right shift 1 digit... what about 2 digit?
    What can I do?? Thx!!

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    What I did like that...


    void main()
    {
    long a;
    a = rollLeft (12345);

    cout << rollLeft (a);

    getch ();
    }


    It doesn't work like 12345 ---> 34512

    Can anyone tell me how to do it?? thx!!

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Can anyone help me? thx...

  15. #15
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Try -

    Code:
    #include <iostream> 
    #include <cmath>
    
    using namespace std;
    int shift_left(int number)
    {
        //Get number of digits
        int i = log10(number)+1;
        //Get far right digit
        int c = number/pow(10,i-1);
        //Remove existing far left digit
        number -= c*pow(10,i-1);
        //Shift existing numbers left
        number*=10; 
        //Add previous far left digit onto right
        number+=c;
    
        return number;
        
    }
    
    
    int main() 
    { 
        int a = 12345;
        a=shift_left(a);
        cout << shift_left(a);
        return 0; 
    }
    Last edited by Sorensen; 02-12-2002 at 12:25 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21