/************************************************************************
* description: 演奏内容
* remark:
************************************************************************/
#ifndef _PLAY_CONTEXT_H_
#define _PLAY_CONTEXT_H_
#include <string>
#include <iostream>
using namespace std;
class playContext
{
public:
string getPlayText()
{
return m_strText;
}
void setPlayText(const string& strText)
{
m_strText = strText;
}
private:
string m_strText;
};
#endif// _PLAY_CONTEXT_H_
/************************************************************************
* description: 表达式类
* remark:
************************************************************************/
#ifndef _EXPRESSION_H_
#define _EXPRESSION_H_
#include "playContext.h"
class expression
{
public:
// 解释器
void interpret(playContext& PlayContext)
{
if (PlayContext.getPlayText().empty())
{
return;
}
else
{
string strPlayKey = PlayContext.getPlayText().substr(0, 1);
string strtemp = PlayContext.getPlayText().substr(2);
PlayContext.setPlayText(strtemp);
size_t nPos = PlayContext.getPlayText().find(" ");
string strPlayValue = PlayContext.getPlayText().substr(0, nPos);
int nPlayValue = atoi(strPlayValue.c_str());
nPos = PlayContext.getPlayText().find(" ");
PlayContext.setPlayText(PlayContext.getPlayText().substr(nPos + 1));
excute(strPlayKey, nPlayValue);
}
}
// 执行
virtual void excute(string& strKey, const int nValue) = 0;
private:
};
#endif// _EXPRESSION_H_
/************************************************************************
* description: 音符类
* remark:
************************************************************************/
#ifndef _NOTE_H_
#define _NOTE_H_
#include "expression.h"
class note : public expression
{
public:
virtual void excute(string& strKey, const int nValue)
{
char szKey[2];
strncpy(szKey, strKey.c_str(), strKey.length());
string strNote;
switch (szKey[0])
{
case 'C':
strNote = "1";
break;
case 'D':
strNote = "2";
break;
case 'E':
strNote = "3";
break;
case 'F':
strNote = "4";
break;
case 'G':
strNote = "5";
break;
case 'A':
strNote = "6";
break;
case 'B':
strNote = "7";
break;
default:
strNote = "error";
break;
}
cout << strNote << " ";
}
};
#endif// _NOTE_H_
/************************************************************************
* description: 音阶类
* remark:
************************************************************************/
#ifndef _SCALE_H_
#define _SCALE_H_
#include "expression.h"
class scale : public expression
{
public:
virtual void excute(string& strKey, const int nValue)
{
string strScale;
switch (nValue)
{
case 1:
strScale = "低音";
break;
case 2:
strScale = "中音";
break;
case 3:
strScale = "高音";
break;
default:
strScale = "error";
break;
}
cout << strScale << " ";
}
private:
};
#endif// _SCALE_H_
#ifndef _SPEED_H_
#define _SPEED_H_
#include "expression.h"
class speed : public expression
{
public:
virtual void excute(string& strKey, const int nValue)
{
string strSpeed;
if (nValue < 3)
{
strSpeed = "快速";
}
else if (nValue >= 6)
{
strSpeed = "慢速";
}
else
{
strSpeed = "中速";
}
cout << strSpeed << " ";
}
};
#endif// _SPEED_H_
// InterpreterApp.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "note.h"
#include "scale.h"
#include "speed.h"
#include "playContext.h"
int _tmain(int argc, _TCHAR* argv[])
{
playContext context;
cout << "Music:";
context.setPlayText("T 2 O 2 E 3 G 5 G 5 ");
expression* expressObj = NULL;
while (!context.getPlayText().empty())
{
string strSep = context.getPlayText().substr(0, 1);
char szKey[2];
strncpy(szKey, strSep.c_str(), strSep.length());
switch (szKey[0])
{
case 'O':
expressObj = new scale();
break;
case 'T':
expressObj = new speed();
break;
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'A':
case 'B':
case 'P':
expressObj = new note();
break;
default:
break;
}
if (NULL != expressObj)
{
expressObj->interpret(context);
}
}
system("pause");
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有