C++ Switch 语句输入

发布于 2024-10-31 14:41:15 字数 287 浏览 0 评论 0原文

我正在编写一个 C++ 程序,提示用户输入,然后跟踪输入的次数。我目前正在使用 do-while 循环和 switch 语句。我遇到问题的部分是 switch 语句。我不知道如何跟踪输入的次数。例如:

Enter Value: 4
Enter Value: 4
Enter Value: 4
Enter Value: 3
Enter Value: 3

// 然后我希望程序能够知道并最终输出数字“4”和“3”被输入了多少次。 我想可能会使用某种增量计数形式,但不是 100% 确定。 谢谢!

I am writing a C++ program that prompts the user for an input and then keeps track of how many times that input is entered. I am currently using a do-while loop and a switch statement. The part I am having trouble with is the switch statement. I can't figure out how to keep track of how many times an input is entered. For example:

Enter Value: 4
Enter Value: 4
Enter Value: 4
Enter Value: 3
Enter Value: 3

// I then want the program to be able to know and then eventually output, how many times the number '4' and '3' were entered.
I thinking possibly using some sort of increment counting form, but not 100% sure.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2024-11-07 14:41:15

您可能需要使用 std::map。原因如下。

让我们看看替代方案,从显而易见的开始:

int count0;
int count1;
int count2;
int count3;
int count4;

...

switch(input) {
case 0: ++count0; break;
case 1: ++count1; break;
case 2: ++count2; break;
case 3: ++count3; break
case 4: ++count4; break;
}

这符合您的要求:评估输入,并跟踪特定输入被看到的次数。这种形式确实存在许多问题:

  • 每个替代方案都需要一行源代码。当用户可以输入任何值(例如 0 到 10,000)时,这就会成为问题!
  • 它有重复的、几乎相同的线条。
  • 它有许多变量,每个变量都必须独立输入,但用途相同。

我们可以通过指定一个数组来减少变量数量:

int count[5];
...
switch(input) {
case 0: ++count[0]; break;
case 1: ++count[1]; break;
case 2: ++count[2]; break;
case 3: ++count[3]; break;
case 4: ++count[4]; break;
}

这仍然受到太多几乎但不完全相同的代码行的影响。让我们尝试摆脱 switch 语句:

int count[5];
...
++count[input];

啊,现在我们已经取得进展了!通过消除 switch 语句,我们有了一行易于维护的代码。但是如果用户(意外或恶意)输入 6 该怎么办?然后我们将增加 count[6],它不存在。这是一件坏事。我们可以增加数组的大小:

int count[50000];
...
++count[input];

现在我们对用户来说是安全的。如果他输入 6,那么坏事就不会再发生。呃哦,如果用户输入 51000 呢?我们将增加不存在的 count[51000]。很明显,我们无法赢得这场游戏——对于我们选择的任何数字,用户可能会选择该数字加 1。

即使我们能赢,我们仍然会输。如果我们只要求用户输入几个数字,那么我们就会浪费数组中的其他 49,997 个条目。

幸运的是,C++ 有一个我们可以使用的数据结构:

  • 可以将任意数字作为其范围,并且
  • 节省空间(与大型浪费数组相比)。

该数据结构称为映射:

std::map<int,int> count;
...
++count[input];

映射有点像数组,但以特殊方式自行增长。只有我们使用的条目才会被分配,并且我们使用的每个条目都会自动分配。

You'll probably want to use a std::map<int,int>. Here's why.

Let's look at alternatives, starting with the obvious:

int count0;
int count1;
int count2;
int count3;
int count4;

...

switch(input) {
case 0: ++count0; break;
case 1: ++count1; break;
case 2: ++count2; break;
case 3: ++count3; break
case 4: ++count4; break;
}

This does what you ask: you evaluate the input, and keep track of the number of times that specific input has been seen. This form does suffer from many problems:

  • It requires one line of source code for each alternative. This becomes a problem when the user can enter any value, say, from 0 to 10,000!
  • It has duplicate, virtually identical lines.
  • It has many variables, each of which has to be entered independently, but uses identically.

We can reduce the variable count by specifing an array:

int count[5];
...
switch(input) {
case 0: ++count[0]; break;
case 1: ++count[1]; break;
case 2: ++count[2]; break;
case 3: ++count[3]; break;
case 4: ++count[4]; break;
}

This still suffers from too many almost-but-not-quite identical lines of code. Let's try to get rid of the switch statement:

int count[5];
...
++count[input];

Ah, now we are getting somewhere! By eliminating the switch statement, we have one easily-maintained line of code. But what if the user (accidentally or maliciously) enters a 6? Then we will increment count[6], which does not exist. This is a Bad Thing. We could increase the size of the array:

int count[50000];
...
++count[input];

Now we are safe from the user. If he enters a 6, the Bad Thing no longer happens. Uh-oh, what about if the user enters 51000? We will increment count[51000] which does not exist. It should be obvious that we can't win this game -- for any number we choose, the user might choose that number plus 1.

Even if we could win, we'd still lose. If we are only asking the user to enter a few numbers, then we will have wasted the other 49,997 entries in the arary.

Fortunately C++ has a data structure that we can use which:

  • can take arbitrary numbers as its range, and
  • is space-efficient (compared to a large wasted array).

That data structure is called a map:

std::map<int,int> count;
...
++count[input];

A map is sort of like an array, but grows itself in a special way. Only the entries that we use are ever allocated, and every entry that we use is automatically allocated.

墨落画卷 2024-11-07 14:41:15
std::map<int, int> frequency;
int value_entered_by_user = f();

frequency[value_entered_by_user]++;
std::map<int, int> frequency;
int value_entered_by_user = f();

frequency[value_entered_by_user]++;
静谧 2024-11-07 14:41:15

如果输入值的范围有限,则可以使用数组。数组的每个元素代表一个输入值。在开始时将元素初始化为 0,并在输入相应的输入值时递增适当的元素。

If your range of input values is limited, you can use an array. Each element of the array represents an input value. Initialize the elements to 0 at the beginning and increment the appropriate element when its corresponding input value is entered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文