天天看點

c語言 eval,分享:自己寫的eval函數

該樓層疑似違規已被系統折疊 隐藏此樓檢視此樓

自然,主程式的源碼也分享如下:

#include

#include "peval.h"

#include

#include

#include

#include

using namespace std;

numtype x,s,t;

string f,g;

numtype varx(string a) {

if (a=="x") return x;

else return 0;

}

int main(int argc,char * argv[]) {

numtype gs[101][102];

string a;

int n=0;

char ch;

if(argc>=2) {

for(int i=1; i

a=argv[i];

if (a=="-h") {

cout<(大于)

break;

}

if (a=="-v") {

cout<

break;

}

if (a=="-t") {

x=eval(argv[i+1]);

s=eval(argv[i+2]);

f=argv[i+3];

cout<

SetVar(varx);

while(1) {

cout<

x+=s;

n++;

if(n==50) {

system("pause");

n=0;

}

}

break;

}

if(a=="-s") {

x=eval(argv[i+1]);

f=argv[i+2];

SetVar(varx);

while(eval(f)!=0) {

s=x;

x=x+0.00000000001;

t=eval(f);

x=s;

if(((eval(f)-t)/(0-0.00000000001))==0) {

cout<

return 0;

}

x=x-((eval(f))/((eval(f)-t)/(0-0.00000000001)));

if(fpclassify(x)!=FP_NORMAL) {

cout<

return 0;

}

n++;

if(n==100) {

cout<

cout<

while((n==100)&&(cin>>ch)){

switch(ch){

case 'y':

n=0;

break;

case 'Y':

n=0;

break;

case 'n':

return 0;

case 'N':

return 0;

default:

cout<

}

}

}

}

cout<

break;

}

if(a=="-e") {

f=argv[i+1];

SetVar(varx);

for(x=1; x<=101; x++) {

for(int y=0; y<=100; y++) {

gs[(int)x-1][y]=pow(x,y);

}

gs[(int)x-1][101]=eval(f);

}

for(x=0; x<=99; x++) {

for(int y=(int)x+1; y<=100; y++) {

t=-(gs[y][(int)x])/(gs[(int)x][(int)x]);

for(int j=0; j<=101; j++) {

gs[y][j]=gs[y][j]+t*gs[(int)x][j];

}

}

}

for(int i=100;i>=0;i--){

a="";

for(x=i; x>=0; x--) {

t=((gs[(int)x][101])/(gs[(int)x][(int)x]));

if(t!=0) {

if(x==0) {

g="";

} else {

if(x==1) {

g="x";

} else {

g="x^"+numtostr(x);

}

}

if(t<0) {

if(t==-1) {

if(g=="") {

a=a+numtostr(t);

} else {

a=a+"-"+g;

}

} else {

if(g=="") {

a=a+numtostr(t);

} else {

a=a+numtostr(t)+"*"+g;

}

}

} else {

if(a=="") {

if(t==1) {

if(g=="") {

a=a+numtostr(t);

} else {

a=a+g;

}

} else {

if(g=="") {

a=a+numtostr(t);

} else {

a=a+numtostr(t)+"*"+g;

}

}

} else {

if(t==1) {

if(g=="") {

a=a+"+"+numtostr(t);

} else {

a=a+"+"+g;

}

} else {

if(g=="") {

a=a+"+"+numtostr(t);

} else {

a=a+"+"+numtostr(t)+"*"+g;

}

}

}

}

}

if(i==100){

for(int j=0; j<=(int)x-1; j++) {

gs[j][101]=gs[j][101]-gs[j][(int)x]*t;

}

}

}

t=0;

for(x=102; x<=202; x++) {

if(eval(a)!=eval(f)) {

t=1;

break;

}

}

if(t==0){

break;

}

}

if(t==0&&a!="") {

cout<

} else {

cout<

}

return 0;

}

cout<

}

} else {

cout<(大于)

while(1) {

cout<

getline(cin,a);

if(a=="")

return 0;

cout<