我编写了一个程序,用来分割用户输入的两个数字" scanf"但是,我想改变它,以便它允许用户输入例如" 8/2"在一条线上,反对在两条不同的线上输入一个数字。这给我带来了很多麻烦,我认为这可能是因为角色" /"正在使用?任何帮助将不胜感激,下面是我的原始代码:
#include <stdio.h>
int main()
{
//defined variables
float n1 = 0.0, n2 = 0.0, quotent = 0.0;
int firstNumberRead = 0;
int secondNumberRead = 0;
//user inputs
firstNumberRead = scanf("%f", &n1);
secondNumberRead = scanf("%f", &n2);
if (firstNumberRead && secondNumberRead == 1)
{
if (n2 == 0)
{
printf("invalid input - divide by zero not allowed");
}
if (n1 == 0)
{
printf("invalid input - divide by zero not allowed");
}
else
{
//divide the first number by the second number
quotent = (n1 / n2);
//print quotent
printf("%f", quotent);
}
}
else
{
printf("invalid input");
}
}