简体   繁体   English

如何在新行之前创建不可见空间?

[英]How to create invisible space until a new line?

I am a beginner of C language. 我是C语言的初学者。 I was recently writing a program to print a histogram of the number of instances of a character in an input. 我最近正在编写一个程序,以打印输入中字符实例数量的直方图。 Printing the histogram horizontally is easy, but vertically is a challenge. 水平打印直方图很容易,但是垂直打印很困难。

Please have a look at the following code: 请看下面的代码:

 #include <stdio.h>

int main()
{

    /*THIS IS JUST A SAMPLE OF WHAT I WANT TO ASK*/
    int occurrence = 5;
    int i;

    for (i=0;i<occurrence;i++){
        printf("\t*\n");
    }




}

For an example, say any letter occurs 5 times. 例如,假设任何字母出现5次。 So I have set the occurrence to 5. And I am printing the bar in the form of asterisks. 因此,我将出现的次数设置为5。然后我以星号的形式打印该条形图。 Now through this code, I am able to print an histogram containing 5 asterisks. 现在,通过此代码,我可以打印包含5个星号的直方图。 But the thing is if I want to print other elements, like the x and y axis, the code creates a \\n character. 但是,如果我要打印其他元素(例如x和y轴),则代码将创建一个\\ n字符。 So if I write the code to print other elements, they start printing from the next line. 因此,如果我编写代码以打印其他元素,则它们将从下一行开始打印。 So I figured out something else. 所以我想出了其他办法。

Now read this code: 现在阅读以下代码:

#include <stdio.h>

int main()
{

    /*THIS IS JUST A SAMPLE OF WHAT I WANT TO ASK*/
    int instances = 5;
    int i;

    for (i=0;i<instances;i++){
        printf("\t*\t\t\t\t\t\t\t\t\t");
    }




}

Now what I did is depending on the size of the output screen, I created 9 tab characters so that the next asterisk moves on to the next line without printing any \\n character. 现在,我的操作取决于输出屏幕的大小,我创建了9个制表符,以便下一个星号移至下一行而不打印任何\\ n字符。

Now the main question: IS THERE ANY OTHER WAY TO CREATE INVISIBLE SPACES UNTIL THE NEXT THING TO BE PRINTED MOVES ON TO THE NEXT LINE? 现在的主要问题是:在要打印的下一个内容移动到下一行之前,还有其他方法可以创建不可见的空间吗?

This question might be stupid but if there is a solution, it will be best for me. 这个问题可能很愚蠢,但是如果有解决方案,那对我来说将是最好的。

NOTE: If there is no such method of creating blank spaces then please suggest a good way to create a vertical histogram. 注意:如果没有这种创建空格的方法,请建议一种创建垂直直方图的好方法。 If someone wants an improvisation in the question, I am ready to do it. 如果有人想在问题中即兴创作,我准备好了。

Thanks for the help! 谢谢您的帮助!

Outputs:: 输出:

If I use the first code and I make other chart elements using printf statements, this is the output:: 如果使用第一个代码,并使用printf语句制作其他图表元素,则输出为: 输出值

Now can you see that the bar made of asterisks is not aligned with the x and y axis. 现在,您可以看到由星号组成的条形图未与x和y轴对齐。 This happens due to the \\n character. 这是由于\\ n字符引起的。

Instead of hacking around with spaces yourself, you might want to look into a library that handles all that graphical stuff for you. 您可能不希望自己动手使用空格,而希望查看一个为您处理所有图形内容的库。 For example, ncurses is a pretty decent library to do pseudo-graphical output on a console. 例如, ncurses是一个相当不错的库,可以在控制台上进行伪图形输出。 However, "ncurses" seems to be for Unix only, but there may be other libraries for Windows. 但是,“ ncurses”似乎仅适用于Unix,但可能还有其他适用于Windows的库。

If using a library is not an option, I'd suggest to work with a 2-dimensional character buffer (that you treat like a bitmap) instead of writing things directly to the console. 如果不能选择使用库,则建议使用二维字符缓冲区(您将其视为位图),而不是直接将内容写入控制台。 It's a lot less "fiddling around". 它比“摆弄”少得多。 Just watch out to truncate your buffer line size to the console line size before printing, in order to avoid automatic line breaks where you don't want them. 只需注意在打印之前将缓冲区的行大小截断为控制台的行大小,以避免在不需要的地方自动换行。

If you do not want to use curses library, for example if you have found a printing terminal in a museum and want that your program can work on it, you have to reverse the problem. 如果您不想使用curses库,例如,如果您在博物馆中找到了一个打印终端,并且希望您的程序可以在它上面运行,那么您就必须解决这个问题。

You must print line by line if you do not use a graphic library. 如果不使用图形库,则必须逐行打印。 So your program could look like : 所以你的程序看起来像:

  • compute the occurence of characters 计算字符的出现
  • compute the maximum occurence 计算最大出现次数
  • for each line from max occurence to 0 从最大发生到0的每一行
    • compute a line for every character printing a space (not reached) 为每个字符打印一行(未到达)
    • for each character 每个字符
      • if the occurence is greater than the line index put a mark at correct place in the line 如果出现次数大于行索引,则在行中的正确位置标记一个标记
    • print the line 打印线

That's the algo, actual coding is left as exercice for the reader :-) 这就是算法,实际的编码留给读者练习:-)

You might want to think about having a two dimensional array. 您可能需要考虑使用二维数组。 Start by filling it with spaces. 首先用空格填充它。 Then replace the spaces with the character you want to print at the correct index. 然后用要在正确索引处打印的字符替换空格。 Using two for loops, traverse the array in order to print. 使用两个for循环,遍历数组以进行打印。 Print a new line at the end of the row. 在行末打印新行。 Changing the order of the index changes a vertical/horizontal print. 更改索引顺序将更改垂直/水平打印。 Negatives are 负数是

  1. having to keep the whole thing in memory. 必须将整个事情保存在内存中。
  2. Needing multiple passes. 需要多次通过。 One to set the characters and another to print. 一个用于设置字符,另一个用于打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM