使用C++中的"struct"修改文本
Modify the text using a "struct" in C++
我有以下诗句:
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上
它是这样工作的:
- 读入行
- 找出每一行的长度,计算其中的空格,找出最大长度
- 对于这行,计算所需的空格数,找出每个单词需要多少空格,以及剩余的空格数
- 依次将每个字放入输出缓冲区
- 放入所需数量的空格
- 如果行不完整,则循环回4
- null终止输出
- 如果未处理全部输入,则循环回3
- 行已完成
我是根据你的代码写的,所以有一些事情我会从头开始做:
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());
//...
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 已修改的LinkedList未在文本文件本身中更新
- 如何修改Lambda表达式以将输出放入文本文件
- 如何修改所有文本元素;Visual Studio c++
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 如何接收、修改和替换文本文件中的一行
- 如何基于文本文件创建或修改QT表单(编译后)
- 如何使用 c++ 修改文本文件的 "Date modified" 属性而不影响内容?
- 使用C++中的"struct"修改文本
- 如何从C++修改QML文本
- 使用一个由c++修改的未关闭的文本文件安全吗
- 逐行修改c++中文本文件的内容