C关于功能

时间:2016-03-07 23:21:32

标签: c

我是编程的新手,我正在尝试制作一个包含3个函数的程序,这些函数要求用户输入圆{0}或 <script type="text/javascript"> $(document).ready(function() { $( "#move" ).draggable({ containment: 'parent' stop: function(event, ui) { var pos_x = ui.offset.left; var pos_y = ui.offset.top; var username = ui.helper.data('username'); //Do the ajax call to the server $.ajax({ type: "POST", url: "coordsave.php", data: {x: pos_x, y: pos_y, username: username} }).done(function( msg ) { alert( "Data Saved: " + msg ); }); } }); }); </script> 表示直角三角形。然后输入半径c或双边三角形。我在使用t函数时遇到问题,它在两种情况下都将答案打印为c。如果我在圆圈或三角形函数内输入display语句,它会正确打印,但我需要函数0。这是我的代码。任何见解都将不胜感激。

printf

1 个答案:

答案 0 :(得分:1)

您的circlert函数只更新其参数,而不是main中的局部变量。您必须将指针传递给circlert以更新它们。

以下是更正后的版本:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void circle(int num, double *area, double *per) {
    *area = M_PI * pow(num, 2);
    *per = 2 * M_PI * num;
}

void rt(int num, int num1, double *area, double *per) {
    *area = 0.5 * num * num1;
    *per = (num + num1) + sqrt(pow(num, 2) + pow(num1, 2));
}

void display(char entry, double area, double per) {
    if (entry == 'c') {
        printf("The circle has area %.1lf and perimeter %.1lf.\n", area, per);
    } else {
        printf("The right triangle has area %.1lf and perimeter %.1lf.\n", area, per);

    }
}

int main() {        
    char entry;
    int num, num1;
    double area = 0;
    double per = 0;

    printf("Please enter 'c' for a circle or 't' for a right triangle\n");
    scanf("%c", &entry);
    if (entry == 'c') {
        printf("Please enter the radius for the circle.\n");
        scanf("%d", &num);
        getchar();
        circle(num, &area, &per);
    } else
    if (entry == 't') {
        printf("Please enter the length of two sides.\n");
        scanf("%d %d", &num, &num1);
        getchar();
        rt(num, num1, &area, &per);
    } else {          
        printf("Invalid input!\n");
    }
    display(entry, area, per);
    return 0;
}