使用C++中的"struct"修改文本

Modify the text using a "struct" in C++

本文关键字:修改 文本 struct 使用 中的 C++      更新时间:2023-10-16

我有以下诗句:

A swarm of bees in May
Is worth a load hey;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is hot a worth a fly.

我必须修改这个文本,使所有行都在同一位置结束。字符串中使用空格进行补码的位置数不足。这些空间必须平均划分我知道我的代码非常庞大,但我必须使用"代码中的结构"

如何找到最长的字符串并在其他字符串中添加空格来执行任务?

谢谢!

#include "stdafx.h"
#include "iostream"
#include <string.h> 
using namespace std;
struct VERSE {
    char row_one[25];
    char row_two[25];
    char row_three[25];
    char row_four[25];
    char row_five[25];
    char row_six[25];
};
int _tmain(int argc, _TCHAR* argv[])
{
    struct VERSE v;
    strcpy_s(v.row_one, "A swarm of bees in May");
    strcpy_s(v.row_two, "Is worth a load hey;");
    strcpy_s(v.row_three, "A swarm of bees in June");
    strcpy_s(v.row_four, "Is worth a silver spoon;");
    strcpy_s(v.row_five, "A swarm of bees in July");
    strcpy_s(v.row_six, "Is hot a worth a fly.");
    cout << v.row_one << endl << v.row_two << endl << v.row_three << endl
        << v.row_four << endl << v.row_five << endl << v.row_six << endl;
    cout << strlen(v.row_one) << endl;
    cout << strlen(v.row_two) << endl;
    cout << strlen(v.row_three) << endl;
    cout << strlen(v.row_four) << endl;
    cout << strlen(v.row_five) << endl;
    cout << strlen(v.row_six) << endl;
    //the length of row
    /*
    int length = 0;
    for(int i = 0; v.row_two[i] != ''; i++) {
        length++;
    }
    printf("Length of second row is: %dn", length);
    */
    return 0;
}
A swarm of bees in May
Is worth a load hey;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is hot a worth a fly.

只是让我讨厌。我想把它重写为:

A swarm of bees in May
Is worth a load of hay;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is not worth a fly.

不管怎样,有了这个:

我在写这个答案时假设这是一个作业,并且你被告知要使用c_string。如果不是这样的话,那么使用std::string会更容易。

不管怎样,我已经想出了这个代码:

#include "iostream"
#include <string.h> 
using namespace std;
const int maxrowlength = 25, maxrowcount = 6;
struct VERSE {
    char rows[maxrowcount][maxrowlength];
    int spaces[maxrowcount];
    int line_length[maxrowcount];
};
char* get_row(VERSE &v, int row)
{
    return &v.rows[row][0];
}
int main()
{
    struct VERSE v;
    strcpy(get_row(v,0), "A swarm of bees in May");
    strcpy(get_row(v,1), "Is worth a load hey;");
    strcpy(get_row(v,2), "A swarm of bees in June");
    strcpy(get_row(v,3), "Is worth a silver spoon;");
    strcpy(get_row(v,4), "A swarm of bees in July");
    strcpy(get_row(v,5), "Is hot a worth a fly.");
    //calculate lengths and count spaces
    int max_space_count = 0;
    for (size_t i = 0; i < maxrowcount; i++)
    {
        char* line = get_row(v,i);
        /*/we could find the length with strlen() and spaces with memchr() but 
           that will involve traversing the string multiple times (at least twice)
           we can do better
        /*/
        v.line_length[i] = 0;
        v.spaces[i] = 0;
        while(*line)
        {
            v.line_length[i]++;
            if(*line == ' '){v.spaces[i]++;}
            line++;
        }
        if (v.line_length[i] > max_space_count){max_space_count = v.line_length[i];}
    }
    for (size_t i = 0; i < maxrowcount; i++)
    {
        int length_diff = max_space_count - v.line_length[i];
        int spaces_to_add = v.spaces[i]?length_diff / v.spaces[i]:0; //number of spaces to add every word
        int extra_spaces  = v.spaces[i]?length_diff % v.spaces[i]:0; //extra spaces to add to make line fit
        char output[maxrowlength];
        char* current_output = output;
        char* current_word = get_row(v,i);
        char* current_word_end = current_word;
        while(*current_word)
        {
            current_word_end++;
            if (*current_word_end == ' ' || *current_word_end == '')
            {
                //write word to output
                strncpy(current_output, current_word, current_word_end - current_word);
                //update pointer to end of new word
                current_output += current_word_end - current_word;
                //write in the number of new spaces needed
                if (*current_word_end == ' ')
                {
                    for (int j = 0; j < spaces_to_add; j++)
                    {
                        *current_output = ' ';
                         current_output++;
                    }
                    //if extra spaces are needed, add those too
                    if (extra_spaces)
                    {
                        extra_spaces--;
                        *current_output = ' ';
                         current_output++;
                    }
                }
                //step current word to look at the next word
                current_word = current_word_end;
            }
        }
        //null terminate
        *current_output = '';
        strcpy(get_row(v,i),output);
    }
    for (size_t i = 0; i < maxrowcount; i++)
        {std::cout << get_row(v,i) << std::endl;}
    return 0;
}

输出:

A  swarm  of bees in May
Is  worth  a  load  hey;
A  swarm of bees in June
Is worth a silver spoon;
A  swarm of bees in July
Is  hot  a  worth a fly.

点击此处查看:在Ideone上

它是这样工作的:

  1. 读入行
  2. 找出每一行的长度,计算其中的空格,找出最大长度
  3. 对于这行,计算所需的空格数,找出每个单词需要多少空格,以及剩余的空格数
  4. 依次将每个字放入输出缓冲区
  5. 放入所需数量的空格
  6. 如果行不完整,则循环回4
  7. null终止输出
  8. 如果未处理全部输入,则循环回3
  9. 行已完成

我是根据你的代码写的,所以有一些事情我会从头开始做:

  • line结构而不是您的VERSE
    • 每个line保存其内容、空格数和长度
    • line将存储在一个数组中,该数组构成诗句

这可能就是这个任务的意图,但现在你的算法工作了,应该很容易做到(我不会为你做所有);-)

您应该使用对象std::string来存储行。然后,使用函数std::max:

unsigned long int max_size = std::max(str1.length(),str2.length());
max_size = std::max(max_size, str3.length());
max_size = std::max(max_size, str4.length());
//...