这个缩写是这样做的:我们写下一个单词的第一个和最后一个字母,在这两个字母之间写下第一个和最后一个字母之间的字母数。该数字是十进制的,并且不包含任何前导零。
因此,"localization"将拼写为"l10n"one_answers"internationalization"将被拼写为"i18n"
这是我的代码:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
int n,c;
std::string str;
c=0;
char word[n][100];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>word[i];
}
for(int j=0;j<n;++j)
{
str = word[j];
c = str.length();
if(c>10)
cout<<str[0]<<c-2<<str.back();
else
cout<<word[j]<<"n";
c=0;
}
}
谁能告诉我我应该改变什么?我们是来帮忙的。好了,任务明白了。现在回答你的问题:
谁能告诉我我应该改变什么?
首先,程序不能在我的机器上编译,因为不兼容的语言结构,如VLA(可变长度数组)。这不是合法的c++代码。然后有一个主要的错误,您使用未初始化的变量n
来设置(也是禁止的)VLA的维度。这将导致未定义的行为或运行时崩溃。
- 你不应该使用c函数,也不应该包含像" stdlib.h "这样的c头文件
- 不使用
using namespace std;
。始终使用完全限定名 - 在定义变量时总是初始化所有变量
- 使用有意义的、描述性的变量名,而不是像n、c这样的单一字母名称 在c++中不要使用C风格的数组。使用
- 你知道
std::string
,因为你使用它。因此,请使用std::string
字符串。永远不要使用旧的c风格字符串与char[]
或char*
- 最后一条语句
c=0;
不需要。c
将在下一轮被strlen
覆盖。 - 对于单词的大小,您使用神奇的数字100。不应该使用魔术数字。如果用户输入的字符串超过100个字符。灾难将会发生。如果有的话,那么在这种情况下请使用
cin.get
。但最好是使用std::string
。
std::array
,这里最好使用std::vector
。没有理由使用c风格的数组。好了,有了上面的智慧,让我们开始重构的第一步。我们将保留您的编程风格,但必须使用新的原始指针将VLA替换为动态内存管理。请注意:在c++中,New和raw指针不能用于自有内存。无论如何。第一步,我们将使用它。当然,对于字符串,我们将使用std::string
。
#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
int n,c;
std::string str;
c=0;
cin>>n;
std::string* word = new std::string[n];
for(int i=0;i<n;i++)
{
cin>>word[i];
}
for(int j=0;j<n;++j)
{
str = word[j];
c = str.length();
if(c>10)
cout<<str[0]<<c-2<<str.back() << "n";
else
cout<<word[j]<<"n";
c=0;
}
delete[] word;
}
下一步。删除不必要的东西。使用有意义的变量名和注释。
#include<iostream>
#include<string>
// Program to create abbreviations for words
// This abbreviation is made like this: we write down the first and the last letter
// of a word and between them we write the number of letters between the first and
// the last letters. That number is in decimal system and doesn't contain any leading
// zeroes.
// Thus, "localization" will be spelt as "l10n", and "internationalization" will be
// spelt as "i18n".
// Words above this length will be transformed
constexpr unsigned int MaxWordLength = 10u;
int main()
{
// We will get the numberOfWords from the user and check for a valid input
unsigned int numberOfWords{};
if ((std::cin >> numberOfWords) and (numberOfWords > 0)) {
// Here we will store all words read from the user
std::string* words = new std::string[numberOfWords]{};
// Now read the specified amount of words from user
for(unsigned int index=0; index<numberOfWords; ++index) {
std::cin >> words[index];
}
// Abbreviation activity
for(unsigned int index=0; index<numberOfWords; ++index) {
// Check length of word
const std::string& word = words[index];
const unsigned int wordLength = word.length();
// If word is longer than threshold, do abbreviation
if(wordLength > MaxWordLength)
// Print abbreviated word
std::cout << word[0] << wordLength-2 << word.back() << 'n';
else
// Print word as is
std::cout << word << 'n';
}
delete[] words;
}
else std::cerr << "nError: Invalid inputnn";
}
接下来,让我们摆脱new
和使用std::vector
。这不会影响整个程序。它是等价的。但是我们将使用更多的改进,比如基于范围的for循环。
#include <iostream>
#include <string>
#include <vector>
// Program to create abbreviations for words
// This abbreviation is made like this: we write down the first and the last letter
// of a word and between them we write the number of letters between the first and
// the last letters. That number is in decimal system and doesn't contain any leading
// zeroes.
// Thus, "localization" will be spelt as "l10n", and "internationalization" will be
// spelt as "i18n".
// Words above this length will be transformed
constexpr size_t MaxWordLength = 10u;
int main()
{
// We will get the numberOfWords from the user and check for a valid input
size_t numberOfWords{};
if ((std::cin >> numberOfWords) and (numberOfWords > 0)) {
// Here we will store all words read from the user
std::vector<std::string> words(numberOfWords);
// Now read the specified amount of words from user
for(std::string& word : words) {
std::cin >> word;
}
// Abbreviation activity
for(std::string& word : words) {
// Check length of word
const size_t wordLength = word.length();
// If word is longer than threshold, do abbreviation
if(wordLength > MaxWordLength)
// Print abbreviated word
std::cout << word[0] << wordLength - 2u << word.back() << 'n';
else
// Print word as is
std::cout << word << 'n';
}
}
else std::cerr << "nError: Invalid inputnn";
}
现在已经很好了。
如果您想查看更高级的c++解决方案,您可以查看以下内容:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
// Program to create abbreviations for words
// This abbreviation is made like this: we write down the first and the last letter
// of a word and between them we write the number of letters between the first and
// the last letters. That number is in decimal system and doesn't contain any leading
// zeroes.
// Thus, "localization" will be spelt as "l10n", and "internationalization" will be
// spelt as "i18n".
// Words above this length will be transformed
constexpr size_t MaxWordLength = 10u;
int main()
{
// We will get the numberOfWords from the user and check for a valid input
if (size_t numberOfWords; (std::cin >> numberOfWords) and (numberOfWords > 0)) {
// Here we will store all words read from the user
std::vector<std::string> words(numberOfWords);
// Now read the specified amount of words from user
std::copy_n(std::istream_iterator<std::string>(std::cin), numberOfWords, words.begin());
// Abbreviation activity and output
std::transform(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout,"n"), [](const std::string& w){
return (w.length() > MaxWordLength) ? (w[0]+std::to_string(w.length()-2)+w.back()) : w;});
}
else std::cerr << "nError: Invalid inputnn";
}