[英]Uninitialized variables in program to convert polar to cartesian and vice-versa(C)
因此,我們的程序是使用原型函數將笛卡爾坐標x,y轉換為極坐標r,theta放置在主函數之后,並從主函數內部完成輸出。 經過TA幫助后,我的程序看起來像這樣,但是編譯器告訴我我的原型尚未初始化。 這是我的代碼:
//
//ESE 124 Homework 8
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
float cartesianToPolar1(float x, float y);
float cartesianToPolar2(float x, float y);
float polarToCartesian1(float r);
float polarToCartesian2(float theta);
int main(){
int runmode;
float x, y, r, theta;
float cartesianToPolar1, cartesianToPolar2, polarToCartesian1, polarToCartesian2;
while(1){
printf("Please enter a value of 1 or 2 for runmode: ");
scanf_s("%d", &runmode);
switch(runmode){
case 1:
printf("Please enter in the keyboard a value for 'x': ");
scanf_s("%f", &x);
printf("Please enter in the keyboard a value for 'y': ");
scanf_s("%f", &y);
r=cartesianToPolar1;
theta=cartesianToPolar2;
printf("\nx=%.2f\ty=%.2f\tr=%.2f\ttheta=%.2f\n", x,y,r,theta);
break;
case 2:
printf("Please enter in the keyboard a value for 'r': ");
scanf_s("%f", &r);
printf("Please enter in the keyboard a value for 'theta': ");
scanf_s("%f", &theta);
x=polarToCartesian1;
y=polarToCartesian2;
printf("\nr=%.2f\ttheta=%.2f\tx=%.2f\ty=%.2f\n", r,theta,x,y);
break;
default:
printf("\nUnallowed value of runmode, Please re-enter a value of 1 or 2.\n");
}
}
return 0;
}
float cartesianToPolar1(float x, float y, float r){
float cartesianToPolar1=r;
r=sqrt(x*x+y*y);
return cartesianToPolar1;
}
float cartesianToPolar2(float y, float x, float s, float theta){
float cartesianToPolar2=theta;
s=y/x;
theta=atan(s);
return cartesianToPolar2;
}
float polarToCartesian1(float theta, float r, float x){
float polarToCartesian1=x;
x=r*cos(theta);
return polarToCartesian1;
}
float polarToCartesian2(float theta, float r, float y){
float polarToCartesian2=y;
y=r*sin(theta);
return polarToCartesian2;
}
我嘗試將它們聲明為0和1,但到目前為止沒有任何效果。 至於實際的代碼,我完全按照教授根據TA所希望的方式來編寫,只是缺少最后一步。 任何幫助,將不勝感激!
原型功能簽名必須與實際功能實現相匹配。 您的代碼不是這種情況。
代碼中有很多錯誤,我在下面的代碼中已對此進行了評論。 歸納起來是:
atan(y/x)
計算中可能除以0的錯誤。 main
具有與函數名稱重復的變量。 最后,我將把float
的用法更改為double
(對scanf
格式說明符進行了相應的更改)。 數學庫函數具有double
功能,並且會對此發出警告。
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
float cartesianToPolar1(float x, float y);
float cartesianToPolar2(float x, float y);
float polarToCartesian1(float r, float theta);
float polarToCartesian2(float r, float theta);
int main(void){
int runmode;
float x, y, r, theta;
while(1){
printf("Please enter a value of 1 or 2 for runmode: ");
scanf_s("%d", &runmode);
switch(runmode){
case 1:
printf("Please enter in the keyboard a value for 'x': ");
scanf_s("%f", &x);
printf("Please enter in the keyboard a value for 'y': ");
scanf_s("%f", &y);
r=cartesianToPolar1(x, y); // <--- pass arguments
theta=cartesianToPolar2(x, y); // <--- pass arguments
printf("\nx=%.2f\ty=%.2f\tr=%.2f\ttheta=%.2f\n", x,y,r,theta);
break;
case 2:
printf("Please enter in the keyboard a value for 'r': ");
scanf_s("%f", &r);
printf("Please enter in the keyboard a value for 'theta': ");
scanf_s("%f", &theta);
x=polarToCartesian1(r, theta); // <--- pass arguments
y=polarToCartesian2(r, theta); // <--- pass arguments
printf("\nr=%.2f\ttheta=%.2f\tx=%.2f\ty=%.2f\n", r,theta,x,y);
break;
default:
printf("\nUnallowed value of runmode, Please re-enter a value of 1 or 2.\n");
}
}
return 0;
}
float cartesianToPolar1(float x, float y){ // <--- remove unnecessary arguments
float r; // <--- correct variable
r=sqrt(x*x+y*y);
return r; // <--- return calculated value
}
float cartesianToPolar2(float x, float y){ // <--- swapped reversed arguments too
float theta; // <--- correct variable
//s=y/x; // <--- avoid divide by zero potential
theta=atan2(y, x);
return theta; // <--- return calculated value
}
float polarToCartesian1(float r, float theta){ // <--- swapped reversed arguments too
float x; // <--- correct variable
x=r*cos(theta);
return x; // <--- return calculated value
}
float polarToCartesian2(float r, float theta){ // <--- swapped reversed arguments too
float y; // <--- correct variable
y=r*sin(theta);
return y; // <--- return calculated value
}
float cartesianToPolar1(float x, float y){
float r;
r=sqrt(x*x+y*y);
return r;
}
您已通過(未初始化的)局部變量隱藏了函數。 這導致混亂。
你有:
float cartesianToPolar1(float x, float y);
float cartesianToPolar2(float x, float y);
float polarToCartesian1(float r);
float polarToCartesian2(float theta);
int main(){
int runmode;
float x, y, r, theta;
// This next line is a major cause of confusion!
float cartesianToPolar1, cartesianToPolar2, polarToCartesian1, polarToCartesian2;
while(1){
…
r=cartesianToPolar1; // Cannot write cartesianToPolar1(x, y) here
theta=cartesianToPolar2; // Cannot write cartesianToPolars2(x, y) here
局部變量意味着main()
不再可以調用函數。 當您隱藏具有相同名稱的本地對象的全局對象時,某些編譯器會生成警告。 對於GCC,您將使用-Wshadow
來獲取信息。
你需要:
float cartesianToPolar1(float x, float y);
float cartesianToPolar2(float x, float y);
float polarToCartesian1(float r);
float polarToCartesian2(float theta);
int main(){
int runmode;
float x, y, r, theta;
// removed
while(1){
…
r = cartesianToPolar1(x, y);
theta = cartesianToPolar2(x, y);
您還會對polarToCartesian[12]()
函數有疑問。 您需要將r
和theta
都傳遞給兩個函數,以便它們可以返回x
或y
。
您還可以改進函數名稱:
float cartesianToPolarTheta(float x, float y);
float cartesianToPolarRadius(float x, float y);
float polarToCartesianX(float r, float theta);
float polarToCartesianY(float r, float theta);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.