首先对当前代码的一些说明
拥有
char choice;
...
scanf ("%s", &choice);
选择中至少写一个字符来放置空字符,行为未定义
你想输入一个字符吗
scanf (" %c", &choice);
%s 之前的空格允许绕过分隔符(换行符/空格)
如果您想循环到 'Q',为什么要使用 return 0; ?删除它。
在所有打印的末尾添加换行符,以将其与之后的问题分开(或者当然将“请输入形状...”替换为“\n请输入形状...”以没有它在前一个打印的同一行)
我鼓励您检查是否通过 scanf 输入了有效的内容,否则您无法知道是否为 scanf("%d", &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'做