怎样用c语言编一个简单的计算器?

如题所述

//简单计算器,含加减乘除、乘方运算。
#include
#include
#include // malloc()等
#include // INT_MAX等
#include // EOF(=^Z或F6),NULL
#include // atoi()
#include // eof()
#include // floor(),ceil(),abs()
#include // exit()
#include // cout,cin
// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
// #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int Boolean; // Boolean是布尔类型,其值是TRUE或FALSE

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

//***************************************************************************
//栈的储存结构

typedef struct{
//运算符栈
char *base;
char *top;
int stacksize;
}SqStack1;

typedef struct{
//运算数栈
float *base;
float *top;
int stacksize;
}SqStack2;

//***************************************************************************
//以下是运算符栈的基本操作函数

Status InitStack(SqStack1 &S){
//初始化一个栈
S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack

Status DestroyStack(SqStack1 &S){
//销毁栈S
free(S.top);
free(S.base);
return OK;
}//DestroyStack

char GetTop(SqStack1 S){
//若栈不空,则返回S的栈顶元素,并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
return *(S.top-1);
}//Gettop

Status Push(SqStack1 &S,char e){
//插入元素e为新的栈顶元素
if(S.top-S.base>=S.stacksize){
//栈满,追加储存空间
S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//Push

Status Pop(SqStack1 &S,char &e){
//若栈不空,则删除S的栈顶元素,用e返回其值;并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
e=*(--S.top);
return OK;
}//Pop

//***************************************************************************
//以下是运算数栈的基本操作函数

Status InitStack(SqStack2 &S){
//初始化一个栈
S.base=(float *)malloc(STACK_INIT_SIZE*sizeof(float));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack

Status DestroyStack(SqStack2 &S){
//销毁栈S
free(S.top);
free(S.base);
return OK;
}//DestroyStack

float GetTop(SqStack2 S){
//若栈不空,则返回S的栈顶元素,并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
return *(S.top-1);
}//Gettop

Status Push(SqStack2 &S,float e){
//插入元素e为新的栈顶元素
if(S.top-S.base>=S.stacksize){
//栈满,追加储存空间
S.base=(float *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(float));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//Push

Status Pop(SqStack2 &S,float &e){
//若栈不空,则删除S的栈顶元素,用e返回其值;并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
e=*(--S.top);
return OK;
}//Pop

//***************************************************************************
//以下是相关的运算符判断函数

char Precede(char A,char B){
//比较运算符A, B的优先关系,A,B的范围仅限于'+','-','*','/','^','(',')','='
//返回'>','<','='
switch(A){
case '+':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '-':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '*':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '/':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '^':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '>';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '(':switch(B){
case '+':return '<';
case '-':return '<';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':return '=';
case '=':printf("表达式错误!\n");exit(0);
default:printf("表达式错误!\n");exit(0);
}
case ')':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '>';
case '(':printf("表达式错误!\n");exit(0);
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '=':switch(B){
case '+':return '<';
case '-':return '<';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':printf("表达式错误!\n");exit(0);
case '=':return '=';
default:printf("表达式错误!\n");exit(0);
}
default:printf("表达式错误!\n");exit(0);
}
}//Precede

Status InOP(char c){
//判断c是否是运算符,是则返回TRUE,否则返回FALSE
switch(c){
case '+':return TRUE;
case '-':return TRUE;
case '*':return TRUE;
case '/':return TRUE;
case '^':return TRUE;
case '(':return TRUE;
case ')':return TRUE;
case '=':return TRUE;
default:return FALSE;
}
}//InOP

//***************************************************************************

float Operate(float a,char theta,float b){
switch(theta){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':
if(b==0){
printf("分母不能为0!\n");
exit(0);
}
else return a/b;
case '^':
if(a==0&&b<=0){
printf("0的指数必须大于0!\n");
exit(0);
}
else return (float)pow(a,b);
default:printf("表达式错误!\n");exit(0);
}
}//Operate

Status EvaluateExpression(){
//算术表达式求值
char c,x,theta,prec;
//c是每次读取的字符,x是存放脱括号后的多余的括号,theta是运算符,prec是c的前一个字符
float a,b,result;//a、b是每次从运算数栈中取出的要进行运算的数,result存放最终结果
float cc,flag,ii,minus=1;
//cc存放由字符串转化而来的浮点数,flag用于标记是否已读取过小数点,
//ii存放小数部分需要缩小的倍数,minus用于记录该数前是否有负号
SqStack1 OPTR;
SqStack2 OPND;
InitStack(OPTR);InitStack(OPND);
Push(OPTR,'=');
prec='=';scanf("%c",&c);
while(c!='='||GetTop(OPTR)!='='){
cc=0;flag=0;ii=10;
if(c=='-'&&(prec=='='||prec=='(')){minus=-1;prec=c;scanf("%c",&c);}
//若某“-”前面是“=”(第一个符号就是“-”)或“(”,则此为负号,不是减号
else if(!InOP(c)){
while(!InOP(c)){
if(c>=48&&c<=57){
if(flag==0)cc=cc*10+c-48;//小数点之前
else if(flag==1){cc=cc+(c-48)/ii;ii*=10;}//小数点之后
else {printf("小数点错误!\n");exit(0);}//小数点有错
}
else if(c=='.')flag++;//读到小数点
else {printf("表达式错误!\n");exit(0);}
prec=c;scanf("%c",&c);
}
cc*=minus;minus=1;
Push(OPND,cc);
}//不是运算符则进OPND栈
else
switch(Precede(GetTop(OPTR),c)){
case '<':Push(OPTR,c);prec=c;scanf("%c",&c);break;//栈顶元素优先级低
case '=':Pop(OPTR,x);prec=c;scanf("%c",&c);break;//脱括号并接收下一字符
case '>'://退栈并将运算结果入栈
Pop(OPTR,theta);
Pop(OPND,b);Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
}
}
result=GetTop(OPND);
printf("%f\n",result);
//DestroyStack(OPTR);
//DestroyStack(OPND);
return OK;
}//EvaluateExpression

void main(){
printf(" **********************\n");
printf(" * 欢迎使用计算器! *\n");
printf(" **********************\n");
printf("请输入表达式,以“=”结束:\n");
printf("(支持实数间的加(+)、减(-)、乘(*)、除(/)、乘方(^)、单目减(-)运算)\n");
EvaluateExpression();
exit (0);
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2019-08-13
#include
<stdio.h>
#include
<stdlib.h>
void
min()
{
float
a,b;
char
op;
printf("输入第一个数:");
scanf("%f",&a);
fflush(stdin);
printf("输入一个运算符+-*
/:");
scanf("%c",&op);
fflush(stdin);
printf("输入第二个数:");
scanf("%f",&b);
fflush(stdin);
switch(op)
{
case
'+':
a=a+b;
break;
case
'-':
a=a-b;
break;
case
'*':
a=a*b;
break;
case
'/':
a=a/b;
break;
default:
printf("+-/*!!!error");
return;
}
printf
("%f\n",a);
return
;
}
int
main()
{
char
ch;
printf("请输入选择\na开始运算c清屏x退出:");
ch=getchar();
while(ch!='x')
{
min();
ch=getchar();
if(ch=='c')
system("cls");
}
return
0;
}
第2个回答  2008-05-18
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <stdio.h>
#include <process.h>
#define EXCAPE 27
#define ENTER 13
main(){
int press,i,x,y,x1,y1,ch_z=0;
int dian=0;
char ch='0'; /*input + - * / */
char emp[80],sum[80],*e,*s;
double yuan=0.000000000000;
void init(void);
void clear_z(char *u);
double strtoflt(char *p);
int getkey();
int gd=DETECT, gm;
initgraph(&gd, &gm, "");
e=emp;
s=sum;
init();
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
while(1){
press = getkey();
switch(press){
case EXCAPE:
exit(0);
case 47:
bar (x + 10, y + 80 + 10, x + 60 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
if (strtoflt(emp)==0.0){
ch='0';
ch_z=0;
dian=0;
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
outtextxy(x+30,y+40,"error!!!!!");
break;
}
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);

}
else{
outtextxy(x+30,y+40,sum);

}
}
ch='/';
ch_z=0;
emp[0]='\0';
e=emp;
dian=0;
break;
case 42:
bar (x + 60 + 10, y + 80 + 10, x + 60 * 2 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='*';
ch_z=0;
dian=0;
break;
case 45:
bar (x + 60 * 2 + 10, y + 80 + 10, x + 60 * 3 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='-';
ch_z=0;
dian=0;
break;
case 43:
bar (x + 60 * 3 + 10, y + 80 + 10, x + 60 * 4 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='+';
ch_z=0;
dian=0;
break;
case 49:
bar (x + 10, y + 80 + 53 + 10, x + 60 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='1';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 50:
bar (x + 60 + 10, y + 80 + 53 + 10, x + 60 * 2 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='2';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 51:
bar (x + 60 * 2 + 10, y + 80 + 53 + 10, x + 60 * 3 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='3';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case ENTER:
bar (x + 60 * 3 + 10, y + 80 + 53 + 10, x + 60 * 4 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else{
outtextxy(x+30,y+40,sum);
}
}
ch='0';
ch_z=1;
dian=0;
break;
case 52:
bar (x + 10, y + 80 + 53 * 2 + 10, x + 60 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='4';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 53:
bar (x + 60 + 10, y + 80 + 53 * 2 + 10, x + 60 * 2 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='5';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 54:
bar (x + 60 * 2 +10, y + 80 + 53 * 2 + 10, x + 60 * 3 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='6';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 46:
bar (x + 60 * 3 + 10, y + 80 + 53 * 2 + 10, x + 60 * 4 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (dian==0){
if (ch_z==0){
*e='.';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
}
else{
if (ch_z==0)
outtextxy(x+30,y+40,emp);
else
outtextxy(x+30,y+40,sum);
}
dian=1;
break;
case 55:
bar (x + 10, y + 80 + 53 * 3 + 10, x + 60 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='7';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 56:
bar (x + 60 + 10, y + 80 + 53 * 3 + 10, x + 60 * 2 -10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='8';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 57:
bar (x + 60 * 2 + 10, y + 80 + 53 * 3 + 10, x + 60 * 3 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='9';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 48:
bar (x + 60 * 3 + 10, y + 80 + 53 * 3 + 10, x + 60 * 4 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='0';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 32:
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
ch='0';
ch_z=0;
dian=0;
init();
break;
case 8:
delay(8000);
for(i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (i==0)
break;
if (i!=79&&i!=0){
i--;
emp[i]='\0';
e=&emp[i];
}
init();
outtextxy(x+30,y+40,emp);
break;
}
}
}
/*---------------------------------------------------------------------*/
void init(void){
int x, y, x1, y1, i, j;
char emp;
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
cleardevice();
setbkcolor(3);
setfillstyle(1, 15);
setcolor(15);
settextstyle(1,0,1);
rectangle (x, y, x1, y1);
rectangle (x - 7, y - 7, x1 + 7, y1 + 7);
rectangle (x + 10, y + 10, x1 - 10, y + 80 - 10);
line (x, y + 80, x1, y + 80);
y = y + 80;
for (j = 1; j <= 4; j++){
x = (getmaxx() / 2) - 120;
for (i = 1; i <= 4; i++){
/* bar (x + 10, y + 10, x + 60 - 10, y + 60 - 10);*/
rectangle(x + 10, y + 10, x + 60 - 10, y + 60 - 10);
if (j == 1){
if (i == 1)
outtextxy(x + 20, y + 20, "/");
if (i == 2)
outtextxy(x + 25, y + 20, "*");
if (i == 3)
outtextxy(x + 27, y + 20, "-");
if (i == 4)
outtextxy(x + 25, y + 20, "+");
}
if (j == 2){
if (i == 1)
outtextxy(x + 25, y + 20, "1");
if (i == 2)
outtextxy(x + 25, y + 20, "2");
if (i == 3)
outtextxy(x + 25, y + 20, "3");
if (i == 4)
outtextxy(x + 25, y + 20, "=");
}
if (j == 3){
if (i == 1)
outtextxy(x + 25, y + 20, "4");
if (i == 2)
outtextxy(x + 25, y + 20, "5");
if (i == 3)
outtextxy(x + 25, y + 20, "6");
if (i == 4)
outtextxy(x + 25, y + 20, ".");
}
if (j == 4){
if (i == 1)
outtextxy(x + 25, y + 20, "7");
if (i == 2)
outtextxy(x + 25, y + 20, "8");
if (i == 3)
outtextxy(x + 25, y + 20, "9");
if (i == 4)
outtextxy(x + 25, y + 20, "0");
}
x = x + 60;
}
y = y + 53;
}
}
/*---------------------------------------------------------------------*/
int getkey(){
char lowbyte;
int press;
while(bioskey(1)==0);
press = bioskey(0);
press = press&0xff? press&0xff: press>>8;

return(press);

}
double strtoflt(char *p)
{
double rtl=0.000000000000;
double pnt=0.000000000000;
double t = 10;
int ispoint = 0;
while (*p!='\0'||*p!='.'){
if(*p<'0'||*p>'9')
break;
rtl*=10;
rtl+=*p-'0';
p++;
}
if (*p=='.'){
ispoint=1;
p++;
}
while(ispoint&&*p!='\0'){
pnt+=(double)(*p-'0')/t;
t*=10;
p++;
}
rtl+=pnt;
return (rtl);
}
/*-----------------------------------------------------------------------*/
void clear_z(char u[]){
int i;
for(i=strlen(u)-1;i>=0;i--){
if (u[i]!='0')
break;
}
if (u[i]=='.'){
u[i]='\0';
}
else{
i++;
u[i]='\0';
}
}本回答被提问者采纳
相似回答