计算器程序遇到困难就帮忙……衷心感谢
C_9 2013-12-12 11:43:59
/*求各位帮帮忙啊,挺急的,我就是写好了各函数部分但不知怎么连接到一起去*/
/*我的思路是想input(),output()函数把第一个要计算的数存放到deshu1里
点击加减乘除后,再通过这俩函数获得第二个要计算的数放到deshu2里,然后把他俩计算后放到deshu3里输出*/
#include <graphics.h> //图形库函数
#include <stdio.h> //调用sprintf时需要的头文件
#include <math.h> //计算时需要的头文件
void Set_table(); //绘制方格
void Clear_device(int x,int y);//清屏
void Display(); //数字的显示区域 (显示屏)
void Key_press(); // 计算器按键
int input(int x,int y); //点击按键输入数字
void output(int x); //输出数字
int Count_type(int x,int y); //计算类型(加减乘除)
void Result(int x); //计算的结果
char q[17]; //存放点击的字符
int weishu; //小数点后面的数
int f=0; //点击计算类型后,f为真
int flag=0;
int point=0; //小数点
long double deshu=0,deshu1=0,deshu2=0,deshu3=0;
//void output(int x);//定义显示内部输出数字的函数
void jisuan(int x);//定义计算的函数
int main()
{
HWND hWnd;//获取窗口句柄
//设置界面
int curlinestyle,curlinethick;
WORD curlinepat;
initgraph(420,560);
setbkcolor(BLACK);
cleardevice();
outtextxy(130, 540, _T("calculator"));
hWnd = GetHWnd();
SetWindowText(hWnd, "计算器");
setlinestyle(PS_SOLID,2);
Set_table();
Display();//显示屏
Key_press();//按键
MOUSEMSG m;
while(1)
{
m = GetMouseMsg();
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
deshu1=input(m.x,m.y);
Count_type(m.x,m.y);
// 点击计算类型后就清除屏幕原有数据
if(f)
{
setfillcolor(BLACK);
fillrectangle(2,2,398,60);
deshu1=0;
point=0;
weishu=0;
flag=0;
// 清屏后继续输入
deshu2= input(m.x,m.y);
}
if((m.x>10 && m.y>300) && (m.x<110 && m.y<370))
{ flag=1;
Result(f);
}
Clear_device(m.x,m.y);
}
}
return 0;
}
void Display()
{
setcolor(RED);
rectangle(2,2,398,60);
}
void Set_table()
{
setcolor(RED);
for(int i=0;i<=420;i+=70)
line(10,90+i,410,90+i);
for(i=0;i<=400;i+=100)
line(10+i,90,10+i,510);
}
void output(int x)
{
if(point)
{
weishu=1+weishu;
deshu=deshu+pow(0.1,(weishu-1))*x;
sprintf(q,"%lf",deshu);
outtextxy(10,10,q);
}
else
{
weishu=weishu+1;
deshu=deshu*10+x;
sprintf(q,"%lf",deshu);
outtextxy(10,10,q);
}
}
/*点击数字在屏幕上输出**/
int input(int x,int y)
{
if((x>10 && y>90) && (x<110 && y<160)) //确定1的位置
{
output(1); //输出1
}
else if((x>110&& y>90) && (x<210 && y<160)) //确定2的位置
{
output(2);
}
else if((x>210 && y>90) && (x<310 && y<160))//确定3的位置
{
output(3);
}
else if((x>310 && y>90) && (x<410 && y<160))//确定0的位置
{
output(0);
}
else if((x>10 && y>160) && (x<110&& y<230))//确定4的位置
{
output(4);
}
else if((x>110 && y>160) && (x<210 && y<230))//确定5的位置
{
output(5);
}
else if((x>210 && y>160) && (x<310 && y<230))//确定6的位置
{
output(6);
}
else if((x>10 && y>230) && (x<110 &&y<300))//确定7的位置
{
output(7);
}
else if((x>110 && y>230) && (x<210 && y<300))//确定8的位置
{
output(8);
}
else if((x>210 && y>230) && (x<300 && y<310))//确定9的位置
{
output(9);
}
else if((x>310 && y>160) && (x<410 && y<230))//确定小数点的位置
{
point=1;//显示小数点
}
if(flag)
deshu=atof(q);
return deshu;
}
/*确定计算类型*/
int Count_type(int x,int y)
{
if((x>10 && y>300) && (x<110 && y<370))
{
f= 1;
}
else if((x>110 && y>300) && (x<210 && y<370))
{
f=2 ;
}
else if((x>210 && y>300) && (x<310 && y<370))
{
f=3;
}
else if((x>310 && y>300) && (x<410 && y<370))
{
f=4;
}
if(f)
flag=1;
else
flag=0;
return f;
}
void Result(int x)
{
switch(f)
{
case 1:
deshu3=deshu1+deshu2;
break;
case 2:
deshu3=deshu1-deshu2;
break;
case 3:
deshu3=deshu1*deshu2;
break;
case 4:
deshu3=deshu1/deshu2;
break;
}
setfillcolor(BLACK);
fillrectangle(2,2,398,60);
sprintf(q,"%lf",deshu3);
outtextxy(10,10,q);
f=0;
}
void Key_press(void)
{
settextstyle(36, 14, _T("Fixedsys"));
// 第 0 行
outtextxy(20, 100, ' 1' );
outtextxy(120, 100, ' 2' );
outtextxy(220, 100, ' 3' );
outtextxy(320, 100, ' 0' );
// 第 1 行
outtextxy(20, 170, ' 4' );
outtextxy(120, 170, ' 5' );
outtextxy(220, 170, ' 6' );
outtextxy(320, 170, ' .');
// 第 2 行
outtextxy(20, 240, ' 7' );
outtextxy(120, 240, ' 8' );
outtextxy(220, 240, ' 9' );
outtextxy(320, 240, ' %' );
// 第 3 行
outtextxy(20, 310, ' +');
outtextxy(120, 310, ' -');
outtextxy(220, 310, ' *');
outtextxy(320, 310, ' /');
// 第 4 行
outtextxy(20, 380, _T(" +/-"));
outtextxy(120, 380, _T(" rec"));
outtextxy(220, 380, _T(" x^2"));
outtextxy(320, 380, _T(" exp"));
// 第 5 行
outtextxy(20, 450, _T(" sqrt"));
outtextxy(120, 450, _T(" <--"));
outtextxy(220, 450, _T("Clear"));
//第 6 行
outtextxy(320, 450, _T(" ="));
}
/*清屏函数**/
void Clear_device(int x,int y)
{
if((x>210 && y>440) && (x<310 && y<510))
{
setfillcolor(BLACK);
deshu =0;
deshu1=0;
deshu2=0;
deshu3=0;
weishu=0;
point=0;
f=0;
fillrectangle(2,2,398,60);
}
}