?? 新建 文本文檔.txt
字號:
#include "graphics.h" /*程序包含的庫函數(shù)*/
#include "math.h"
#include "stdarg.h"
void surface() /*用圖形函數(shù)編輯的計算器界面*/
{ 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(); /*設(shè)計的有關(guān)說明信息*/
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); /*運(yùn)算顯示屏幕*/
bar(175,80,375,110);
setcolor(14);setlinestyle(0,0,2);
line(160,120,390,120);
for(i=0;i<5;i++) /*按鈕的大小和邊緣設(shè)定*/
{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); /*按鈕的符號設(shè)定*/
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() /*遇到無意義的運(yùn)算用于輸出錯誤信息*/
{setfillstyle(1,6);
bar(175,80,375,110);outtextxy(180,90," -E-");}
void string(char *s) /*用于輸出進(jìn)行運(yùn)算的實(shí)數(shù)*/
{char buf[21];
setfillstyle(1,14); bar(175,80,375,110);
sprintf(buf,"%17s",s);outtextxy(200,90,buf);
}
void point(double f) /*用于輸出結(jié)果*/
{ 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) /*四則運(yùn)算函數(shù)*/
{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() /*運(yùn)算器操作函數(shù)*/
{ 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用來接收輸入,當(dāng)輸入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) /*按鍵發(fā)出不同的聲音*/
{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判別優(yōu)先級*/
{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); /*輸出操作,調(diào)用point和string函數(shù)*/
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) /*動態(tài)按鈕效果*/
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() /*主函數(shù)*/
{surface();
calculator();
closegraph();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -