c语言多种图像求面积

多种图形求面积

描述

编写一个程序,实现求矩形,三角形,圆形的面积(PI取3. 1415926,输出结果取整)

输入

先输入一个整数type,

如果type=1,则输入长宽a b求矩形面积。如果type=2,则输入底高a b求三角形面积,如果type=3,则输入半径r求圆形面积。

输出

输出面积

输入样例1自

输出样例1

123

6

img

img

img


#include <stdio.h>
#include <stdarg.h>
#define PI 3.1415926

double S(int n, ...)
{
    double x;
    double s = 0.0;
    va_list ap;
    va_start(ap, n);
    x = va_arg(ap, double);
    if (n == 1)

        s = va_arg(ap, double) * x;

    else if (n == 2)

        s = va_arg(ap, double) * x / 2;

    else if (n == 3)

        s = x * x * PI;

    va_end(ap);
    return s;
}

int main(int argc, char *argv[])
{
    int n;
    double x, y;
    scanf("%d%lf", &n, &x);
    if (n == 1)
    {
        scanf("%lf", &y);
        printf("矩形面积:%.0lf\n", S(n, x, y));
    }
    else if (n == 2)
    {
        scanf("%lf", &y);
        printf("三角形面积:%.0lf\n", S(n, x, y));
    }
    else if (n == 3)
        printf("圆面积:%.0lf\n", S(n, x));

    return 0;
}

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

#define PI 3.1415926

int main(int argc, char* argv[]) {
  int type;
  scanf("%d", &type);

  if (type == 1) {
    // 求矩形面积
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", a * b);
  } else if (type == 2) {
    // 求三角形面积
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", a * b / 2);
  } else if (type == 3) {
    // 求圆形面积
    int r;
    scanf("%d", &r);
    printf("%d\n", (int)(PI * r * r));
  } else {
    printf("无效的类型\n");
  }

  return 0;
}