fork(1) download
  1. #include <vector>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <iostream>
  8.  
  9.  
  10. void ParseTablature(const std::string& input)
  11. {
  12. using namespace std;
  13.  
  14. static const char* NoteOrder[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
  15.  
  16. istringstream file(input);
  17. if (file)
  18. {
  19. vector<unsigned int> noteBases;
  20. vector<istringstream> lines;
  21. do
  22. {
  23. string line;
  24. getline(file, line);
  25. lines.push_back(istringstream(line));
  26. noteBases.push_back(find(begin(NoteOrder), end(NoteOrder), line.substr(0, 1)) - begin(NoteOrder));
  27. } while (file);
  28.  
  29. vector<unsigned int> octaves = { 4, 3, 3, 3, 2, 2 };
  30. fill_n(back_inserter(octaves), lines.size() - octaves.size(), 0); // For safety in case we have more than 6 lines (???)
  31. transform(noteBases.begin(), noteBases.end(), octaves.begin(), noteBases.begin(), [](auto noteBase, auto octave)
  32. {
  33. return noteBase + (octave * 12); // Encode base octave
  34. });
  35.  
  36. while (any_of(lines.begin(), lines.end(), [](const auto& line) { return line.good(); }))
  37. {
  38. unsigned int lineIndex = 0;
  39. for (auto& line : lines)
  40. {
  41. if (line)
  42. {
  43. char first = line.get();
  44. if (isdigit(first))
  45. {
  46. string indexString;
  47. indexString += first;
  48. if (line)
  49. {
  50. char second = line.get();
  51. if (isdigit(second))
  52. {
  53. indexString += second;
  54. }
  55. }
  56.  
  57. unsigned int noteIndex;
  58. istringstream indexStream(indexString);
  59. indexStream >> noteIndex;
  60.  
  61. auto absoluteNote = noteIndex + noteBases[lineIndex];
  62. auto octave = absoluteNote / 12;
  63. auto relativeNote = absoluteNote % 12;
  64.  
  65. cout << NoteOrder[relativeNote] << octave << " ";
  66. }
  67. }
  68.  
  69. ++lineIndex;
  70. }
  71. }
  72. }
  73. }
  74.  
  75. int main(int argc, const char** argv)
  76. {
  77. std::string input;
  78. std::string line;
  79. std::cin >> line;
  80. while (line != "0")
  81. {
  82. input += line;
  83. input += '\n';
  84. std::cin >> line;
  85. }
  86.  
  87. ParseTablature(input);
  88. }
Success #stdin #stdout 0s 3484KB
stdin
E|---------------0-------------------|
	B|--------------------1--------------|
	G|------------------------2----------|
	D|---------2-------------------------|
	A|----------------------------0------|
	E|-0--12-----------------------------|
	0
stdout
E2 E3 E3 E4 C4 A3 A2