我如何从每个钢琴钥匙的一个阵列中构造正确的和弦
How can I construct the right chord from one array per piano key?
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;
}
相关文章:
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- 特征:创建一个具有函数的三维阵列
- 将积分类型的数组作为另一个不相关的积分类型的阵列进行访问的安全且符合标准的方法
- 围绕二维阵列做一个圆圈
- 为什么一个二维阵列会导致赛段故障,而另一个则不会导致?
- 如何将X 2维数组连接到一个三维阵列中
- 如何打印一个字符阵列和其中的特定字符
- 从一个文件读取到三个并行阵列
- 内存C++复制是否将内存地址复制到另一个阵列
- 为什么以下代码在阵列中显示上个月的最后一个月,而不是显示降雨量最高和最低降雨量的月份
- 如果在C 中已经回荡了另一个相同值的阵列元素,请防止循环回荡
- 通过从当地的C风格阵列中返回指针来获取一个悬空的指针
- 新的一个一维阵列,非常大,例如60000*60000
- 阵列操作,复制最后一个元素
- 将25个甚至整数存储到一个名为intlist的整数阵列中,然后在屏幕上显示数组,并在屏幕上显示名为fivintegers
- 指针阵列在一个对象数组中函数
- 创建一个阵列到结合的阵列
- 删除指向排序的字符串阵列的指针会在运行后会有一个错误
- 将多维阵列甚至元素传输到一个维数阵列
- C 是平行阵列一个定义的结构