我如何从每个钢琴钥匙的一个阵列中构造正确的和弦

How can I construct the right chord from one array per piano key?

本文关键字:阵列 一个 钢琴 钥匙      更新时间:2023-10-16

i当前有一个数组,该数组包含音乐中的12个主要尺度(钥匙列表)。我需要一种在数组中循环的方法,并返回用户进入的和弦,而无需硬编码和弦进入我的程序。例如,如果用户输入字符串CEG,则程序将以:"您输入C-E-G,CMAJOR和弦"。我的问题在于我的构造函数。我无法返回我的密钥列表数组的3个单独的索引,因此我需要一种基于用户输入的字符串从数组中形成新字符串的方法吗?..如果我需要更具体或澄清,请告诉我,让我知道,知道但是任何帮助将不胜感激。

github:chordquiz-program

编辑:我以前从未在这里发布过,并试图使其具体。如果仍然是一个不好的问题,那就告诉我。更好的是,告诉我为什么...

我的问题在我的构造函数中。我无法返回我的钥匙列表数组

的3个单独的索引

我真的希望我理解了吗?

是的,你可以!使用C 11,它很容易,而C 17它的魔力。我正在使用int作为我的示例。

C 11

#include <tuple>
std::tuple<int, int, int> Music::constructChord(ChordType chord)
{
    // do something to calculate int a, b, c
    return std::make_tuple(a, b, c);   // just an example of course 
}

C 17

#include <tuple>
std::tuple<int, int, int> Music::constructChord(ChordType chord)
{
    // do something to calculate int a, b, c
    return {a, b, c};   // that is really cool, isn´t it? 
}

sanitization and Tupleing输入

这是一个快速的hack示例,说明如何对输入进行消毒和培养。我已经放入一个主(),以便可以直接编译并使用:

#include <iostream>
#include <map>
#include <regex>
int main()
{
    // this map is only a stub of course, a lot is missing ...
    std::map<std::string, int> sanitation = {{"a",0}, {"bb",1}, {"c",2}, {"c#",3}, {"#c",3}, {"db",3},  {"bd",3}};
    // input block from your code
    std::string myChord;
    std::cout << "Please enter a chord, at least three different piano keys:n";
    getline(std::cin, myChord);
    transform(myChord.begin(), myChord.end(), myChord.begin(), ::tolower);
    // parsing the input
    std::regex regex(  R"(([a-g#]{1,2}) ([a-g#]{1,2}) ([a-g#]{1,2}))");
    std::smatch m;
    std::regex_search(myChord, m, regex);
    // sanitizing und tupleing it
    std::vector<int> matched;
    for (int i=1; i<m.size(); i++)
    {
        auto hit = sanitation.find(m[i]);
        if(hit!=sanitation.end())
            matched.push_back(hit->second);
    }
    auto my_tuple = std::make_tuple(matched);
    return 0;
}