首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

简易四则运暗算算器

2012-10-17 
简易四则运算计算器请那位高手给 看看这个计算器程序为什么不能进行计算。#includestdio.h#includegraph

简易四则运算计算器
请那位高手给 看看这个计算器程序为什么不能进行计算。



#include<stdio.h>
#include<graphics.h> /* 就是需要引用这个图形库*/
#include<conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h> 
void MainFace();
void CalFace();
void main()
{
MainFace();
}
void MainFace()
{
int i,j;
initgraph(500,300); //这里和 TC 略有区别 //
bar(15,15,480,70);
setcolor(LIGHTMAGENTA);
rectangle(10,10,490,290); 
setcolor(LIGHTMAGENTA);
for(j=0;j<2;j++)
for(i=0;i<7;i++)
{
if(i<3)
circle(55+65*i,110+65*j,20);  
else 
rectangle(55+65*i-20,110+65*j-20,55+65*i+20,110+65*j+20);
}  
for(i=0;i<7;i++)
{
if(i<4) circle(55+65*i,110+65*j,20); 
else rectangle(55+65*i-20,110+65*j-20,55+65*i+20,110+65*j+20);
}
outtextxy(50,105,"7");
outtextxy(115,105,"8");
outtextxy(180,105,"9");
outtextxy(240,105,"C");
outtextxy(310,105,"+");
outtextxy(375,105,"(");
outtextxy(440,105,")");
outtextxy(50,170,"4");
outtextxy(115,170,"5");
outtextxy(180,170,"6");
outtextxy(240,170,".");
outtextxy(310,170,"-");
outtextxy(370,170,"sqrt");
outtextxy(440,170,"<-");
outtextxy(50,235,"3");
outtextxy(115,235,"2");
outtextxy(180,235,"1");
outtextxy(245,235,"0");
outtextxy(310,235,"*");
outtextxy(375,235,"/");
outtextxy(440,235,"=");
CalFace();
//以上是画图图形界面//
//getchar(); // 按任意键继续//
closegraph(); // 关闭图形界面//

}

void CalFace()
{
int i,n;
char string[50]="",str1[50]="",str2[10]="";
MOUSEMSG m; //定义一个鼠标动作//

m = GetMouseMsg();//记录鼠标操作//
i=0;
while(true)
{
m = GetMouseMsg();//记录鼠标操作//
if(m.uMsg==WM_LBUTTONDOWN)
{
setcolor(LIGHTMAGENTA);
//outtextxy(25,40,"OK");

if(m.x>35&&m.x<=75&&m.y>90&&m.y<130) {strcat(string,"7");outtextxy(25,40,string);}
if(m.x>100&&m.x<=140&&m.y>90&&m.y<130) {strcat(string,"8");outtextxy(25,40,string);}
if(m.x>165&&m.x<=205&&m.y>90&&m.y<130) {strcat(string,"9");outtextxy(25,40,string);}
if(m.x>225&&m.x<=265&&m.y>90&&m.y<130)  
{setcolor(LIGHTMAGENTA); bar(15,15,480,70);strcpy(string,"");}
if(m.x>290&&m.x<=330&&m.y>90&&m.y<130) {strcat(string,"+");outtextxy(25,40,string);}
if(m.x>355&&m.x<=395&&m.y>90&&m.y<130) {strcat(string,"(");outtextxy(25,40,string);}
if(m.x>420&&m.x<=460&&m.y>90&&m.y<130) {strcat(string,")");outtextxy(25,40,string);}
if(m.x>35&&m.x<=75&&m.y>155&&m.y<195) {strcat(string,"4");outtextxy(25,40,string);}
if(m.x>100&&m.x<=140&&m.y>155&&m.y<195) {strcat(string,"5");outtextxy(25,40,string);}
if(m.x>165&&m.x<=205&&m.y>155&&m.y<195) {strcat(string,"6");outtextxy(25,40,string);}
if(m.x>225&&m.x<=265&&m.y>155&&m.y<195) {strcat(string,".");outtextxy(25,40,string);}
if(m.x>290&&m.x<=330&&m.y>155&&m.y<195) {strcat(string,"-");setcolor(BLUE);outtextxy(25,40,string);}


if(m.x>355&&m.x<=395&&m.y>155&&m.y<195) 
{
n=strlen(string);
switch(n)//不超过位
{
case 5:
i=string[0]*10000+string[1]*1000+string[2]*100+string[3]*10+string[4];break;
case 4:
i=string[0]*1000+string[1]*100+string[2]*10+string[3];break;
case 3:
i=string[0]*100+string[1]*10+string[2];break;
case 2:
i=string[0]*10+string[1];break;
case 1:
i=string[1];break;
}
itoa(sqrt(i),string,10);
setcolor(LIGHTMAGENTA); bar(15,15,480,70);setcolor(BLUE); 
outtextxy(25,40,string);
}
if(m.x>420&&m.x<=460&&m.y>155&&m.y<195) {n=strlen(string);printf("n=%d.",n);string[n-1]='\0'; bar(15,15,480,70);setcolor(BLUE);outtextxy(25,40,string);}
if(m.x>35&&m.x<=75&&m.y>215&&m.y<255) {strcat(string,"3");outtextxy(25,40,string);}
if(m.x>100&&m.x<=140&&m.y>215&&m.y<255) {strcat(string,"2");outtextxy(25,40,string);}
if(m.x>165&&m.x<=205&&m.y>215&&m.y<255) {strcat(string,"1");outtextxy(25,40,string);}
if(m.x>225&&m.x<=265&&m.y>215&&m.y<255) {strcat(string,"0");outtextxy(25,40,string);}
if(m.x>290&&m.x<=330&&m.y>215&&m.y<255) {strcat(string,"*");outtextxy(25,40,string);}
if(m.x>355&&m.x<=395&&m.y>215&&m.y<255) {strcat(string,"/");outtextxy(25,40,string);}
if(m.x>420&&m.x<=460&&m.y>215&&m.y<255) 
{
strcpy(str1,string);
strcat(string,"=");
outtextxy(25,40,string);
strcat(str1,"#");
//itoa(ExpEvaluation(str1),str2,10);
strcat(string,str2);
outtextxy(25,40,string);
}
}
}
getchar(); // 按任意键继续//
closegraph(); //关闭图形界面//
}
#define true 1 
#define false 0 
#define OPSETSIZE 8 
typedef int Status; 
unsigned char Prior[8][8] = { // 表3.1 算符间的优先关系 

// '+' '-' '*' '/' '(' ')' '#' '^' 

/*'+'*/'>','>','<','<','<','>','>','<', 

/*'-'*/'>','>','<','<','<','>','>','<', 

/*'*'*/'>','>','>','>','<','>','>','<', 

/*'/'*/'>','>','>','>','<','>','>','<', 

/*'('*/'<','<','<','<','<','=',' ','<', 

/*')'*/'>','>','>','>',' ','>','>','>', 

/*'#'*/'<','<','<','<','<',' ','=','<', 

/*'^'*/'>','>','>','>','<','>','>','>' 

}; 

typedef struct StackChar{ 
char c; 
struct StackChar *next; 
}SC; 
typedef struct StackFloat{ 

float f; 

struct StackFloat *next; 

}SF; 

SC* Push(SC *s,char c){ 

SC *p=(SC*)malloc(sizeof(SC)); 

p->c=c; 

p->next=s; 

return p; 



SF* Push(SF *s,float f){ 

SF *p=(SF*)malloc(sizeof(SF)); 

p->f=f;  

p->next=s; 

return p; 



SC* Pop(SC *s){ 

SC *q=s; 

s=s->next; 

free(q); 

return s; 



SF* Pop(SF *s){ 

SF *q=s; 

s=s->next; 

free(q); 



return s; 



float Operate(float a,unsigned char theta, float b) { 

switch(theta) { 

case '+': return a+b; 

case '-': return a-b; 

case '*': return a*b; 

case '/': return a/b; 

case '^': return pow(a,b); 

default : return 0; 





char OPSET[OPSETSIZE]={'+' , '-' , '*' , '/' ,'(' , ')' , '#','^'}; 

Status In(char Test,char* TestOp) { 

int Find=false; 

for (int i=0; i< OPSETSIZE; i++) { 

if (Test == TestOp[i]) Find= true; 



return Find; 



ReturnOpOrd(char op,char* TestOp) { 

for(int i=0; i< OPSETSIZE; i++) 

if (op == TestOp[i]) return i; 



char precede(char Aop, char Bop) { 

return Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)]; 



float EvaluateExpression(char* MyExpression) 
{ // 算法3.4 

// 算术表达式求值的算符优先算法。 

// 设OPTR和OPND分别为运算符栈和运算数栈,OP为运算符集合。 

SC *OPTR=NULL; // 运算符栈,字符元素 

SF *OPND=NULL; // 运算数栈,实数元素 

char TempData[20]; 

float Data,a,b; 

char theta,*c,x,Dr[]={'#','\0'}; 

OPTR=Push(OPTR,'#'); 

c=strcat(MyExpression,Dr); 

strcpy(TempData,"\0");//字符串拷贝函数 

while (*c!= '#' || OPTR->c!='#') { 

if (!In(*c, OPSET)) { 

Dr[0]=*c; 

strcat(TempData,Dr);//字符串连接函数 

c++; 

if (In(*c, OPSET)) { 

Data=atof(TempData);//字符串转换函数(double) 

OPND=Push(OPND, Data); 

strcpy(TempData,"\0"); 



} else { // 不是运算符则进栈 

switch (precede(OPTR->c, *c)) { 

case '<': // 栈顶元素优先权低 

OPTR=Push(OPTR, *c); 

c++; 

break; 

case '=': // 脱括号并接收下一字符 

OPTR=Pop(OPTR); 

c++; 

break; 

case '>': // 退栈并将运算结果入栈 

theta=OPTR->c;OPTR=Pop(OPTR); 

b=OPND->f;OPND=Pop(OPND); 

a=OPND->f;OPND=Pop(OPND); 

OPND=Push(OPND, Operate(a, theta, b)); 

break; 

} // switch 



} // while 

return OPND->f; 

} // EvaluateExpression 


void 



[解决办法]
单步调试和设断点调试是程序员必须掌握的技能之一。

仅供参考

C/C++ code
/*---------------------------------------函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成功能:目前提供了10多个常用数学函数:    ⑴正弦sin    ⑵余弦cos    ⑶正切tan    ⑷开平方sqrt    ⑸反正弦arcsin    ⑹反余弦arccos    ⑺反正切arctan    ⑻常用对数lg    ⑼自然对数ln    ⑽e指数exp    ⑾乘幂函数∧用法:如果要求2的32次幂,可以打入2^32<回车>如果要求30度角的正切可键入tan(Pi/6)<回车>注意不能打入:tan(30)<Enter>如果要求1.23弧度的正弦,有几种方法都有效:sin(1.23)<Enter>sin 1.23 <Enter>sin1.23  <Enter>如果验证正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2 <Enter>或sin1.23^2+cos1.23^2 <Enter>此外两函数表达式连在一起,自动理解为相乘如:sin1.23cos0.77+cos1.23sin0.77就等价于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)当然你还可以依据三角变换,再用sin(1.23+0.77)也即sin2验证一下。本计算器充分考虑了运算符的优先级因此诸如:2+3*4^2 实际上相当于:2+(3*(4*4))另外函数名前面如果是数字,那么自动认为二者相乘.同理,如果某数的右侧是左括号,则自动认为该数与括弧项之间隐含一乘号。如:3sin1.2^2+5cos2.1^2 相当于3*sin2(1.2)+5*cos2(2.1)又如:4(3-2(sqrt5-1)+ln2)+lg5 相当于4*(3-2*(√5 -1)+loge(2))+log10(5)此外,本计算器提供了圆周率 Pi键入字母时不区分大小写,以方便使用。----------------------------------------*/#include <iostream>#include <iomanip>#include <cstdlib>#include <cstring>#include <cctype>#include <cmath>#include <stdio.h>#include <string.h>#include <windows.h>using namespace std;const char Tab=0x9;const int  DIGIT=1;const int MAXLEN=16384;char s[MAXLEN],*endss;int pcs=15;double fun(double x,char op[],int *iop) {    while (op[*iop-1]<32) //本行使得函数嵌套调用时不必加括号,如 arc sin(sin(1.234)) 只需键入arc sin sin 1.234<Enter>        switch (op[*iop-1]) {        case  7: x=sin(x);  (*iop)--;break;        case  8: x=cos(x);  (*iop)--;break;        case  9: x=tan(x);  (*iop)--;break;        case 10: x=sqrt(x); (*iop)--;break;        case 11: x=asin(x); (*iop)--;break;        case 12: x=acos(x); (*iop)--;break;        case 13: x=atan(x); (*iop)--;break;        case 14: x=log10(x);(*iop)--;break;        case 15: x=log(x);  (*iop)--;break;        case 16: x=exp(x);  (*iop)--;break;        }    return x;}double calc(char *expr,char **addr) {    static deep; //递归深度    static char *fname[]={ "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp",NULL};    double ST[10]={0.0}; //数字栈    char op[10]={'+'}; //运算符栈    char c,*rexp,*pp,*pf;    int ist=1,iop=1,last;    if (!deep) {        pp=pf=expr;        do {            c = *pp++;            if (c!=' '&& c!=Tab)                *pf++ = c;        } while (c!='\0');    }    pp=expr;    if ((c=*pp)=='-'||c=='+') {        op[0] = c;        pp++;    }    last = !DIGIT;    while ((c=*pp)!='\0') {        if (c=='(') {//左圆括弧            deep++;            ST[ist++]=calc(++pp,addr);            deep--;            ST[ist-1]=fun(ST[ist-1],op,&iop);            pp = *addr;            last = DIGIT;            if (*pp == '('||isalpha(*pp) && strnicmp(pp,"Pi",2)) {//目的是:当右圆括弧的右恻为左圆括弧或函数名字时,默认其为乘法                op[iop++]='*';                last = !DIGIT;                c = op[--iop];                goto operate ;            }        }        else if (c==')') {//右圆括弧            pp++;            break;        } else if (isalpha(c)) {            if (!strnicmp(pp,"Pi",2)) {                if (last==DIGIT) {                    cout<< "π左侧遇)" <<endl;exit(1);                }                ST[ist++]=3.14159265358979323846264338328;                ST[ist-1]=fun(ST[ist-1],op,&iop);                pp += 2;                last = DIGIT;                if (!strnicmp(pp,"Pi",2)) {                    cout<< "两个π相连" <<endl;exit(2);                }                if (*pp=='(') {                    cout<< "π右侧遇(" <<endl;exit(3);                }            } else {                for(int i=0; (pf=fname[i])!=NULL; i++)                    if (!strnicmp(pp,pf,strlen(pf)))break;                if (pf!=NULL) {                    op[iop++] = 07+i;                    pp += strlen(pf);                } else {                    cout<< "陌生函数名" <<endl;exit(4);                }            }        } else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='^') {            char cc;            if (last != DIGIT) {                cout<< "运算符粘连" <<endl;exit(5);            }            pp++;            if (c=='+'||c=='-') {                do {                    cc = op[--iop];                    --ist;                    switch (cc) {                    case '+':  ST[ist-1] += ST[ist];break;                    case '-':  ST[ist-1] -= ST[ist];break;                    case '*':  ST[ist-1] *= ST[ist];break;                    case '/':  ST[ist-1] /= ST[ist];break;                    case '^':  ST[ist-1] = pow(ST[ist-1],ST[ist]);break;                    }                } while (iop);                op[iop++] = c;            } else if (c=='*'||c=='/') {operate:        cc = op[iop-1];                if (cc=='+'||cc=='-') {                    op[iop++] = c;                } else {                    --ist;                    op[iop-1] = c;                    switch (cc) {                    case '*':  ST[ist-1] *= ST[ist];break;                    case '/':  ST[ist-1] /= ST[ist];break;                    case '^':  ST[ist-1] = pow(ST[ist-1],ST[ist]);break;                    }                }            } else {                cc = op[iop-1];                if (cc=='^') {                    cout<< "乘幂符连用" <<endl;exit(6);                }                op[iop++] = c;            }            last = !DIGIT;        } else {            if (last == DIGIT) {                cout<< "两数字粘连" <<endl;exit(7);            }            ST[ist++]=strtod(pp,&rexp);            ST[ist-1]=fun(ST[ist-1],op,&iop);            if (pp == rexp) {                cout<< "非法字符" <<endl;exit(8);            }            pp = rexp;            last = DIGIT;            if (*pp == '('||isalpha(*pp)) {                op[iop++]='*';                last = !DIGIT;                c = op[--iop];                goto operate ;            }        }    }    *addr=pp;    if (iop>=ist) {        cout<< "表达式有误" <<endl;exit(9);    }    while (iop) {        --ist;        switch (op[--iop]) {        case '+':  ST[ist-1] += ST[ist];break;        case '-':  ST[ist-1] -= ST[ist];break;        case '*':  ST[ist-1] *= ST[ist];break;        case '/':  ST[ist-1] /= ST[ist];break;        case '^':  ST[ist-1] = pow(ST[ist-1],ST[ist]);break;        }    }    return ST[0];}int main(int argc,char **argv) {    if (argc<=1) {        if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页        cout << "计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;        while (1) {            cout << "请输入表达式:";            gets(s);            if (s[0]==0) break;//            cout << s <<"=";            cout << setprecision(15) << calc(s,&endss) << endl;        }    } else {        strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;        if (argc>=3) {            pcs=atoi(argv[2]);            if (pcs<0||15<pcs) pcs=15;            printf("%.*lf\n",pcs,calc(s,&endss));        } else {            printf("%.15lg\n",calc(s,&endss));        }    }    return 0;} 


[解决办法]
我的资源中有个计算器。。你可以参考下!!自己找吧!!

热点排行