开发者

How can structures be used to represent complex numbers

开发者 https://www.devze.com 2023-02-12 18:12 出处:网络
I need to write a program which uses structures to define complex numbers ie z1 = x + yi. And then adds 2 complex numbers. I need to figure out how to initalise them properly before moving on with 开发

I need to write a program which uses structures to define complex numbers ie z1 = x + yi. And then adds 2 complex numbers. I need to figure out how to initalise them properly before moving on with 开发者_开发百科my code. I've tried a few things so far, this is the best I have come up with and it's still not compiling.

Here's a copy of my code, I just need to fix this part and then I will be able to do the rest myself.

#include<stdio.h>

typedef struct complex1{
    float *real1;
    float *imaginary1;
} complex1;


typedef struct complex2{
    float *real2;
    float *imaginary2;
} complex2;


int main(){
  struct complex1 real;
  struct complex1 *realptr;
  struct complex1 imaginary;
  struct complex1 *imaginaryptr;
  struct complex2 real;
  struct complex2 *realptr;
  struct complex2 imaginary;
  struct complex2 *imaginaryptr;

  printf("Please enter variable x1.");
  scanf("%d", &real.real1);
  printf("Please enter variable y1.");
  scanf("%d", &imaginary.imaginary1);
  printf("Please enter variable x2.");
  scanf("%d", &real.real2);
  printf("Please enter variable y2.");
  scanf("%d", &imaginary.imaginary2);
  printf("You have entered: %d,%d,%d,%d\n", 
  real.real1, imaginary.imaginary1,real.real2, imaginary.imagnary2);
  return 0;
}


Your code makes little sense:

  • You are defining two identical structures, which seems pointless.
  • The structures contain pointers to floats, rather than actual floats, which seems ill-advised.
  • Your code that reads in floats using scanf() is using non-initialized pointers to store values, which leads to undefined behavior.
  • You should not use the %d format specifier to read in floating-point numbers, it's for integers.

Try:

typedef struct {
  float real;
  float imaginary;
} complex;

complex a, b;

scanf("%f", &a.real);
scanf("%f", &a.imaginary);


There were multiple errors in the file, but maybe something like this is more what you want ?

#include<stdio.h>

typedef struct _complex1{
    double real1;
    double imaginary1;
} complex1;

Do not name the struct twice with the same name, and I do believe you want to skip the pointers for real1 and imaginary1 -- since they do not gain you anything.

int main(){
  complex1 real;
  complex1 *realptr;
  complex1 imaginary;
  complex1 *imaginaryptr;
  complex2 real2;
  complex2 *realptr2;
  complex2 imaginary2;
  complex2 *imaginaryptr2;

The complex typedefs already tell the compiler that it is a struct. And you can not have two variables with the same name.

  printf("Please enter variable x1.");
  scanf("%lf", &real.real1);

You need to align what you are sending to scanf with what it expects. %f expects a double * and not a double ** or (float ** in your case).


I'll give you a hint, since this seems like a homework problem. structs are used to define or group standard data types together to form a new data type, like an imaginary number. Once defined, you are free to use it. It looks like you are using C so I will go with that. First defining the new type:

struct complex 
{
    float real;
    float imaginary;
};

In C, to declare a struct type, you usually have to type in "struct" again, so most programmers typedef it as well. We can do it separately like this:

typedef complex ComplexType;

or Combined:

typedef struct complex {
    float real;
    float imaginary;
} ComplexType;

Then to declare and assign:

ComplexType myComplexType;
myComplexType.real = 0.5f;
myComplexType.imaginary = 0.5f;

Or:

ComplexType myComplexType = { 0.0f, 0.0f };

From there you are free to use your new type. A few mistakes in your code are you are declaring two new data types when it looks like you only need one. The other is you are declaring pointer to floats, which is probably not what you need. Pointers are usually used when you want t allocate the memory for the type in heap.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号