【发布时间】:2021-12-14 18:22:52
【问题描述】:
有点像my last post 的延续,我正在尝试使用结构和函数编写一个复数计算器。我的程序必须具有从用户输入中读取复数的功能,并且必须具有添加它们的另一个功能。这是给我的函数原型:
Complex read_complex(void)
这是我必须使用的原型,无法更改。现在,我正在尝试将我从上述函数中扫描的值传递到我的函数中以添加复数。这是我的代码:
#include <stdio.h>
#include <math.h>
#include<string.h>
typedef struct Complex_ {
double RealPart;
double ImagPart;
} Complex;
Complex read_complex(void);
Complex add_complex(Complex z1, Complex z2);
Complex mul_complex(Complex z1, Complex z2);
int main(void) {
char ent[50];
Complex user1, user2;
printf("Enter Add for addition, Mult for multiplication, MA for magnitude and angle, or Exit to quit: ");
scanf("%s", ent);
if (ent[0] == 'A') {
read_complex();
add_complex(user1, user2);
}
else if (ent[0] == 'M' && ent[1] == 'u') {
read_complex();
mul_complex(user1, user2);
}
else if (ent[0] == 'M' && ent[1] == 'A') {
read_complex();
}
else {
}
return(0);
}
Complex read_complex(void) {
Complex* user1;
Complex* user2;
printf("Enter first complex number: ");
scanf("%lf %lf", &user1->RealPart, &user1->ImagPart);
printf("Enter the second complex number: ");
scanf("%lf %lf", &user2->RealPart, &user2->ImagPart);
return;
}
Complex add_complex(Complex z1, Complex z2) {
Complex z3;
z3.RealPart = z1.RealPart + z2.RealPart;
z3.ImagPart = z1.ImagPart + z2.ImagPart;
printf("(%lf + %lfi) + (%lf + %lfi) = %lf + %lfi", z1.RealPart, z1.ImagPart, z2.RealPart, z2.ImagPart, z3.RealPart, z3.ImagPart);;
return(z3);
}
Complex mul_complex(Complex z1, Complex z2) {
Complex z3;
z3.RealPart = z1.RealPart * z2.RealPart;
z3.ImagPart = z1.ImagPart * z2.ImagPart;
return(z3);
}
(现在大部分代码不完整,因为我只是想弄清楚添加部分)。我目前遇到的问题是,当我运行代码时,我收到一条错误消息,提示 user1 和 user2 变量未初始化,我不知道如何初始化结构变量。
【问题讨论】:
-
read_complex应该是 one 复数。 -
printingwrong3434,节省时间(您和我们的)。启用所有警告。
Complex read_complex(void) { ... return; }应该投诉。 -
看看
add_complex和mul_complex如何返回Complex类型的值?read_complex也需要这样做。
标签: c function pointers struct