我正在评估,必须用C编写一个简短的程序来输入顶点和边的大小,以显示多边形的周长和面积。 程序终止后,我必须显示:
这是如何用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');
}
我希望这是通过数组完成的,但是我不确定数组如何工作。
答案 0 :(得分:2)
首先对当前代码做一些说明
拥有
char choice; ... scanf ("%s", &choice);
您从 choice 中至少写了一个字符来放置空字符,行为是不确定的
您要输入字符吗
scanf (" %c", &choice);
%s
之前的空格允许绕过分隔符(换行符/空格)
为什么要return 0;
循环到“ Q”?删除它。
在所有打印品的末尾添加换行符,以将结果与问题分开(或者当然将“请输入形状...”替换为“ \ n请输入形状...”)它与上一个打印的同一行)
我鼓励您检查是否通过 scanf 输入了有效的内容,否则您不知道是否为scanf("%d", &radius);
输入了整数,因此请检查 scanf 返回1
我希望这是通过数组完成的,但是我不确定数组如何工作。
您不需要数组,对于“ a”,“ b”,“ c”和“ d”,总和可以每次更新,对于“ e”每次更新乘积,最后在< em>正方形
无论如何,如果您想记住,则需要多个数组,每个主题一个,则有两种解决方案:
然后在结尾('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'