我一直在解决这个问题,我相信我的结构中的指针指向的地方有错误。但是,我似乎无法弄清楚我哪里出错了。
我的代码意味着接受2个复数,然后乘以并除以它们,然后吐出两个答案。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct complex_t
{
double real; // real part
double imag; // imaginary part
} complex_t;
// Multiplication Function //
void *multiply()
{
struct complex_t a, b, c;
c.real == ((a.real * b.real) - (a.imag * b.imag));
c.imag == ((a.imag * b.real) + (a.real * b.imag));
if ( c.imag >= 0 )
printf("Multiplication = %d + %di\n", c.real, c.imag); // Postive Imaginary case
else
printf("Multiplication = %d %di\n", c.real, c.imag); // Negative Imaginary case
}
// Division Function //
void *divide()
{
int numer1, numer2, denom;
struct complex_t a, b, c;
if ( b.real == 0 || b.imag == 0 ) // Case to Avoid Dividing by 0
printf("Division by 0 + 0i is not allowed.");
else
{
numer1 = ((a.real * b.real) + (a.imag * b.imag));
numer2 = ((a.imag * b.real) - (a.real * b.imag));
denom = ((b.real * b.real) + (b.imag * b.imag));
c.real == (numer1/denom);
c.imag == (numer2/denom);
if (numer2/denom >= 0)
printf("Division = %d + %di \n", c.real, c.imag); // Postive Imaginary case
else
printf("Division = %d %di \n", c.real, c.imag); // Negative imaginary case
}
}
// Main - to execute the two functions //
int main() {
struct complex_t a, b, c;
printf("Enter a and b where a + ib, for the first complex number.");
printf("\na = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.imag);
printf("Enter c and d where c + id, for the second complex number.");
printf("\nc = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.imag);
multiply();
divide();
return 0;
}
这是该程序产生的一个例子:
乘法= 69144 -4196352i
Division = -13339222 0i
关于我可以从何处开始计算此错误的任何提示都会很棒。
答案 0 :(得分:2)
C是一种确切的语言。没有syntax
足够接近的事情。这在C中是一个很大的优势,但它是一个障碍开始程序员必须在任何真正的学习之前接受并理解可以发生。这包括了解每行的每个部分,包括格式字符串。如果您不完全了解每行的每个部分正在做什么 - 查找。阅读手册页,搜索更多信息,直到您这样做。从长远来看,它将为您节省大量时间。
最重要的是,您可以做的一件事就是帮助您找到代码中的问题,在启用警告的情况下进行编译。这意味着在您的代码中包含至少-Wall -Wextra
编译字符串。例如,在您的代码中,屏幕上出现了警告,包括code with no apparent function
和expected double but have int
。那些东西告诉你,你可以尝试运行你的代码 - 但不要指望它能正常工作。你必须先解决这些问题,然后再合理地确信你的代码不会出现垃圾(或崩溃)。
必须进行的另一个主要学习方法是始终初始化变量(如果没有别的话,则为零)。尝试访问未初始化的变量是未定义的行为。 (这是任何人猜测会发生什么。)
话虽如此。你有部分代码是正确的。您的问题基本上是放慢速度,阅读编译器告诉您错误的内容,修复它并再次尝试。这是C的关键,放慢速度并使其正确。
够了 - 你要去帮忙吗?当然。请仔细阅读以下内容。了解为什么变更是必要的,并且您将能够将其视为当天的一些学习。但是,下面代码中的修复程序并不像上面的C编程指南那么重要。 (给一个人一条鱼....):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
double real; // real part
double imag; // imaginary part
} complex_t;
// Multiplication Function //
void multiply (complex_t *a, complex_t *b, complex_t *c) {
/* struct complex_t a, b, c; */
c->real = ((a->real * b->real) - (a->imag * b->imag));
c->imag = ((a->imag * b->real) + (a->real * b->imag));
if (c->imag >= 0)
printf ("\nMultiplication = %f + %fi\n", c->real, c->imag); // Postive Imaginary case
else
printf ("\nMultiplication = %f %fi\n", c->real, c->imag); // Negative Imaginary case
}
// Division Function //
void divide (complex_t *a, complex_t *b, complex_t *c) {
int numer1, numer2, denom;
/* struct complex_t a, b, c; */
if (b->real == 0 || b->imag == 0) // Case to Avoid Dividing by 0
printf ("Division by 0 + 0i is not allowed.");
else {
numer1 = ((a->real * b->real) + (a->imag * b->imag));
numer2 = ((a->imag * b->real) - (a->real * b->imag));
denom = ((b->real * b->real) + (b->imag * b->imag));
c->real = (numer1 / denom);
c->imag = (numer2 / denom);
if (numer2 / denom >= 0)
printf ("\nDivision = %f + %fi \n", c->real, c->imag); // Postive Imaginary case
else
printf ("\nDivision = %f %fi \n", c->real, c->imag); // Negative imaginary case
}
}
// Main - to execute the two functions //
int main () {
complex_t a = { 0, 0 }, b = { 0, 0 }, c = { 0, 0 };
printf ("\nEnter a and b where a + ib, for the first complex number.\n\n");
printf (" a (a.real) = ");
scanf ("%lf", &a.real);
printf (" b (a.imag) = ");
scanf ("%lf", &a.imag);
printf ("\nEnter c and d where c + id, for the second complex number.\n\n");
printf (" c (b.real) = ");
scanf ("%lf", &b.real);
printf (" d (b.imag) = ");
scanf ("%lf", &b.imag);
multiply (&a, &b, &c);
divide (&a, &b, &c);
printf ("\n");
return 0;
}
<强>输出:强>
$ ./bin/divmult
Enter a and b where a + ib, for the first complex number.
a (a.real) = 10
b (a.imag) = 3
Enter c and d where c + id, for the second complex number.
c (b.real) = 5
d (b.imag) = 5
Multiplication = 35.000000 + 65.000000i
Division = 1.000000 + 0.000000i
答案 1 :(得分:0)
void *multiply()
结构变量a
和b
的值未在此函数中初始化,您应该将main()
中的扫描值传递给此函数以及divide()
< / p>
这里没有理由我明白你为什么要回归void *
。通过引用或按值传递参数。