Thursday 6 June 2013

Somewhat addicted. Changes to the previous program.

I've been fiddling around with the previous program (The one that draws the lines). I'll try to write a complete program out of it that will do some semi-useful stuff.

It is turning into a big program and I know for certain that this program is in no way the best way to write it, ah but what the hell, I'm still a newb.

The screen size is set at line 39 (system("mode 158,77");). It fits my square screen. I don't think it should be changed, because that would mess the program up badly. :(     But I tried it on my Laptop and all it needs is a little scrolling on my part as I use it.

Only the graph menu option for WEEK works for now, and it still needs work. I have left in the comments I made to myself regarding the bugs I need to fix & other stuff I need to remember.

It's killing me that there probably are easy fixes for these but I don't know them. Well, live and learn.

I have added another function to <myconstdwin.h> and renamed it. I'll post it after I have posted this program. The new function will allow for moving the DOS window around the screen, besides that it also allows limited resizing of the window. [EDIT: Here It Is]

These ASCII drawings may look lame and amateurish, but they give me the satisfaction of seeing them being drawn on the screen. It's something I wanted to do when I was a teen, let me satisfy that urge.

Working on this, I have also learnt to detect the Function keys and others usually undetectable with the 'getc' family of functions.

When will I learn how to save data to disk? =[

As always: Copy paste into Code::Blocks and hit F9.

#include <myconstdwin.h> //contains the color, gotoxy and move window functions includes windows, conio.h & stdio.h
#include <time.h>
#include <ctype.h>

#define TRUE 1
#define FALSE !TRUE

void movewin(int x, int y); //Move the DOS Window
void tab(void);
void backspace(void);
void entr(void);
void drawbox(void);
void menuoptionsmain(void);
void menuoptionsgraph(char mop1[25],char mop2[25],char mop3[25]);
void fillbox2(void);
void TimeNDate(void);
void helpmain(void);
int get_key();

void graph(void);
void drawmesh(void);
void weekgraph(void);
void yeargraph(void);

void erase_box1(void);
void erase_box3(int b); //The int that is passed is used as gotoxy(x,passed Int), so that it doesnt have to backspace
                        //the blank lines. It was much slower if it backspaces the whole box.
void erase_box4(void);

int state;

/*horizontal characters 157 without lines*/
/*gotoxy(horizontal,vertical)*/
/*SPACE FROM THE TOP OF THE BOTTOM BOX TO ITS BOTTOM IS 63*/

main()
{
    movewin(1,1);                      //Move Dos Window to top left
    system("mode 158,77");             //We use this to resize the window
    drawbox();                         //Draw the lines
    menuoptionsmain();                 //Print menu options
    fillbox2();
    state=TRUE;
    while(state)
    {
        int key;
        gotoxy(1,12);
        key=get_key();
        if(key==27) //Esc Key
        {
            state=FALSE;
        }
        else if(key==315)   //F1 Key
        {
            helpmain();
        }

        else if(key==321)   //F7
        {
            graph();
            menuoptionsmain();                 //Print menu options
        }
    }
}

void drawbox(void)
{
    system("cls");
    /*This part of the code draws the lines*/
    int x;
    SetColorAndBackground(8,0);

    for(x=0;x<75;x++)   //Middle verticle line
    {
        gotoxy(80,x);
        printf("%c",186);
    }

    for(x=1;x<157;x++)      //outside middle horizontal line
    {
        gotoxy(x,11);
        printf("%c",205); //196
    }

    for(x=1;x<80;x++)   //First half of Outside top line
    {
        gotoxy(x,0);
        printf("%c",205); //196
    }


    gotoxy(80,0);
    printf("%c",203);   //Middle joint of outside top.

    for(x=81;x<157;x++)   //Second half of Outside top line
    {
        gotoxy(x,0);
        printf("%c",205); //196
    }

    gotoxy(157,0);  //Top outside right joint
    printf("%c",187);

    for(x=1;x<75;x++)
    {
        gotoxy(0,x);
        printf("%c",186); //186,
    }

    gotoxy(0,75); //Outside left verticle bottom Joint
    printf("%c",200);

    for(x=1;x<80;x++)   //First half of Horizontal bottom
    {
        gotoxy(x,75);
        printf("%c",205);
    }

    for(x=1;x<75;x++)   //Left outside verticle line
    {
        gotoxy(157,x);
        printf("%c",186);
    }

    gotoxy(80,11);
    printf("%c",206);   //Middle line middle joint.

    gotoxy(0,0);
    printf("%c",201);   //Home joint

    gotoxy(0,11);       //left verticle line middle joint
    printf("%c",204);

    gotoxy(157,11);     //right verticle line middle joint
    printf("%c",185);

    gotoxy(80,75);      //Bottom horizontal line, middle joint.
    printf("%c",202);

    for(x=81;x<157;x++)   //Second half of Horizontal bottom
    {
        gotoxy(x,75);
        printf("%c",205); //196
    }

    gotoxy(157,75);     //Bottom horizontal right joint.
    printf("%c",188);

    /*The Outside lines are done. Now to draw the inside boxes*/

    gotoxy(1,1);    //Inside Top left joint. Left Box
    printf("%c",201);

    for(x=2;x<79;x++)   //First half Inside top Horizontal line. Left Box
    {
        gotoxy(x,1);
        printf("%c",205);
    }

    gotoxy(79,1);
    printf("%c",187);   //Inside top right joint. Left Box

    gotoxy(81,1);    //Inside Top left joint. Second box
    printf("%c",201);

    for(x=82;x<156;x++)   //Inside top Horizontal line. Right Box
    {
        gotoxy(x,1);
        printf("%c",205);
    }

    gotoxy(156,1);      //Inside Top right joint (2nd box)
    printf("%c",187);

    for(x=2;x<10;x++)   //first box left vertical inside
    {
        gotoxy(1,x);
        printf("%c",186);
    }

    gotoxy(1,10);       //first box, bottom left joint
    printf("%c",200);

    for(x=2;x<79;x++)       //first box bottom hori inside
    {
        gotoxy(x,10);
        printf("%c",205);
    }

    gotoxy(79,10);        //First box, bottom right joint.
    printf("%c",188);

    for(x=2;x<10;x++)   //first box right vertical inside
    {
        gotoxy(79,x);
        printf("%c",186);
    }

    for(x=2;x<10;x++)   //Second box left vertical inside
    {
        gotoxy(81,x);
        printf("%c",186);
    }

    gotoxy(81,10);      //Second inside box. Left bottom joint
    printf("%c",200);

    for(x=2;x<10;x++)   //Second box Right vertical inside
    {
        gotoxy(156,x);
        printf("%c",186);
    }

    for(x=82;x<156;x++)       //Second box bottom hori inside
    {
        gotoxy(x,10);
        printf("%c",205);
    }

    gotoxy(156,10);         //Second box, bottom right joint. (inside)
    printf("%c",188);
}

void menuoptionsmain(void)  //Display Menu Options
{
    erase_box1();
    gotoxy(2,2);
    printf("Esc\t-\tExit Program");
    gotoxy(2,3);
    printf("F1\t-\tHelp");
    gotoxy(2,4);
    printf("F2\t-\tOpen Records");
    gotoxy(2,5);
    printf("F3\t-\tSearch");
    gotoxy(2,6);
    printf("F4\t-\tSave");
    gotoxy(2,7);
    printf("F5\t-\tUsers");
    gotoxy(2,8);
    printf("F6\t-\tBackup");
    gotoxy(2,9);
    printf("F7\t-\tBar Graph");
}

/*Complete This Function*/
void menuoptionsgraph(char mop1[25],char mop2[25],char mop3[25])
{
    erase_box1();
    gotoxy(2,2);
    printf("%s",mop1);
    gotoxy(2,3);
    printf("%s",mop2);
    gotoxy(2,4);
    printf("%s",mop3);
}

void fillbox2(void)
{
    int w=0,x,y=2,z;
    while (w<8)
    {
        for(x=0;x<74;x++)
        {
            gotoxy(x+82,y);
            printf("%c",178);
        }
        w++;
        y++;
    }
    SetColorAndBackground(8,0);
    gotoxy(110,3);              //This Displays the logo
    printf(" %c  %c%c%c %c   %c",220,220,220,220,220,220);
    gotoxy(110,4);
    printf("%c %c  %c  %c%c %c%c",219,219,219,219,219,219,219);
    gotoxy(110,5);
    printf("%c%c%c  %c  %c %c %c",219,220,219,219,219,219,219);
    gotoxy(110,6);
    printf("%c %c %c%c%c %c   %c",219,219,220,219,220,219,219);
    //gotoxy(83,3);
    //printf("Accounts & Inventory"); 
    //gotoxy(88,4);
    //printf("Management.");
    //gotoxy(104,4);
    //printf(" Shop Name ");
    SetColorAndBackground(8,0);
    gotoxy(85,8);
    TimeNDate();
    gotoxy(109,8);
    /*Backspace out the time & replace with ASCII 178 because it is static. We only want the date.*/
    printf("\b\b\b\b\b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b%c%c%c%c%c%c%c%c%c",178,178,178,178,178,178,178,178,178);
}

void TimeNDate(void) //Displays time & date when needed.
{
time_t t;
time(&t);
printf("%s",ctime(&t));
}

void helpmain(void)     //Main menu help.
{
    int key;
    gotoxy(1,12);
    printf("Shop Name. "); 
    printf("Accounts & Inventory Management (AIM).");
    gotoxy(1,14);
    printf("Press one of the keys in the menu for");
    gotoxy(1,15);
    printf("a discription.");
    key=get_key();
    erase_box3(15);
    if(key==315)
    {
        gotoxy(1,12);
        printf("This displays help for any area of the");
        gotoxy(1,13);
        printf("program you may be in. It will attempt");
        gotoxy(1,14);
        printf("to explain what you can do on the");
        gotoxy(1,15);
        printf("current screen.");
        gotoxy(1,22);
        printf("Press any key....");
        getch();
        erase_box3(22);
    }
}

int get_key()   //reads key and returns an appropriate value including function keys.
{
    int c = getch();
    switch (c)
    {
      case 0:   return getch()+256;
      case 224: return getch()+512;
    }
    return c;
}

void graph(void)    //Draws graph.
{
    char c;

    menuoptionsgraph("B\t-\tBack","W\t-\tGraph For A Week","Y\t-\tGraph For A Year");
    gotoxy(1,12);
    printf("AIM only provides the ability to create bar graphs for a Week (by day) or Year");
    gotoxy(1,13);
    printf("(By Month).");
    gotoxy(1,14);
    printf("The graphs display an approximate & should not be considered completely");
    gotoxy(1,15);
    printf("accurate.");
    gotoxy(1,17);
    printf("Would you like a Week or Year chart?(W/Y/Back):");
    for(;;)
    {
       c=toupper(getch());
       if(c=='Y')
       {
           erase_box3(17);
            yeargraph();
            break;
       }

       else if(c=='W')
       {
           erase_box3(17);
           weekgraph();
           break;
       }

       else if(c=='B')
       {
           erase_box3(17);
           break;
       }
    }

}

void weekgraph(void)
{
    int a[7],h[7],i,j,k=0;
    int x,y;
    float temp,b[7];
    char day[7] [10]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

    /*Using 'day1' to properly indent the sales the second time it is used when the graph is displayed
     since I cannot seem to reassign the values to 'char day' once i've declared it*/
    char day1[7] [10]={"Monday\t","Tuesday","Wednesday","Thursday","Friday\t","Saturday","Sunday\t"};



    /*Need to figure out 1. If user presses enter key without entering a value*/
    menuoptionsgraph("Enter Values"," "," ");
    for(x=0;x<7;x++)
    {
        gotoxy(1,12+x);
        printf("Sale %s (0 for closed): ",day[x]);
        scanf(" %i",&a[x]);
        h[x]=a[x];
    }
    erase_box3(26);

    /*Display Sales by day*/
    for(x=0;x<7;x++)
    {
        gotoxy(1,12+x);
        printf("%s\t-%6i",day1[x],h[x]);
    }
    getch();

    /*Sort the input in ascending order & store it in a[x]*/
    gotoxy(1,12);
    for (i=0;i<7;i++)
        for(j=i+1;j<7;j++)
        {
            if(a[i]>a[j])
            {
                temp=a[j];
                a[j]=a[i];
                a[i]=temp;
            }
        }

        /*a[x] now contains the numbers in ascending order.
          h[x] has the input in the original order.*/

          /*Divide the largest number (contained in a[6]) by 100
            store the result (1% of largest number), store it in a float variable (temp)
            and divide all the input by temp. Assign the multiple to b[x].
            2% will add one character of growth for the bar
            1 to 100 is assigned by percentages.*/

    temp=(float)a[6]/100;  //Divide largest number by 100, store in temp.

    for(x=0;x<7;x++)    //Divide all the input by 1% of the largest number & store in b[x] array
    {
        b[x]=(float)h[x]/temp;
        //b[x]=(int)b[x];
    }


    /*Start drawing the graph*/

    drawmesh(); //Draws a mesh where the graph will be drawn. with percentages printed to left

    gotoxy(87,74);
    printf("Monday    Tuesday  Wednesday  Thursday   Friday   Saturday  Sunday"); //Bottom of Graph

    gotoxy(82,73); //Line for dividing the day names and the bars begins here
    for(x=0;x<74;x++)
    {
        printf("%c",196);  //Displays the bottom line for the graph.
    }
/*


*/
    SetColorAndBackground(12,0); //red
    y=72;
    for(x=0;x<b[0];x+=2)        //Monday Draws the bar (2 lines and 2 spaces) of ASCII 219 in red)
    {
        for(temp=0;temp<1000000;temp++); //Provide a slight pause between drawing
        gotoxy(89,y);                      //First character
        printf("%c",219);
        gotoxy(90,y);                   //Second character
        printf("%c",219);
        y--;                            //decrement y (verticle space)
    }

    SetColorAndBackground(15,0);    //Bright White for the input value
    gotoxy(88,21);                  //Display the amount entered on top of the bar
    printf("%4i",h[0]);

    /*repeat the above 6 more times*/

    SetColorAndBackground(12,0);
    y=72;
    for(x=0;x<b[1];x+=2)        //Tuesday
    {
        for(temp=0;temp<1000000;temp++);
        gotoxy(100,y);
        printf("%c",219);
        gotoxy(101,y);
        printf("%c",219);
        y--;
    }

    SetColorAndBackground(15,0);
    gotoxy(99,21);
    printf("%4i",h[1]);

    SetColorAndBackground(12,0);
    y=72;
    for(x=0;x<b[2];x+=2)        //Wednesday
    {
        for(temp=0;temp<1000000;temp++);
        gotoxy(111,y);
        printf("%c",219);
        gotoxy(112,y);
        printf("%c",219);
        y--;
    }

    SetColorAndBackground(15,0);
    gotoxy(110,21);
    printf("%4i",h[2]);

    SetColorAndBackground(12,0);
    y=72;
    for(x=0;x<b[3];x+=2)        //Thursday
    {
        for(temp=0;temp<1000000;temp++);
        gotoxy(121,y);
        printf("%c",219);
        gotoxy(122,y);
        printf("%c",219);
        y--;
    }

    SetColorAndBackground(15,0);
    gotoxy(120,21);
    printf("%4i",h[3]);

    SetColorAndBackground(12,0);
    y=72;
    for(x=0;x<b[4];x+=2)        //Friday
    {
        for(temp=0;temp<1000000;temp++);
        gotoxy(131,y);
        printf("%c",219);
        gotoxy(132,y);
        printf("%c",219);
        y--;
    }

    SetColorAndBackground(15,0);
    gotoxy(130,21);
    printf("%4i",h[4]);

    SetColorAndBackground(12,0);
    y=72;
    for(x=0;x<b[5];x+=2)        //Saturday
    {
        for(temp=0;temp<1000000;temp++);
        gotoxy(141,y);
        printf("%c",219);
        gotoxy(142,y);
        printf("%c",219);
        y--;
    }

    SetColorAndBackground(15,0);
    gotoxy(140,21);
    printf("%4i",h[5]);

    SetColorAndBackground(12,0);
    y=72;
    for(x=0;x<b[6];x+=2)        //Sunday
    {
        for(temp=0;temp<1000000;temp++);
        gotoxy(151,y);
        printf("%c",219);
        gotoxy(152,y);
        printf("%c",219);
        y--;
    }

    SetColorAndBackground(15,0);
    gotoxy(150,21);
    printf("%4i",h[6]);
    SetColorAndBackground(8,0); //back to grey

    /*Find & display total earned in the week*/
    k=0;
    for(x=0;x<7;x++)
    {
        k=k+h[x];
    }
    gotoxy(1,19);
    printf("______________________");
    gotoxy(11,20);
    printf("Total-%6i",k);
    gotoxy(23,20); //Move cursor to last digit after total.
    menuoptionsgraph("Any Key\t-\tMain Menu","F1\t\t-\tHelp","F4\t\t-\tSave");
    getch();
    erase_box3(26);
    erase_box4();
    gotoxy(1,12);
}

void yeargraph(void)
{
    gotoxy(1,12);
    printf("Yeargraph Code here.");
    getch();
    erase_box3(12);
}

void erase_box1(void)
{
    int x,y=9;
    while(y>1)
    {
        for(x=3;x<80;x++)
        {
            gotoxy(x,y);
            printf("\b \b");
        }
        y--;
    }
}

void erase_box3(int b)
{
    int x,y=b,z=b;
    while(y>11)
    {
        gotoxy(79,y);
        for(x=1;x<79;x++)
        {
            printf("\b \b");
        }
        y--;
    }
}

void erase_box4(void)
{
    int x,y=74,z=0;
    while(y>11)
    {
        gotoxy(156,y);
        for(x=0;x<75;x++)
        {
            printf("\b \b");
        }
        y--;
    }
}

/*Move Window*/
void movewin(int x, int y)
{
    HWND hWnd=GetConsoleWindowNT();
    MoveWindow(hWnd,x,y,800,400,TRUE); //x,y moves screen. 800,400 takes the screen max size cannot make it
                                       //larger using this we have to use the 'system x,y' DOS command
}

/*Draws the mesh behind the graph*/
void drawmesh(void)
{
    int w,x,y,z=22;

    /*This part puts the percentage to the left of the mesh*/
    y=72;
    for(x=0;x<102;x+=10)
    {
        gotoxy(81,y);
        printf("%3i%%",x);
        y-=5;
    }


    gotoxy(86,z);
    printf("%c",218);
    for(w=0;w<16;w++)
    {
        for(x=0;x<3;x++)
        {
            printf("%c",196);

        }
        printf("%c",194);
    }
    for(x=0;x<3;x++)
        {
            printf("%c",196);

        }
    printf("%c",191);


    for(y=0;y<25;y++)
    {
        z++;
    gotoxy(86,z);
    printf("%c",179);

    for(w=0;w<16;w++)
    {
        for(x=0;x<3;x++)
        {
            printf("%c",255);

        }
        printf("%c",179);
    }
    for(x=0;x<3;x++)
        {
            printf("%c",255);

        }
    printf("%c",179);


        z++;
    gotoxy(86,z);
    printf("%c",195);

    for(w=0;w<16;w++)
    {
        for(x=0;x<3;x++)
        {
            printf("%c",196);

        }
        printf("%c",197);
    }
    for(x=0;x<3;x++)
        {
            printf("%c",196);

        }
    printf("%c",180);
    }
}

No comments:

Post a Comment