-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
179 lines (140 loc) · 3.07 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include<fstream>
#include<iomanip>
#include<dlfcn.h>
using namespace std;
int main()
{
void mfun(string s);
double mlib(void);
void new_function(void);
cout<<"Hello,I am b !\nIf U wanna exit, please input \"quit\"\n";
string s;
do
{
getline(cin,s);
if(s=="quit")
{
cout<<"see U !"<<endl;
break;
}
if(strstr(s.data(),"new") != NULL)
{
new_function();
continue;
}
//write .cpp
mfun(s);
//make .so
double status=mlib();
//read the erro text
fstream r_erro;
string s_erro;
int n_erro(0);
r_erro.open("./erro.txt",ios::in);
while(r_erro.good())
{
n_erro++;
getline(r_erro,s_erro);
if(n_erro>1)
{
cout<<s_erro<<endl;
}
}
if(n_erro != 1 )
{
cout<<"please input again ! "<<endl;
continue;
}
void *pdlHandle = dlopen("./libfun.so",RTLD_LAZY);
char *pszErr = dlerror();
if( !pdlHandle || pszErr )
{
cout<<"Load libfun failed!\n";
return 1;
}
double (*f)(void);
f = (double(*)())(dlsym(pdlHandle, "f"));
if( ! f)
{
pszErr = dlerror();
cout<<"Find symbol failed!\n"<<pszErr<<endl;
return 1;
}
cout<<f()<<endl;
dlclose(pdlHandle);
}while(1);
return 0;
}
void mfun(string s)
{
ofstream fout("fun.cpp");
fout<<"#include\"newfun.h\""<<endl;
fout<<"#include\"fun.h\""<<endl;
fout<<"#include<math.h>"<<endl;
fout<<"double f(void)\n{"<<endl;
fout<<"return "+s+";\n}"<<endl;
fout.close();
}
double mlib(void)
{
int status(0);
status= system("g++ -fpic -shared fun.cpp -o libfun.so &>erro.txt");
return status;
}
//need a new function to remember the new function
//it is a circle whit while{} when input "qiut" will end input
//
void new_function(void)
{
cout<<"Please tell me the name of the new function "<<endl;
string s_name;
getline(cin,s_name);
cout<<"if there is an input? Y/N"<<endl;
string s_input;
bool b_input;
string s_inputname;
getline(cin,s_input);
if(s_input == "Y" || s_input == "y")
{
b_input = true;
cout<<"input the verable name and its type,like double a"<<endl;
getline(cin,s_inputname);
cout<<s_inputname<<endl;
}
else if(s_input == "N" || s_input == "n")
{
b_input = false;
}
else
{
cout<<"erro\nquit make new function"<<endl;
return ;
}
//as double input
ofstream fout_newh("./newfun.h",ios::app);
fout_newh<<"// "+s_name+"\n";
if(b_input)
{
fout_newh<<"double "+s_name+"("+s_inputname+")"<<endl;
}
else
{
fout_newh<<"double "+s_name+"(void)"<<endl;
}
cout<<"ok,Let's start write the function !\nif u wanna quit ,please input quit"<<endl;
//need two function:1.check has the author have write the return 2.is to delete the wrong one Not Right
fout_newh<<"{\n";
do
{
string s_gn;
getline(cin,s_gn);
if(s_gn == "quit")
{
break;
}
fout_newh<<s_gn<<endl;
}while(1);
fout_newh<<"}\n//end "+s_name+"\n";
fout_newh.close();
}