【问题标题】:How do you add certain values in map based off of user input?您如何根据用户输入在地图中添加某些值?
【发布时间】:2020-12-13 06:11:09
【问题描述】:

我正在尝试用 c++ 制作 Gematria 计算器,并希望获得与 java 中相同的结果。我如何在 C++ 中得到这样的结果,它可以打印用户输入的值的总和,并获得与 Java 相同的最终结果?请注意我是 C++ 的初学者,所以请原谅我对这个问题的任何无知。

我管理thanks to stackoverflow 用Java 制作了一个。在 Java 中,我可以输入多个单词并获得 Gematria,我想在 C++ 中获得相同的结果。

Java 代码

package com.company;
`import java.util.Hashtable;`
`import java.util.Scanner;`

public class English_Gematria {
   public static void main(String[] args) {
     Hashtable<String, Integer> Gematria_Values = new Hashtable<>();
     Gematria_Values.put(" ", 0);
     Gematria_Values.put("A", 1);
     Gematria_Values.put("B", 2);
     Gematria_Values.put("C", 3);
// Goes through rest of alphabet.
System.out.println("What do you want the gematria of?");
Scanner i = new Scanner(System.in);
String Gematria = i.nextline();
int sum = 0;
for (int j = 0; Gematria.length(); j++) {
    sum += Gematria_Values.get(String.valueOf(Gematria.toUpperCase().charAt(j)));
}
System.out.println("The Gematria of " + Gematria + " is " + sum);

C++ 代码

#include <iostream>
#include <cmath>
#include <map>

using namespace std;

int main() {
std::map<std::string, std::double_t> Gematria_Values;
Gematria_Values[" "] = 0;
Gematria_Values["A"] = 1;
Gematria_Values["B"] = 2;
Gematria_Values["C"] = 3;
//  Goes through rest of alphabet.
std::string Gematria;
std::cout << "What do you want the gematria of?" << std::endl;
std::cin >> Gematria;
transform(Gematria.begin(), Gematria.end(), Gematria.begin(), ::toupper);
int sum = 0;
for (int i = 0; i < Gematria.length(); i++)
{ 
sum += Gematria_Values.at(Gematria);
}
std::cout << "The Gematria is " << sum;

return 0;

}

在 C++ 中,当用户输入(不带引号)“a”或“A”时,我得到

The Gematria is 1.

当用户输入(不带引号)“b”或“B”时,我得到

The Gematria is 2.

但是,当用户输入(不带引号)“ab”或“bb”时,我会得到

terminate called after throwing an instance of 'std::out_of_range'
  what():  map::at

我的问题是:我如何才能获得与 Java 相同的最终结果?

【问题讨论】:

    标签: c++ maps


    【解决方案1】:
    int main()
    {
        std::unordered_map<char, double> Gematria_Values = {
            {' ', 0},
            {'A', 1},
            {'B', 2},
            {'C', 3}
        };
    
        std::string Gematria;
        std::cout << "What do you want the gematria of?" << std::endl;
        std::cin >> Gematria;
        transform(Gematria.begin(), Gematria.end(), Gematria.begin(), ::toupper);
    
        int sum = 0;
        for (auto ch : Gematria)
            sum += Gematria_Values[ch];
            
        std::cout << "The Gematria is " << sum;
    }
    

    Gematria_Values[ch];behavior:如果在它创建的映射中找不到ch,它会在映射中插入一个默认的新对(键为ch,值0)。

    如果你想(你应该)检查有效的用户输入:你可以坚持使用at,它会抛出,或者,如果你不想要/不需要异常,你可以这样做:

    for (auto ch : Gematria)
    {
        auto found = Gematria_Values.find(ch);
        if (found == Gematria_Values.end())
        {
            // deal with error
        }
        else
        {
            sum += found->second;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多