新建 文本文档.txt

来自「用c语言实现的简易计算器」· 文本 代码 · 共 213 行

TXT
213
字号
#include "graphics.h"                     /*程序包含的库函数*/
#include "math.h"
#include "stdarg.h"
void surface()                    /*用图形函数编辑的计算器界面*/
{ int i,j,t=0;
  char *sign[]={"7","4","1","0","8","5","2",".","9","6","3"," "};
  int gdriver=DETECT,gmode;
  initgraph(&gdriver,&gmode,"d:\\tc");
  setgraphmode(gmode);setbkcolor(BLUE);
 cleardevice();                           /*设计的有关说明信息*/
 outtextxy(300,400,"Designer:  SCUT 02(4)22 LinWeiJu");   
  outtextxy(160,350,"'o':on    'f':off    'space':+/-  ");
  outtextxy(160,50,"Welcome to use this calculator!");
  setfillstyle(1,8);                              /*计算器面板*/
  bar(160,70,390,330);
  setfillstyle(1,7);
  bar(165,75,385,115);
  setfillstyle(1,14);                          /*运算显示屏幕*/
  bar(175,80,375,110);
  setcolor(14);setlinestyle(0,0,2);
line(160,120,390,120);
for(i=0;i<5;i++)                                /*按钮的大小和边缘设定*/
     {for(j=0;j<4;j++)
        {setfillstyle(1,8); bar(180+50*j,130+40*i,220+50*j,160+40*i);
         setcolor(14);setlinestyle(0,0,2);
		    line(180+50*j,130+40*i,180+50*j,160+40*i);
		    line(180+50*j,130+40*i,220+50*j,130+40*i);
         setcolor(2);line(220+50*j,130+40*i,220+50*j,160+40*i);
		  line(180+50*j,160+40*i,220+50*j,160+40*i);}
		  }
 setlinestyle(0,0,3);line(390,70,390,330);line(160,330,390,330);
setlinestyle(0,0,2);setcolor(14);
line(160,70,160,330);line(160,70,390,70); setcolor(4);outtextxy(190,140,"ON"); 
setcolor(0);outtextxy(240,140,"OFF");
 setcolor(14);  settextstyle(1,0,2);
 for(i=0;i<3;i++)
     for(j=0;j<4;j++)
      {outtextxy(195+50*i,175+40*j,sign[t]);
       t=t+1;
      }
 settextstyle(1,0,1);                /*按钮的符号设定*/
   setcolor(14);outtextxy(283,295,"+/-");setcolor(2);
   outtextxy(345,135,"+");outtextxy(345,215,"*");
   outtextxy(345,295,"=");outtextxy(345,175,"-");outtextxy(345,255,"/");
   line(285,145,315,145);line(285,145,300,150);line(285,145,300,140);
}

void error()                         /*遇到无意义的运算用于输出错误信息*/                                
{setfillstyle(1,6);
 bar(175,80,375,110);outtextxy(180,90," -E-");}

void string(char *s)                   /*用于输出进行运算的实数*/
{char buf[21];
 setfillstyle(1,14); bar(175,80,375,110);
 sprintf(buf,"%17s",s);outtextxy(200,90,buf);
 }

void point(double f)                           /*用于输出结果*/
{ char buf[19];
  setfillstyle(1,9); bar(175,80,375,110);
  sprintf(buf,"%17.9g",f); outtextxy(200,90,buf);}

double calc(double op1,double op2,char op)       /*四则运算函数*/
{int i,j;
switch (op)
   { case '+': return op1+op2;
     case '-': return op1-op2;
     case '*': return op1*op2;
     case '/':
       if(op2==0)
	{error();
	getch();
	return op1;
	}
	else
	   return op1/op2;
       case 0: return op2;
       }
       return op2;
       }

void calculator()                             /*运算器操作函数*/
{  int key,point_f=0,num=1,count=0,length,i,j,k,e_f=0;
   char buf[20]="0",last1=0,last2=0;
   double op1=0,op2=0,op3=0;
   string(buf);
   while((key=getch())!=27)         /*key用来接收输入,当输入Esc时跳出*/
   {if(key==0) {key=getch();continue;}
     switch (key)                             
      {case'1':  i=0;j=4;de(i,j);break;
 case'2':  i=1;j=4;de(i,j);break;
       case'3':  i=2;j=4;de(i,j);break; 
case'/':  i=3;j=4;de(i,j);break;
       case'4':  i=0;j=3;de(i,j);break;
case'5':  i=1;j=3;de(i,j);break;
       case'6':  i=2;j=3;de(i,j);break; 
case'*':  i=3,j=3;de(i,j);break;
       case'7':  i=0;j=2;de(i,j);break;
 case'8':  i=1;j=2;de(i,j);break;
       case'9':  i=2;j=2;de(i,j);break;
 case'-':  i=3;j=2;de(i,j);break;
       case'0':  i=0;j=5;de(i,j);break; 
case'+':  i=3;j=1;de(i,j);break;
       case'o':  i=0,j=1;de(i,j);break;
 case'f':  i=1,j=1;de(i,j);break;}
      switch (key)                       /*按键发出不同的声音*/
       {case'1':sound(900);delay(2000);nosound();
if(count<17)buf[count++]=key,num=1;break;
	      case'2':sound(1000);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'3':sound(1100);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'4':sound(1200);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'5':sound(1300);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'6':sound(1500);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'7':sound(1700);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'8':sound(1800);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'9':sound(1900);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
	      case'0':sound(2000);delay(2000);nosound();
if(count<17) buf[count++]=key,num=1;break;
         case 'f':setfillstyle(1,11);bar(175,80,375,110);k=0;
                  buf[0]='\0';break;                   /*off的功能*/
	      case '.':i=1;j=5;de(i,j); sound(600);
                  delay(1000);nosound();
	          if(!point_f&&count<17) buf[count++]=key,num=point_f=1;break;
          case '+':case'-':case'*':case'/':sound(600);
                       delay(1000);nosound();
	       if(num)
	          { op1=atof(buf);
	            num=point_f=count=0;
	               if(last1=='*' || last1=='/')  /*last1、last2判别优先级*/
	                 {op1=op2=calc(op2,op1,last1),last1=key;
	                  if(last2 && (key=='+' || key=='-'))
	                     op1=op2=calc(op3,op2,last2),last2=0;
	                 }
	                  else if(key=='+' || key=='-')
		                 op1=op2=calc(op2,op1,last1),last1=key;
	                  else
	                        { if(last1) last2=last1,op3=op2;
		                  last1=key,op2=op1;
	                         }
	                 }
	         else
	                    last1=key;
	                    continue;
	                    case '=': case 0xd:
	                    i=3;j=5;de(i,j);sound(600);
                            delay(1000);nosound();
	                    if(num)
	                      {op1=atof(buf);
	                       num=point_f=count=0;
	                      }
	          op1=op2=calc(op3,calc(op2,op1,last1),last2),last1=last2=0;
	                  point(op1); continue;
           case  8 :i=2;j=1;de(i,j); sound(600);delay(1000);nosound();
	            if(num)                         /*BackSpace的功能*/ 
	               { if(count>0) count--;
	                 if(buf[count]=='e') e_f=0;
	                 if(buf[count]=='.')point_f=0;
	                 buf[count]=0;
	               }
	             break;
	         case 'o':sound(600);delay(1000);nosound();  /*on的功能*/
	       op1=op2=op3=last1=last2=count=num=e_f=point_f=0;k=1 break;
            case'': i=2;j=5;de(i,j);sound(600);delay(1000);nosound();
	                if(count>0 && count<17 && count>0)
	                  {if(buf[0]=='-')
	                     {for(i=0;i<count;i++) buf[i]=buf[i+1]; count--;
	                     }
	                  else
	                     {for(i=count;i>=0;i--)
		                      buf[i+1]=buf[i];
	                          buf[0]='-'; count++;
	                    }
	               }
	              else if(num==0) op2=op1=-op1;break;
	               default: continue;
	              }
	   gotoxy(35,6);          /*输出操作,调用point和string函数*/
	   if(num)
	   {buf[count]=0;
	    op1=atof(buf);
	    length=strlen(buf);
	    if(length>17) string(buf+length-17);
	    else if(length==0&&k!=0) point(0.0);
	    else if(k!=0) string(buf);
	   }
            else  if(k!=0) point(op1);
        }
 }
 de(i,j)                                  /*动态按钮效果*/
int i,j; 
{setcolor(2);line(180+50*i,90+40*j,220+50*i,90+40*j);
line(180+50*i,90+40*j,180+50*i,120+40*j);
 setcolor(14);line(220+50*i,90+40*j,220+50*i,120+40*j);
 line(180+50*i,120+40*j,220+50*i,120+40*j);delay(6000);
 setcolor(14);line(180+50*i,90+40*j,220+50*i,90+40*j);
 line(180+50*i,90+40*j,180+50*i,120+40*j);
 setcolor(2);line(220+50*i,90+40*j,220+50*i,120+40*j);
 line(180+50*i,120+40*j,220+50*i,120+40*j);}

main()                                     /*主函数*/
{surface();
calculator();
closegraph();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?