螺旋打印

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <bios.h>
#include <process.h>
#include <string.h>
#include <alloc.h>
#include <dos.h>

//The screen is 640*480
#define SCR_MAX_X 639
#define SCR_MAX_Y 479

//Define colors
#define NOCOLOR  -1
#define BLACK    0
#define BLUE   1
#define GREEN   2
#define CYAN   3
#define RED   4
#define MAGENTA   5
#define BROWN   6
#define LGRAY   7
#define DGRAY   8
#define LBLUE   9
#define LGREEN  10
#define LCYAN  11
#define LRED  12
#define LMAGENTA 13
#define YELLOW  14
#define WHITE  15

#define ESC 0x011b

//Public declare
enum BOOL{TRUE = 1, FALSE = 0}; //Bool
int tag[100] = {0};
int xxx = 0, yyy = 0;
int pos[11][11] = {0};

//Declare classes
class CRect         //This class is design for draw a rectangle with shadow
{
public:
 int x0,y0,x1,y1;                           //(x0,y0),(x1,y1)
 CRect(int xx0,int yy0,int xx1,int yy1)     //Get x0,y0,x1,y1
 {
  x0=xx0;
  y0=yy0;
  x1=xx1;
  y1=yy1;
 }
 //Draw a rectangle with shadow
 void Draw(int color,int lineColor=WHITE,BOOL shadow=FALSE,int deep=3);
};

//Function about class CRect
/*-----------------------------
Name:CRect::Draw(int color,int lineColor,BOOL shadow,int deep)
Function:Draw a rectangle
Input:
  int color:fillcolor
  int lineColor:linecolor
  BOOL shadow: TRUE(1):with shadow FALSE(0):noshadow
  int deep: the deep of the shadow
Output:void
-------------------------------*/
void CRect::Draw(int color,int lineColor,BOOL shadow,int deep)
{
 setfillstyle(SOLID_FILL,color);
 bar(x0,y0,x1,y1);

 if(shadow)
 {
  //Draw right shadow
  int poly[8];
  poly[0] = x1;            //1st vertex
  poly[1] = y0;
  poly[2] = x1+deep;       //2nd vertex
  poly[3] = y0+deep;
  poly[4] = x1+deep;       //3rd vertex
  poly[5] = y1+deep;
  poly[6] = x1;            //4th vertex
  poly[7] = y1;
  setcolor(DGRAY);
  setfillstyle(SOLID_FILL,DGRAY);
  fillpoly(4,poly);      //Fill the poly
  //Draw the bottom shadow
  poly[0] = x1 + deep;       //1st vertex
  poly[1] = y1 + deep;
  poly[2] = x0 + deep;       //2nd vertex
  poly[3] = y1 + deep;
  poly[4] = x0;            //3rd vertex
  poly[5] = y1;
  poly[6] = x1;            //4th vertex
  poly[7] = y1;

  setcolor(BLACK);
  setfillstyle(SOLID_FILL,BLACK);
  fillpoly(4,poly);      //Fill the poly
 }

 if (lineColor != -1)             //-1:nocolor
 {
  setcolor(lineColor);
  rectangle(x0,y0,x1,y1);
 }

}

/*-----------------------------
Name:ShowWinDlg(char ttl[],char msg1[],char msg2[])
Function:Show a windows dialog and wait for a key('y','n')
Input:
  char ttl[]:title
  char msg1[]:the first line
  char msg2[]:the second line
Output:
  1:accept a key 'y'
  0:accepy a key 'n'
-------------------------------*/
int ShowWinDlg(char ttl[],char msg1[],char msg2[])
{
 CRect rect(165,180,475,280);
 CRect rectyes(210,250,310,270);
 CRect rectno(325,250,425,270);
 CRect title(165,180,475,195);
 void *oldImage;                    //Declare a image point
 unsigned int size;                 //Recort the image size
 int key=0;                         //Reset the key

 //Calculate the size of the image
 size = imagesize(rect.x0,rect.y0,rect.x1+10,rect.y1+10);
 //Allocate memory to hold the image
 oldImage = malloc(size);
 //Grab the image
 getimage(rect.x0,rect.y0,rect.x1+10,rect.y1+10,oldImage);

 //Draw the dialog
 rect.Draw(LBLUE,WHITE,TRUE,5);
 title.Draw(WHITE,WHITE);
 rectyes.Draw(WHITE,BLACK,TRUE,3);
 rectno.Draw(WHITE,BLACK,TRUE,3);
 outtextxy(rectyes.x0+10,rectyes.y0+5,"Yes");
 outtextxy(rectno.x0+10,rectno.y0+5,"No");

 //Output the text
 setcolor(BLACK);
 settextjustify(LEFT_TEXT,TOP_TEXT);
 outtextxy(title.x0+10,title.y0+5,ttl);
 outtextxy(rect.x0+12,rect.y0+30,msg1);
 outtextxy(rect.x0+12,rect.y0+50,msg2);
 setcolor(WHITE);
 outtextxy(rect.x0+11,rect.y0+29,msg1);
 outtextxy(rect.x0+11,rect.y0+49,msg2);
 do               //Do until press y or Y or n or N
 {
  key=bioskey(0);
  if (key==5497 || key==5465)   //y or Y
  {
   //Close the dialog
   putimage(rect.x0,rect.y0,oldImage,COPY_PUT);
   // clean up
   free(oldImage);
   return 0;
  }
  else if(key==12654 || key==12622)  //n or N
  {
   //Close the dialog
   putimage(rect.x0,rect.y0,oldImage,COPY_PUT);
   // clean up
   free(oldImage);
   return 1;
  }
 }while(1);
}

/*-----------------------------
Name:WaitDlg(int nDelay,char msg[])
Function:Show a windows dialog
Input:
  int nDelay: delay some times
  char msg[]: message
Output:void
-------------------------------*/
void WaitDlg(int nDelay,char msg[])
{
 CRect rect(200,210,440,270);
 CRect title(200,210,440,225);
 void *oldImage;      //Declare a image point
 unsigned int size;                 //Recort the image size

 //Calculate the size of the image
 size = imagesize(rect.x0,rect.y0,rect.x1+10,rect.y1+10);
 //Allocate memory to hold the image
 oldImage = malloc(size);
 //Grab the image
 getimage(rect.x0,rect.y0,rect.x1+10,rect.y1+10,oldImage);

 //Draw the wait dialog
 rect.Draw(LBLUE,WHITE,TRUE,5);
 title.Draw(WHITE,WHITE);
 setcolor(BLACK);
 outtextxy(title.x0+10,title.y0+5,"Loading...   --by ReachStar");
 settextjustify(LEFT_TEXT,TOP_TEXT);
 outtextxy(rect.x0+15+1,rect.y0+30+2,msg); //Draw the shadow
 setcolor(WHITE);
 outtextxy(rect.x0+15,rect.y0+30,msg);     //Draw the msg
 
 delay(nDelay);                            //Delay some times

 //Hide the dialog
 putimage(rect.x0,rect.y0,oldImage,COPY_PUT);
 //Clean up
 free(oldImage); 
}

/*-----------------------------
Name:ShowWelcome(int nDelay,int ln,char msg[][50])
Function:Show a windows about My97
Input:void
Output:void
-------------------------------*/
void ShowWelcome(char strtitle[] = "")
{
 char msg[][30] = {"College: College of Science","         & Technology","MyClass: Computer 1,Grade 3","Name1: Li bingbing","Name2: Huang zhongzhou","Name3: Ye Junqiang","Company: ReachStar"};
 int ln = 7;
 CRect rect(200,150,440,315);
 CRect middle(200,165,440,300);
 void *oldImage;      //Declare a image point
 unsigned int size;                 //Recort the image size

 //Calculate the size of the image
 size = imagesize(rect.x0,rect.y0,rect.x1+10,rect.y1+10);
 //Allocate memory to hold the image
 oldImage = malloc(size);
 //Grab the image
 getimage(rect.x0,rect.y0,rect.x1+10,rect.y1+10,oldImage);

 //Draw the dialog
 rect.Draw(GREEN,BLACK,TRUE,5);                //Draw the GREEN rectangle
 middle.Draw(LBLUE,BLACK);                     //Draw the LBLUE rectangle
 setcolor(WHITE);
 outtextxy(rect.x0+10,rect.y0+5,"About the program");
 settextjustify(LEFT_TEXT,TOP_TEXT);
 ln--;
 for(; ln>=0; ln--)
 {
  setcolor(BLUE);
  outtextxy(rect.x0+15+2,rect.y0+25+1+ln*18,msg[ln]); //Draw the shadow
  setcolor(WHITE);
  outtextxy(rect.x0+15,rect.y0+25+ln*18,msg[ln]);     //Draw the msg
 }
 //Draw the title
 if(strtitle[0] != '/0')          //Input a title
 {
  CRect title(200,80,440,120);
  title.Draw(LBLUE,WHITE,TRUE,5);                //Draw the GREEN rectangle
  setcolor(YELLOW);
  outtextxy(title.x0+15,title.y0+15,strtitle);   //Draw the title
 }

 while (!kbhit())
 {
  setcolor(GREEN);
  outtextxy(rect.x0+15,middle.y1+5,"Press any key to continue!");
  delay(500);
  setcolor(WHITE);
  outtextxy(rect.x0+15,middle.y1+5,"Press any key to continue!");
  delay(500);
 };
 ln = bioskey(0);                   //Recieve the waste key
 //Hide the dialog
 putimage(rect.x0,rect.y0,oldImage,COPY_PUT);
 //Clean up
 free(oldImage);
}

/*-----------------------------
Name:ShowMessage(int nDelay,int ln,char msg[][50])
Function:Show a windows about My97
Input:void
Output:void
-------------------------------*/
char ShowMessage(char msg[30],int x1=200,int y1=150,int x2=440,int y2=215)
{
 int key;
 CRect rect(x1,y1,x2,y2);
 CRect middle(x1,y1+15,x2,y2-15);
 void *oldImage;      //Declare a image point
 unsigned int size;                 //Recort the image size

 //Calculate the size of the image
 size = imagesize(rect.x0,rect.y0,rect.x1+10,rect.y1+10);
 //Allocate memory to hold the image
 oldImage = malloc(size);
 //Grab the image
 getimage(rect.x0,rect.y0,rect.x1+10,rect.y1+10,oldImage);

 //Draw the dialog
 rect.Draw(GREEN,BLACK,TRUE,5);                //Draw the GREEN rectangle
 middle.Draw(LBLUE,BLACK);                     //Draw the LBLUE rectangle
 setcolor(WHITE);
 outtextxy(rect.x0+10,rect.y0+5,"About My97");
 settextjustify(LEFT_TEXT,TOP_TEXT);

 setcolor(BLUE);
 outtextxy(rect.x0+15+2,rect.y0+25+1,msg); //Draw the shadow
 setcolor(WHITE);
 outtextxy(rect.x0+15,rect.y0+25,msg);     //Draw the msg

 while(1)
 {
  key = bioskey(0);                   //Recieve the key
  if((char)key >= '0' && (char)key <='9')
  {
   //Hide the dialog
   putimage(rect.x0,rect.y0,oldImage,COPY_PUT);
   //Clean up
   free(oldImage);
   return (char)key;
  }
 }
}

/*-----------------------------
Name:DrawBackground()
Function:DrawBackground
Input:void
Output:void
-------------------------------*/
void DrawBackground()
{
 //Draw the background
 setfillstyle(LTBKSLASH_FILL,GREEN);
 bar(0,0,SCR_MAX_X,SCR_MAX_Y);

 //Draw the top
 setfillstyle(SOLID_FILL,WHITE);
 bar(0,0,SCR_MAX_X,15);
 setcolor(BLACK);
 outtextxy(5,4,"Build:2004-12-11 20:00:00  Powered by Computer 1,Grade 3  CopyRight:ReachStar");
 line(0,15,SCR_MAX_X,15);

 WaitDlg(1500,"Now loading the program...");        //Show loading...

 setcolor(BLACK);
}

void DrawPic(int startX,int startY,int x,int y,int color,int width=8,int height=8,int lineColor=LGRAY,BOOL shadow = TRUE,int deep=3)
{
 int realX,realY;
 realX = startX + x*width;
 realY = startY + y*height;
 CRect rect(realX,realY,realX+width,realY+height);
 rect.Draw(color,lineColor,shadow,5);
}

void DrawM1(int direction,int x,int y,int width=8,int height=8,int color=GREEN,int lineColor=LGRAY,BOOL shadow = TRUE,int deep=3)
{
 if(direction==1)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,0,2,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,2,2,color);
 }
 else if(direction==2)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,0,2,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,1,3,color);
 }
 else if(direction==3)
 {
  DrawPic(x,y,0,1,color);
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,2,1,color);
  DrawPic(x,y,1,2,color);
 }
 else if(direction==4)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,1,3,color);
  DrawPic(x,y,2,2,color);
 }
}

void DrawM2(int direction,int x,int y,int width=8,int height=8,int color=BLUE,int lineColor=LGRAY,BOOL shadow = TRUE,int deep=3)
{
 if(direction==1)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,1,3,color);
  DrawPic(x,y,2,3,color);
 }
 else if(direction==2)
 {
  DrawPic(x,y,2,1,color);
  DrawPic(x,y,0,2,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,2,2,color);
 }
 else if(direction==3)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,2,1,color);
  DrawPic(x,y,2,2,color);
  DrawPic(x,y,2,3,color);
 }
 else if(direction==4)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,2,2,color);
  DrawPic(x,y,3,2,color);
 }
}

void DrawM3(int direction,int x,int y,int width=8,int height=8,int color=YELLOW,int lineColor=LGRAY,BOOL shadow = TRUE,int deep=3)
{
 if(direction==1 || direction==3)
 {
  DrawPic(x,y,1,0,color);
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,1,3,color);
 }
 else if(direction==2 || direction==4)
 {
  DrawPic(x,y,0,2,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,2,2,color);
  DrawPic(x,y,3,2,color);
 }
}

void DrawM4(int direction,int x,int y,int width=8,int height=8,int color=MAGENTA,int lineColor=LGRAY,BOOL shadow = TRUE,int deep=3)
{
 if(direction==1 || direction==3)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,1,2,color);
  DrawPic(x,y,2,2,color);
  DrawPic(x,y,2,3,color);
 }
 else if(direction==2 || direction==4)
 {
  DrawPic(x,y,1,1,color);
  DrawPic(x,y,2,1,color);
  DrawPic(x,y,0,2,color);
  DrawPic(x,y,1,2,color);
 }
}

void DrawImagesXY(int imgType,int imgDirection,int CellX,int CellY,int width=8,int height=8,int color=MAGENTA,int lineColor=LGRAY,BOOL shadow = TRUE,int deep=3)
{
 int x,y;
 x = 120 + 40*CellX;
 y = 40 + 40*CellY;
 if(imgType==1)
 {
  DrawM1(imgDirection,x,y);
 }
 else if(imgType==2)
 {
  DrawM2(imgDirection,x,y);
 }
 else if(imgType==3)
 {
  DrawM3(imgDirection,x,y);
 }
 else if(imgType==4)
 {
  DrawM4(imgDirection,x,y);
 }
}

//算出拐點,放入tag數組中
void Fun(int inputNum)
{
 int p=0;
 for(int i=1; i<inputNum; i++)
 {
  tag[p] = i*i;
  tag[++p] = i*(i+1);
  p++;
 }
 //tap[p] = -1;//-1表示拐點結束
}
//Init the array
void Get()
{
 int client = 2;
 int i=4, j=5, m, n1;


 while(client <= 100)
 {
  while(1)
  {
   pos[i][j] = client++;
   if(pos[i][j-1] == 0)
    break;
   i--;
  }
  j--;
  while(1)
  {
   pos[i][j] = client++;
   if(pos[i+1][j] == 0)
    break;
   j--;
  }
  i++;
  while(1)
  {
   pos[i][j] = client++;
   if(pos[i][j+1] == 0)
    break;
   i++;
  }
  j++;
  while(1)
  {
   pos[i][j] = client++;
   if(pos[i-1][j] == 0)
    break;
   j++;
  }
  i--;
 }
 for(m=0; m < 10; m++)
 {
  for(n1 = 0; n1 < 10; n1++)
  {
   printf("%2d ",pos[m][n1]);
  }
  printf("/n");
 }

}
/*-----------------------------------
function:found the (yyy,xxx) from n
input n (1 - 100)
output (yyy,xxx)
-----------------------------------*/
void GetPos(int n)
{
 int m , n1;
 for(m=0; m < 10; m++)
 {
  for(n1 = 0; n1 < 10; n1++)
  {
   if( pos[m][n1] == n)
   {
    xxx = m;
    yyy = n1;
   }
  }
 }
}

void main()
{
 int inputNum=10;
 int i=0,key;
 int x,y;
 x=0;
 y=0;
 pos[5][5] = 1;
 Get();
 int j=0;//表示指到數組的哪個位置
 Fun(inputNum);
 // Request auto detection
 int gdriver = DETECT, gmode, errorcode;
 // Initialize graphics mode
 initgraph(&gdriver, &gmode, "");
 // Read result of initialization
 errorcode = graphresult();
 if (errorcode != grOk)  // An error occurred
 {
  printf("Graphics error: %s/n", grapherrormsg(errorcode));
  printf("Press any key to halt:");
  getch();
  exit(1);             // Return with error code
 }
 else   //No error
 {
  ShowWelcome("Welcome to our program!");//Show about My97 when program start
  do
  {
   if(key == ESC)
    if(ShowWinDlg("RESET THE PROGRAM?","Do you want to reset the program?","Press 'y' to reset,press 'n' to quit!")==1)
     exit(0);
   DrawBackground();
   inputNum = (int)ShowMessage("Please input a number(0-9)?")-'0';
   if(inputNum == 0)
    inputNum = 10;
   //draw the first image
   DrawImagesXY(1,1,5,5);
   //draw other images
   for(i = 1; i < inputNum * inputNum; i++)
   {
    GetPos(i+1);
    //bool found = false;
    if(i == tag[j])
    {
    // found = true;
     j++;
     y++;
    }
    x = i % 4;
    y = y % 4;
    /*---------------------
    x+1  image type
    y+1  image direction
    yyy,xxx image coordinates
    ----------------------*/
    delay(300);
    DrawImagesXY(x+1,y+1,yyy,xxx);
   }
   WaitDlg(1000,"Draw images compeleted!");        //Show loading...
  }while(key = bioskey(0));//End do while
 }   //End if errorcode
 // Clean up
 getch();
 closegraph();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章