在Thermo Mini打印机上用C语言包装

我真的很想帮助我的一个项目。 我是一名平面设计专业的学生,​​几乎没有编程经验。 我已经为热迷你打印机创建了一个程序,该程序根据所使用的特定主题标签识别在Twitter上发布的推文并自动打印。

但是,它基于32个字符的行长度,并将单词分成两半而不是将整个单词移动到另一行。 我的一个朋友建议自动换行,但我找不到任何在线帮助我,而且我发现的大多数代码往往是c ++或c#。

到目前为止的代码可以在下面找到:

// Build an ArrayList to hold all of the words that // we get from the imported tweets ArrayList words = new ArrayList(); Twitter twitter; import processing.serial.*; Serial myPort; // Create object from Serial class int val; // Data received from the serial port void setup() { String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); //Set the size of the stage, and the background to black. size(550,550); background(0); smooth(); //Make the twitter object and prepare the query twitter = new TwitterFactory(cb.build()).getInstance(); } void draw() { Query query = new Query("#RIP"); query.setRpp(1); //Try making the query request. try { QueryResult result = twitter.search(query); ArrayList tweets = (ArrayList) result.getTweets(); for (int i = 0; i < tweets.size(); i++) { Tweet t = (Tweet) tweets.get(i); String user = t.getFromUser(); String msg = t.getText(); Date d = t.getCreatedAt(); println("Tweet by " + user + " at " + d + ": " + msg); msg = msg.replace("\n"," "); myPort.write(msg+"\n"); }; } catch (TwitterException te) { println("Couldn't connect: " + te); }; println("------------------------------------------------------"); delay(20000); } 

既然你有这条线的长度,那就不那么难……

迭代字符串,一次一个字符。 如果你看到一个空格保存位置,例如last_space 。 如果迭代超过最大行长度,则转到last_space位置并将空间转换为换行符,将位置计数器重置为零,然后从该位置重新开始。


也许实现它是这样的:

 #include  #include  #include  #define LINE_LENGTH 32 char text[256] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ac risus elit, id pellentesque magna. Curabitur tempor rutrum enim, sit amet interdum turpis venenatis vel. Praesent eu urna eros. Mauris sagittis tempor felis, ac feugiat est elementum sed. Praesent et augue in nibh pharetra egestas quis et lectus. Lorem ipsum."; static void wrap(char *text, const int length) { int last_space = 0; int counter = 0; for (int current = 0; text[current] != '\0'; current++, counter++) { if (isspace(text[current])) // TODO: Add other delimiters here last_space = current; if (counter >= length) { text[last_space] = '\n'; counter = 0; } } } int main(void) { printf("Before wrap:\n%s\n", text); wrap(text, LINE_LENGTH); printf("\nAfter wrap:\n%s\n", text); return 0; } 

重要说明: wrap函数修改作为text参数传入的缓冲区。 这意味着它无法处理文字字符串,因为它们是只读的。 它的工作原理是用换行符替换空格。 如果修改函数以添加多个字符,那么更好的解决方案是(重新)分配一个足够大的新缓冲区来保存修改后的字符串。