C ++文本完全对齐 [英] C++ text full justification

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

问题描述

我试图做一个程序,完全证明/左右对齐一个给定的字符串。我已经做了你知道字符串中的单词之间需要多少空格的部分,但是我没有得到的是如何在单词之间添加或插入这些空格。

I'm trying to do a program that full justifies/left and right justification a given string. I've already made the part where you know how many spaces needs to be between words in a string, but what I don't get is how to add or insert those spaces in between words.

推荐答案

一个简单的算法做传播是

A simple algorithm to do a spreading is

for (int i=0; i<num_words-1; i++) {
    int s0 = i * extra_spaces / (num_words - 1);
    int s1 = (i + 1) * extra_spaces / (num_words - 1);
    // add (s1 - s0) spaces between word[i] and word [i+1]
}

这篇关于C ++文本完全对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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