C##闪亮登场
2024-11-21 22:55:58
发布于:广东
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
string s1,s2,s3;
long long i1,i2,i3;
bool flag;
map<string,string> sv;
map<string,double> iv;
map<string,map<int,double>> liv;
map<string,map<int,string>> lsv;
map<string,bool> cv;
string mn;
bool is_int(double a){
int b=a;
if(b==a) return 1;
return 0;
}
bool ist(string a){
bool an=0;
for(int i=0;i<a.size();i++){
if(a[0]=='-'&&i==0) continue;
else if(a[i]=='.'&&i!=0&&an==0){
an=1;
continue;
}
else if(a[i]<'0'||a[i]>'9'){
return false;
}
}
return true;
}int ism(string a){
bool jiba=0;
int kuo=0;
string gb;
for(int i=0;i<a.size();i++){
if(a[i]=='(') kuo++;
else if(a[i]==')'){
if(kuo==0) return 0;
else kuo--;
}
else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='|'||a[i]=='^'||a[i]=='&'){
if(ist(gb)==0&&(cv[gb]==0||(sv[gb].size()))) return 0;
if(i==0||i==a.size()-1) return 0;
else if(a[i-1]=='+'||a[i-1]=='-'||a[i-1]=='/'||a[i-1]=='|'||a[i-1]=='^'||a[i-1]=='&') return 0;
else if(a[i-1]=='*'&&a[i]!='*') return 0;
jiba=1;
gb="";
}
else gb+=a[i];
}
if(kuo!=0) return 0;
else if(jiba==0) return 1;
return 2;
}
string mturn(string a){
string b;
for(int i=0;i<a.size();i++){
if(a[i]=='*'&&a[i+1]=='*') b+='P',i++;
else b+=a[i];
}
return b;
}
double math(string a){
stack<string> inv;
stack<string> invv;
stack<string> invvv;
stack<string> kong;
stack<char> fv;
stack<char> fvv;
stack<char> fvvv;
stack<char> kong2;
inv.push("");
a=mturn(a);
if(a.find("(",0)!=string::npos){
string aa="",ab="";
int kuo=0,kk=a.find("(",0);
for(int i=kk+1;i<a.size();i++){
if(a[i]==')'&&kuo==0){
for(int j=0;j<kk;j++){
ab+=a[j];
}
ab+=to_string(math(aa));
for(int j=i+1;j<a.size();j++){
ab+=a[j];
}
a=ab;
ab="";
}
else if(a[i]==')') kuo-=1;
else if(a[i]=='(') kuo+=1;
else aa+=a[i];
}
}
int jia=0,jian=0,cheng=0,chu=0,ands=0,ors=0,yhs=0,pows=0;
for(int i=0;i<a.size();i++){
if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='|'||a[i]=='^'||a[i]=='&'||a[i]=='P'){
if(a[i]=='+') jia++;
else if(a[i]=='-') jian++;
else if(a[i]=='*') cheng++;
else if(a[i]=='/') chu++;
else if(a[i]=='&') ands++;
else if(a[i]=='|') ors++;
else if(a[i]=='^') yhs++;
else if(a[i]=='P') pows++;
if(!ist(inv.top())&&cv[inv.top()]==0){
cout<<"<<<MathError:算式错误。\n";
flag = 0;
return 0;
}
else if(!ist(inv.top())){
string ggb=inv.top();
inv.pop();
inv.push(to_string(iv[ggb]));
}
inv.push("");
fv.push(a[i]);
}
else{
string g;
g=inv.top()+a[i];
inv.pop();
inv.push(g);
}
}
if(!ist(inv.top())&&cv[inv.top()]==0){
cout<<"<<<MathError:算式错误。\n";
flag = 0;
return 0;
}
else if(!ist(inv.top())){
string ggb=inv.top();
inv.pop();
inv.push(to_string(iv[ggb]));
}
fvv=fv;
invv=inv;
if(pows){
while(fvv.size()){
fvvv.push(fvv.top());
fvv.pop();
}
while(invv.size()){
invvv.push(invv.top());
invv.pop();
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
while(pows){
if(fvv.top()=='P'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(powf(stoi(g),stod(gg))));
invvv.push(invv.top());
fvv.pop();
pows--;
}
else{
invvv.push(invv.top());
fvvv.push(fvv.top());
fvv.pop();
invv.pop();
}
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
}
if(cheng||chu){
while(fvv.size()){
fvvv.push(fvv.top());
fvv.pop();
}
while(invv.size()){
invvv.push(invv.top());
invv.pop();
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
while(cheng||chu){
if(fvv.top()=='*'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(stod(g)*stod(gg)));
invvv.push(invv.top());
fvv.pop();
cheng--;
}
else if(fvv.top()=='/'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(stod(g)/stod(gg)));
invvv.push(invv.top());
fvv.pop();
chu--;
}
else{
invvv.push(invv.top());
fvvv.push(fvv.top());
fvv.pop();
invv.pop();
}
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
}
if(jia||jian){
while(fvv.size()){
fvvv.push(fvv.top());
fvv.pop();
}
while(invv.size()){
invvv.push(invv.top());
invv.pop();
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
while(jia||jian){
if(fvv.top()=='+'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(stod(g)+stod(gg)));
invvv.push(invv.top());
fvv.pop();
jia--;
}
else if(fvv.top()=='-'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(stod(g)-stod(gg)));
invvv.push(invv.top());
fvv.pop();
jian--;
}
else{
invvv.push(invv.top());
fvvv.push(fvv.top());
fvv.pop();
invv.pop();
}
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
}
if(ands){
while(fvv.size()){
fvvv.push(fvv.top());
fvv.pop();
}
while(invv.size()){
invvv.push(invv.top());
invv.pop();
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
while(ands!=0){
if(fvv.top()=='&'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(stoi(g)&stoi(gg)));
invvv.push(invv.top());
fvv.pop();
ands--;
}
else{
invvv.push(invv.top());
fvvv.push(fvv.top());
fvv.pop();
invv.pop();
}
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
}
if(yhs){
while(fvv.size()){
fvvv.push(fvv.top());
fvv.pop();
}
while(invv.size()){
invvv.push(invv.top());
invv.pop();
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
while(yhs!=0){
if(fvv.top()=='^'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(stoi(g)^stoi(gg)));
invvv.push(invv.top());
fvv.pop();
yhs--;
}
else{
invvv.push(invv.top());
fvvv.push(fvv.top());
fvv.pop();
invv.pop();
}
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
}
if(ors){
while(fvv.size()){
fvvv.push(fvv.top());
fvv.pop();
}
while(invv.size()){
invvv.push(invv.top());
invv.pop();
}
invv=invvv;
fvv=fvvv;
invvv=kong;
fvvv=kong2;
while(ors!=0){
if(fvv.top()=='|'){
string g,gg;
g=invv.top();
invv.pop();
gg=invv.top();
invv.pop();
invv.push(to_string(stoi(g)|stoi(gg)));
invvv.push(invv.top());
fvv.pop();
ors--;
}
else{
invvv.push(invv.top());
fvvv.push(fvv.top());
fvv.pop();
invv.pop();
}
}
invv=invvv;
fvv=fvvv;
}
return stod(invv.top());
}
void isd(string sact3){
string sact1,sact2;
int sp;
sp=sact3.find("=",0);
sact1.clear();
sact2.clear();
if(sp!=-1){
for(int i=0;i<sp;i++){
sact1+=sact3[i];
}
for(int i=sp+1;i<sact3.size();i++){
sact2+=sact3[i];
}
}
else{
sact1=sact3;
}
if((sact1[0]>='0'&&sact1[0]<='9')){
cout<<"<<<NameError:变量名违规。\n";
flag = 0;
return ;
}
for(int i=0;i<sact1.size();i++){
if(sact1[i]!='_'&&(!(sact1[i]>='0'&&sact1[i]<='9'))&&(!(sact1[i]>='a'&&sact1[i]<='z'))&&(!(sact1[i]>='A'&&sact1[i]<='Z'))){
cout<<"<<<NameError:变量名违规。\n";
flag = 0;
return;
}
}
if(sact2[0]=='\"'||sact2[0]=='\''){
sv[sact1]=sact2;
cv[sact1]=1;
}
else{
if(ism(sact2)==0){
cout<<"<<<InsideError:输入错误。\n";
flag = 0;
return;
}
else {
cv[sact1]=1;
iv[sact1]=math(sact2);
}
}
return;
}void osd(string s4){
if(s4[0]=='"')for(int i=1;i<s4.size()-1;i++)cout << s4[i];
else if(s4 == "_32")cout << " ";
else if(s4 == "endl")cout << endl<<">>>";
else if(cv[s4]){
if(sv[s4].empty()){
if(is_int(iv[s4])) cout<<(int)(iv[s4]);
else printf("%.8f",iv[s4]);
}
else cout<<sv[s4];
}
else if(s4[0]=='"')for(int i=1;i<s4.size()-1;i++)cout << s4[i];
else{
if(ism(s4)!=0){
double a=math(s4);
if(is_int(a)) cout<<(int)(a);
else printf("%.8f",a);
}
else{
cout<<"<<<OutError:不存在此变量。\n";
flag = 0;
}
}
return ;
}bool tof(string st){
if(st.find("==",0)!=-1){
int a = st.find("==",0);
int b = a + 2;
string sm1,sm2;
for(int i=0;i<a;i++)sm1+=st[i];
for(int i=b;i<st.size();i++)sm2+=st[i];
return math(sm1)==math(sm2);
}else if(st.find("<=",0)!=-1){
int a = st.find("<=",0);
int b = a + 2;
string sm1,sm2;
for(int i=0;i<a;i++)sm1+=st[i];
for(int i=b;i<st.size();i++)sm2+=st[i];
return math(sm1)<=math(sm2);
}else if(st.find(">=",0)!=-1){
int a = st.find(">=",0);
int b = a + 2;
string sm1,sm2;
for(int i=0;i<a;i++)sm1+=st[i];
for(int i=b;i<st.size();i++)sm2+=st[i];
return math(sm1)>=math(sm2);
}else if(st.find("<",0)!=-1){
int a = st.find("<",0);
int b = a + 1;
string sm1,sm2;
for(int i=0;i<a;i++)sm1+=st[i];
for(int i=b;i<st.size();i++)sm2+=st[i];
return math(sm1)<math(sm2);
}else if(st.find(">",0)!=-1){
int a = st.find(">",0);
int b = a + 1;
string sm1,sm2;
for(int i=0;i<a;i++)sm1+=st[i];
for(int i=b;i<st.size();i++)sm2+=st[i];
return math(sm1)>math(sm2);
}else if(st.find("!",0)==0){
string sm1;
for(int i=1;i<st.size();i++)sm1+=st[i];
return !math(sm1);
}else{
cout << "<<<CodeError代码错误"<<endl;
flag = 0;
return false;
}
}
void running(){
// 隐藏水印 作者:bits/stdc++.h
cout << ">>>";
int idx = 0,ed=0;
bool ccr = 1;
flag = 1;
while(s1.find("\n",idx)!=-1 and flag){
bool ff = 1;
ed = s1.find("\n",idx);
s3.clear();
for(int i=idx;i<ed;i++)s3+=s1[i];
if(s3.find("}",0)!=-1)ff=1;
else if(s3.find("if",0)!=-1){
string s4 = "";
if(s3.find("{",0)==-1){
int i;
for(i=3;s3[i]!=')';i++)if(s3[i])s4+=s3[i];
if(tof(s4)){
idx = i+1;
continue;
}else {
idx = ed + 1;
continue;
}
}
if(s1.find("}",idx)==-1){
cout <<"<<<lack'}'缺少'}'"<<endl;
flag = 0;
return;
}
for(int i=3;s3[i]!=')';i++)if(s3[i])s4+=s3[i];
if(!tof(s4)){
ff = 0;
idx = s1.find("}",idx)+1;
}
}else if(s3.find("cout",0)!=-1){
string ss = "";
for(int i=6;i<s3.size();i++){
if(s3[i] == '<'){
i++;
osd(ss);
ss.clear();
}else ss += s3[i];
}osd(ss);
}else if(s3.find("goto(",0)!=-1){
string s4 = "";
for(int i=5;i<s3.size()-1;i++)s4+=s3[i];
idx = s1.find("\n",s1.find(s4,0))+1;
ff = 0;
}else if(s3.find("=",0)!=-1)isd(s3);
else if(s3.find("*",0)!=-1)ff=1;
else if(s3.find("return",0)!=-1){
flag = 0;
cout <<endl<<"Process exited after some seconds with return value "<<s3[s3.size()-1]<<endl;
return;
}
else{
cout << "<<<CodeError代码错误"<<endl;
flag = 0;
return;
}
if(ff)idx = ed+1;
}flag = 0;
cout << endl<<"Process exited after some seconds with return value 0"<<endl;
return;
}
int main(){
bool f = 1;
while(f){
// 没有此水印必是盗版,盗版可耻,正版光荣 发布者:bits/stdc++.h
// break;
cout << "--- Dev-C## 17 ---"<<endl;
flag = 1;
s1.clear();
while(flag){
getline(cin,s2);
if(s2.find("run()",0)!=-1)running();
if(s2.find("//clearcode",0)!=-1)break;
if(s2.find("close",0)!=-1){
f = 0;
break;
}
for(int i=0;i<s2.size();i++)if(s2[i] != ' ')s1 += s2[i];
s1 += "\n";
}getch();
system("cls");
}
return 0;
}
以下为真实可运行段落代码,非一次一行代码,以下文字不属于代码内容,只属于代码解析
i = 1 将i 设为1
s = "3.14=" 将s设为字符串"3.14="
hh 建立传送点,名字为hh
cout << i <<""<<i<<s<<ii3.14 输出 如果i是10 则输出 10103.14=314 如要输出空格请在后面添加<<_32 换行则变成<<endl
i = i + 1 运算有 + - * / & | ^ 可有多个符号共同计算
if(i>20)return 0 如果i大于20程序结束,可以写成下面三行那种形式,但下面三行只做于展示作用
if(i>20){
return 0
}
goto(hh) 回到传送点hh
以上是代码
还有几行全局代码
run() 运行所编写的所有代码
close 结束总程序,真正的return 0;
//clearcode 写错代码时重新写代码
每运行过一次会有一次确认,需要按任意键才能重新开始写代码
全局代码啥时候写都可以
祝大家coding愉快
盗版可耻 正版光荣
全部评论 1
7小时前 来自 广东
0
有帮助,赞一个