【问题标题】:How to display the total sum and calculations from an input in C如何显示C中输入的总和和计算
【发布时间】:2019-05-08 12:22:43
【问题描述】:

我正在做一个评估,必须用 C 编写一个简短的程序来输入顶点和边的大小来显示多边形的周长和面积。 当程序终止时,我必须显示:

  • 一个。执行的计算总数
  • 乙。所有顶点的总和
  • c。所有边尺寸的总和
  • d.所有周长的总和
  • e.所有区域乘积的平方根

这是如何在 C 中完成的?谢谢

我尝试将它们存储在一个数组中,然后显示它们

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

int main() {
    float PI = 3.1415;
    char choice;
    float area, parameter;
    int radius, side, length, width;

    do {
        printf("Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quiit> ");
        scanf("%s", &choice);

        switch (choice) {
          case 'C':
            printf("Enter a radius of the circle: ");
            scanf("%d", &radius);
            area = (2 * radius) * PI;
            parameter = 2 * PI * radius;
            printf("The area of the circle is %.02f and parameter is %.02f",
                   area, parameter);
            break;

          case 'S':
            printf("Enter the side of the square: ");
            scanf("%d", &side);
            area = side * side;
            parameter = 4 * side;
            printf("The area of the circle is %.02f and parameter is %.02f",
                   area, parameter);
            break;

          case 'R':
            printf("Enter the width of the rectangle: ");
            scanf("%d", &width);
            printf("Enter the length of the rectangle: ");
            scanf("%d", &length);
            area = length * width;
            parameter = (2 * length) + (2 * width);
            printf("The area of the circle is %.02f and parameter is %.02f",
                   area, parameter);
            break;

          case 'Q':
            printf ("Thank and bye");
            break;

          default:
            printf ("Invalid input");
        }
        return 0;
    } while (choice != 'Q');
}

我希望这是通过数组完成的,但我不确定数组是如何工作的。

【问题讨论】:

  • 如果您能弄清楚问题所在,将会很有帮助。就目前而言,您刚刚给了我们代码和“如何?”。请附上您面临的错误或意外行为

标签: c geometry


【解决方案1】:

首先对当前代码的一些说明

拥有

 char choice;
 ...
 scanf ("%s", &choice);

选择中至少写一个字符来放置空字符,行为未定义

你想输入一个字符吗

scanf (" %c", &choice);

%s 之前的空格允许绕过分隔符(换行符/空格)

如果您想循环到 'Q',为什么要使用 return 0; ?删除它。

在所有打印的末尾添加换行符,以将其与之后的问题分开(或者当然将“请输入形状...”替换为“\n请输入形状...”以没有它在前一个打印的同一行)

我鼓励您检查是否通过 scanf 输入了有效的内容,否则您无法知道是否为 scanf("%d", &amp;radius); 输入了整数,因此请检查 scanf 返回1

我希望这是通过数组完成的,但我不确定数组是如何工作的。

您不需要数组,对于 'a' 'b' 'c' 和 'd' 可以每次更新总和,对于 'e' 每次更新产品然后最后执行 正方形

无论如何,如果你喜欢记住你需要几个数组,每个主题一个,你有两个解决方案:

  • 您使用静态大小的数组,在这种情况下,您必须限制输入的数量以免超出它
  • 您使用动态数组使用 malloc 然后 realloc 来增加它们的大小

然后在最后('Q')你从数组内容中计算所需的值


例如,根据预处理器标识符 ARRAYS 以两种方式管理“a”和“e”:

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

#ifdef ARRAYS
typedef struct Vector {
  float * values;
  size_t nValues;
} Vector;

// initialize the array, must be called because to add values
void init(Vector * v)
{
  v->values = malloc(0);
  v->nValues = 0;
}

// a a new value into the vector
void add(Vector * v, float f)
{
  v->values = realloc(v->values, (++v->nValues) * sizeof(float));
  v->values[v->nValues - 1] = f;
}

// print the content of the array
void pr(Vector * v, const char * what)
{
  printf("there are %d %s :", v->nValues, what);
  for (size_t i = 0; i != v->nValues; ++i)
    printf(" %.02f", v->values[i]);
  putchar('\n');
}
#endif

int main ()
{
  float PI = 3.1415;
  char choice;
  float area, parameter;
  int radius, side, length, width;
#ifdef ARRAYS
  Vector calculations, areas;

  init(&calculations);
  init(&areas);
#else
  int calcNumber = 0;
  float prodAreas = 1;
#endif

  do {
    printf("Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quit> ");
    scanf (" %c", &choice);

    switch (choice) {
    case 'C':
      printf ("Enter a radius of the circle: ");
      if (scanf ("%d", &radius) != 1) {
        puts("invalid value");
        return -1;
      }
      area = (2 * radius) * PI;
      parameter = 2 * PI * radius;
      printf ("The area of the circle is %.02f and parameter is %.02f\n",
              area, parameter);
#ifdef ARRAYS
      add(&calculations, area);
      add(&areas, area);
      add(&calculations, parameter);
#else
      calcNumber += 2;
      prodAreas *= area;
#endif
      break;

    case 'S':
      printf ("Enter the side of the square: ");
      if (scanf ("%d", &side) != 1) {
        puts("invalid value");
        return -1;
      }
      area = side * side;
      parameter = 4 * side;
      printf ("The area of the circle is %.02f and parameter is %.02f\n",
              area, parameter);
#ifdef ARRAYS
      add(&calculations, area);
      add(&areas, area);
      add(&calculations, parameter);
#else
      calcNumber += 2;
      prodAreas *= area;
#endif
      break;

    case 'R':
      printf ("Enter the width of the rectangle: ");
     if ( scanf ("%d", &width) != 1) {
        puts("invalid value");
        return -1;
      }
      printf ("Enter the length of the rectangle: ");
      if (scanf ("%d", &length) != 1) {
        puts("invalid value");
        return -1;
      }
      area = length * width;
      parameter = (2 * length) + (2 * width);
      printf ("The area of the circle is %.02f and parameter is %.02f\n",
              area, parameter);
#ifdef ARRAYS
      add(&calculations, area);
      add(&areas, area);
      add(&calculations, parameter);
#else
      calcNumber += 2;
      prodAreas *= area;
#endif
      break;

    case 'Q':
      puts ("Thank and bye");
      break;

    default:
      puts ("Invalid input");
    }
  } while (choice != 'Q');

#ifdef ARRAYS
  pr(&calculations, "calculations");

  pr(&areas, "areas");

  float e = 1;

  for (size_t i = 0; i != areas.nValues; ++i)
    e *= areas.values[i];
  printf("square root of the product of all areas : %.02f\n", sqrt(e));
#else
  printf("there are %d calculations\n", calcNumber);
  printf("square root of the product of all areas : %.02f\n", sqrt(prodAreas));
#endif

  return 0;
}

使用数组编译和执行:

pi@raspberrypi:/tmp $ gcc -DARRAYS -pedantic -Wall  -Wextra c.c -lm
pi@raspberrypi:/tmp $ ./a.out
Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quit> C
Enter a radius of the circle: 1
The area of the circle is 6.28 and parameter is 6.28
Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quit> S
Enter the side of the square: 1
The area of the circle is 1.00 and parameter is 4.00
Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quit> Q
Thank and bye
there are 4 calculations : 6.28 6.28 1.00 4.00
there are 2 areas : 6.28 1.00
square root of the product of all areas : 2.51
pi@raspberrypi:/tmp $ 

没有数组的编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wall  -Wextra c.c -lm
pi@raspberrypi:/tmp $ ./a.out
Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quit> C
Enter a radius of the circle: 1
The area of the circle is 6.28 and parameter is 6.28
Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quit> S
Enter the side of the square: 1
The area of the circle is 1.00 and parameter is 4.00
Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quit> Q
Thank and bye
there are 4 calculations
square root of the product of all areas : 2.51

我让你为'b''c'和'd'做

【讨论】:

    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2022-11-02
    • 2013-06-02
    • 2023-02-08
    • 2020-03-11
    相关资源
    最近更新 更多