文字环绕在C对热微型打印机 [英] Text Wrapping in C on Thermo Mini Printer

查看:121
本文介绍了文字环绕在C对热微型打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的想一些帮助,我的项目之一。我是一名平面设计学生,几乎没有任何编程经验。我创建了一个程序识别基于使用的特定主题标签在Twitter微博和自动打印他们一个热敏微型打印机。

然而,它是基于对32个字符线长,将在半分成,而不是整个单词移动到另一行的话。我的一个朋友建议换行,但我无法找到任何东西在网上帮我,最code我发现往往是对C ++或C#。

在code到目前为止可以发现如下:

  //构建一个ArrayList来保存所有的单词
//我们从进口鸣叫得到ArrayList的<串GT;字=新的ArrayList();
叽叽喳喳叽叽喳喳;
导入processing.serial *。串行MyPort上; //创建从串行类对象
INT VAL;从串口接收//数据无效设置(){
    字符串PORTNAME = Serial.list()[0];
    MyPort上=新的串行(这一点,PORTNAME,9600);
    //设置阶段的大小,背景为黑色。
    大小(550550);
    背景(0);
    光滑();    //使Twitter的对象和prepare查询
    叽叽喳喳=新TwitterFactory(cb.build())的getInstance()。
}无效的draw(){    查询查询=新的查询(#R.I.P);
    query.setRpp(1);    //尝试使查询请求。
    尝试{
        QueryResult中结果= twitter.search(查询);
        ArrayList的鸣叫=(ArrayList的)result.getTweets();        的for(int i = 0; I< tweets.size();我++){
            鸣叫T =(鸣叫)tweets.get(I)
            字符串用户= t.getFromUser();
            弦乐味精= t.getText();
            日期D = t.getCreatedAt();
            的println(鸣叫由+使用者+在+ D +:+ MSG);
            味精= msg.replace(\\ n,);
            myPort.write(MSG +\\ n);
        };
    }
    赶上(TwitterException TE){
        的println(无法连接:+ TE);
    };
    的println(----------------------------------------------- -------);
    延迟(20000);
}


解决方案

既然你的线的长度,它并不难...

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


也许实现它是这样的:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;#定义LINE_LENGTH 32字符文本[] =Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。整数交流risus ELIT,ID pellentesque麦格纳。Curabitur tempor rutrum enim,坐阿梅德interdum turpis venenatis VEL。Praesent欧盟乌仁娜爱神。Mauris sagittis tempor猫,AC feugiat EST elementum sed的Praesent等augue在NIBH pharetra egestas QUIS等lectus Lorem存有。;静态无效包(字符*文本,const int的长度)
{
    INT last_space = 0;
    INT计数器= 0;    对于(INT电流= 0;文[现行] ='\\ 0';!当前++,计数器++)
    {
        如果(isspace为(文[电流]))
            last_space =电流;        如果(计数器> =长度)
        {
            文[last_space] ='\\ n';
            计数器= 0;
        }
    }
}INT主要(无效)
{
    的printf(包前:\\ n%S \\ n,文本);    换行(文字,LINE_LENGTH);    的printf(\\ n在包装:\\ n%S \\ n,文本);    返回0;
}

I would really like some help with one of my projects. I am a graphic design student and have little to no programming experience. I have created a program for a thermo mini printer that identifies tweets made on twitter based on specific hashtags used and prints them automatically.

However, it's based on a line length of 32 chars and will split words in half instead of moving the entire word to another line. A friend of mine suggested word wrapping but I can't find anything online to help me and most code I've found tends to be for c++ or c#.

The code so far can be found below:

// Build an ArrayList to hold all of the words that
// we get from the imported tweets

ArrayList<String> 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("#R.I.P");
    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);
}

解决方案

Since you have the length of the line, it's not that hard...

Iterate over the string, one character at a time. If you see a space save the position as e.g. last_space. If your iteration goes over the max line length, then go to the last_space position and convert the space to a newline, reset the position counter to zero, and start over from that position.


Maybe implement it something like this:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LINE_LENGTH 32

char text[] = "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]))
            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;
}

这篇关于文字环绕在C对热微型打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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