C语言构造体的调用函数程序,请高手修改,真诚请你贴上能成功运行的程序

来源:百度知道 编辑:UC知道 时间:2024/07/04 07:02:16
我想通过构造体函数的形式来表示复数,然后通过调用函数,输出函数的实部与虚部,但是还没有运行成功,敬请赐教。特别说明,我用的是microsoft visual c++ 2008.

#include<stdio.h>

struct well{
int re;
int im;
};

typedef struct well well;

int add(well *a, well *b, well *c);

int main(void)
{
int a, b, c;

add(a, b, c);
printf("a->re = %d\na->im = %d\n", a->re, a->im);
}

int add(well *a, well *b, well *c)
{
a->re = b->re + c->re;
a->im = b->im + c->im;
return a->re, a->im;
}
我还忘记初始化了。
做一下初始化。
(*a = 0 + 0r;
*b = 2 + 3r;
*c = 1 + 4r;
这里r表示虚部)

that is
初始化
a->re = 0;
a->im = 0;

b->re = 2;
b->im = 3;

c->re = 1;
c->im = 4;

根据你对add函数的定义,main函数的int a, b,c ; add(a,b,c);这两行是不对的。另外你没有对b和c初始化, 这样的计算也没有意义,因为这样的自动变量值是随机的。
可以改成以下这个样子。

#include<stdio.h>
struct well{
int re;
int im;
};
typedef struct well well;
int add(well *a, well *b, well *c);
int main(void)
{
well a, b, c;
b.re = 3; b.im = 4;
c.re = 4; c.im = 7;
add(&a, &b, &c);
printf("a->re = %d\na->im = %d\n", a.re, a.im);
}

int add(well *a, well *b, well *c)
{
a->re = b->re + c->re;
a->im = b->im + c->im;
return a->re, a->im;
}

#include<stdio.h>

struct rwell{
int re;
int im;
}well;

well owell;

well *add(well a, well b);

int main(void)
{
well a, b, *c;
scanf("a.re = %d\na.im = %d\n", a.re, a.im);
scanf("b.re = %d\nb.im = %d\n&quo