简体   繁体   English

如何在 C 中将字符串转换为复数?

[英]How do I convert a string to a complex number in C?

So, basically, I have to build a simple complex number calculator using C.所以,基本上,我必须使用 C 构建一个简单的复数计算器。 However, both input and output have to be on string form.但是,输入和 output 都必须是字符串形式。 Keep in mind complex.h is not allowed, that's why a simple struct for the real and imaginary parts are needed.请记住 complex.h 是不允许的,这就是为什么需要一个用于实部和虚部的简单结构的原因。

For example, the string "-3.2+2.4i" would have to become:例如,字符串“-3.2+2.4i”必须变成:

a.real = -3.2; a.real = -3.2;

a.img = 2.4; a.img = 2.4;

And then back to string form after some sort of calculation was completed, which likely is way easier since all you need to do is convert it back using gcvt() and merge them both with strcat().然后在某种计算完成后返回到字符串形式,这可能更容易,因为您需要做的就是使用 gcvt() 将其转换回来,并使用 strcat() 将它们合并。

I have tried using strtok to split them but it was such a headache and it failed to work almost every time.我曾尝试使用 strtok 来拆分它们,但这太让人头疼了,而且几乎每次都无法正常工作。 It would be possible if both parts were always positive but the '+' and '-' signs were making everything a mess.如果这两个部分总是积极的,但“+”和“-”符号让一切变得一团糟,这将是可能的。

struct complex {
  double real, img;

};

int main() {
  struct complex a, b, c;

  //calculator itself would be here
}

struct convertNumber() {

  //conversion would happen here. Can I return both the real and imaginary parts at once with just this one fuction?
}

Use strtod .使用strtod It will parse the real part from the string and set a pointer to the first character after the real part.它将从字符串中解析实部,并设置一个指向实部之后的第一个字符的指针。 Then you can call strtod again with that pointer to parse the imaginary part.然后您可以使用该指针再次调用strtod来解析虚部。

Here is an extremely simple (slightly unsafe) example:这是一个非常简单(有点不安全)的例子:

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

int main(int argc, char** argv) {
    if (argc != 2) {
        return 1;
    }

    char* startptr = argv[1];
    char* endptr = NULL;
    double real = strtod(startptr, &endptr);
    if (startptr == endptr) {
        return 1;
    }

    startptr = endptr;
    endptr = NULL;
    double imag = strtod(startptr, &endptr);
    if (startptr == endptr) {
        return 1;
    }

    if (*endptr != 'i') {
        return 1;
    }

    printf("%f + %fi\n", real, imag);

    return 0;
}

sscanf() affords another approach: sscanf()提供了另一种方法:

// Return `complex` value.
// Store in *endptr location of end of conversion
struct complex convertNumber(const char *source, char **endptr) {
  struct complex destination;
  int n = 0;
  sscanf(source, "%f %f %*1[Ii] %n", &destination.real, &destination.img, &n);
  if (endptr) {
    *endptr = (char*) &source[n];
  }
  return destination;
}

Sample usage:示例用法:

char buffer[100];
if (fgets(buffer, sizeof buffer, stdin)) {
  char *endptr;
  struct complex y = convertNumber(buffer, &endptr);
  // If no conversion or junk at the end ...
  if (buffer == endptr || *endptr) {
    printf("Conversion failed '%s'\n", buffer);
  } else {
    printf("Conversion %e %+ei\n", y.real, y.img);
  }
}

You used struct in the function the wrong way, it's not a redefinable type.您在 function 中以错误的方式使用了struct ,它不是可重新定义的类型。

Define it like this:像这样定义它:

typedef struct Complex {
  double real, img;
} Complex;

then you'll be able to define this function like that:那么你就可以像这样定义这个 function :

Complex convertNumber(double realpart, double imgpart) {
  Complex newComplex;
  newComplex.real = realpart;
  newComplex.img = imgpart;
  return newComplex;
}

There is also a variant with dynamic memory mapping, although it was suggested the above would work as expected.还有一个带有动态 memory 映射的变体,尽管有人建议上述方法可以按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM