C++加EasyX实现将学号转换成二进制(可刷新)

作业要求:
将学号的后四位以四个个位数的形式转换成二进制形式

#include <iostream>
#include <graphics.h>
#include <conio.h>
int cnt = 0;
class  Button
{
	 int Buttonwidth = 80;
	 int  Buttonheight = 40;
	 const char* str;
	 int x, y; 
    public:
	 Button(int x, int y, const char* str, int Buttonwidth = 80, int  Buttonheight = 40)
	 {
		  this->x = x;
		  this->y = y;
		  this->str = str;
		  this->Buttonwidth = Buttonwidth;
		  this->Buttonheight = Buttonheight;
	 }
	 //位置(x,y)处画按钮
	 void drawButton()
	 {
		  setfillcolor(WHITE);
		  fillrectangle(x, y, x + Buttonwidth, y + Buttonheight);
		  setbkmode(TRANSPARENT);
		  RECT r1 = { x,y,x + Buttonwidth,y + Buttonheight };
		  settextstyle(20, 0, _T("华文楷体"), 0, 0, 100, false, false, false);
		  settextcolor(BLACK);
		  drawtext(str, &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	 }
};
//判断点(x,y)是不是在按钮(bx,by)范围内
bool judgeButton(int x, int y, int bx, int by, int Buttonwidth = 80, int  Buttonheight = 40) 
{
	if (x >= bx && x <= bx + Buttonwidth && y >= by && y <= by + Buttonheight)
	return true;
	return false;
}


int main ()
{
	initgraph(640, 480);
	 to:
	 cleardevice();
	 //规定原点
	 if (cnt == 0)
	 {
		  setorigin(50, 240);
		  TCHAR o = _T('O');
		  outtextxy(-10, 0, o);
	 }
	 //创建一个按钮
	 Button proceed(500, 50, _T("刷新"), 80, 40);
	 proceed.drawButton();
	 MOUSEMSG msg;
	 //建设X轴
	 line(0,0,550,0);
	 POINT ptsx[] = { {550,0}, {530,10}, {530,-10} };
	 fillpolygon(ptsx, 3);
	 //建设Y轴
	 setaspectratio(1, -1);
	 line(0, -200, 0, 200);
	 POINT ptsy[] = { {0,200}, {-10,180}, {10,180} };
	 fillpolygon(ptsy, 3);
	 //输入学号
	 char s[10];
	 InputBox(s, 10, "请输入你学号的后四位");
	 int N = 0;
	 sscanf_s(s, "%d", &N);
	 //开始转换
	 int a[4];
	 int i = 0;
	 for (i = 3; i >= 0; i--)
	 {
		  a[i] = N % 10;
		  N = N / 10;
	 }
	 int t = 1;
	 for (i=0;i<4;i++)
	 {
		  //位运算,遍历每一个bit 
		  unsigned mask = 1u << 3;
		  for (; mask; mask >>= 1)
		  {
			   //判断该位上是0还是1
			   int A = (a[i] & mask ? 1 : 0);
			   char a[5];
			   sprintf_s(a,"%d", A);
			   setaspectratio(1, 1);
			   //绘制单极性不归零码
			   if (A == 1)
			   {
				    line((t - 1) * 30, 0, (t - 1) * 30, -30);
				    line((t - 1) * 30, -30, t * 30, -30);
				    line(t * 30, 0, t * 30, -30);
			    }
			   //Y轴坐标
			   char Y[5];
			   sprintf_s(Y, "%d", 1);
			   outtextxy(-10, -35, Y);
			   //输出二进制
			   settextcolor(WHITE);
			   outtextxy(t*30-15, 10, a);
			   t++;
		 }
		//下一位数字的二进之前输出分号
	 	TCHAR c=_T(';');
 		outtextxy((t*30)-35,10,c);
 	}
 	while (true) 
 	{	
		  while(MouseHit())// 当有鼠标消息的时候执行
		  {
			   msg = GetMouseMsg();
			   if (msg.uMsg == WM_LBUTTONDOWN)
			   {
				    if (judgeButton(msg.x, msg.y, 550, 290, 80, 40))//按钮坐标加原点坐标
				    {
					     cnt++;
					     goto to;
				    }
	
			   }
		  }
 	}
	//  冻结屏幕 按任意键继续 
 	system("PAUSE");
 	//关闭图形界面 
 	closegraph();
}

不足之处请不吝指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值