-3

I need to convert a string in the a ± bi form to an int Real and an int Im. And in the case that the string isn't in that form, print an error. I have an idea to convert the a to the int and also the b but I don't know what to do with de imaginary number if it is positive.

i.e.

0,034 - 1,2i

>a=0,034
>b=-1,2

0,25

>Error, you must write in "a±bi" form

3,234 + 34,43i

>a=3,234
>b=34,43

ps: I found this link but it is in C++ and I don't know what it is doing

EDIT: THE REAL NUMBER COULD HAVE A PLUS OR MINUS.

Community
  • 1
  • 1
Agustin Luques
  • 57
  • 1
  • 10

2 Answers2

0

It is quite simple, the C-standard has all you need:

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

#define BUFFER_SIZE 100

// ALL CHECKS OMMITTED!

int main()
{
  double a, b;
  char buffer[BUFFER_SIZE];
  int count = 0;
  char c;
  char *endptr;

  while ((c = fgetc(stdin)) != EOF) {
    if (c == '\n') {
      // make a proper C-string out of it
      buffer[count] = '\0';
      // reset counter for the next number
      count = 0;
      // strtod() is intelligent enough to
      // stop at the end of the number
      a = strtod(buffer, &endptr);
      // endptr points to the end of the number
      // skip whitespace ("space" only)
      while (*endptr == ' ') {
        endptr++;
      }
      // skip the +/- in the middle
      endptr++;
      // strtod() skips leading space automatically
      b = strtod(endptr, NULL);
      printf("a = %g, b = %g\n", a, b);
    }
    // read all into a big buffer
    buffer[count++] = (char) c;
    if (count >= BUFFER_SIZE) {
      fprintf(stderr, "Usage: type in complex numbers in the form \"a + b\"\n");
      exit(EXIT_FAILURE);
    }
  }

  exit(EXIT_SUCCESS);
}

See? No need to worry.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • So the only case the program exits with failure is when the line we are reading is bigger than de buffer size? What about writing letters? – Agustin Luques Sep 02 '16 at 16:37
  • @AgustinLuques that's the reason there is this all-caps note at the top of that code "ALL CHECKS OMMMITED!" (including spellcheck ;-) ). Either check the return and error of `strtod()` (see manpage of `strtol()` for example code) or check the input if it is a proper floating point number. Checking `strtod()` is much simpler. – deamentiaemundi Sep 02 '16 at 18:08
  • Code fail to meet OP's goal as it skips the +/- in the middle. That is needed for correct formation of `b` as well as to detect errant input. Try `0,034 - 1,2i`. – chux - Reinstate Monica Sep 02 '16 at 19:35
  • @chux yepp, that's correct. I even wrote that as a comment. Hey, I do other peoples homework sometimes, yes, but am I also supposed to make them get an A if I do so? ;-) They have to do at least *something* themselves, don't you think? – deamentiaemundi Sep 02 '16 at 19:42
  • Fair enough for OP. – chux - Reinstate Monica Sep 02 '16 at 19:53
0

Use sscanf():

"%lf" scan 0 or more white spaces and then scan a double
" " scan 0 or more white spaces
"%1[+-]" scan a non-empty string made up of + or - and only up to 1 character in length.
"i" scan for an i character
"%n" store the count of characters scanned so far. (does not add to the return count.)

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

double complex ALconvert(const char *s) {
  int n = 0;
  double re, im;
  char sign[2];

  if (sscanf(s, "%lf %1[+-] %lf i %n", &re, sign, &im, &n) != 3 || s[n]) {
    puts("Error, you must write in \"a+/-bi\" form");
    return 0.0/0.0; // TBD_ErrorCode;
  }
  if (sign[0] == '-') {
    im = -im;
  }
  return re + im*_Complex_I;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256