Я новичок в Stackoverflow, и я очень, совсем новичок в кодировании.Просто возиться с C. Вот что я пытаюсь сделать здесь (не берите эту программу с научной точки зрения), это программа, которая рассчитывает для специальных уравнений относительности длины, массы и времени.На самом деле у меня есть 3 вопроса:
Когда я пытаюсь вставить другие символы в вопросы y / n, все работает, но, например, если я ввожу "sfkl", предупреждение появляется 4 раза, потому что я ввел 4 символа.И если я поставлю пробел, он даже не выдаст предупреждение, пока я не поставлю еще один символ и не войду в него.Могу ли я заставить его выдавать 1 предупреждение независимо от того, сколько символов я ввожу в одну строку (включая пробел)?
Другой мой вопрос: я как бы запретил вводить что-либо, кроме y / n, но для ввода с двойным значением(масса, длина и время), я не могу понять подобную систему (запрашивая двойное значение снова и снова).Можете ли вы предложить мне решение?
И мой третий вопрос: когда я выполняю "scanf_s ("% c ", & answer);", если я не ставлю пробел перед "% c", это нене работает должным образом.Он регистрирует ввод и просит меня ввести только y / n.Зачем нужен пробел перед этим?
Вот код:
#include <stdio.h>
#include <math.h>
#define LIGHT 299792458
int input();
int main()
{
printf("\n\n\tThis program calculates how length, mass and time changes with respect to your speed.\n\n\tThe values you enter are the quantites which are observed by a stationary observer and the output values are the quantites observed by the person in a vehicle which is moving at the speed that you enter.");
input();
return 0;
}
int input()
{
double length, mass, utime, speed;
char answer;
do
{
printf("\n\n **************************************************");
printf("\n\n\tPlease enter a quantity of length: ");
scanf_s("%lf", &length);
printf("\n\tPlease enter a quantity of mass: ");
scanf_s("%lf", &mass);
printf("\n\tPlease enter a quantity of time: ");
scanf_s("%lf", &utime);
printf("\n\tNow enter the speed of the vehicle (m/s): ");
scanf_s("%lf", &speed);
while (speed > LIGHT)
{
printf("\n\n\tNothing can surpass the speed of light in the universe. Enter a smaller value: ");
scanf_s("%lf", &speed);
}
double newlength = length * (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));
double newmass = mass / (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));
double newutime = utime / (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));
if (speed == LIGHT)
{
printf("\n\n **************************************************");
printf("\n\n\n\tIt's technically impossible to reach the speed of light if you have mass but here are the mathematical limit results:\n\n\t*The new length quantity is 0\n\n\t*The new mass quantity is infinity\n\n\t*The new time quantity is infinity\n\n\n\t- Time successfully dilated -\n\n");
printf("\n\tDo you want to start over? (y/n): ");
scanf_s(" %c", &answer);
if (answer == 'n')
{
return 0;
}
else if (answer == 'y')
{
continue;
}
else
{
while (answer != 'y' && answer != 'n')
{
printf("\n\tPlease only enter 'y' or 'n': ");
scanf_s(" %c", &answer);
}
}
}
if (speed < LIGHT)
{
printf("\n\n **************************************************");
printf("\n\n\n\t*The new length quantity is %.20lf\n\n\t*The new mass quantity is %.20lf\n\n\t*The new time quantity is %.20lf\n\n\n\t- Time successfully dilated -\n\n", newlength, newmass, newutime);
printf("\n\tDo you want to start over? (y/n): ");
scanf_s(" %c", &answer);
if (answer == 'n')
{
return 0;
}
else if (answer == 'y')
{
continue;
}
else
{
while (answer != 'y' && answer != 'n')
{
printf("\n\tPlease only enter 'y' or 'n': ");
scanf_s(" %c", &answer);
}
}
}
}
while (answer == 'y');
return 0;
}
Спасибо, хорошего дня