如何在C垂直打印? [英] How to print vertically in C?

查看:127
本文介绍了如何在C垂直打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环用printfs一个我想打印在两行输出,而不是混杂在一行中。

这样的:

 的printf(|%-7.2f,华氏);

主要生产:

  | -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 |

当我加入的printf(|%-6D摄氏度); 上面的printf下,它打印旁边/在我的第一个printf的中间。

我所要的输出如:

  | -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 |
| -300 | -275 | -250 | -225 | -200 | -175 | -150 | -125 |

代替两组值被混合在同一行上。

我的code的一部分:

 为(摄氏= CelsiusMin;摄氏< = CelsiusMax;摄氏度+ = CelsiusStep)
   {
      华氏= FahrenheitZero +摄氏度* CelsiusToFahrenheit;     的printf(|%-7.2f,华氏);
     的printf(|%-6D摄氏度);   }
   返回EXIT_SUCCESS;
}


解决方案

您不能直接拥有的printf()写入1号线报表和第二的一个序列的printf并发序列()语句写入线2条,所以你将不得不捏造事实。

您可能需要的东西,建立了输出的两行,然后打印他们时,他们已经准备好:

 字符一号线[128];
2号线的char [128];
字符* NEXT1 =一号线;
字符* NEXT2 = 2号线;对于(INT C = -325; C< = -125; C + = 25)
{
     双F =(C + 40.0)*(9.0 / 5.0) - 40.0;
     NEXT1 + = sprintf的(NEXT1|%-7.2f,F);
     NEXT2 + = sprintf的(NEXT2|%-7D,C);
}的printf(%S | \\ n,一号线);
的printf(%S | \\ n,2号线);

示例输出:

  | -553.00 | -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 | -238.00 | -193.00 |
| -325 | -300 | -275 | -250 | -225 | -200 | -175 | -150 | -125 |

其换算公式为比通常一个你看到引述简单,而且是对称的转换°F至°C或反之亦然,区别在于转换系数(9.0 / 5.0) VS (5.0 / 9.0)。它依赖于-​​40°C = -40°F。试试吧:


  • C = 0℃; (C + 40)= 40; (C + 40)* 9 = 360; (C + 40)* 9/5 = 72; (C + 40)* 9 / 5-40 = 32°F

  • F = 32°F; (F + 40)= 72; (F + 40)* 5 = 360; (F + 40)* 5/9 = 40; (F + 40)* 5 / 9-40 = 0°C

您可以用摄氏度作为打印为做的更好双击过;我不停地重新选择了presentation整数,但确信这些数字排队的两行。

FYI:绝对零度是-273.15°C,0K,-459.57°F。所以你尝试打印不存在的温度。

I have to printfs in a loop an I want to print the output in two lines, instead of intermingled on one line.

Like this:

printf("| %-7.2f ", Fahrenheit);

which produces:

| -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 |

When I add printf("| %-6d", Celsius); under the printf above, it prints right next to/in the middle of my first printf.

I want the output to be like:

| -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 |
| -300  | -275  | -250  | -225  | -200  | -175  | -150  | -125|

instead of the two sets of values being intermingled on the same line.

part of my code:

for(Celsius = CelsiusMin;Celsius <= CelsiusMax; Celsius += CelsiusStep)
   {
      Fahrenheit = FahrenheitZero + Celsius * CelsiusToFahrenheit;

     printf("| %-7.2f ", Fahrenheit);
     printf("| %-6d", Celsius);

   }
   return EXIT_SUCCESS;
}

解决方案

You can't directly have one sequence of printf() statements writing to line 1 and a second concurrent sequence of printf() statements writing to line 2, so you're going to have to fake it.

You probably need something that builds up the two lines of output, and then prints them when they're ready:

char line1[128];
char line2[128];
char *next1 = line1;
char *next2 = line2;

for (int c = -325; c <= -125; c += 25)
{
     double f = (c + 40.0) * (9.0 / 5.0) - 40.0;
     next1 += sprintf(next1, "| %-7.2f ", f);
     next2 += sprintf(next2, "| %-7d ", c);
}

printf("%s|\n", line1);
printf("%s|\n", line2);

Sample output:

| -553.00 | -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 | -238.00 | -193.00 |
| -325    | -300    | -275    | -250    | -225    | -200    | -175    | -150    | -125    |

The conversion formula is simpler than the usual one you see quoted, and is symmetric for converting °F to °C or vice versa, the difference being the conversion factor (9.0 / 5.0) vs (5.0 / 9.0). It relies on -40°C = -40°F. Try it:

  • C =  0°C; (C+40) = 40; (C+40)*9 = 360; (C+40)*9/5 = 72; (C+40)*9/5-40 = 32°F.
  • F = 32°F; (F+40) = 72; (F+40)*5 = 360; (F+40)*5/9 = 40; (F+40)*5/9-40 =  0°C.

You might do better with the degrees Celsius as printed as a double too; I kept the integer representation you chose, but made sure the numbers line up on the two lines.

FYI: Absolute zero is -273.15°C, 0K, -459.57°F. So you're attempting to print non-existent temperatures.

这篇关于如何在C垂直打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆