📄 calcex.cpp
字号:
// CalcEx.cpp : Defines the entry point for the console application.
//
/*
* Generated by MyEclipse Struts
*
* written by Yang Huaisheng
* Homepage: http://codefan.spaces.live.com
* version 0.01
* create at 2008-04-30
*
* Distribute freely, except: don't remove my name from the source or
* documentation (don't take credit for my work), mark your changes (don't
* get me blamed for your possible bugs), don't alter or remove this
* notice.
* No warrantee of any kind, express or implied, is included with this
* software; use at your own risk, responsibility for damages (if any) to
* anyone resulting from the use of this software rests entirely with the
* user.
*
* Send bug reports, bug fixes, enhancements, requests, flames, etc. to
* codefan@hotmial.com
*
*/
#include "stdafx.h"
#include "CalcEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
#include "stdafx.h"
#include "..\FormulaEX\Interface\IFormula.h"
#include "..\Formule\Calc\RegularOpt.h"
#include "..\FormulaEX\Interface\ConstDef.h"
LPVOID GetFormulaEx();
IFormulaEnvir * GetFormulaInterface()
{
return (IFormulaEnvir *) GetFormulaEx();
}
CString GetLine()
{
char buffer[1024];
int i, ch;
// printf( "CMP>" ); printf( "...>" );
/* Read in single line from "stdin": */
for( i = 0; (i < 1023)
&& ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
buffer[i] = (char)ch;
/* Terminate string with null character: */
buffer[i] = '\0';
return CString(buffer);
}
CString GetLine(LPCTSTR szSource,int & nPos,const int nLen)
{
char buffer[1024];
int i, ch;
for( i = 0; (i < 1023) && (nPos < nLen); ){
ch = szSource[nPos];
nPos++;
if(ch==13) break;
if(ch==10) continue;
buffer[i] = (char)ch;
i++;
}
buffer[i] = '\0';
return CString(buffer);
}
void ShowHelp(LPCTSTR szHelpTitle=NULL)
{
if(szHelpTitle==NULL)
goto listhelp;
if(strcmp(szHelpTitle,"list")==0){
printf("list \t( m[odule] | f[ormula] | v[ariable] | e[rror] \n"
"\t| r[eserved_key] | s[ystem_function] | sou[rce])\n");
return;
}
if(strcmp(szHelpTitle,"put")==0){
printf("put <modulename>\n");
return;
}
if(strcmp(szHelpTitle,"get")==0){
printf("get <modulename>\n");
return;
}
if(strcmp(szHelpTitle,"set")==0){
printf("set <varialbe name> <variable value>\n");
return;
}
if(strcmp(szHelpTitle,"save")==0){
printf("save <source file path>\n");
return;
}
if(strcmp(szHelpTitle,"load")==0){
printf("load <source file path\n");
return;
}
if(strcmp(szHelpTitle,"#")==0){
printf("The calculator tag!\n");
printf("The end tag of source!\n");
return;
}
if(strcmp(szHelpTitle,"run")==0){
printf("run Run the current source!\n");
printf("run <moudle index> Run the index module!\n");
printf("run <moudle name> Run the the named module!\n");
return;
}
if(strcmp(szHelpTitle,"cmd")==0){
printf("/?\t\thelp\n");
printf("/f <file path>\tCompile file and run the code\n");
printf("/v\t\tversion and infomation\n");
printf("<formula>\tCalculate the formule\n");
return;
}
listhelp:
printf("#\thelp\trun\tset\tlist\n");
printf("put\tget\tload\tsave\n");
}
int main(int argc, char* argv[])
{
// printf("%d,%d,%d\n", 7 % 2 , 3*7 % 2 +2, 3+7 % 2 * 2);
CString szSource;
CString szCurLine,szPrompt,szWord;
int i,nSp,nLine,nEmptyLine,nES;
IFormulaEnvir * pIFE = GetFormulaInterface();
if(argc>1){
char sFormula[4096];
strcpy(sFormula,argv[1]);
for(int i=2; i<argc; i++){
strcat(sFormula," ");
strcat(sFormula,argv[i]);
}
int nPos=0, nSel = strlen(sFormula);
szWord = CRegularOpt::GetARegularWord(sFormula,nPos);
if(szWord=="/"){
szWord = CRegularOpt::GetARegularWord(sFormula,nPos);
if(szWord=="f" || szWord=="file"){
CString sFilepath = sFormula;
sFilepath = sFilepath.Mid(nPos);
sFilepath.TrimLeft();
sFilepath.TrimRight();
CFileStatus status;
if(! CFile::GetStatus( sFilepath, status ) ){ // static function
printf("file \"%s\" is not exist!\n",sFilepath);
return 0;
}
pIFE->CompileFile(sFilepath);
nES = pIFE->GetCompileErrorSum();
if(nES>0){
for( i=0; i<nES; i++){
CString sError = pIFE->GetCompileError(i);
printf(sError);
printf("\n");
}
printf("There is %d error(s)!\n",nES);
}else{
CString s = pIFE->Run();
nES = pIFE->GetOutputSum();
for( i=0; i<nES; i++){
CString sOutput = pIFE->GetOutputDesc(i);
printf(sOutput);
printf("\n");
}
if(nES>0) printf("\n");
printf("The Result:%s\n",s);
}
}else if(szWord=="v" || szWord=="version"){
printf("Like C++ Compile : Release 1.0.0.1 - Production on 2007-03-01\n");
printf("Copyright (c) 2002, 2007, 杨淮生. All rights reserved.\n" );
printf("Author MSN:codefan@hotmail.com\n" );
for(i=0; i<CConstDef::SYSSTRSUM; i++){
printf("%s:\t",CConstDef::SYSSTRINGS[i].sName);
if(strlen(CConstDef::SYSSTRINGS[i].sName)<7)
printf("\t");
printf("%s\n",CConstDef::SYSSTRINGS[i].sValue);
}
}else {
ShowHelp("cmd");
}
return 0;
}
//run formula
szSource = "return ";
szSource += sFormula;
szSource += ";";
pIFE->CompileFormula(szSource);
nES = pIFE->GetCompileErrorSum();
if(nES>0){
for( i=0; i<nES; i++){
CString sError = pIFE->GetCompileError(i);
printf(sError);
printf("\n");
}
printf("There is %d error(s)!\n",nES);
}else{
CString s = pIFE->Run();
if(CRegularOpt::IsNumber(s))
s = CRegularOpt::PrecisionNum(s);
printf("%s\n",s);
}
return 0;
}
printf("Like C++ Compile : Release 1.0.0.1 - Production on 2007-03-01\n");
printf("Copyright (c) 2002, 2007, 杨淮生. All rights reserved.\n" );
printf("Author MSN:codefan@hotmail.com\n" );
while(1){
printf( "CMP> " );
szCurLine = GetLine();
//判断是否是指令
nSp = 0;
szWord = CRegularOpt::GetARegularWord(szCurLine,nSp);
if(szWord.IsEmpty())
continue;
if( szWord == "exit" || szWord == "quit")
break;
if( szWord == "help"){
CString szName = CRegularOpt::GetARegularWord(szCurLine,nSp);
ShowHelp(szName);
continue;
}
if( szWord == "test"){
// set value;
pIFE->SetVariable("a","1,2,3,4,5,6,7,8,9,0,12,21,3,345,45,543,542");
pIFE->SetVariable("b","500");
pIFE->CompileFormula("return (a[1.3]+a[1.5]*a[1.7]) * (a[7.3]+a[10.5]*a[13.7]);");
pIFE->SaveModule("mod1");
pIFE->CompileFormula("return (a[1.3]+a[1.5]*a[1.7]) * (a[7.3]+a[10.5]*a[13.7]);");
pIFE->SaveModule("mod2");
printf("%d\n",GetTickCount());
for(int i=0;i<25000;i++){
pIFE->SetFloat("b",500);
//pIFE->SetVariable("a","1,2,3,4,5,6,7,8,9,0,12,21,3,345,45,543,542");
pIFE->Calc(1);
pIFE->SetFloat("b",500);
pIFE->Calc(2);
//pIFE->SetVariable("b","500");
}
printf("%d\n",GetTickCount());
continue;
}
if( szWord == "set"){
// set value;
CString szName = CRegularOpt::GetARegularWord(szCurLine,nSp);
CString szValue= szCurLine.Mid(nSp);
szValue.TrimLeft();
if(szName.IsEmpty() || szValue.IsEmpty())
ShowHelp("set");
else
pIFE->SetVariable(szName,szValue);
continue;
}
if( szWord == "get"){
CString szName = CRegularOpt::GetARegularWord(szCurLine,nSp);
if(!pIFE->GetModule(szName))
printf("Module %s is not exist!\n",szName);
continue;
}
if( szWord == "put"){
CString szName = CRegularOpt::GetARegularWord(szCurLine,nSp);
if(szName.IsEmpty())
ShowHelp("put");
else
pIFE->SaveModule(szName);
continue;
}
if( szWord == "list"){
szWord = CRegularOpt::GetARegularWord(szCurLine,nSp);
if(szWord=="module" || szWord=="m"){
for(i=0; i<pIFE->GetModuleSum(); i++){
printf( pIFE->GetModuleName(i));
printf("\n");
}
}else if(szWord=="formula" || szWord=="f" ){
for(i=0; i<pIFE->GetFormulaItemSum(); i++){
printf(pIFE->ListFormulaItem(i));
printf("\n");
}
}else if(szWord=="variable" || szWord=="v" ){
//CString szName = CRegularOpt::GetARegularWord(szCurLine,nSp);
//if(szName.IsEmpty()){
for(i=0; i<pIFE->GetVariableSum(); i++){
printf(pIFE->ListVariable(i));
printf("\n");
}
//}else{}
}else if(szWord=="error" || szWord=="e" ){
nES = pIFE->GetCompileErrorSum();
for( i=0; i<nES; i++){
printf(pIFE->GetCompileError(i));
printf("\n");
}
printf("There is %d error(s)!\n",nES);
}else if(szWord=="reserved_key" || szWord=="r" ){
for( i=0; i<CConstDef::RESKEYSUM; i++){
printf(CConstDef::RESKEYS[i].sName);
if( (i+1) % 6 )
printf("\t");
else
printf("\n");
}
if(CConstDef::RESKEYSUM % 6)
printf("\n");
}else if(szWord=="system_function" || szWord=="s" ){
for(i=0; i<CConstDef::SYSFUNSUM; i++){
CString sDesc;
sDesc.Format("%s(%d)",CConstDef::SYSFUNCS[i].sName,CConstDef::SYSFUNCS[i].nPrmSum);
printf(sDesc);
if( (i+1) % 4 ){
printf("\t");
if(sDesc.GetLength()<8)
printf("\t");
}else
printf("\n");
}
if(CConstDef::SYSFUNSUM % 4)
printf("\n");
}else if(szWord=="source" || szWord=="sou" ){
LPCSTR szSou = pIFE->GetSource();
int nPos=0,nLen=pIFE->GetSourceLen();
nLine=0;
szCurLine = GetLine(szSou,nPos,nLen);
printf( "CMP> " );
printf( szCurLine );
printf( "\n" );
while(nPos<nLen){
nLine++;
szPrompt.Format("...%d> ",nLine);
szPrompt = szPrompt.Mid(szPrompt.GetLength()-5);
printf( szPrompt);
szCurLine = GetLine(szSou,nPos,nLen);
printf( szCurLine );
printf( "\n" );
}
}else{
for(i=0; i<CConstDef::SYSSTRSUM; i++){
printf("%s:\t",CConstDef::SYSSTRINGS[i].sName);
if(strlen(CConstDef::SYSSTRINGS[i].sName)<7)
printf("\t");
printf("%s\n",CConstDef::SYSSTRINGS[i].sValue);
}
}
continue;
}
if( szWord == "run"){
int nCurModule=-1;
szWord = CRegularOpt::GetARegularWord(szCurLine,nSp);
if(szWord.GetLength() > 0){
if(CRegularOpt::IsNumber(szWord)){
nCurModule = int(atof(szWord));
if(nCurModule>=pIFE->GetModuleSum() || nCurModule<1)
nCurModule=-1;
}else
nCurModule=-2; // named
}
CString sResult;
if(nCurModule == -1){
nES = pIFE->GetCompileErrorSum();
if(nES>0){
printf("There is %d error(s)!\n",nES);
continue;
}
sResult = pIFE->Run();
}else if(nCurModule == -2){
sResult = pIFE->Run(szWord);
}else{
sResult = pIFE->Run(nCurModule);
}
nES = pIFE->GetOutputSum();
for( i=0; i<nES; i++){
CString sOutput = pIFE->GetOutputDesc(i);
printf(sOutput);
printf("\n");
}
if(nES>0) printf("\n");
printf("The Result:%s\n",sResult);
continue;
}
if(szWord == "#"){
szSource ="return "+ szCurLine.Mid(nSp)+";";
pIFE->CompileFormula(szSource);
nES = pIFE->GetCompileErrorSum();
if(nES>0)
printf("There is %d error(s)!\n",nES);
else{
CString s = pIFE->Run();
// if(CRegularOpt::IsNumber(s))
// s = CRegularOpt::PrecisionNum(s);
printf("%s\n",s);
}
continue;
}
if( szWord == "load"){
CString szFilepath = szCurLine.Mid(nSp);
szFilepath.TrimLeft();
szFilepath.TrimRight();
if(szFilepath.IsEmpty()){
ShowHelp("load");
}else{
CFile txtFile;
if(!txtFile.Open(szFilepath,CFile::modeRead)){
printf("File %s could not be opened.\n",szFilepath);
}else{
int nPos=0,nLen = txtFile.GetLength();
char * szFormula = new char[nLen+1];
txtFile.ReadHuge(szFormula,nLen);
txtFile.Close();
//show source
nLine=0;
szCurLine = GetLine(szFormula,nPos,nLen);
printf( "CMP> " );
printf( szCurLine );
printf( "\n" );
while(nPos<nLen){
nLine++;
szPrompt.Format("...%d> ",nLine);
szPrompt = szPrompt.Mid(szPrompt.GetLength()-5);
printf( szPrompt);
szCurLine = GetLine(szFormula,nPos,nLen);
printf( szCurLine );
printf( "\n" );
}
//compile source
pIFE->CompileFormula(szFormula,nLen);
delete [] szFormula;
nES = pIFE->GetCompileErrorSum();
printf("\n");
for( i=0; i<nES; i++){
CString sError = pIFE->GetCompileError(i);
printf(sError);
printf("\n");
}
printf("There is %d error(s)!\n",nES);
}
}
continue;
}
if( szWord == "save"){
CString szFilepath = szCurLine.Mid(nSp);
szFilepath.TrimLeft();
szFilepath.TrimRight();
if(szFilepath.IsEmpty()){
ShowHelp("save");
}else{
CFile txtFile;
if(!txtFile.Open(szFilepath,CFile::modeCreate|CFile::modeWrite)){
printf("File %s could not be opened.\n",szFilepath);
}else{
LPCSTR szSou = pIFE->GetSource();
int nLen=pIFE->GetSourceLen();
txtFile.WriteHuge(szSou,nLen);
txtFile.Close();
printf("Source code be saved.\n",szFilepath);
}
}
continue;
}
szSource = szCurLine;
nLine=0;nEmptyLine=0;
while(1){
nLine++;
szPrompt.Format("...%d> ",nLine);
szPrompt = szPrompt.Mid(szPrompt.GetLength()-5);
printf( szPrompt);
szCurLine = GetLine();
szCurLine.TrimRight();
if(szCurLine.IsEmpty())
nEmptyLine++;
else
nEmptyLine = 0;
if( (nEmptyLine>1) ||
(szCurLine=="#") )
break;
szSource +="\r\n" + szCurLine;
}
pIFE->CompileFormula(szSource);
nES = pIFE->GetCompileErrorSum();
printf("\n");
for( i=0; i<nES; i++){
CString sError = pIFE->GetCompileError(i);
printf(sError);
printf("\n");
}
printf("There is %d error(s)!\n",nES);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -