我正在做一个小作业,我应该做一个食物菜单.无论如何,我的开关不工作.我正在尝试使用一个简单的函数,我可以传递"fish","drink"或"chips"的值,然后它将输出:
"Are you ordering FISH?" (or chips/drink)
Run Code Online (Sandbox Code Playgroud)
我无法让开关工作,它应该检测我传入的内容,然后根据开关盒输出printf.
码:
#include <stdio.h>
void menu() {
printf("\nWelcome to Sunny FISH & CHIPS!\n\n");
printf("######## Fish : Haddock(K) Large(L) | $5.00\n");
printf("# FOOD # Halibut(T) Large(L) | $4.00\n");
printf("######## Chips: Cut(C) Large(L) | $2.00\n");
printf(" Ring(R) Large(L) | $3.00\n");
printf(" | \n");
printf("########## Soft Drinks(S) Large(L) | $2.00\n");
printf("# DRINKS # Coffee(C) Large(L) | $1.75\n");
printf("########## Tea(T) Large(L) | $1.50\n");
printf("---------------------------------------------\n");
printf("Note: Medium price: 80%% of large.\n");
printf(" Small price: 60%% of large.\n");
printf("TAX is 10%%.\n");
printf("More than 5 fish, 10%% discount on drink.\n");
printf("Every 10 fish purchased, get 1 free softdrink.\n");
printf(" - size of drink is according to size of fish\n");
}
void question (char choice[5]) {
switch (choice[5])
{
case choice["fish"]:
printf("Do you order FISH?\n");
case choice["drink"]:
printf("Do you order CHIPS?\n");
case choice["chips"] :
printf("Do you order DRINKS?\n");
default :
printf("Enter a valid choice: \n");
}
}
main() {
// menu();
question("fish");
}
Run Code Online (Sandbox Code Playgroud)
Ç 不支持该类型的交换机,但如果它会,语法将是
switch(choice)
{
case "fish":
something();
break;
case "drink":
other_thing();
break;
}
Run Code Online (Sandbox Code Playgroud)
转向我通常比if(else ifs)的(长)列表更清晰.即使在这种情况下,它似乎过于复杂,我会更喜欢这样的方法:
#include <stdio.h>
#include <string.h>
enum menu_items { FISH, DRINK, CHIPS, UNKNOWN };
struct items
{
char *name;
enum menu_items id;
} items_list[] = {
{ "fish", FISH },
{ "drink", DRINK },
{ "chips", CHIPS }
};
int main(void)
{
int i;
enum menu_items mid;
struct items *choice = NULL;
// ...
for(i = 0, choice = NULL; i < sizeof items_list/sizeof (struct items); i++)
{
if (strcmp(answer, items_list[i].name) == 0)
{
choice = items_list + i;
break;
}
}
mid = choice ? choice->id : UNKNOWN;
// the following would be enough to obtain the output of your example;
// I've not embodied the code into a func, but it's easy to do if you need
if ( mid != UNKNOWN )
{
// the function a_func transforms the string of the food
// e.g. to uppercase, or it could map it to whatever according to some
// other data... or expand the struct to hold what you want to output
// with "fish", "drink", "chips", e.g. choice->screen_name
printf("Do you order %s?\n", a_func(choice->name));
}
else
{
printf("Enter a valid choice:\n");
}
// ---------
// or if you prefer the switch you have something like:
switch(mid)
{
case FISH:
printf("fish\n");
break;
case DRINK:
printf("drink\n");
break;
case CHIPS:
printf("chips\n");
break;
default:
printf("unknown choice\n");
break;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果您选择正确的方法,您的代码可能始终相同,只有您的数据增长.
您不能将switch语句与字符串一起使用.
您可以考虑使用strcmp比较字符串.
if (strcmp(choice,"fish")==0) {
//fish
}
else if (strcmp(choice,"drink")==0) {
//drink
}
.
.
.
Run Code Online (Sandbox Code Playgroud)
除了其他答案之外,如果您发现您的选择列表都以一个独特的字母开头(或者在另一个位置有一个唯一的字母),那么您可以switch在那封信上:
switch (choice[0]) {
case 'f':
// they chose fish
break;
case 'c':
// they chose chips
break;
case 'd':
// they chose drink
}
Run Code Online (Sandbox Code Playgroud)
这将比使用更快strcmp(虽然它对您的情况无关紧要)并且可维护性较差.但是,了解所有选项并了解如何使用其中一些内容是很好的.
| 归档时间: |
|
| 查看次数: |
46286 次 |
| 最近记录: |