我上一篇文章的延續,我正在嘗試使用結構和函式撰寫一個復數計算器。我的程式必須具有從用戶輸入中讀取復數的函式,并且必須具有用于添加復數的另一個函式。這是我得到的函式原型:
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 變數未初始化,而且我不知道如何初始化 struct 變數。
uj5u.com熱心網友回復:
您似乎對指標的作業方式有誤解。
按照您撰寫的方式read_complex,您似乎認為該函式中的指標user1和 會user2自動參考該函式中的同名變數main。這不是 C 的作業方式。
您實際擁有的是兩個未初始化的指標read_complex,然后您嘗試通過->運算子取消參考。即使宣告這樣做,您也不會從此函式回傳任何內容。
此函式應該回傳 a 的單個副本Complex,因此您應該在本地創建一個副本,將值讀入其中,然后回傳它。然后應將回傳值分配給區域變數。所以這也意味著你需要為你閱讀的每個人 呼叫這個函式Complex。
此外,您可能想查看如何乘以復數。
uj5u.com熱心網友回復:
這是我得到的函式原型:
Complex read_complex(void)這是我必須使用的原型,無法更改。
read_complex()回傳 1 個型別的物件Complex。用它來讀取 1,而不是 2,復數。
讓我們使用該函式來讀取并創建另一個呼叫它來讀取 2 個復數的函式。
// Receive the address of 2 objects,
// so this code knows where to save the result.
void read_complex2(Complex *a, Complex *b) {
printf("Enter first complex number: ");
*a = read_complex();
printf("Enter the second complex number: ");
*b = read_complex();
}
read_complex()
read_complex()變得簡單:閱讀 2 double。檢查結果。
Complex read_complex(void) {
Complex x;
if (scanf("%lf %lf", &x.RealPart, &x.ImagPart) != 2) {
fprintf(stderr, "Invalid input.\n");
exit(EXIT_FAILURE);
}
return x;
}
主要的()
調整如下:
if (ent[0] == 'A') {
//read_complex();
read_complex2(&user1, &user2);// Pass in addresses of objects.
add_complex(user1, user2);
...
uj5u.com熱心網友回復:
考慮這個函式:
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,然后訪問它們的欄位。但是您實際上并沒有為這些指標分配任何內容。你要動態分配它們,或者非動態分配他們。
Complex *user1 = malloc(sizeof(Complex));
Complex user1;
你的另一個問題是你不回傳任何東西。您可能只想一次讀取一個Complex數字,然后根據您的函式簽名按值回傳它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/341511.html
