簡體   English   中英

我的 C 代碼給了我很多錯誤

[英]My code in C is giving me a lot of errors

問題說明:任何三角形的面積公式是:

A = √(s(s-a)(s-b)(s-c))     where   s=  (a+b+c)/2   

編寫一個程序,讓用戶至少進行一次面積計算,您的程序將提示用戶是否希望繼續,一旦用戶輸入字符 N 或 n,它就會停止。 假設長度單位是厘米。 您的解決方案必須包含一個用戶定義的函數,該函數返回三角形的面積,其輸入是三邊。

float areaTriangle(float sideA, float sideB, float sideC); 

以下是我目前擁有但由於錯誤而無法完成的代碼。

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

float areaTriangle(float sideA, float sideB, float sideC);

int main() {
    float sideA, sideB, sideC;
    char answer;

    printf("Please enter the length of side A: \n");
    scanf("%f", &sideA);

    printf("Please enter the length of side B: \n");
    scanf("%f", &sideB);

    printf("Please enter the length of side C: \n");
    scanf("%f", &sideC);

    do {
        printf("Would you like to solve another\n"
               "exercise?\n"
               " Y or N ");
        scanf(" %c", &answer);
        printf("The area of the triangle is: %f", &areaTriangle(sideA, sideB, sideC));
    } while (char == 'Y');

    do {
        printf("Would you like to solve another\n"
               "exercise?\n"
               " Y or N ");
        scanf(" %c", &answer);
        printf("You have selected to quit out of the program.\n"
               "Thank you for using the program.\n");
    } while (char == 'N');

    return 0;
}

float areaTriangle(float sideA, float sideB, float sideC) {
    float sideVariable;

    sideVariable = (sideA + sideB + sideC) / 2;

    areaTriangle = sqrt(sideVariable * (sideVariable - sideA) * 
                        (sideVariable - sideB) * (sideVariable - sideC));
    return areaTriangle;
}

我相信我搞砸了我的函數定義和函數調用。 另外,我不確定是否應該擁有我包含的所有變量。

我得到的錯誤列表:

45:error: lvalue required as unary '&' operand
46 and 51:expected expression before 'char'
60:note declared here
64:error: lvalue required as left operand of assignment
68:error: expected expression before 'float'
69:warning: control reaches end of non-void function

左值需要作為一元“&”操作數

在調用areaTriangle()之前你不應該有& 它返回一個float ,這就是與%f對應的參數所需要的。

'char' 之前的預期表達式

char是一個類型說明符。 while (char == 'Y')應該比較一個變量,而不是一個類型,例如while (answer == 'Y')

左值需要作為賦值的左操作數

您不能分配給函數的名稱。 您應該聲明一個新變量:

float result = sqrt(sideVariable*(sideVariable - sideA)*(sideVariable - sideB)
                    *(sideVariable - sideC));

'float' 之前的預期表達式

您不應該在return語句中包含類型聲明。 應該是return result;

您不應該使用do-while來測試對問題的響應,只需使用if 你可以在整個身體周圍放一個循環來重復這一切。

使用scanf()讀取單個字符時,您應該在%c之前放置一個空格以跳過輸入緩沖區中剩余的任何空格。 請參閱程序不會在 scanf("%c", &ch) 行上停止,為什么? 詳情。

完整修改后的代碼:

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

float areaTriangle(float sideA, float sideB, float sideC);

int main()
{
    while (1) {
        float sideA,sideB,sideC;
        char answer;

        printf("Please enter the length of side A: \n");
        scanf("%f", &sideA);

        printf("Please enter the length of side B: \n");
        scanf("%f", &sideB);

        printf("Please enter the length of side C: \n");
        scanf("%f", &sideC);

        printf("The area of the triangle is: %f", areaTriangle(sideA,sideB,sideC));

        printf("Would you like to solve another\n"
               "exercise?\n"
               " Y or N ");
        scanf(" %c", &answer);

        if (toupper(answer) != 'Y') {
            printf("You have selected to quit out of the program.\n"
                   "Thank you for using the program.\n");
            break;
        }
    }

    return 0;
}

float areaTriangle(float sideA, float sideB, float sideC)
{
    float sideVariable;

    sideVariable = (sideA + sideB + sideC) / 2;
    float result = sqrt(sideVariable*(sideVariable - sideA)*(sideVariable - sideB)
                        *(sideVariable - sideC));
    return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM