程序中未初始化的变量将极性转换为笛卡尔,反之亦然(C)

时间:2015-10-29 16:42:49

标签: c floating-point function-prototypes

所以我们的程序是将笛卡尔坐标x,y转换为极坐标r,theta使用原型函数放置在主函数之后,从main中输出完成。在一些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,只是错过了最后一步。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:1)

原型函数签名必须与实际的函数实现相匹配。你的代码不是这种情况。

答案 1 :(得分:1)

代码中存在许多错误,我在下面的代码中对此进行了评论。总结一下:

  • 函数原型与函数实现不匹配。
  • 功能&#39;局部变量表示为函数参数。
  • 以错误的顺序定义的函数参数。
  • 尝试返回函数名称作为其值。
  • 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
}

答案 2 :(得分:0)

float cartesianToPolar1(float x, float y){
    float r;
    r=sqrt(x*x+y*y);
    return r;
}

答案 3 :(得分:0)

您已通过(未初始化的)本地变量隐藏了您的函数。这导致了混乱。

你有:

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]()函数也会出现问题;您需要将rtheta都传递给这两个函数 - 这样他们就可以返回xy

您还可以改进功能名称:

  float cartesianToPolarTheta(float x, float y);
  float cartesianToPolarRadius(float x, float y);

  float polarToCartesianX(float r, float theta);
  float polarToCartesianY(float r, float theta);