對給出的文件里的表達式分別進行計算,分別將結果添加在后面:運行環境 TURBOC
----------------------------主文件 file.c -------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#define INCREASE_SIZE 5
#define STACK_INIT_SIZE 10
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef char SElemType;
typedef float ElemType;
typedef int Status;
/*************************** stack *********************************/
struct SqStack{
/* Define the structure of SqStack */
SElemType *base;
SElemType *top;
int stacksize;
};
Status InitStack(struct SqStack *s){
/* Initial the SqStack s */
s->base = (SElemType * )malloc (sizeof(SElemType)* STACK_INIT_SIZE);
if(! s->base){
printf("malloc feiled! ");
exit (OVERFLOW);
}
s->top = NULL;
s->stacksize = STACK_INIT_SIZE;
return OK;
}
Status ShowStack(struct SqStack s){
/* Show the content of SqStack s */
int i;
printf("/nstacksize = %d /n",s.stacksize);
printf("/n/t---stack---/n");
if(s.top == NULL){
printf("/nThe stock is NULL ! /n");
}
else{
for(i=0;i<=s.top-s.base;i++){
printf("/t| %d/t|/n", *(s.base+i));
}
}
return OK;
}
Status Push(struct SqStack *s,SElemType e){
/* Push the element e into the SqStack s */
if(s->top - s->base +1 == s->stacksize){
s->base=(ElemType *) realloc (s->base , sizeof(SElemType)*(s->stacksize+INCREASE_SIZE));
if(!s->base){
exit (OVERFLOW);
}
s->top = s->base + s->stacksize ;
*(s->top) = e;
s->stacksize += INCREASE_SIZE;
}
else if(s->top == NULL){
s->top = s->base;
*(s->top) = e;
}
else{
s->top ++;
*(s->top) = e;
}
return OK;
}
Status Pop(struct SqStack *s,SElemType *e){
/* Pop the element from the top of SqStack, and assign the Value of element to e */
if(CheckEmpty(s)) {
printf("The stack is null, Pop Failed ! /n");
return ERROR;
}
else if(s->top == s->base){
e=*(s->top);
s->top = NULL;
return OK;
}
e=*(s->top--);
return OK;
}
SElemType Pop2(struct SqStack *s){
/* Pop the element from the top of SqStack s , and assign the Value of element to e ,return e*/
SElemType e;
if(CheckEmpty(s)) {
printf("The stack is null, Pop Failed ! /n");
return ERROR;
}
else if(s->top == s->base){
e=*(s->top);
s->top = NULL;
return e;
}
e=*(s->top--);
return e;
}
Status DestroyStack(struct SqStack *s){
/* Deatroy the existing stack s*/
free(s->base);
free(s->top);
s->base = NULL ;
s->top = NULL;
s->stacksize = 0;
return OK;
}
Status ClearStack(struct SqStack *s){
/* Clear the SqStack s */
s->top = NULL;
return OK;
}
Status CheckEmpty(struct SqStack s){
/* check if the SqStack is Empty , Yes : return TRUE ; NO : return FALSE */
if(s.top == NULL)
return TRUE;
else
return FALSE;
}
int StackLength(struct SqStack s){
/* return the number of elements */
if(s.top == NULL )
return 0;
return s.top - s.base +1;
}
Status GetTop(struct SqStack s,SElemType *e){
/* Return the value e of the top of SqStack s */
if(s.top >= s.base){
e = *(s.top);
return OK;
}
else
return ERROR;
}
SElemType GetTop2(struct SqStack s){
/* Return the value e of the top of SqStack s */
SElemType e;
if(s.top >= s.base){
e = *(s.top);
return e;
}
return ERROR;
}
/***************** stack ******************/
/*********************** change ****************************/
float str2F(char *a){
/* String will be converted to float */
float ans = 0.0 , flag=0.1;
int status = 1,count=0,temp=0;
while( *a != '/0'){
if(*a=='.')
status=-1;
else{
if(status==1){
ans = ans * 10 + *a -48 ;
}else{
temp = count;
flag=0.1;
while(temp>0){
flag*=0.1;
temp--;
}
ans = ans + (*a-48) * (flag);
count++;
}
}
a++;
}
return ans;
}
int f2Str(float f , char *s){
/* float will be converted to String */
sprintf(s,"%.3f",f);
return 1;
}
/*********************** change ****************************/
/****************** math ************************/
Status CheckOperate(SElemType c){
/* To determine whether one of the seven symbols */
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '^':
case '=': return TRUE;
default : return FALSE;
}
}
Status CheckValue(SElemType c){
/* Check whether the symbols is number */
switch(c){
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': return TRUE;
default : return FALSE;
}
}
Status CheckValid(SElemType c){
/* Check whether the symbols is valid */
if(CheckValue(c)||CheckOperate(c))
return TRUE;
return ERROR;
}
char Precede(SElemType t1,SElemType t2){
/*determine the relationship between a and b (a ? b) */
char f;
switch(t2){
case '+':
case '-':
if( t1=='(' || t1=='=' )
f='<'; /*t1<t2*/
else
f='>';
break;
case '*':
case '/':
case '^':
if( t1=='*' || t1=='/' || t1=='^' || t1==')' )
f='>';
else
f='<';
break;
case '(':
if( t1==')' ){
printf("Does not match the brackets ./n");
exit(ERROR);
}else{
f='<';
}
break;
case ')':
switch(t1){
case '(': f='=';
break;
case '=': printf("Lack of the left brackets./n");
exit(ERROR);
default: f='>';
}
break;
case '=':
switch(t1){
case '=': f='=';
break;
case '(': printf("Lack of the right brackets./n");
exit(ERROR);
default: f='>';
}
}
return f;
}
float Operate(float a,SElemType theta,float b){
/* Do arithmetic operations : a ? b */
float ans = 0;
switch(theta){
case '+' : ans = a+b; break;
case '-' : ans = a-b; break;
case '*' : ans = a*b; break;
case '/' :
if(b==0.0)
return ERROR;
else
ans = a/b;
break;
case '^' :
ans = pow(a,(int)b);
}
return ans;
}
/**************************** math *******************************/
/*************************** Operation ******************************/
Status EvaluateExpression(SElemType* express,float *result2){
/* Evaluate the expression-express ,The value to result2[0]*/
int i,add=0;
float numArr[100];
float a1=0,b1=0,result=0;
SElemType theta,d,*c,a,b;
char z[11];
struct SqStack OPTR;
InitStack(&OPTR);
Push(&OPTR,'=');
theta = GetTop2(OPTR);
c=express;
result = 0;
while( *c!='=' || theta!= '='){
if(CheckValid(*c)){
if(CheckOperate(*c)){
switch(Precede(theta,*c)){
case '<':
Push(&OPTR,*c);
c++;
break;
case '=':
Pop(&OPTR,theta);
c++;
break;
case '>':
theta = Pop2(&OPTR);
add--;
b1=numArr[add];
add--;
a1=numArr[add];
result = Operate(a1,theta,b1);
numArr[add]= result;;
add++;
}/*switch*/
}
else if(CheckValue(*c)){
i=0;
do{
z[i++] = *c;
c++;
}
while(CheckValue(*c));
z[i]='/0';
result=str2F(z);
numArr[add] = result;
add++;
}/*if-CheckValue*/
}
else{
printf("Invalid characters exist!/n");
return ERROR;
}
theta = GetTop2(OPTR);
}/*while*/
add--;
result = numArr[add];
result2[0] = result;
if(add!=0){
printf("Expression is Error !/n");
return ERROR;
}
free(c);
DestroyStack(&OPTR);
return OK;
}
/*************************** Operation ******************************/
/*************************** file oprate *************************/
Status Rewrite(char *file1,char *file2){
/* copy file2 to file1 */
FILE *in ,*out;
int ch;
if((in=fopen(file2,"r"))==NULL){
printf("2The document does not exist,cannot open it ! /n");
exit(ERROR);
}
if((out=fopen(file1,"w"))==NULL){
printf("The document does not exist,cannot open it ! /n");
exit(ERROR);
}
while((ch = fgetc(in)) != EOF){
putc(ch,out);
}
fclose(in);
fclose(out);
printf("/n Rewrite OK/n");
return OK;
}
Status ViewFile(char * file){
/* Display the contents of the file */
FILE *in ;
if((in=fopen(file,"r"))==NULL){
printf("2The document does not exist,cannot open it ! /n");
exit(ERROR);
}
while(!feof(in)){
printf("%c",fgetc(in));
}
fclose(in);
printf("/n ViewFile OK/n");
return OK;
}
Status Extraction(char *fileName,char *fileName2){
/* read file fileName by one character , put valid characters into array express , write expresses and results into file fileName2 */
char str[10]="ERROR";
FILE *fp,*buf;
char express[1024]="*";
char result2Str[20];
char flag;
float result[3];
int len=0,i=0,temp=0;
if((fp=fopen(fileName,"r"))==NULL){
printf("The document does not exist,cannot open it ! /n");
exit(ERROR);
}
if((buf=fopen(fileName2,"w"))==NULL){
printf("2The document does not exist,cannot open it ! /n");
exit(ERROR);
}
while((flag = fgetc(fp)) != EOF){
fputc(flag,buf);
if(flag==' '||flag=='/n'){
strcpy(express,"");
temp=0;
continue;
}
if(flag=='='){
express[temp++] = flag;
express[temp] = '/n';
express[temp]='/0';
if(EvaluateExpression(express,result)){
f2Str(result[0],result2Str);
}else{
strcpy(result2Str,str);
;
}
i=0;
len=strlen(result2Str);
while(len-- >0){
fputc(result2Str[i++],buf);
}
strcat(express,result2Str);
printf("%s/n",express);
temp=0;
strcpy(express,"");
}
else{
express[temp++] = flag;
}
}
fclose(fp);
fclose(buf);
return OK;
}
/*************************** file oprate *************************/
/*************************** main ********************************/
int main(){
char fileName[20],fileName2[24]="d://bak.txt";
char str[30]="del ";
int len = 0;
printf("/n/n/n/n----- main -------/n");
printf("/tPlease input the file name : ");
scanf("%s",fileName);
len = strlen(fileName);
scrncpy(fileName2,fileName,len-4);
strcat(fileName2,".bak");
*/ printf("%s ",fileName);
printf("%s /n",fileName2);
Extraction(fileName,fileName2);
printf("/n/n Extraction OK /n/n");
Rewrite(fileName,fileName2);
printf("/n/n Rewrite OK /n/n");
strcat(str, fileName2);
system(str);
return OK;
}
---------------測試文檔---------------------------------------
test.txt
15+6*2=
(7*5-2)/8=
12.36+956.2=
(5+)=
(7*5-2)/8=
-------------運行后文檔結果------------------------------------
test.txt
15+6*2=27.000
(7*5-2)/8=4.125
12.36+956.2=968.560
(5+)=ERROR
(7*5-2)/8=4.125