我将解释该程序应该如何工作。该计划将询问您的体重,然后询问您想要在哪个星球上进行体重训练。之后,它应该告诉你在这个星球上你会有多重。任何人都可以给我一些提示如何让它工作。是否有任何方式打印出单词和它具有的数字值。例如Fox = 12然后打印出值12和单词Fox。
#include <stdio.h>
#include <math.h>
int main()
{
float merkuur=0.378, veenus=0.907, maa=1, marss=0.377, jupiter=2.364, saturn=1.064, uraan=0.889, neptuun=1.125, K, X;
char P;
printf("Enter your weight:");
scanf("%f", &K);
printf("Enter planet (merkuur, veenus, maa, marss, jupiter, saturn, uraan, neptuun):");
scanf("%s", &P);
X = P*K;
printf("Your weight on the planet %.2s would be %s.2f kilograms\n",P,X);
system ("pause");
}
答案 0 :(得分:2)
以简单明了的方式实施示例:
#include <stdio.h>
#include <string.h>
int main(void)
{
struct elem_t {
char name[32];
float multiplier;
} table[] = {{"merkuur", 0.378}, {"veenus", 0.907}, {"maa", 1}, {"marss", 0.377}, {"jupiter", 2.364}, {"saturn", 1.064}, {"uraan", 0.889}, {"neptuun", 1.125}, {"", -1}};
float K, X;
int i;
char P[32];
printf("Enter your weight:");
if (scanf("%f", &K) != 1)
{
puts("input error");
return 1;
}
printf("Enter planet (merkuur, veenus, maa, marss, jupiter, saturn, uraan, neptuun):");
if (scanf("%31s", P) != 1)
{
puts("input error");
return 1;
}
X = -1;
for (i = 0; table[i].multiplier > 0; i++)
{
if (strcmp(table[i].name, P) == 0)
{
X = table[i].multiplier*K;
break;
}
}
printf("Your weight on the planet %.2s would be %.2f kilograms\n",P,X);
return 0;
}