初学者 C - 以相反的顺序打印用户的浮点输入 [英] Beginner C - Print user's float inputs in reverse order
问题描述
我正在编写一个程序,它使用 1 条语句从用户那里读取 6 个浮点数.然后让它在 3 行中打印 6 个数字,并满足以下所有要求:
I'm writing a program that uses 1 statement to read 6 floating numbers from user. Then have it print the 6 numbers in 3 lines, with all of following requirements:
(1) 数字以与读入相反的顺序打印
(1) the numbers are printed in the reverse order that they are read in
(2) 它们分 3 行:第一行 1 个数字,下一行 2 个数字,最后一行 3 个数字
(2) they are on 3 lines: 1 number on the first line, 2 numbers on the next line, 3 numbers on the last line
(3) 将数字排列成列格式,右对齐,小数点后保留 1 位
(3) line up the numbers so they are in column format, right justified, with 1 digit after the decimal point
这是我对前两个要求的尝试
Here's my attempt for the first 2 requirements
#include <stdio.h>
int main(void)
{
//variable definitions
float f1,f2,f3,f4,f5,f6;
printf ("Enter 6 float numbers, separated by commas: ");
scanf ("%f1,%f2,%f3,%f4,%f5,%f6",&f1,&f2,&f3,&f4,&f5,&f6);
printf ("%f6\n",f6);
printf ("%f5,%f4\n",f5,f4);
printf ("%f3,%f2,%f1\n",f3,f2,f1);
return 0;
}
在我的初学者看来,这是完全合理的.
To my beginner mind, it makes perfect sense.
运行结果如下
输入 6 个浮点数,以逗号分隔:0.2,3.2,0.1,0.5,0.6,0.7
Enter 6 float numbers, separated by commas: 0.2,3.2,0.1,0.5,0.6,0.7
数字是:
-107374176.0000006
-107374176.0000006
-107374176.0000005,-107374176.0000004
-107374176.0000005,-107374176.0000004
-107374176.0000003,-107374176.0000002,0.2000001
-107374176.0000003,-107374176.0000002,0.2000001
按任意键继续...
除了最后一个,其他都是垃圾输出.感谢所有帮助!
All of them are garbage outputs except for the last one. Appreciate all the helps!
推荐答案
你的格式
scanf ("%f1,%f2,%f3,%f4,%f5,%f6",&f1,&f2,&f3,&f4,&f5,&f6);
期望在第一个 float
之后和下一个逗号之前有一个 1
,在下一个等之后有一个 2
.
expects a 1
after the first float
and before the following comma, a 2
after the next etc.
应该是
scanf ("%f,%f,%f,%f,%f,%f",&f1,&f2,&f3,&f4,&f5,&f6);
由于未提供分隔数字,因此第二次转换(以及以下转换)失败,其他 float
仍未初始化.
Since the separating digits weren't provided, the second conversion (and the following) failed, and the other float
s remained uninitialised.
这篇关于初学者 C - 以相反的顺序打印用户的浮点输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!