C语言编写计算器程序,实在不知道错在哪里

我是小白求轻喷 2014-03-23 07:27:26
学c语言半学期了,对字符的处理实在不擅长,请高手们帮我看看 谢谢了

// 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;
}
...全文
270 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-03-24
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 String Manipulation These routines operate on null-terminated single-byte character, wide-character, and multibyte-character strings. Use the buffer-manipulation routines, described in Buffer Manipulation, to work with character arrays that do not end with a null character. String-Manipulation Routines Routine Use _mbscoll, _mbsicoll, _mbsncoll, _mbsnicoll Compare two multibyte-character strings using multibyte code page information (_mbsicoll and _mbsnicoll are case-insensitive) _mbsdec, _strdec, _wcsdec Move string pointer back one character _mbsinc, _strinc, _wcsinc Advance string pointer by one character _mbslen Get number of multibyte characters in multibyte-character string; dependent upon OEM code page _mbsnbcat Append, at most, first n bytes of one multibyte-character string to another _mbsnbcmp Compare first n bytes of two multibyte-character strings _mbsnbcnt Return number of multibyte-character bytes within supplied character count _mbsnbcpy Copy n bytes of string _mbsnbicmp Compare n bytes of two multibyte-character strings, ignoring case _mbsnbset Set first n bytes of multibyte-character string to specified character _mbsnccnt Return number of multibyte characters within supplied byte count _mbsnextc, _strnextc, _wcsnextc Find next character in string _mbsninc. _strninc, _wcsninc Advance string pointer by n characters _mbsspnp, _strspnp, _wcsspnp Return pointer to first character in given string that is not in another given string _mbstrlen Get number of multibyte characters in multibyte-character string; locale-dependent sprintf, _stprintf Write formatted data to a string strcat, wcscat, _mbscat Append one string to another strchr, wcschr, _mbschr Find first occurrence of specified character in string strcmp, wcscmp, _mbscmp Compare two strings strcoll, wcscoll, _stricoll, _wcsicoll, _strncoll, _wcsncoll, _strnicoll, _wcsnicoll Compare two strings using current locale code page information (_stricoll, _wcsicoll, _strnicoll, and _wcsnicoll are case-insensitive) strcpy, wcscpy, _mbscpy Copy one string to another strcspn, wcscspn, _mbscspn, Find first occurrence of character from specified character set in string _strdup, _wcsdup, _mbsdup Duplicate string strerror Map error number to message string _strerror Map user-defined error message to string strftime, wcsftime Format date-and-time string _stricmp, _wcsicmp, _mbsicmp Compare two strings without regard to case strlen, wcslen, _mbslen, _mbstrlen Find length of string _strlwr, _wcslwr, _mbslwr Convert string to lowercase strncat, wcsncat, _mbsncat Append characters of string strncmp, wcsncmp, _mbsncmp Compare characters of two strings strncpy, wcsncpy, _mbsncpy Copy characters of one string to another _strnicmp, _wcsnicmp, _mbsnicmp Compare characters of two strings without regard to case _strnset, _wcsnset, _mbsnset Set first n characters of string to specified character strpbrk, wcspbrk, _mbspbrk Find first occurrence of character from one string in another string strrchr, wcsrchr,_mbsrchr Find last occurrence of given character in string _strrev, _wcsrev,_mbsrev Reverse string _strset, _wcsset, _mbsset Set all characters of string to specified character strspn, wcsspn, _mbsspn Find first substring from one string in another string strstr, wcsstr, _mbsstr Find first occurrence of specified string in another string strtok, wcstok, _mbstok Find next token in string _strupr, _wcsupr, _mbsupr Convert string to uppercase strxfrm, wcsxfrm Transform string into collated form based on locale-specific information vsprintf, _vstprint Write formatted output using a pointer to a list of arguments
SuperThreeX 2014-03-24
  • 打赏
  • 举报
回复
有桌面版的吗?我想知道c语言是不是有必要值得一学。现在都是java c#一类的语言。
  • 打赏
  • 举报
回复
谢谢楼上 已经可以运行了 我再研究研究
tuzhutuzhu 2014-03-23
  • 打赏
  • 举报
回复
不会用vs,所以没法重现你的问题。代码改了一点,具体哪些你diff一下。用gcc编译,简单的测了一下 代码中小问题不少,不过才学半年来说已经很不错了
 
 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 }
  • 打赏
  • 举报
回复
错误是这样的
没事人 2014-03-23
  • 打赏
  • 举报
回复
我的编译软件里没有其中的一个头文件,而且我是菜鸟,不好意思帮不到你了
没事人 2014-03-23
  • 打赏
  • 举报
回复
看了以后眼都晕了,程序怎么报错啊,

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧