69,373
社区成员
发帖
与我相关
我的任务
分享
// Calculator.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
typedef struct Link{
char s[20];
Link *next;
}Link, *pLink;
void Push(pLink top,char* a);
void Add(pLink top,char s);
char *Calculate(float s1,float s2, char s);
bool Pop(pLink top,char *a);
char Judge_type(char *s);
char Get(pLink top);
int Judge(char s1, char s2);
bool Evaluate(const char* strFile1,const char* strFile2);
void ReleaseLink(pLink top);
void Clear(pLink top);
bool FirstOutput(const char* strFile, pLink top,int n);
bool Output(const char* strFile, pLink top,int n);
char *ftoa(float n);
void main(){
char strFile1[] = "D:\\data.txt";
char strFile2[] = "D:\\output.txt";
Evaluate(strFile1,strFile2);
}
void Push(pLink top,char* a){
pLink Cur = new Link;
strcpy(Cur->s,a);
Cur->next = top->next;
top->next = Cur;
}
void Add(pLink top,char s){
char a[20];
a[0] = s;
strcat(top->next->s,a);
}
char* Calculate(float s1, float s2, char s){
char a[20];
//if(s=='*') a = ftoa(s1*s2);
//if(s=='/') a = ftoa(s1/s2);
//if(s=='+') a = ftoa(s1+s2);
if(s=='+') sprintf(a,"%.10f",s1+s2);
//if(s=='-') a = ftoa(s1-s2);
//if(s=='^') a = ftoa(pow(s1,s2));
//if(s=='%') a = ftoa((int)s1%(int)s2);
return a;
}
bool Pop(pLink top,char *a){
if(top->next==NULL) return false;
pLink next = top->next->next;
strcpy(a,top->next->s);
delete top->next;
top->next = next;
return true;
}
char Judge_type(char *s){
int i;
for(i=0;i<100;i++){
if (*s++ = '.') return '1';
}
return '0';
}
char Get(pLink top){
char s = *top->next->s;
return s;
}
int Judge(char s1, char s2){
//if(s2!='+'||s2!='-'||s2!='x'||s2!='/'||s2!='^'||s2!='%'||s2!='{'||s2!='}'||s2!=10) return 4;
if(s1=='+'||s1=='-'){
if(s2=='+'||s2=='-'||s2==')'||s2==10) return 1;
else return 2;
}
if(s1=='*'||s1=='/'){
if(s2=='('||s2=='^'||s2=='%') return 2;
else return 1;
}
if(s1=='^'||s1=='%'){
if(s2=='(') return 2;
else return 1;
}
if(s1=='('){
if(s2==')') return 3;
else if(s2==10) return 4;
else return 2;
}
if(s1==')'){
if(s2=='(') return 2;
else if(s2=='(') return 4;
else return 1;
}
if(s1==10){
if(s2==10) return 3;
else if (s2==')') return 4;
else return 2;
}
}
bool Evaluate(const char* strFile1,const char* strFile2){
FILE* fp = fopen(strFile1,"rt");
if(!fp) {
return false;
printf("cannot open the file");}
pLink OPTR = new Link, OPND = new Link;
OPTR->next = new Link;
*OPTR->next->s = 10, OPND->next = NULL, OPTR->next->next = NULL;
char c = fgetc(fp);
char* a=&c;
while(c!=EOF){
int n = 1;
int i = 1;
char *x=NULL;
char s1[20], s2[20], s[20], *s3;
while(c!=10||*OPTR->next->s!=10){
if(c<=57&&c>=48){
Push(OPND,a);
c = fgetc(fp);
while(c<=57&&c>=48){
Add(OPND,c);
c = fgetc(fp);
}
if(c=='.'){
Add(OPND,c);
c = fgetc(fp);
while(c<=57&&c>=48){
Add(OPND,c);
c = fgetc(fp);}
}
}
else switch(Judge(Get(OPTR),c)){
case 2 : Push(OPTR,a); c = fgetc(fp); break;
case 3 : Pop(OPTR,x); c = fgetc(fp); break;
case 1 : {
if(OPND->next->next==NULL&&OPND->next==NULL) {n=0; break;}
Pop(OPND,s1), Pop(OPND,s2), Pop(OPTR,s);
float a1 = atof(s1), a2 = atof(s2);
s3 = Calculate(a2,a1,*s);
Push(OPND,s3);
break;
}
case 4 : n=0; c = fgetc(fp); break;
}
}
if (OPND->next->next!=NULL) n=0;
if (i==1) FirstOutput(strFile2,OPND,n);
if (i>1) Output(strFile2,OPND,n);
Clear(OPTR), Clear(OPND);
delete OPND->next;
OPND->next = NULL;
c = fgetc(fp);
}
fclose(fp);
ReleaseLink(OPTR), ReleaseLink(OPND);
return true;
}
void ReleaseLink(pLink top){
pLink Cur = top->next;
delete top;
while (Cur){
pLink next = Cur->next;
delete Cur;
Cur = next;
}
}
void Clear(pLink top){
while(top->next!=NULL){
pLink Cur = new Link;
Cur = top->next;
top->next = top->next->next;
delete Cur;
}
pLink Cur= new Link;
*Cur->s = 10;
Cur->next = NULL;
top->next = Cur;
}
bool FirstOutput(const char* strFile, pLink top,int n){
FILE *fp = fopen(strFile,"wt");
if(!fp){
printf("ERROR:cannot write the file");
return false;
}
if(n==1){
fprintf(fp,"%s",top->s);
fprintf(fp,"\n");
fclose(fp);
return true;
}
if(n==0){
fprintf(fp,"ERROR");
fprintf(fp,"\n");
fclose(fp);
return true;
}
}
bool Output(const char* strFile, pLink top,int n){
FILE *fp = fopen(strFile,"at");
if(!fp){
printf("ERROR:cannot write the file");
return false;
}
if(n==1){
fprintf(fp,"%s",top->s);
fprintf(fp,"\n");
fclose(fp);
return true;
}
if(n==0){
fprintf(fp,"ERROR");
fprintf(fp,"\n");
fclose(fp);
return true;
}
}
char *ftoa(float n)
{
char *a=new char[100];
sprintf(a,"%.10f",n);
return a;
}
1 //#include <stdafx.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5 #include <stdio.h>
6 typedef struct Link{
7 char s[20];
8 struct Link *next;
9 }Link, *pLink;
10
11 void Push(pLink top,char* a);
12 void Add(pLink top,char s);
13 char *Calculate(float s1,float s2, char s);
14 bool Pop(pLink top,char *a);
15 char Judge_type(char *s);
16 char Get(pLink top);
17 int Judge(char s1, char s2);
18 bool Evaluate(const char* strFile1,const char* strFile2);
19 void ReleaseLink(pLink top);
20 void Clear(pLink top);
21 bool FirstOutput(const char* strFile, pLink top,int n);
22 bool Output(const char* strFile, pLink top,int n);
23 char *ftoa(float n);
24
25 int main(){
26 char strFile1[] = "data.txt";
27 char strFile2[] = "output.txt";
28 Evaluate(strFile1,strFile2);
29
30 return 0;
31 }
32
33
34 void Push(pLink top,char* a){
35 pLink Cur = new Link;
36 strcpy(Cur->s,a);
37 Cur->next = top->next;
38 top->next = Cur;
39 }
40
41 void Add(pLink top,char s){
42 char a[20];
43 a[0] = s;
44 strcat(top->next->s,a);
45 }
46
47 char* Calculate(float s1, float s2, char s){
48 //char a[20];
49 int ret = 0;
50 char *a = (char *)malloc(20);
51 if(s=='+') {
52 ret = sprintf(a,"%.10f",s1+s2);
53 }
54 return a;
55 }
56
57 bool Pop(pLink top,char *a){
58 if(top->next==NULL) return false;
59 pLink next = top->next->next;
60 strcpy(a,top->next->s);
61 delete top->next;
62 top->next = next;
63 return true;
64 }
65 char Judge_type(char *s){
66 int i;
67 for(i=0;i<100;i++){
68 if (*s++ = '.') return '1';
69 }
70 return '0';
71 }
72 char Get(pLink top){
73 char s = *top->next->s;
74 return s;
75 }
76 int Judge(char s1, char s2){
77 if(s1=='+'||s1=='-'){
78 if(s2=='+'||s2=='-'||s2==')'||s2==10) return 1;
79 else return 2;
80 }
81 if(s1=='*'||s1=='/'){
82 if(s2=='('||s2=='^'||s2=='%') return 2;
83 else return 1;
84 }
85 if(s1=='^'||s1=='%'){
86 if(s2=='(') return 2;
87 else return 1;
88 }
89 if(s1=='('){
90 if(s2==')') return 3;
91 else if(s2==10) return 4;
92 else return 2;
93 }
94 if(s1==')'){
95 if(s2=='(') return 2;
96 else if(s2=='(') return 4;
97 else return 1;
98 }
99 if(s1==10){
100 if(s2==10) return 3;
101 else if (s2==')') return 4;
102 else return 2;
103 }
104 }
105
106 bool Evaluate(const char* strFile1,const char* strFile2){
107 FILE* fp = fopen(strFile1,"rt");
108 if(!fp) {
109 printf("cannot open the file");
110 return false;
111 }
112 pLink OPTR = new Link, OPND = new Link;
113 OPTR->next = new Link;
114 *OPTR->next->s = 10, OPND->next = NULL, OPTR->next->next = NULL;
115 char c = fgetc(fp);
116 char* a=&c;
117 while(c!=EOF){
118 int n = 1;
119 int i = 1;
120 char *x=NULL;
121 char s1[20], s2[20], s[20], *s3;
122 while(c!=10||*OPTR->next->s!=10){
123 if(c<=57&&c>=48){
124 Push(OPND,a);
125 c = fgetc(fp);
126 while(c<=57&&c>=48){
127 Add(OPND,c);
128 c = fgetc(fp);
129 }
130 if(c=='.'){
131 Add(OPND,c);
132 c = fgetc(fp);
133 while(c<=57&&c>=48){
134 Add(OPND,c);
135 c = fgetc(fp);}
136 }
137 }
138 else switch(Judge(Get(OPTR),c)){
139 case 2 : Push(OPTR,a); c = fgetc(fp); break;
140
141 case 3 : Pop(OPTR,x); c = fgetc(fp); break;
142
143 case 1 : {
144 if(OPND->next->next==NULL&&OPND->next==NULL) {n=0; break;}
145 Pop(OPND,s1), Pop(OPND,s2), Pop(OPTR,s);
146 float a1 = atof(s1), a2 = atof(s2);
147 s3 = Calculate(a2,a1,*s);
148 Push(OPND, s3);
149 break;
150 }
151 case 4 : n=0; c = fgetc(fp); break;
152 }
153 }
154 if (OPND->next->next!=NULL) n=0;
155 if (i==1) FirstOutput(strFile2,OPND,n);
156 if (i>1) Output(strFile2,OPND,n);
157 Clear(OPTR), Clear(OPND);
158 delete OPND->next;
159 OPND->next = NULL;
160 c = fgetc(fp);
161 }
162 fclose(fp);
163 ReleaseLink(OPTR), ReleaseLink(OPND);
164 return true;
165 }
166
167 void ReleaseLink(pLink top){
168 pLink Cur = top->next;
169 delete top;
170 while (Cur){
171 pLink next = Cur->next;
172 delete Cur;
173 Cur = next;
174 }
175 }
176 void Clear(pLink top){
177 while(top->next!=NULL){
178 pLink Cur = new Link;
179 Cur = top->next;
180 top->next = top->next->next;
181 delete Cur;
182 }
183 pLink Cur= new Link;
184 *Cur->s = 10;
185 Cur->next = NULL;
186 top->next = Cur;
187 }
188
189 bool FirstOutput(const char* strFile, pLink top,int n){
190 FILE *fp = fopen(strFile,"wt");
191 int ret = 0;
192 if(!fp){
193 printf("ERROR:cannot write the file");
194 return false;
195 }
196
197 if(n==1){
198 //ret = fprintf(fp,"%s",top->s);
199 ret = fprintf(fp,"%s",top->next->s);
200 ret = fprintf(fp,"\n");
201 fclose(fp);
202 return true;
203 }
204 if(n==0){
205 fprintf(fp,"ERROR");
206 fprintf(fp,"\n");
207 fclose(fp);
208 return true;
209 }
210 }
211
212 bool Output(const char* strFile, pLink top,int n){
213 FILE *fp = fopen(strFile,"at");
214 if(!fp){
215 printf("ERROR:cannot write the file");
216 return false;
217 }
218 if(n==1){
219 fprintf(fp,"%s",top->s);
220 fprintf(fp,"\n");
221 fclose(fp);
222 return true;
223 }
224 if(n==0){
225 fprintf(fp,"ERROR");
226 fprintf(fp,"\n");
227 fclose(fp);
228 return true;
229 }
230 }
231
232 char *ftoa(float n)
233 {
234 char *a=new char[100];
235 sprintf(a,"%.10f",n);
236 return a;
237 }