fork download
  1. #include <iostream>
  2. #include <cctype>
  3. #include <iterator>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. const std::vector<std::string> notes {"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"};
  8. const std::vector<std::string> frets {"e", "B", "G", "D", "A", "E"};
  9.  
  10. std::string nth_fret(const std::string& note, int N);
  11. std::string handle_column(const std::vector<std::string>& input, size_t& col);
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. std::vector<std::string> input;
  16. for(size_t i = 0; i < 6; i++)
  17. {
  18. std::string line;
  19. getline(std::cin, line);
  20. input.push_back(line);
  21. }
  22. input[0][0] = 'e';
  23. for(size_t col = 2; col < input[0].length() - 1; col++)
  24. {
  25. std::string note = handle_column(input, col);
  26. if(note == "?")
  27. continue;
  28. std::cout << note << ' ';
  29. }
  30. std::cout << std::endl;
  31. return 0;
  32. }
  33.  
  34. std::string handle_column(const std::vector<std::string>& input, size_t& col)
  35. {
  36. for(size_t row = 0; row < 6; row++)
  37. {
  38. if(isdigit(input[row][col]))
  39. {
  40. int N = input[row][col] - '0';
  41. if(col + 1 < input[0].length() - 1 && isdigit(input[row][col + 1])) //two digits
  42. {
  43. col++;
  44. N *= 10;
  45. N += input[row][col] - '0';
  46. }
  47. std::string note = nth_fret(frets[row] == "e" ? "E" : frets[row], N);
  48. return note;
  49. }
  50. }
  51. return "?";
  52. }
  53.  
  54. std::string nth_fret(const std::string& note, int N)
  55. {
  56. size_t idx = N + std::distance(
  57. notes.begin(),
  58. std::find(notes.begin(), notes.end(), note)
  59. );
  60. idx %= notes.size();
  61. return notes[idx];
  62. }
Success #stdin #stdout 0s 3476KB
stdin
E|-----------------|-----------------|-----------------|-----------------|
	B|-----------------|-----------------|-----------------|-----------------|
	G|-7-7---7---------|-7-7---7---------|-------------7---|-----------------|
	D|---------9---7---|---------9---7---|-6-6---6-9-------|-6-6---6-9--12---|
	A|-----------------|-----------------|-----------------|-----------------|
	E|-----------------|-----------------|-----------------|-----------------|
stdout
D D D B A D D D B A G# G# G# B D G# G# G# B D