当谈到C ++时,我是一个业余爱好者,我很想看看我能做些什么来改进这个程序,我写这篇文章是为了帮助我完成我的AP英语作业。基本上它打印单词的定义,你必须正确匹配它们。如果错误则显示“错误,正确的单词是:(正确的单词)”,如果正确则显示“太棒了!”
所以我也有一些问题,为什么这些东西有效,我正在编码,就像wtf一样,不应该工作,但确实如此
(代码下面的问题)
#include <iostream>
#include <string>
#include <ctime>
#include <Windows.h>
using namespace std;
class APMC {
public:
// Array of the words
string word[56] = { "Footnote", "Blatant", "Bounded", "Assertion", "Faulty",
"Cynicism", "Compunction", "Aberration", "Degeneration", "Solemnity",
"Treatise", "Grotesque", "Exposition", "Ambiguous", "Tirades", "Appropriate",
"Ornamentation", "Prattling", "Nomenclature", "Berate", "Aesthetics", "Homiletic",
"Innate", "Merits", "Fallocious", "Jeremiads", "Cornucopia", "Enumerate", "Extraneous",
"Ineluctable", "Ascertain", "Interpolation", "Emulation", "Sanguine", "Nostalgia", "Fluted",
"Disillusion", "Derivation", "Discursive", "Digression", "Prose", "Caduceus", "Conspicuous",
"Contemptuous", "Enunciates", "Admonitions", "Alludes", "Empirical", "Abstractions", "Satiety",
"Futility", "Disparate", "Indicative", "Facetiously", "Definitively", "Abstract"};
// Array of the definitions of the words
string description[56] = { "An additional piece of information printed at the bottom of a page","Offensively loud, flagrant",
"Leaping strides", "A confident and forceful statement of fact or belief", "Not working or made correctly; having defects",
"An inclination to believe that people are motivated purely by self-interest; skepticism",
"A feeling of Guilt or moral scruple that prevents or follows the doing of something bad", "A deperature from what is normal, usual, or expected, typically an unwelcome one",
"The state or process of being or becoming degenerate; decline or deterioration", "The state or quality of being serious and dignified",
"A written work dealing formally and systematically with a subject", "A very ugly or comically distorted figure or image", "A comprehensive description and explanation of an idea or theory",
"Open to more than one interpretation; not having one obvious meaning", "A long, angry speech of criticism or accusation", "Suitable or proper in the circumstances",
"Decorative elements added to something to enhance its appearance", "Foolish or inconsequential talk", "The body or system of names used in a particular specialist field",
"Scold or criticize (Ssomeone) angrily", "A set of principles concerned with the nature and appreciation of beauty", "The art of preaching or writing sermons",
"Inborn, Natural", "The quality of being particularly good or worthy, especially to deserve praise or reward", "Based on a mistaken belief", "A long, mournful complaint or lamentation; a list of woes",
"A symbol of plenty consisting of a goat's horn overflowing with flowers, fruit, and corn", "Mention (a number of things) one by one", "Irrelevant or unrelated to the subject being dealt with",
"Unable to be resisted or avoided; inescapable", "Find (something) out for certain; make sure of", "The insertion of something of a different nature into something else",
"Effort to match or surpass a person or achievement, typically by imitation", "Optimistic or positive, especially in an aparrently bad or difficult situation",
"A sentimental longing or wistful affection for a period in the past", "Having flutes or grooves; ridged", "Disappointment resulting from the discovery that something is not as good as one believed it to be",
"The action of obtaining something from a source or origin", "Digressing from subject to subject", "A temporary departure from the main subject in speech or writing",
"Written or spoken language in its ordinary form, without metrical structure", "An ancient greek or roman herald's wand", "Clearly visible", "Showing contempt; scornful",
"Say or pronounce clearly", "A firm warning or reprimand", "Suggest or call attention to indirectly; hint at",
"Based on, concerned with, or verifiable by observeration or experience rather than theory or pure logic", "The quality of dealing with ideas rather than events",
"The feeling or state of being sated", "Pointlessness or uselessness", "Essentially different in kind; not able to be compared",
"Serving as a sign or indication of something", "Treating serious issues with deliberately inappropriate humour; flippant",
"Decisively and with authority; conclusively", "Existing in thought or as an idea but not having a physical or concrete existence" };
};
int main() {
// Is the game running?
bool isGameRunning = true;
// Casts an integer to time and stores it into random so it is for SURE random!
srand((int)time(0));
// Classes object key thing
APMC words;
// The whole program is ran in a loop, declared above
while (isGameRunning) {
// Random between 0 and 56 because there is 56 words
int random = (rand() % 56) + 0;
string userInput;
// Displays a random definition
cout << "The definition is: " << words.description[random] << endl;
cin >> userInput;
system("cls");
// This is where im confused on WHY this worked
if (userInput == words.word[random]) {
cout << "Great!" << endl;
}
else {
// Also confused on why this worked, but displays the correct answer if it is wrong
cout << "Wrong, the answer was: " << words.word[random] << endl;
}
}
return 0;
}
问题: 1.它所说的部分
"if (userInput == words.word[random]) {
cout << "Great!" << endl;
}"
好的,为什么这个有效?这是偶然的,基本上它表示如果userInput,一个字符串等于字符串中的单词,那么它匹配。但是,不是按数字分类的数组.. 0 - 56?那么数组如何知道userInput,它接受一个字符串值,与数组中的INTEGER值匹配?
对其所说的部分感到困惑
“cout&lt;&lt;”错了,答案是:“&lt;&lt; words.word [random]&lt;&lt; endl;”
好吧,这是一个随机数吗?怎么会记得它显示的时候是什么字?我肯定认为这将是一个不同的词。我的猜测是当它循环并在开始时声明随机部分时,它组成了新的数字。
谢谢你们,对不起,如果我措辞严厉......
寻找改进措施!
答案 0 :(得分:1)
你需要记住的事情是:随机数只生成一次(对于每个循环)并存储在整数'random'中,如下所示:
int random = (rand() % 56) + 0;
在语句之后,random的值不会改变(对于当前循环)。让我们说随机值为0.然后,它将显示相应的描述并从我们那里获取输入:
cout << "The definition is: " << words.description[random] << endl;
cin >> userInput;
在我们的例子中,它将显示“在页面底部打印的另一条信息”。让我们说然后我们在userInput中输入“banana”。
然后,它将使用words.word [random]检查我们的输入userInput。 请注意,整数随机数尚未更改,因此words.word [random]为“Footnote”。但由于我们的输入“banana”不等于“Footnote”,因此执行了else语句,显示正确的答案。
答案 1 :(得分:0)
您的程序知道正确答案的索引,因为它选择本身(随机)号码,它记住变量{ {1}}并提出了适当的问题
random
正确答案是
words.description[random]
您的猜测是正确的 - 在 words.word[random]
循环的每次迭代中,它都会设置一个新的(随机)数字,并将其分配给变量while
(保持其值直到下一次迭代)。
答案 2 :(得分:0)
问题1:
Read it up here.我认为这个问题可以帮到你。
问题2:
int random = (rand() % 56) + 0;
您有一个名为random
的变量,并使用值(rand() % 56)
对其进行初始化,因此您可以调用随机数函数来获取值并将该数字写入您的局部变量。在您结束/重复while循环之前,您的本地号码将相同。
注意:这个给出的值介于0和55之间,而不是0和56。
答案 3 :(得分:0)
您可以做的一件事(非常简单)是使用clang-format
等自动重新格式化工具格式化代码:
#include <iostream>
#include <string>
#include <ctime>
#include <Windows.h>
using namespace std;
class APMC {
public:
// Array of the words
string word[56] = {
"Footnote", "Blatant", "Bounded", "Assertion",
"Faulty", "Cynicism", "Compunction", "Aberration",
"Degeneration", "Solemnity", "Treatise", "Grotesque",
"Exposition", "Ambiguous", "Tirades", "Appropriate",
"Ornamentation", "Prattling", "Nomenclature", "Berate",
"Aesthetics", "Homiletic", "Innate", "Merits",
"Fallocious", "Jeremiads", "Cornucopia", "Enumerate",
"Extraneous", "Ineluctable", "Ascertain", "Interpolation",
"Emulation", "Sanguine", "Nostalgia", "Fluted",
"Disillusion", "Derivation", "Discursive", "Digression",
"Prose", "Caduceus", "Conspicuous", "Contemptuous",
"Enunciates", "Admonitions", "Alludes", "Empirical",
"Abstractions", "Satiety", "Futility", "Disparate",
"Indicative", "Facetiously", "Definitively", "Abstract"};
// Array of the definitions of the words
string description[56] = {
"An additional piece of information printed at the bottom of a page",
"Offensively loud, flagrant", "Leaping strides",
"A confident and forceful statement of fact or belief",
"Not working or made correctly; having defects",
"An inclination to believe that people are motivated purely by "
"self-interest; skepticism",
"A feeling of Guilt or moral scruple that prevents or follows the "
"doing of something bad",
"A deperature from what is normal, usual, or expected, typically an "
"unwelcome one",
"The state or process of being or becoming degenerate; decline or "
"deterioration",
"The state or quality of being serious and dignified",
"A written work dealing formally and systematically with a subject",
"A very ugly or comically distorted figure or image",
"A comprehensive description and explanation of an idea or theory",
"Open to more than one interpretation; not having one obvious "
"meaning",
"A long, angry speech of criticism or accusation",
"Suitable or proper in the circumstances",
"Decorative elements added to something to enhance its appearance",
"Foolish or inconsequential talk",
"The body or system of names used in a particular specialist field",
"Scold or criticize (Ssomeone) angrily", "A set of principles "
"concerned with the nature "
"and appreciation of beauty",
"The art of preaching or writing sermons", "Inborn, Natural",
"The quality of being particularly good or worthy, especially to "
"deserve praise or reward",
"Based on a mistaken belief",
"A long, mournful complaint or lamentation; a list of woes",
"A symbol of plenty consisting of a goat's horn overflowing with "
"flowers, fruit, and corn",
"Mention (a number of things) one by one",
"Irrelevant or unrelated to the subject being dealt with",
"Unable to be resisted or avoided; inescapable",
"Find (something) out for certain; make sure of",
"The insertion of something of a different nature into something "
"else",
"Effort to match or surpass a person or achievement, typically by "
"imitation",
"Optimistic or positive, especially in an aparrently bad or "
"difficult situation",
"A sentimental longing or wistful affection for a period in the past",
"Having flutes or grooves; ridged",
"Disappointment resulting from the discovery that something is not "
"as good as one believed it to be",
"The action of obtaining something from a source or origin",
"Digressing from subject to subject",
"A temporary departure from the main subject in speech or writing",
"Written or spoken language in its ordinary form, without metrical "
"structure",
"An ancient greek or roman herald's wand", "Clearly visible",
"Showing contempt; scornful", "Say or pronounce clearly",
"A firm warning or reprimand",
"Suggest or call attention to indirectly; hint at",
"Based on, concerned with, or verifiable by observeration or "
"experience "
"rather than theory or pure logic",
"The quality of dealing with ideas rather than events",
"The feeling or state of being sated", "Pointlessness or uselessness",
"Essentially different in kind; not able to be compared",
"Serving as a sign or indication of something",
"Treating serious issues with deliberately inappropriate humour; "
"flippant",
"Decisively and with authority; conclusively",
"Existing in thought or as an idea but not having a physical or "
"concrete existence"};
};
int main() {
// Is the game running?
bool isGameRunning = true;
// Casts an integer to time and stores it into random so it is for SURE random!
srand((int)time(0));
// Classes object key thing
APMC words;
// The whole program is ran in a loop, declared above
while (isGameRunning) {
// Random between 0 and 56 because there is 56 words
int random = (rand() % 56) + 0;
string userInput;
// Displays a random definition
cout << "The definition is: " << words.description[random] << endl;
cin >> userInput;
system("cls");
// This is where im confused on WHY this worked
if (userInput == words.word[random]) {
cout << "Great!" << endl;
} else {
// Also confused on why this worked, but displays the correct answer
// if it is wrong
cout << "Wrong, the answer was: " << words.word[random] << endl;
}
}
return 0;
}
我用常量替换重复的56
:
constexpr size_t WORDCOUNT = 56;
大部分评论都是多余的。
…(int)time(0)…
是一个C风格的演员表,应该用更慎重的static_cast<int>(time(0))
替换
这些是一些风格的建议;其他人已经回答了你的逻辑问题!