预测在C中是否可以用3个角(以及三角形的类型)形成三角形

预测在C中是否可以用3个角(以及三角形的类型)形成三角形,c,C,我是c语言的初学者。我的任务是检查用户输入3个角度是否可以形成三角形,并且三角形的类型(例如斜三角形、斜角三角形等)也必须打印到屏幕上。我被要求分两个部分来完成这项工作,并试图在主要部分中完成第一部分,同时将第二部分声明为函数 但是,如果不再次声明,我似乎无法在函数中使用主脚本中使用的相同变量。有没有办法做到这一点 我在谷歌上搜索了它,了解到了全局变量,但是,它是否真的适用于这种情况,因为变量的值是用户输入的,因此不能在主脚本和其他函数之外执行 这是我的代码: #include<stdio

我是c语言的初学者。我的任务是检查用户输入3个角度是否可以形成三角形,并且三角形的类型(例如斜三角形、斜角三角形等)也必须打印到屏幕上。我被要求分两个部分来完成这项工作,并试图在主要部分中完成第一部分,同时将第二部分声明为函数

但是,如果不再次声明,我似乎无法在函数中使用主脚本中使用的相同变量。有没有办法做到这一点

我在谷歌上搜索了它,了解到了全局变量,但是,它是否真的适用于这种情况,因为变量的值是用户输入的,因此不能在主脚本和其他函数之外执行

这是我的代码:

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

int main(){
    double ang1,ang2,ang3;

    printf("This program determines if a triangle can be formed with an input of 3 angles.\n");
    printf("Enter the first angle:  ");
    scanf("%lf",&ang1);
    printf("\nEnter the second angle:  ");
    scanf("%lf",&ang2);
    printf("\nEnter the third angle:  ");
    scanf("%lf",&ang3);

    if (ang1<=0.000000 || ang2<=0.000000 || ang3<=0.000000){//handles any angles below or equal to 0 degrees
        printf("\nError 404: Angle(s) are not above 0 degrees.\nPlease restart the program again\n");
    }
    else{
        if (ang1+ang2+ang3==180.000000){ //checks if angles add up to 180 degrees
            printf("\nA triangle can be formed by this three angles.\n");
        }
        else{
            printf("A triangle cannot be formed by this three angles.\n");
        }
    }
    task2();
}
task2(){ //In a new file
   double ang1,ang2,ang3;

    printf("This program determines if a triangle can be formed with an input of 3 angles.\n");
    printf("Enter the first angle:  ");
    scanf("%lf",&ang1);
    printf("\nEnter the second angle:  ");
    scanf("%lf",&ang2);
    printf("\nEnter the third angle:  ");
    scanf("%lf",&ang3);

    if (ang1<=0.000000 || ang2<=0.000000 || ang3<=0.000000){//handles any angles below or equal to 0 degrees
        printf("\nError 404: Angle(s) are not above 0 degrees.\nPlease restart the program again\n");
    }
    else if (ang1+ang2+ang3==180.000000){ //checks if angles add up to 180 degrees
            printf("\nA triangle can be formed by this three angles.");
        if (ang1==ang2==ang3){ //checks for equilateral triangle
            printf("\nThis is an equilateral triangle.");
            }
        if (ang1==ang2 || ang2==ang3 || ang1==ang3){ //checks for isoceles triangle
            printf("\nThis is an isoceles triangle.");
            }
        if (ang1!=ang2 || ang2!=ang3 || ang3!=ang1){ // chacks for scalene triangle
            printf("\nThis is a scalene triangle.");
        }
        if (ang1==90.000000 || ang2==90.000000 || ang3==90.000000){ //checks for right angles
            printf("\nThis is a right angle.");
        }
        else{
            printf("\nThis is an oblique triangle."); //when no angles are 90 degrees
        }
        if (ang1<90.000000 && ang2<90.000000&& ang3<90.000000){ //checks if all angles are acute
            printf("\nThis is an acute triangle.");
        }
        if (ang1>90.000000 || ang2>90.000000 || ang3>90.000000){ //checks if one angle is obtuse
            printf("\nThis is an obtuse triangle.");

        }

    }
    else {
        printf("A triangle cannot be formed by this three angles.\n");
    }

}

将角度作为参数传递给被调用函数:

void task2(double ang1, double ang2, double ang3) {
    // check what's necessary here
}

int main() {
    double ang1, ang2, ang3;
    // read user input
    // check whether angles make a triangle
    task2(ang1, ang2, ang3);
}

将变量作为参数传递给task2函数

void task2(double ang1, double ang2, double ang3);
int main (){
    ...
    task2(ang1, ang2, ang3);
}

void task2(double ang1, double ang2, double ang3) {
   ... 
}

您可以将这3个值作为参数传递给第二个函数。 定义如下:

void task2 (double ang1, double ang2, double ang3){
...
...
}
在第一个问题上这样称呼:

task2(ang1, ang2, ang3);
那么你就不必像现在这样要求他们两次了


此外,除非您真的需要全局变量,否则使用全局变量是不明智的

正如其他答案所说,将角度作为参数传递。除此之外,我还发现了一些问题:

        if (ang1==ang2 || ang2==ang3 || ang1==ang3){ //checks for isoceles triangle
            printf("\nThis is an isoceles triangle.");
            }
这应该是一个else if。如果是普通的旧If,等边三角形将被声明为等边和等腰三角形

在这个代码段中,每个或都应该变成AND&。不只是一对不平等,而是所有的不平等。另一个选择就是使用ang1=ang2=ang3


我觉得其他一切都很好。祝你好运

只是一个提示,您实际上不必键入那些尾随的零。0.00000完全等同于0.00000。还有一件事,不要将浮点值与==进行比较。它可能不会产生正确的值。代码的另一个问题是,在使用task2之前没有声明它,并且隐式int返回类型已被弃用。使用void task2void before main或向函数添加void将整个函数体移动到before main
if (ang1!=ang2 || ang2!=ang3 || ang3!=ang1){ // chacks for scalene triangle
                printf("\nThis is a scalene triangle.");
            }