试图从字符串中提取复数
trying to extract complex numbers from strings
我正在尝试从文本文件中提取两列数字。第一列是数字的实部,第二列是虚部。我设法从文件中提取数字列表作为字符串,但我不知道如何将字符串分成两部分。我曾尝试使用 sscanf 函数,但没有成功。困难的部分是数字可以是正数也可以是负数,因此我不能在 strtok 函数的分隔符中使用 + 和 - 因为它会删除负数。我被困了几天,所以任何建议将不胜感激。提前致谢。这是我在 sscanf 行出错的代码。
#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
我注意到有一些错误,比如试图在 strchr 中搜索 *arrayp 但它是一个指针我不知道如何将指针转换为字符串以便我可以将它放入此文件中。感谢您提前的帮助和努力。
自己解析每个数字。从索引 1 开始扫描字符串(因为索引 0 是 +/- 或数字),查找“=”或“-”。一旦找到它,您就知道如何拆分字符串了。
到目前为止一切顺利,但在
sscanf(arrayp[j],"%f%fi", real, imaginary);
有两个错误。首先,scanf
函数族需要 %lf
作为 double
目标。
其次,它需要目标的地址,所以
sscanf(arrayp[j], "%lf%lfi", &real, &imaginary);
此外,我不明白为什么您需要先构建一个字符串指针数组 - 只需检查 strtok
生成的每个非 NULL
标记指针即可。
编辑:这是一个小测试程序。
#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
中的return值。
我正在尝试从文本文件中提取两列数字。第一列是数字的实部,第二列是虚部。我设法从文件中提取数字列表作为字符串,但我不知道如何将字符串分成两部分。我曾尝试使用 sscanf 函数,但没有成功。困难的部分是数字可以是正数也可以是负数,因此我不能在 strtok 函数的分隔符中使用 + 和 - 因为它会删除负数。我被困了几天,所以任何建议将不胜感激。提前致谢。这是我在 sscanf 行出错的代码。
#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
我注意到有一些错误,比如试图在 strchr 中搜索 *arrayp 但它是一个指针我不知道如何将指针转换为字符串以便我可以将它放入此文件中。感谢您提前的帮助和努力。
自己解析每个数字。从索引 1 开始扫描字符串(因为索引 0 是 +/- 或数字),查找“=”或“-”。一旦找到它,您就知道如何拆分字符串了。
到目前为止一切顺利,但在
sscanf(arrayp[j],"%f%fi", real, imaginary);
有两个错误。首先,scanf
函数族需要 %lf
作为 double
目标。
其次,它需要目标的地址,所以
sscanf(arrayp[j], "%lf%lfi", &real, &imaginary);
此外,我不明白为什么您需要先构建一个字符串指针数组 - 只需检查 strtok
生成的每个非 NULL
标记指针即可。
编辑:这是一个小测试程序。
#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
中的return值。