📄 explain_entity.h
字号:
#include"explain.h"
int explain::FindToken( const char *s)
{
int first= 0, last = 17,length= strlen(s);
int m;
char *cache = new char[length + 1];
for(m = 0; m < length; m++)
if(s[m] >= 'A' && s[m] <= 'Z')
cache[m]=tolower(s[m]); //将大写的字符统一转化为小写
else
cache[m] = s[m];
cache[m] = '\0';
while(first <last+1)
{
m = (first + last) / 2;
if(strcmp(cache, Token_elem[m][1]) == 0)
return m;
if(strcmp(cache, Token_elem[m][1]) <0)
last = m - 1;
else
first = m + 1;
}
return -1;
};
double explain::calculate( const char *s)
{
double m=0.0,n=1.0,truevalue=0.0;
char const *p=s;
while(*p!='\0'&&*p!='.')
{
m=*p-'0';
truevalue=truevalue*10+m;
p++;
}
///////////////////
if(*p++=='.')
{
do
{
n/=10.0;
m=*p-'0';
truevalue+=m*n;
p++;
}while(*p!='\0');
return truevalue;
}
return truevalue;
//
};
unsigned explain::letter(char ch)
{
return ch>='a'&& ch<='z' || ch>='A' && ch<='Z';
};
void explain::importance(char *ss)
{
ifstream s;
s.open(ss);
char ch;
char *cache=new char[100];
int cnt;
int flag;
while(!s.eof())
{
cnt = 0;
do
{
ch=s.get();
}
while((ch==' '||ch=='\t'||ch=='\n'));
if(s.eof())
{
cout<<"已经到文件结尾了,请重新输入。";
cout<<'\n';
return;
}
cache[cnt++]=ch;
if(letter(ch))
goto station_1;
if(isdigit(ch))
goto station_2;
switch(ch)
{
case '*':
goto station_4;
break;
case '/':
goto station_6;
break;
case '-':
goto station_7;
break;
case '+':
case ',':
case ';':
case '(':
case ')':
goto station_5;
break;
default :break;
}
flag=0;
ch=s.get();
cnt++;
goto end;
station_1: /////标示符
ch=s.get();
cache[cnt++]=ch;
if (isdigit(ch)||letter(ch))
goto station_1;
flag=1;
goto end;
station_2: ////////数字
ch=s.get();
cache[cnt++]=ch;
if (ch=='.')
goto station_3;
if (isdigit(ch))
goto station_2;
flag=2;
goto end;
station_3:
ch=s.get();
cache[cnt++] = ch;
if(isdigit(ch))
goto station_3;
flag= 3;
goto end;
station_4:
ch=s.get();
cache[cnt++]=ch;
if(ch=='-')
goto station_5;
flag=3;
goto end;
station_5:
ch=s.get();
cnt++;
flag = 5;
goto end;
station_6:
ch=s.get();
cache[cnt++] = ch;
if(ch == '/')
goto station_5;
flag = 6;
goto end;
station_7:
ch=s.get();
cache[cnt++] = ch;
if(ch == '-')
goto station_5;
flag = 7;
goto end;
end:
s.putback(ch);
cache[cnt - 1] = '\0';
Token &replace = Token_flow[token_flow_size++];
if(flag == 0)
{
replace.type = "error";
replace.content.caption = new char[100];
strcpy(replace.content.caption,cache);
continue;
}
if(flag == 1) //必定为标示符
{
////比较重要的地方
replace.content.caption = new char[100];
strcpy(replace.content.caption,cache);
int p = FindToken(cache);
if(p == -1)
replace.type = "error";
else
{
replace.type = Token_elem[p][0];
if(Token_elem[p][0] == "constant")
{
if(Token_elem[p][1] == "pi")
replace.content.value = 3.1415926;
if(Token_elem[p][1] == "e")
replace.content.value = 2.71828;
continue;
}
if(Token_elem[p][0] == "sin")
replace.content.func = sin;
if(Token_elem[p][0] == "cos")
replace.content.func= cos;
if(Token_elem[p][0] == "tan")
replace.content.func= tan;
if(Token_elem[p][0] == "log")
replace.content.func = log;
if(Token_elem[p][0] == "exp")
replace.content.func = exp;
if(Token_elem[p][0] == "sqrt")
replace.content.func = sqrt;
}
continue;
}
switch(flag)
{
case 1:
break;
case 2:
case 3:
if(1)
{
replace.type = "digit";
replace.content.value = calculate(cache);
break;
}
case 4:
case 6:
case 7:
if(1)
{
replace.type = "operator";
replace.content.caption = new char[100];
strcpy(replace.content.caption,cache);
break;
}
case 5:
if(strcmp(cache, "**") == 0)
{
replace.type = "power";
replace.content.caption = new char[100];
strcpy(replace.content.caption,cache);
}
if(strcmp(cache, "//") == 0 || strcmp(cache, "--") == 0) //忽略过所有注释
{
token_flow_size--;
while(s.get(ch),ch!='\n');
}
if(strcmp(cache, "+") == 0)
{
replace.type = "operator";
replace.content.caption = new char[100];
strcpy(replace.content.caption,cache);
}
if(strcmp(cache, ",") == 0 || strcmp(cache, ";") == 0 || strcmp(cache, "(") == 0 || strcmp(cache, ")") == 0)
{
replace.content.caption = new char[100];
strcpy(replace.content.caption,cache);
replace.type =new char[100];
strcpy(replace.type,cache);
}
break;
default:
break;
}
}
}
void explain::RestoreResult(char *file)
{
ofstream fout(file);
for(int i = 0; i < token_flow_size; i++)
{
fout << setw(15) << i << setw(20) << Token_flow[i].type;
if(Token_flow[i].type == "const" || Token_flow[i].type == "digit")
{
fout << setw(20) << Token_flow[i].content.value << endl;
continue;
}
if(Token_flow[i].type == "function")
{
fout << setw(20) << Token_flow[i].content.func << endl;
continue;
}
fout << setw(20) << Token_flow[i].content.caption << endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -