【发布时间】:2014-02-12 16:14:17
【问题描述】:
我需要正确对齐我的数字。我在处理一位数和三位数的浮点数时遇到问题。这是我的输出:
ID G1 G2 G3 Average
000000065 92.000000 93.000000 86.000000 90.333336
000000101 85.500000 75.500000 90.000000 83.666664
000002202 100.000000 92.000000 87.250000 93.083336
000022227 96.000000 84.000000 75.500000 85.166664
000031303 99.000000 87.000000 62.000000 82.666664
101010010 0.000000 81.000000 91.000000 57.333332
424242428 77.000000 77.000000 87.500000 80.500000
700666124 88.000000 65.000000 89.000000 80.666664
812345676 95.000000 76.000000 87.000000 86.000000
999999999 99.000000 99.500000 100.000000 99.500000
这是我的打印功能:
//function that prints out contents of tree
int print_tree (NodePtr treePtr){
// if statement begins
if (treePtr != NULL){
print_tree(treePtr->left);
printf( "%.9d %f %f %f %f\n\n", treePtr->studentID, treePtr->g1, treePtr->g2, treePtr->g3, treePtr->average);
print_tree(treePtr->right);
}//if statement ends
return 0;//indicates successful termination
}//end print_tree
正如您所见,由于一位和三位长的浮点数(我需要打印浮点数),一些数字没有正确排列。
【问题讨论】:
-
.9 用于整数。它之所以有效,是因为 ID 的所有内容都是均匀分布的,而不是 G1、G2、G3 和平均值
-
整理好数字间距后,您可能需要返回并包含标题标签的格式说明符,以使它们在数字上方右对齐或左对齐。 @ryyker 在他的回答中指出的教程应该涵盖它。