更新日志:
V1.0->V2.0 增加了存曲谱和打开曲谱的功能。
V2.0->V2.1 优化了使用界面。
V2.1->V2.2 增加了p暂停键。
V2.2->V2.3 增加了界面优化。
#include<bits/stdc++.h>
#include<windows.h>
#include<fstream>
#include<conio.h>
#include<io.h>
using namespace std;
void getFiles(string path, vector<string>& files) {
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {
if (!(fileinfo.attrib & _A_SUBDIR)) {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
string prefix="0123456789abcdefghijklmnopqrstuvwxyz";
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
system("color F0");
char path[100]="C:\\WsyPianoMusic";
CreateDirectory(path,NULL);
stofsad:
printf("----------wsy的电子钢琴----------\n");
printf("按1,2,3,4,5,6,7,8,9,0来演奏,按G来保存曲谱/打开已有曲谱!\n");
printf(" 1 2 3 4 5 6 7 8 9 0\n");
while(true){
char c=getch();
if(c=='1') Beep(200,100);
else if(c=='2') Beep(300,100);
else if(c=='3') Beep(500,100);
else if(c=='4') Beep(800,100);
else if(c=='5') Beep(1000,100);
else if(c=='6') Beep(1200,100);
else if(c=='7') Beep(1500,100);
else if(c=='8') Beep(1800,100);
else if(c=='9') Beep(2000,100);
else if(c=='0') Beep(2500,100);
else if(c=='g' || c=='G'){
system("cls");
printf("请问你要打开曲谱(1)还是输入新曲谱(2)?\n");
char c1=getch();
if(c1=='1'){
int t=0,p;
system("cls");
printf("曲谱列表:\n");
string dirpath="C:\\WsyPianoMusic\\";
vector<string> files;
getFiles(dirpath,files);
for(const auto& file : files){
cout<<prefix[t]<<"."<<file<<endl;
++t;
}
char cc=getch(),cn;
if('0'<=cc && '9'>=cc) p=cc-'0';
else p=cc-'a'+10;
if(p>t) throw "err_file_not_found";
ifstream infile;
infile.open(files[p]);
while(!infile.eof()){
infile>>cn;
if(cn=='1') Beep(200,100);
else if(cn=='2') Beep(300,100);
else if(cn=='3') Beep(500,100);
else if(cn=='4') Beep(800,100);
else if(cn=='5') Beep(1000,100);
else if(cn=='6') Beep(1200,100);
else if(cn=='7') Beep(1500,100);
else if(cn=='8') Beep(1800,100);
else if(cn=='9') Beep(2000,100);
else if(cn=='0') Beep(2500,100);
printf("%c ",cn);
}
system("cls");
goto stofsad;
} else {
string ss;
char nm[100],ch;
printf("请输入歌曲名:");
scanf("%s",nm);
dasd:
ss.append("C:\\WsyPianoMusic\\");
ss.append(nm);
ss.append(".wsym");
ofstream outfile(ss);
outfile.close();
fstream fs(ss,ios::out);
printf("\n请输入歌词,如果输入错误按D来退格。按F来结束。");
while(ch!='F' && ch!='f'){
ch=getch();
if(ch>='0' && ch<='9') fs.put(ch);
else if(ch=='d' || ch=='D') fs.put((char)8);
else if(ch=='f' || ch=='F');
else throw "按键不正确!";
printf("%c",ch);
}
printf("输入完毕,是否确认?确认(1)不确认(其他)");
ch= getch();
if(ch=='1'){
system("cls");
fs.close();
goto stofsad;
} else {
goto dasd;
}
}
}
else throw "你必须按1~9之间的按键!";
}
return 0;
}