/* ----------------------------------------------/
/ CHUONG TRINH TINH GIA TRI BIEU THUC /
/ /
/ /
/ ----------------------------------------- */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <alloc.h>
#include <conio.h>
#include <graphics.h>
main()
{int x[101],y[101];
int mh=0,mode=0,i,n;
char *p[10];
initgraph(&mh,&mode,"");
if(graphresult()!=0)exit(1);
setcolor(BLUE);
/*Ve bau troi day sao*/
for(i=1;i<=1000;++i)
{
putpixel(random(getmaxx()),random(getmaxy()),random(getmaxcolor()));
}
//Luu hien trang 100 hinh chu nhat tren man hinh de khoi phuc
for(i=1;i<=100;++i)
{
x[i]=random(getmaxx())-10;
y[i]=random(getmaxy())-10;
if(x[i]<0) x[i]=0;if(y[i]<0)y[i]=0;
n=imagesize(x[i],y[i],x[i]+10,y[i]+10);
getimage(x[i],y[i],x[i]+10,y[i]+10,p[i]);
}
//Chu trinh ban phao hoa
do{
//Dua 100 qua phao len man hinh tai cac vi tri quy dinh
for(i=1;i<=100;++i)
setfillstyle(SOLID_FILL,i%15+1);
pieslice(x[i]+5,y[i]+5,0,360,5);
delay(500);
//xoa chum phao hoa vua ban bang cach khoi phuc man hinh
for(i=100;i>=1;-i)
putimage(x[i],y[i],p[i],COPY_PUT);
delay(500);
}
while(!kbhit());
getch();getch();
closegraph();
}
const stacklimit=256;
char *operators="+-*/()";
char *digits="0123456789";
double stack[stacklimit];
unsigned int top;
void error(int code);
char *strcadd(char *dest, char source) {
}
int strcfind(char subs, const char *str) {
int i=0;
for (;i
<strlen(str
);i
++) if (str
[i
]==subs
) return i
; return -1;
}
char *strcreplace(char *dest, int pos, const char* newsubstr) {
int i,j;
for (i=n+m-1;i>pos;i--) dest[i]=dest[i-m+1];
for (i=pos;i-pos<m;i++) dest[i]=newsubstr[i-pos];
dest[m+n]=0;
return dest;
}
char *strtrimall(char *dest) {
int i=0,j;
if (dest[i]==32) {
for (j
=i
;j
<strlen(dest
)-1;j
++) dest
[j
]=dest
[j
+1]; i--;
}
return dest;
}
char round(float x) {
return (char)(x);
}
int isin(char ch,char *st) {
int i=0;
if (ch==st[i]) return 1; }
return 0;
}
void initstack() {
int i=0;
for (;i<=stacklimit-1;i++) stack[i]=0;
top=0;
}
int emptystack() {
return (top==0);
}
void push(double x) {
top++;
stack[top]=x;
}
double pop() {
if (!emptystack()) {
top--;
return stack[top+1];
}
else return 0;
}
float get() {
if (!emptystack()) return stack[top];
else return 0;
}
void deltop() {
top--;
}
unsigned char order(char x) {
switch (x) {
case '(' : return 0;
case '+' :
case '-' : return 1;
case '*' :
case '/' : return 2;
default : return 0;
}
}
void PRN(char *s){
unsigned char i;
char s1[256]="";
if (!isin(s[i],operators)) {
if ((i>0)&&(isin(s[i-1],operators))) strcadd(s1,' ');
strcadd(s1,s[i]);
}
else {
if (s[i]!=')') {
while ((!emptystack())&&(s[i]!='(')
&&(order(s[i])<=order(round(get()))))
strcadd(s1,round(pop()));
push(s[i]);
}
else {
do strcadd(s1,round(pop()));
while (round(get())!='(');
deltop();
}
}
}
while (!emptystack()) strcadd(s1,round(pop()));
}
double cost(char *s) {
char c;
int i;
double x;
long temp;
i=0;
initstack();
do {
x=0;
while (s[i]==' ') i++;
switch (s[i]) {
case '*': x=pop()*pop();break;
case '+': x=pop()+pop();break;
case '-': x=pop();x=pop()-x;break;
case '/': x=pop(); if (x) x=pop()/x; else error(200);break;
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9': temp=1;
do {
if (temp>1) {
x+=(double)(s[i]-'0')/temp;
temp*=10;
}
else x=x*10+(s[i]-'0');
i++;
if (s[i]=='.') {
temp=10;
i++;
}
} while (isin(s[i],digits));
i--;
break;
}
push(x);
i++;
return x;
}
void error(int code) {
if (code
<200) printf("Syntax-error #%d : ",code
); else printf("\nRuntime-error #%d : ",code
); switch (code) {
case 1: printf("Invalid expression syntax.");break; case 2: printf("Invalid expression direction.");break; case 3: printf("Invalid bracket \"(\".");break; case 4: printf("Invalid bracket \")\".");break; case 5: printf("Invalid brackets \"()\".");break; case 6: printf("Invalid decimal symbol \".\".");break; case 7: printf("Empty expression.");break; case 8: printf("Expression too long (256 characters max).");break; case 9: printf("Invalid symbol. Only use \"+-*/().\", 0-9, A-Z, a-z and space.");break; case 200: printf("Devision by zero (0).");break; }
printf("\r\nPress any key ...\r\n"); getch();
exit(code);
}
char *replacevar(char *dest, char var, const char *value) {
int i,j,k;
char temp[256]="";
do {
i=strcfind(var,dest);
if (i==-1) return dest;
else {
if (i+1<n) if (!isin(dest[i+1],operators)) {
temp[k]='*';
temp[k+1]=0;
}
if (i>0) if (!isin(dest[i-1],operators)) {
for (j=k;j>=0;j--) temp[j]=temp[j-1];
temp[0]='*';
temp[k+1]=0;
}
strcreplace(dest,i,temp);
}
} while (i!=-1);
return dest;
}
char *fixexp(char *s) {
int i=0,j=0,k=0;
if (s[i]=='(') j++;
else if (s[i]==')') k++;
if (k>j) error(4);
}
if (j>k) error(3); else if (j<k) error(4);
switch (s[i]) {
case '.' : if (i
==strlen(s
)-1) {s
[i
]=0;break;} if (i==0) {strcreplace(s,0,"0."); i++; break;}
if (i>0)
switch (s[i-1]) {
case '+' : case '-' : case '*' : case '/' : strcreplace(s,i,"0."); i++; break;
case ')' : strcreplace(s,i,"*0."); i+=2; break;
case '(' : strcreplace(s,i,"0+0."); i+=3; break;
}
switch (s[i+1]) {
case '+' : case '-' : case '*' : case '/' : case ')' : strcreplace(s,i,".0");break;
case '(' : strcreplace(s,i,".0*");break;
case '.' : error(6);
}
break;
switch (s[i+1]) {
case '+' : case '-' : case ')' : s[i]=32;break;
case '*' : case '/' : error(1);
}
break;
switch (s[i+1]) {
case '+' : s[i+1]=32;break;
case '-' : s[i]=32;s[i+1]='+';break;
case '*' : case '/' : error(1);
case ')' : s[i]=32;break;
}
break;
switch (s[i+1]) {
case '+' : s[i+1]=32;break;
case '*' : error(2);
case '-' : j=i;
while (isin
(s
[j
],digits
) || s
[j
]=='.' || j
<strlen(s
)) j
++; strcreplace
(s
,j
,strcat(")",&s
[j
])); strcreplace(s,i+1,"(-");
break;
case '/' : error(1);
case ')' : s[i]=32;break;
}
break;
switch (s[i+1]) {
case '+' : s[i+1]=32;break;
case '-' : j=i;
while (isin
(s
[j
],digits
) || s
[j
]=='.' || j
<strlen(s
)) j
++; strcreplace
(s
,j
,strcat(")",&s
[j
])); strcreplace(s,i+1,"(-");
break;
case '*' : error(1);
case '/' : error(2);
case ')' : s[i]=32;break;
}
break;
case '(' : if (i>0)
switch (s[i-1]) {
case '+' : case '-' : case '*' : case '/' : break;
default : strcreplace(s,i,"*("); i++; break;
}
switch (s[i+1]) {
case '+' : strcreplace(s,i+1,"0+");break;
case '-' : strcreplace(s,i+1,"0-");break;
case '*' : strcreplace(s,i+1,"1*");break;
case '/' : strcreplace(s,i+1,"1/");break;
case ')' : error(5);
default : k=0;
j=i+1;
while (s[j]!=')') {
if (isin(s[j],operators)) k++;
j++;
}
if (!k) strcreplace(s,i,"(0+");
break;
}
break;
switch (s[i+1]) {
case '+' : case '-' : case '*' : case '/' : case ')' : case '.' : break;
default : strcreplace(s,i,")*"); break;
}
break;
}
}
strtrimall(s);
if (!j) error(7); else if (j>256) error(8);
switch (s[j-1]) {
case '+' : case '-' : s[j]='0';s[j+1]=0;break;
case '*' : case '/' : s[j]='1';s[j+1]=0;break;
case '.' : s[j-1]=0;break;
}
return s;
}
int main() {
int i=0;
float var;
int sig=5;
char vars[25]="";
clrscr();
printf("\n\t\tTINH GIA TRI BIEU THUC\n");
if (!isin(s[i],digits) && !isin(s[i],operators) && s[i]!='.' && s[i]!=32) {
if ((s[i]>='A' && s[i]<='Z') || (s[i]>='a' && s[i]<='z')) {
gcvt(var,sig,vars);
replacevar(s,s[i],vars);
}
else error(9);
}
}
fixexp(s);
fixexp(s);
PRN(s);
printf(" = %1.4f\r\n",cost
(s
));
printf("Bam phim bat ki de ket thuc ... \r\n"); getch();
return 0;
}