我闲来无事,在家里搞了一个这样的代码:
输入:代码。
输出:输入的代码输出的内容。
(就是eval)
#include <iostream>
#include <fstream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
string eval(string a){
fstream fs("eval.cpp", ios::out | ios::trunc);
for(int i=0;i<a.length();i++){
fs << a[i];
}
fs.close();
system("g++ eval.cpp -o eval.exe");
system(".\\eval.exe");
ifstream infile;
infile.open("data.txt");
string res;
infile >> res;
infile.close();
system("del eval.exe");
system("del eval.cpp");
system("del data.txt");
return res;
}
int main(){
string a;
stringstream A;
A << "#include <fstream>" << endl;
while(getline(cin,a)){
if(a!="return 0;"){
A << a << endl;
if(a=="int main(){"){
A << "ofstream cout;" << endl << "cout.open(\"data.txt\",ios::out|ios::trunc);" << endl;
}
}
else{
A << "cout.close()" << endl;
A << a << endl;
}
}
cout << eval(A.str());
return 0;
}
但是,出现了问题:
输入的代码:
#include<iostream>
using namespace std;
int main(){
cout << 3 ;
return 0;
}
在我的电脑上试的时候,输出:
3
在我谷ide中,运行成功了,但是什么也没有输出。
谁能帮我解决一下这个问题?