我是愿意学习c的学生。我的问题是,用户需要输入形状名称,并且该形状名称应通过switch语句。但是我做不到。因为我不知道如何将字符串传递给switch语句。那有人可以帮我吗?我将添加代码以供参考。
#include <stdio.h>
int main(){
int h,w,r,l,area;
char shape[8];
printf("Please select a shape = ");
scanf("%s",&shape);
switch(shape[8]){
case 'circle':
printf("Enter value for radius = ");
scanf("%d",&r);
area = 3.14*r*r;
printf("Area of circle is = %d",area);
break;
case 'square':
printf("Enter value for width = ");
scanf("%d",&w);
area = w*w;
printf("Area of square is = %d",area);
break;
case 'rectangle':
printf("Enter value for width = ");
scanf("%d",&w);
printf("Enter value for height = ");
scanf("%d",&h);
area = w*h;
printf("Area of rectangle is %d",area);
break;
case 'triangle':
printf("Enter value for length = ");
scanf("%d",&l);
printf("Enter value for height = ");
scanf("%d",&h);
area = 1/2*l*w;
printf("Area of triangle is = %d",area);
break;
default:
printf("Invalid Shape");
}
return 0;
}
答案 0 :(得分:1)
不推荐使用switch()
来处理此问题,但是您可以做到。您将创建一个查找表(有效形状的字符串文字数组),并在检查用户输入是否为有效形状时,将保存与有效形状相对应的索引,并将该索引传递为{ {1}}变量。然后,您的案例将是整数值,您可以采取适当的措施。
例如,您可以使用以下命令创建一个简单的查找表:
switch()
/* lookup-table (array of pointers to string-literals) */
const char *shapes[] = { "circle", "square", "rectangle", "triangle" };
/* if you need a constant, #define one (or more) */
#define NSHAPES (int)(sizeof shapes / sizeof *shapes)
是NSHAPES
数组中形状的数量(您可以只使用shapes
,但要避免对数字进行硬编码... MagicNumbers 。 )
然后,您可以创建一个简单的函数来遍历数组,将用户输入的形状与查找表中的每个形状进行比较,如果找到则返回索引,如果找不到则返回4
,例如
-1
然后在/* function to validate shape is one of the valid shapes */
int validshape (const char *shape)
{
for (int i = 0; i < NSHAPES; i++) /* loop over each shape */
if (strcmp (shape, shapes[i]) == 0) /* compare shape with shapes[i] */
return i; /* return index if found */
return -1; /* return -1 if no match found */
}
中,接受用户输入,然后简单地将字符串传递给函数并进行验证,例如
main()
使用 int shape = -1;
...
shape = validshape (line); /* get shape index (-1 if none) */
if (shape != -1) /* check index is valid */
break; /* (break loop if valid) */
/* otherwise handle error, show allowed */
fputs (" error : invalid input.\n", stderr);
中的有效索引,您可以然后shape
,例如
switch(shape)
您可以更新每种情况以处理您的算术。
完整版本(使用 fputs ("\nvalid : ", stdout);
switch (shape) {
case 0: puts (shapes[0]); break;
case 1: puts (shapes[1]); break;
case 2: puts (shapes[2]); break;
case 3: puts (shapes[3]); break;
default: fputs ("oops -- shouldn't get here\n", stderr);
}
进行用户输入,因此新的C程序员不会陷入与fgets()
进行用户输入相关的许多陷阱中,可以这样做:
scanf()
使用/输出示例
#include <stdio.h>
#include <string.h>
/* lookup-table (array of pointers to string-literals) */
const char *shapes[] = { "circle", "square", "rectangle", "triangle" };
/* if you need a constant, #define one (or more) */
#define NSHAPES (int)(sizeof shapes / sizeof *shapes)
#define MAXC 1024
/* simple function to show allowed shapes */
void showallowed (void)
{
fputs (" allowed :", stdout); /* output prefix */
for (int i = 0; i < NSHAPES; i++) /* loop over each shape */
printf (" %s", shapes[i]); /* output shape */
putchar ('\n'); /* tidy up with newline */
}
/* function to validate shape is one of the valid shapes */
int validshape (const char *shape)
{
for (int i = 0; i < NSHAPES; i++) /* loop over each shape */
if (strcmp (shape, shapes[i]) == 0) /* compare shape with shapes[i] */
return i; /* return index if found */
return -1; /* return -1 if no match found */
}
int main (void) {
char line[MAXC]; /* buffer to hold all user-input */
int shape = -1;
while (1) { /* loop continually */
fputs ("\nenter shape: ", stdout); /* output prompt for shape */
if (!fgets (line, MAXC, stdin)) /* validate user input */
return 1;
line[strcspn (line, "\n")] = 0; /* trim \n from end of line */
shape = validshape (line); /* get shape index (-1 if none) */
if (shape != -1) /* check index is valid */
break; /* (break loop if valid) */
/* otherwise handle error, show allowed */
fputs (" error : invalid input.\n", stderr);
showallowed();
}
fputs ("\nvalid : ", stdout);
switch (shape) {
case 0: puts (shapes[0]); break;
case 1: puts (shapes[1]); break;
case 2: puts (shapes[2]); break;
case 3: puts (shapes[3]); break;
default: fputs ("oops -- shouldn't get here\n", stderr);
}
}
如果您还有其他问题,请告诉我。
答案 1 :(得分:0)
不幸的是,C不支持在case语句中传递字符串,因此要避免这种情况,您可以将if-else与strcmp()
一起使用,或者为每个形状分配一个数字,然后将其传递给switch语句。