计算器的设计及工作源代码.pdf

上传人:奔*** 文档编号:92972630 上传时间:2023-06-18 格式:PDF 页数:8 大小:1.49MB
返回 下载 相关 举报
计算器的设计及工作源代码.pdf_第1页
第1页 / 共8页
计算器的设计及工作源代码.pdf_第2页
第2页 / 共8页
点击查看更多>>
资源描述

《计算器的设计及工作源代码.pdf》由会员分享,可在线阅读,更多相关《计算器的设计及工作源代码.pdf(8页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、计 算 器 的 设 计 及 工 作 源 代 码 Sinclude/*D0S 接 口 函 数*/#include/*数 学 函 数 的 定 义*/Sinclude/*屏 幕 操 作 函 数*/#include/*I/0 函 数*/#include/*库 函 数*/Winclude/*变 量 长 度 参 数 表*/#include/*图 形 函 数*/#include/*字 符 串 函 数*/include/*字 符 操 作 函 数*/#define UP 0 x48/*光 标 上 移 键*/#define DOWN 0 x50/*光 标 下 移 键*/Sdefine LEFT 0 x4b/*光

2、标 左 移 键*/#define RIGHT 0 x4d/*光 标 右 移 键*/define ENTER OxOd/*回 车 键*/void*rar;/*全 局 变 量,保 存 光 标 图 象*/struct palettetype palette;/*使 用 调 色 板 信 息*/int GraphDriver;/*图 形 设 备 驱 动*/int GraphMode;/*图 形 模 式 值*/int ErrorCode;/*错 误 代 码*/int MaxColors;/*可 用 颜 色 的 最 大 数 值*/int MaxX,MaxY;/*屏 幕 的 最 大 分 辨 率*/double

3、 AspectRatio;/*屏 幕 的 像 素 比*/void drawboder(void);/*画 边 框 函 数*/void initialize(void);/*初 始 化 函 数*/void computer(void);/*计 算 器 计 算 函 数*/void changetextstyle(int font,int direction,int charsize);/*改 变 文 本 样 式 函 数*/void mwindow(char*header);/*窗 口 函 数*/int specialkey(void);/*获 取 特 殊 键 函 数*/int arrov();/*

4、设 置 箭 头 光 标 函 数*/*主 函 数*/int main()initialize();/*设 置 系 统 进 入 图 形 模 式*/computer。;/*运 行 计 算 器*/closegraph();/*系 统 关 闭 图 形 模 式 返 回 文 本 模 式*/return(0);/*结 束 程 序*/)/*设 置 系 统 进 入 图 形 模 式*/void initialize(void)|int xasp,yasp;/*用 于 读 x 和 y 方 向 纵 横 比*/GraphDriver=DETECT;/*自 动 检 测 显 示 器*/initgraph(feGraphDri

5、ver,feGraphMode,);/*初 始 化 图 形 系 统*/ErrorCode=graphresult();/*读 初 始 化 结 果*/if(ErrorCode!=grOk)/*如 果 初 始 化 时 出 现 错 误*/(printf(Graphics System Error:%sn”,grapherrormsg(ErrorCode);/*显 示 错 误 代 码*/exit(1);/*退 出*/)getpalette(fepalette);/*读 面 板 信 息*/MaxColors=getmaxcolor()+1;/*读 取 颜 色 的 最 大 值*/MaxX=getmaxx(

6、);/*读 屏 幕 尺 寸*/MaxY=getmaxy();/*读 屏 幕 尺 寸*/getaspectratio(&xasp,&yasp);/*拷 贝 纵 横 比 到 变 量 中*/AspectRatio=(double)xasp/(double)yasp;/*计 算 纵 横 比 值*/*计 算 器 函 数*/void computer(void)(struct viewporttype vp;/*定 义 视 口 类 型 变 量*/int color,height,width;int x,y,xO,yO,i,j,v,m,n,act,f lag=l;float numl=0,num2=0,re

7、sult;/*操 作 数 和 计 算 结 果 变 量*/char cnum5,str220=,c,temp20=;char str 1=1230.456+-789*/Qc=;/*定 义 字 符 串 在 按 钮 图 形 上 显 示 的 符 号*/mwindow(Calculator);/*显 示 主 窗 口*/color=7;/*设 置 灰 颜 色 值*/getviewsettings(&vp);/*读 取 当 前 窗 口 的 大 小*/width=(vp.right+l)/10;/*设 置 按 钮 宽 度*/height=(vp.bottom-10)/10;/*设 置 按 钮 高 度*/x=w

8、idth/2;/*设 置 x 的 坐 标 值*/y=height/2;/*设 置 y 的 坐 标 值*/setfillstyle(SOLID_FILL,color+3);bar(x+width*2,y,x+7*width,y+height);/*画 一 个 二 维 矩 形 条 显 示 运 算 数 和 结 果*/setcolor(color+3);/*设 置 淡 绿 颜 色 边 框 线*/rectangle(x+width*2,y,x+7*width,y+height);/*画 一 个 矩 形 边 框 线*/setcolor(RED);/*设 置 颜 色 为 红 色*/outtextxy(x+3

9、*width,y+height/2,0.);/*输 出 字 符 串 0.*/x=2*width-vidth/2;/*设 置 x 的 坐 标 值*/y=2*height+height/2;/*设 置 y 的 坐 标 值*/for(j=0;j<4;+j)/*画 按 钮*/for(i=0;i<5;+i)(setfillstyle(SOLIDFILL,color);setcolor(RED);bar(x,y,x+width,y+height);/*画 一 个 矩 形 条*/rectangle(x,y,x+width,y+height);sprintf(str2,%c,strlj*5+i);

10、/*将 字 符 保 存 到 str2中*/outtextxy(x+(width/2),y+height/2,str2);x=x+width+(width/2);/*移 动 列 坐 标*/)y+=(height/2)*3;/*移 动 行 坐 标*/x=2*width-width/2;/*复 位 列 坐 标*/x0=2*width;y0=3*height;x=xO;y=yO;gotoxy(x,y);/*移 动 光 标 到 x,y位 置*/arrow();/*显 示 光 标*/put image(x,y,rar,XOR_PUT);m=0;n=0;strcpy(str2,);/*设 置 str2 为

11、空 串*/while(v二 specialkey()!=45)/*当 压 下 Alt+x键 结 束 程 序,否 则 执 行 下 面 的 循 环*/(while(v=specialkey()!=ENTER)/*当 压 下 键 不 是 回 车 时*/(putimage(x,y,rar,XOR PUT);/*显 示 光 标 图 象*/if(v=RIGHT)/*右 场 箭 头 时 新 位 置 计 算*/if(x>=x0+6*width)/*如 果 右 移,移 到 尾,则 移 动 到 最 左 边 字 符 位 置*/x=x0;m=0;)else(x=x+width+width/2;m+;/*否 则,

12、右 移 到 下 一 个 字 符 位 置*/if(v=LEFT)/*左 移 箭 头 时 新 位 置 计 算*/if(x<=xO)x=x0+6*width;m=4;/*如 果 移 到 头,再 左 移,则 移 动 到 最 右 边 字 符 位 置*/else(x=x-width-width/2;m-;/*否 则,左 移 到 前 一 个 字 符 位 置*/if(v=UP)/*上 移 箭 头 时 新 位 置 计 算*/if(y<=yO)(y=y0+4*he i ght+he i ght/2;n=3;/*如 果 移 到 头,再 上 移,则 移 动 到 最 下 边 字 符 位 置*/elsey=y

13、-height-height/2;n一;/*否 则,移 到 上 边 一 个 字 符 位 置*/if(v=DOWN)/*下 移 箭 头 时 新 位 置 计 算*/if(y>=7*height)(y=yO;n=0;/*如 果 移 到 尾,再 下 移,则 移 动 到 最 上 边 字 符 位 置*/else(y=y+height+height/2;n+;/*否 则,移 到 下 边 一 个 字 符 位 置*/put image(x,y,rar,XOR_PUT);/*在 新 的 位 置 显 示 光 标 箭 头*/)c=strl n*5+m;/*将 字 符 保 存 到 变 量 c 中*/if(isdi

14、git(c)|c=.)/*判 断 是 否 是 数 字 或 小 数 点*/(if(flag=-l)/*如 果 标 志 为 T,表 明 为 负 数*/(strcpy(str2,;/*将 负 号 连 接 到 字 符 串 中*/flag=l;/*将 标 志 值 恢 复 为 1*/sprintf(temp,%c,c);/*将 字 符 保 存 到 字 符 串 变 量 temp中*/strcat(str2,temp);/*将 temp中 的 字 符 串 连 接 到 str2中*/setfillstyle(SOLID FILL,color+3);bar(2*width+width/2,height/2,15*

15、width/2,3*height/2);outtextxy(5*width,height,str2);/*显 示 字 符 串*/if(c=,+,)(numl=atof(str2);/*将 第 一 个 操 作 数 转 换 为 浮 点 数*/strcpy(str2,;/*将 str2 清 空*/act=l;/*做 计 算 加 法 标 志 值*/setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,0.);/*显 示 字 符 串*/if

16、(c=-)(if(strcmp(str2,)=0)/*如 果 str2为 空,说 明 是 负 号,而 不 是 减 号*/flag=-l;/*设 置 负 数 标 志*/else(numl=atof(str2);/*将 第 二 个 操 作 数 转 换 为 浮 点 数*/strcpy(str2,;/*将 str2 清 空*/act=2;/*做 计 算 减 法 标 志 值*/setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);/*画 矩 形*/outtextxy(5*width,hei

17、ght,0.);/*显 示 字 符 串*/)if(c=*,)(numl=atof(str2);/*将 第 二 个 操 作 数 转 换 为 浮 点 数*/strcpy(str2,;/*将 str2 清 空*/act=3;/*做 计 算 乘 法 标 志 值*/setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,0.);/*显 示 字 符 串*/)if(c=,/)(numl=atof(str2);/*将 第 二 个 操 作 数 转 换

18、 为 浮 点 数*/strcpy(str2,;/*将 str2 清 空*/act=4;/*做 计 算 除 法 标 志 值*/setf i1Istyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*vidth,height,0.);/*显 示 字 符 串*/if(c=)numl=atof(str2);/*将 第 二 个 操 作 数 转 换 为 浮 点 数*/strcpy(str2,;/*将 str2 清 空*/act=5;/*做 计 算 乘 方 标 志 值*/setfi 1

19、 Istyle(SOLID_FILL,color+3);/*设 置 用 淡 绿 色 实 体 填 充*/bar(2*width+width/2,height/2,15*width/2,3*height/2);/*画 矩 形*/outtextxy(5*width,height,0.);/*显 示 字 符 串*/)if(c=%)(numl=atof(str2);/*将 第 二 个 操 作 数 转 换 为 浮 点 数*/strcpy(str2,;/*将 str2 清 空*/act=6;/*做 计 算 模 运 算 乘 方 标 志 值*/setfi 11 style(SOLID_FILL,color+3)

20、;/*设 置 用 淡 绿 色 实 体 填 充*/bar(2*width+width/2,height/2,15*width/2,3*height/2);/*画 矩 形*/outtextxy(5*width,height,0.);/*显 示 字 符 串*/)if(c=,=)(num2=atof(str2);/*将 第 二 个 操 作 数 转 换 为 浮 点 数*/switch(act)/*根 据 运 算 符 号 计 算*/case 1:result=numl+num2;break;/*做 加 法*/case 2:resu 1 t=num 1-num2;break;/*做 减 法*/case 3:

21、result=numl*num2;break;/*做 乘 法*/case 4:result=numl/num2;break;/*做 除 法*/case 5:result=pow(numl,num2);break;/*做 x 的 y 次 方*/case 6:result=fmod(nuinl)num2);break;/*做 模 运 算*/setfi 11 style(SOLID_FILL,color+3);/*设 置 用 淡 绿 色 实 体 填 充*/bar(2*width+width/2,height/2,15*width/2,3*height/2);/*覆 盖 结 果 区*/sprintf(

22、temp,V,result);/*将 结 果 保 存 到 temp 中*/outtextxy(5*width,height,temp);/*显 示 结 果*/)if(c=c)(numl=0;/*将 两 个 操 作 数 复 位 0,符 号 标 志 为 1*/num2=0;flag=l;strcpy(str2,;/*将 str2 清 空*/setfillstyle(SOLID_FILL,color+3);/*设 置 用 淡 绿 色 实 体 填 充*/bar(2*width+width/2,height/2,15*width/2,3*height/2);/*覆 盖 结 果 区*/outtextxy(

23、5*width,height,0.);/*显 示 字 符 串*/)if(c=Q)exit(O);/*如 果 选 择 了 q 回 车,结 束 计 算 程 序*/)put image(x,y,rar,X0R_PUT);/*在 退 出 之 前 消 去 光 标 箭 头*/return;/*返 回*/)/*窗 口 函 数*/void mwindow(char*header)int height;cleardevice();/*清 除 图 形 屏 幕*/setcolor(MaxColors-1);/*设 置 当 前 颜 色 为 白 色*/setviewport(20,20,MaxX/2,MaxY/2,1)

24、;/*设 置 视 口 大 小*/height=textheight(H);/*读 取 基 本 文 本 大 小*/settextstyle(DEFAULT_FONT,HORIZ_DIR,1);/*设 置 文 本 样 式*/settextjustify(CENTER_TEXT,TOP_TEXT);/*设 置 字 符 排 列 方 式*/outtextxy(MaxX/4,2,header);/*输 出 标 题*/setviewport(20,20+height+4,MaxX/2+4,MaxY/2+20,1);/*设 置 视 口 大 小*/drawboder();/*画 边 框*/)void drawb

25、oder(void)/*画 边 框*/|struct viewporttype vp;/*定 义 视 口 类 型 变 量*/setcolor(MaxColors-1);/*设 置 当 前 颜 色 为 白 色*/setlinestyle(SOLID_LINE,0,NORM_WIDTH);/*设 置 画 线 方 式*/getviewsettings(&vp);/*将 当 前 视 口 信 息 装 入 vp所 指 的 结 构 中*/rectangle(0,0,vp.right-vp.left,vp.bottom-vp.top);/*画 矩 形 边 框*/)/*设 计 鼠 标 图 形 函 数*/int

26、arrow()(int size;int raw=4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4;/*定 义 多 边 形 坐 标*/setfillstyle(SOLID_FILL,2);/*设 置 填 充 模 式*/fillpoly(8,raw);/*画 出 一 光 标 箭 头*/size=imagesize(4,4,16,16);/*测 试 图 象 大 小*/rar=malloc(size);/*分 配 内 存 区 域*/getimage(4,4,16,16,rar);/*存 放 光 标 箭 头 图 象*/put image(4,4,rar,XOR_PUT);/*消 去 光 标 箭 头 图 象*/return 0;)/*按 键 函 数*/int specialkey(void)(int key;while(bioskey(l)=0);/*等 待 键 盘 输 入*/key=bioskey(0);/*键 盘 输 入*/key=key&0 xff?key&Oxff:key>>8;/*只 取 特 殊 键 的 扫 描 值,其 余 为 0*/return(key);/*返 回 键 值*/)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com