试图从字符串中提取复数 [英] trying to extract complex numbers from strings
问题描述
我正在尝试从文本文件中提取两列数字.第一列是数字的实数部分,第二列是虚数部分.我设法从文件中提取数字列表作为字符串,但是我不知道如何将字符串分成两部分.我试图使用sscanf函数,但还没有工作.困难的部分是数字既可以是正数也可以是负数,因此我不能在strtok函数的定界符中使用+和-,因为它将删除负数.我被困了几天,所以任何建议将不胜感激.提前致谢.这是我在sscanf行上出错的代码.
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);
}
}
}
输出应该是这样的:
0 0
-4 100
45 -234
-56 -78
The output should be something like this:
0 0
-4 100
45 -234
-56 -78
我注意到有一些错误,例如尝试在strchr中搜索* arrayp,但是它的指针我不知道如何将指针转换为字符串,因此我可以将其放入此文件中.谢谢您的事先帮助和努力.
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.
推荐答案
到目前为止很好,但是在
So far so good but in
sscanf(arrayp[j],"%f%fi", real, imaginary);
有两个错误.首先,scanf
函数族需要%lf
作为double
目标.
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);
此外,我不明白为什么需要首先构建一个字符串指针数组-仅检查strtok
产生的每个非NULL
令牌指针.
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.
编辑:这是一个小测试程序.
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;
}
程序输出:
1 1i
-4 100i
45 -234i
-56 -78i
程序应更加严格,并检查sscanf
中的返回值.
The program should be more rigorous and check the return value from sscanf
.
这篇关于试图从字符串中提取复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!