Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中区域程序的问题_C_Math_Area - Fatal编程技术网

c语言中区域程序的问题

c语言中区域程序的问题,c,math,area,C,Math,Area,所以我一直在做一个程序,可以找到一个形状的面积 我的编译器上出现了多个错误,说我正在比较 一个指针和一个整数,但我不是。。。至少我不这么认为。程序运行,但是当我输入字符串时,它崩溃了 这是我的密码 #include <stdio.h> #include <math.h> int main(void){ char str_1; printf("Hello! This program will find the area of selceted figrues!\n");

所以我一直在做一个程序,可以找到一个形状的面积 我的编译器上出现了多个错误,说我正在比较 一个指针和一个整数,但我不是。。。至少我不这么认为。程序运行,但是当我输入字符串时,它崩溃了

这是我的密码

#include <stdio.h>
#include <math.h>
int main(void){

char str_1;

printf("Hello! This program will find the area of selceted figrues!\n");
printf("What is your shape? The only shapes that work are Triangles,\
Rectangles, and Circles\n");

scanf("%c", str_1);
    do {
        printf("That is an invalid input the only shapes that work are Triangles, \
        Rectangles, and Circles\n");
        scanf("%c", str_1);
    } 
    while(str_1 != "circle", "rectangle", "triangle");

    if (str_1 == "circle") {
        printf("What is the radius?\n");
        scanf("%i");
    }
}
#包括
#包括
内部主(空){
charstru1;
printf(“您好!此程序将找到所选图片的区域!\n”);
printf(“你的形状是什么?唯一有效的形状是三角形\
矩形和圆形\n“;
scanf(“%c”,str_1);
做{
printf(“这是一个无效的输入,唯一有效的形状是三角形\
矩形和圆形\n“;
scanf(“%c”,str_1);
} 
而(str_1!=“圆形”、“矩形”、“三角形”);
如果(str_1==“圆圈”){
printf(“半径是多少?\n”);
scanf(“%i”);
}
}

您应该对字符串使用
char*
char[]
,并重新构造程序流:

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

void msg() {
    printf("What is your shape? The only shapes that work are Triangles,\
Rectangles, and Circles\n");
}

int main(void) {
    char *str_1 = malloc(32);
    float f, f2, f3;
    printf("Hello! This program will find the area of selected figures!\n");
    msg();
    scanf("%s", str_1);
    for (; ;) {
        if (strcmp(str_1, "circle") && strcmp(str_1, "rectangle") && strcmp(str_1, "triangle")) {
            printf("That is an invalid input the only shapes that work are Triangles, \
        Rectangles, and Circles\n");
            msg();
            scanf("%s", str_1);
        }
        if (!strcmp(str_1, "circle")) {
            printf("What is the radius?\n");
            scanf("%f", &f);
            printf("The area is %f\n", f * M_PI * M_PI);
            msg();
            scanf("%s", str_1);
            continue;
        }
        else if (!strcmp(str_1, "rectangle")) {
            printf("What is the length?\n");
            scanf("%f2", &f2);
            printf("What is the height?\n");
            scanf("%f3", &f3);
            printf("The area is %f\n", f2*f3);
            msg();
            scanf("%s", str_1);
            continue;
        }
    }
    free(str_1); /* unreachable */
}

char
-->
char
array.use
strcmp()
while(str_1!=“圆形”、“矩形”、“三角形”)C不会这样做。你在用什么教科书?我们将向您介绍适当的函数和逻辑运算符。
scanf(“%c”,str_1)->
scanf(“%c”和stru_1)
在格式字符串中添加一个空格,并使用
str_1
上的
&
运算符。请相信编译器。它比你更了解C。除非你为
str_1
@RSahu分配内存,否则它会崩溃的。好的,我会立即改进代码。你似乎是说我们需要做mallocEither,或者保留
字符类型的
str_1
,并在
scanf
中使用
&str_1
。我发现您的程序还有一个问题,即它没有考虑使用负数的用户。我的也没有,这是我刚刚注意到的
Debug/gnu
Hello! This program will find the area of selected figures!
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles
circle
What is the radius?
5
The area is 49.348022
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles
cube
That is an invalid input the only shapes that work are Triangles,         Rectangles, and Circles
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles
rectangle
What is the length?
2
What is the height?
4
The area is 8.000000
What is your shape? The only shapes that work are Triangles,Rectangles, and Circles