trying to extract complex numbers from strings

牧云@^-^@ 提交于 2019-12-11 16:39:25

问题


I am attempting to extract two columns of numbers from a text file. first column is the real part of the number and the second in the imaginary part. I managed to extract the list of numbers from the file as strings but I don't know how to separate the strings into two parts. I have attempted to use the sscanf function but just hasnt worked. The difficult part is the numbers can be both positive and negative therefore I cant use + and - in the delimiter of the strtok function since it will remove the negatives. Iv been stuck for a few days so any suggestions would be appreciated. Thanks in advance. Here is the code I worte which errors at the sscanf line.

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <complex.h>

        char array[35]= "[[[1+1i -4+100i 45-234i -56-78i]]]";
        char *arrayp[35];
        int count,n,i,j;
        double complex z1;
        double real = 0;
        double imaginary = 0;

        int main()
        {
            arrayp[0] = strtok(array," []");
            n=1;
            while (arrayp[n-1]!=NULL)
            {
               arrayp[n] = strtok(NULL, " []");
               n++;
            }
        // up to this point it has been tested to work. the remaining code is very 
        // sloppy since I have tried 8 different things and have quickly written one of 
        // the options tried.    

            for(j=0;j<n;j++)
            {
                if (strchr(*arrayp, '+'))
                {
                    sscanf(arrayp[j],"%f+%fi", real, imaginary);
                }
                else if(arrayp string has equal to more than 1 '-')
                {
                     sscanf(arrayp[j],"%f%fi", real, imaginary);
                }
            }
        }

The output should be something like this:
0 0
-4 100
45 -234
-56 -78

I noticed there are mistakes such are trying to search *arrayp in strchr but its a pointer I dont know how to convert a pointer into a string so i can put it into this file. Thank you for the help and effort in advance.


回答1:


So far so good but in

sscanf(arrayp[j],"%f%fi", real, imaginary);

there are two errors. Firstly the scanf function family needs %lf for a double target.

Secondly, it needs the address of the target, so

sscanf(arrayp[j], "%lf%lfi", &real, &imaginary);

Also, I don't see why you need to build an array of string pointers first - just examine every non-NULL token pointer that strtok produces.

Edit: this is a little test program.

#include <stdio.h>
#include <string.h>

int main ()
{
    double r, i;
    char array[]= "[[[1+1i -4+100i 45-234i -56-78i]]]";
    char *tok;
    tok = strtok(array, " []");
    while(tok) {
        sscanf(tok, "%lf%lfi", &r, &i);
        printf("%.0f %.0fi\n", r, i);
        tok = strtok(NULL, " []");
    }
    return 0;
}

Program output:

1 1i
-4 100i
45 -234i
-56 -78i

The program should be more rigorous and check the return value from sscanf.




回答2:


parse every number yourself. start scanning the string from index 1 (since index 0 is either +/- or a number), looking for either a "=" or a "-". once you find it, you know how to split the string.



来源:https://stackoverflow.com/questions/47679916/trying-to-extract-complex-numbers-from-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!