这个程序应该起到复数计算器的作用。我需要在给定参数的情况下使用这些函数。当我运行程序时,我可以在选择操作后输入4个数字。在我按下第四个数字的回车键后,我收到一个名为"分段错误:11"的错误。我不知道为什么会收到这条消息。
我已经包含了代码。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void read_num(float *real_part, float *imaginary_part) {
float realnumberholder, imagnumberholder;
printf("Please type in the real component: ");
scanf("%f", &realnumberholder);
*real_part = realnumberholder;
printf("Please type in the imaginary component: ");
scanf("%f", &imagnumberholder);
*imaginary_part = imagnumberholder;
}
void read_nums(float *real_part_1, float *imaginary_part_1,
float *real_part_2, float *imaginary_part_2) {
printf("Reading the first imaginary number...");
read_num(real_part_1, imaginary_part_1);
printf("Reading the second imaginary number...");
read_num(real_part_2, imaginary_part_2);
}
void print_complex(float real_part, float imaginary_part) {
printf("The operation yields %6.3f + %6.3fi", real_part,
imaginary_part);
}
void add(float real_part_1, float imaginary_part_1, float
real_part_2, float imaginary_part_2, float *real_result, float
*imaginary_result) {
read_nums(&real_part_1, &imaginary_part_1, &real_part_2,
&imaginary_part_2);
*real_result = (real_part_1 + real_part_2);
*imaginary_result = (imaginary_part_1 + imaginary_part_2);
print_complex(*real_result, *imaginary_result);
}
void subtract(float real_part_1, float imaginary_part_1, float
real_part_2, float imaginary_part_2, float *real_result, float
*imaginary_result) {
read_nums(&real_part_1, &imaginary_part_1, &real_part_2,
&imaginary_part_2);
*real_result = (real_part_1 - real_part_2);
*imaginary_result = (imaginary_part_1 - imaginary_part_2);
print_complex(*real_result, *imaginary_result);
}
void multiply(float real_part_1, float imaginary_part_1, float
real_part_2, float imaginary_part_2, float *real_result, float
*imaginary_result) {
read_nums(&real_part_1, &imaginary_part_1, &real_part_2,
&imaginary_part_2);
*real_result = (real_part_1 * real_part_2);
*imaginary_result = (imaginary_part_1 * imaginary_part_2);
print_complex(*real_result, *imaginary_result);
}
int main()
{
int option;
float realone = 0;
float imaginaryone = 0;
float realtwo = 0;
float imaginarytwo = 0;
float *realresult;
float *imaginaryresult;
printf("nComplex Number Arithmetic Program.n");
printf("1) Add two complex numbersn2) Subtract two complex
numbersn3) Multiply two complex numbern4) QuitnChoose an
option (1-4): ");
scanf("%d", &option);
while(option != 4) {
switch(option) {
case 1 :
add(realone, imaginaryone, realtwo,
imaginarytwo, realresult, imaginaryresult);
printf("1) Add two complex numbersn2)
Subtract two complex numbersn3) Multiply
two complex numbern4) QuitnChoose an
option (1-4): ");
scanf("%d", &option);
break;
case 2 :
subtract(realone, imaginaryone, realtwo,
imaginarytwo, realresult, imaginaryresult);
printf("1) Add two complex numbersn2)
Subtract two complex numbersn3) Multiply
two complex numbern4) QuitnChoose an
option (1-4): ");
scanf("%d", &option);
break;
case 3 :
multiply(realone, imaginaryone, realtwo,
imaginarytwo, realresult, imaginaryresult);
printf("1) Add two complex numbersn2)
Subtract two complex numbersn3) Multiply
two complex numbern4) QuitnChoose an
option (1-4): ");
scanf("%d", &option);
break;
default:
printf("Please input a valid menu
optionn");
printf("1) Add two complex numbersn2)
Subtract two complex numbersn3) Multiply
two complex numbern4) QuitnChoose an
option (1-4): ");
scanf("%d", &option);
break;
}
}
if (option == 4) {
printf("Bye!n");
return 0;
}
}
realresult和imaginaryresult被用作主函数中的指针。它们应该只是浮点变量,并且应该将这些变量的地址传递给相应的函数。更改以下行
float *realresult;
float *imaginaryresult;
至
float realresult;
float imaginaryresult;
此外,函数调用需要修改为
add(realone, imaginaryone, realtwo, imaginarytwo, &realresult, &imaginaryresult);
...
subtract(realone, imaginaryone, realtwo, imaginarytwo, &realresult, &imaginaryresult);
....
multiply(realone, imaginaryone, realtwo, imaginarytwo, &realresult, &imaginaryresult);
更改此
float *realresult;
float *imaginaryresult;
对此:
float realresult;
float imaginaryresult;
然后更改函数调用以获取地址:
add(realone, imaginaryone, realtwo,
imaginarytwo, &realresult, &imaginaryresult);
(注意&
符号(
它会起作用的。就像以前一样,如果没有这些内存分配,这两个指针就不会指向任何有效的内存。它们是未初始化的,像您那样访问它们是未定义的行为。在实践中通常会发生的情况是,它试图将垃圾值解释为地址,并试图访问该地址,这导致了分段错误。