Thread: Storing components of a complex number

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Storing components of a complex number

    I'm writing a program in which a user enters a complex number (a + bi). I need to store the real numbers a and b in floating points *x1 and *i1 respectively but I'm having a hard time figuring out how to approach it.

    It would be easy to ask for each part individually, but the program I have to write doesn't allow for that. The user enters the entire number.

    Can anyone give me any suggestions on how to look at it?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'll need to "parse" the string "by hand". You can perhaps use strtok() to split the string, or write your own code that walks through the string and splits it into it's respective components.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Okay, using strtok(a, b), the command will search through a to find a character that doesn't match what is in b and then turns the character after that into a null character.

    so if a = 1.000+2i, if I use strtok(a, "+") it will stop at + and change 2 into null, leaving the i.

    Is there a way with strtok to search for a token that is an arbitrary number to that the + can be changed into null instead of the number following the +?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    strtol() and strtod() will respectively parse ints and floats, AND set a pointer to the point where they managed to parse to (in other words, where your + might be).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    strtod() can be used to parse floats. And according to all of the sites I've found explaining the function, it is of the format:

    strtod(const char *nptr, char **endptr, base)

    so assuming nptr is going to be the variable containing the complex number, it will read the value until it gets to the + and put it in endptr?

    Sorry if this is a stupid question, I'm just not understanding what I'm reading on the pages I've found and the examples are cryptic.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    This is what I currently have. I've been messing with declarations and things just to see if it makes any difference, so I know the type identifiers are probably all wrong.

    Code:
    void sub_sum(float *x1, float *i1, float *x2, float *i2)
    {
            const char num1, num2;
            float a1, a2, b1, b2;
            char endptr1, junk, endptr2;
    
            a1 = 0;
            a2 = 0;
            b1 = 0;
            b2 = 0;
    
            printf("Enter first complex number: ");
            scanf("%c", &num1);
    
            printf("Enter second complex number: ");
            scanf("%c", &num2);
    
            a1 = strtod(num1, &endptr1);
            b1 = strtod(endptr1, &junk);
            a2 = strtod(num2, &endptr2);
            b2 = strtod(endptr2, &junk);
            x1 = &a1;
            i1 = &b1;
            x2 = &a2;
            i2 = &b2;
    
            printf("%.6f %.6f \n", *x1, *i1);
            printf("%.6f %.6f \n", *x2, *i2);
    
    }

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Why use strtok() for this?

    Code:
    float realpart, imagpart;
    if(sscanf(input, "%f+%fi", &realpart, &imagpart) != 2)
    {
        /* Parse failed */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  2. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 08:28 AM
  3. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:23 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. Replies: 2
    Last Post: 01-04-2004, 05:52 PM