如何能结构用于重新present复数 [英] How can structures be used to represent complex numbers
问题描述
我需要写它使用结构来定义复杂的数字,即Z1 = X +易的程序。然后添加2复数。我需要弄清楚如何与我的code移动之前正确initalise他们。我已经尝试了一些事情,到目前为止,这是我想出最好的,它仍然没有编制。
下面是我的code的副本,我只需要解决这个问题的一部分,那么我就可以完成剩下的我自己。
#包括LT&;&stdio.h中GT;typedef结构complex1 {
浮* real1;
浮* imaginary1;
} complex1;
typedef结构complex2 {
浮* REAL2;
浮* imaginary2;
} complex2;
诠释主(){
结构complex1实;
结构complex1 * realptr;
结构complex1虚;
结构complex1 * imaginaryptr;
结构complex2实;
结构complex2 * realptr;
结构complex2虚;
结构complex2 * imaginaryptr; 的printf(请输入变量X1。);
scanf函数(%d个,&安培; real.real1);
的printf(请输入变量Y1。);
scanf函数(%d个,&安培; imaginary.imaginary1);
的printf(请输入变量X2);
scanf函数(%d个,&安培; real.real2);
的printf(请输入变量Y2。);
scanf函数(%d个,&安培; imaginary.imaginary2);
的printf(你输入:%D,%D,%D,%d个\\ N,
real.real1,imaginary.imaginary1,real.real2,imaginary.imagnary2);
返回0;
}
有文件中的多个错误,但也许这样的事情更是你想要什么?
#包括LT&;&stdio.h中GT;typedef结构_complex1 {
双real1;
双imaginary1;
} complex1;
不要使用相同的名称命名结构两次,我相信你想跳过real1和imaginary1指针 - 因为他们没有得到你什么
INT的main(){
complex1实;
complex1 * realptr;
complex1虚;
complex1 * imaginaryptr;
complex2 REAL2;
complex2 * realptr2;
complex2 imaginary2;
complex2 * imaginaryptr2;
复杂类型定义已经告诉编译器,它是一个结构。而且你不能有两个同名变量。
的printf(请输入变量X1。);
scanf函数(%LF,&安培; real.real1);
您需要调整您发送什么,它预计scanf函数是什么。 %F预计双*,而不是双**或(浮动**你的情况)。
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;
}
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).
这篇关于如何能结构用于重新present复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!