所以,我正在努力制作一个suvat解算器(物理方程式),我需要阅读他们需要找到的东西(找到)以及他们不知道并且不需要的东西知道(未知)。它会在查找中进行扫描,但在询问未知之前会直接询问变量。为什么呢?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[]) {
printf ("What are you trying to find:\nDistance, Inital Velocity, Final Velocity, Accelartion or Time\n(Enter first letter)\n");
char find;
scanf("%c", &find);
char unknown;
printf ("What don't you know: \n");
scanf("%c", &unknown);
int i = 0; //initial velocity
int f = 0; //final velocity
int a = 0; //accelaration
int t = 0; //time
int d = 0; //distance
if (((find != 'i') && (find != 'I')) && ((unknown != 'i') && (unknown != 'I'))){
printf("What is the Inital Velocity: \n");
scanf("%d", &i);
}
if (((find != 'd') && (find != 'D')) && ((unknown != 'd') && (unknown != 'D'))){
printf("What is the Distance: \n");
scanf("%d", &d);
}
if (((find != 'f') && (find != 'F')) && ((unknown != 'f') && (unknown != 'F'))){
printf("What is the Final Velocity: \n");
scanf("%d", &f);
}
if (((find != 'a') && (find != 'A')) && ((unknown != 'a') && (unknown != 'A'))){
printf("What is the Accelartion: \n");
scanf("%d", &a);
}
if (((find != 't') && (find != 'T')) && ((unknown != 't') && (unknown != 'T'))){
printf("What is the Time: \n");
scanf("%d", &t);
}
//printf("find: %c, i: %d, f: %d, a: %d, d: %d, t: %d", find, i, f, a, d, t);
if ((find == 'i') || (find == 'I')) {
if ((unknown == 's') || (unknown == 'S')) {
i = a*t - f;
printf("%d\n", i);
}
else if ((unknown == 'a') || (unknown == 'A')) {
i = ((2*d)/t) - f;
printf("%d\n", i);
}
else if ((unknown == 't') || (unknown == 'T')) {
i = sqrt(pow(f,2)-(2*a*d));
printf("%d\n", i);
}
else if ((unknown == 'f') || (unknown == 'F')) {
i = (d - (0.5*a*pow(t,2)))/t;
printf("%d\n", i);
}
}
return EXIT_SUCCESS;
}
答案 0 :(得分:2)
scanf(" %c", &find);
// ^ space
应该是
scanf
在您的计划中,\n
从您的输入键获取 bodyStyle:'overflowY: auto',
autoScroll:false,
setAutoScroll:false,
作为第二个字符
答案 1 :(得分:1)
发生这种情况是因为, ENTER 键在第一个scanf()
存储在输入缓冲区后按[a scanf()
]并提供自动输入[next] scanf("%c", &unknown);
。
在您的代码中,您需要更改
scanf(" %c", &unknown);
^
|
notice here
到
\n
这将忽略所有类似空白的输入(包括argc
)并读取第一个非空白字符(即预期的输入)。
建议:
如果您不打算使用argv
和main()
,int main(void)
的推荐内容为switch
。
您应该使用if-else if-else
个案而非长期<line>
条件链。