Archive for the ‘C/C++ Help’ Category

CPP Project for Chess Game

This is a very basic 2 player chess program. This is an easy to use package, it is limited in it’s uses. This program has some inherant limiatations. First, since only jpeg support is available, I had to make twice as many images (since jpeg format does not support transparent pixels). This program will not detect check or checkmate as I did not have time to implement these features. As you will see, there is no undo feature. This could be somewhat easily implemented. The largest error I made was with the way pieces are stored in memory. They reside within a 8 x 8 matrix but have a wrong orientation. This makes it extremely confusing to calculate the ammount of pieces between 2 positions diagonally.
Please read the Orientation.txt file for more information on this problem.
Lastly, I have implemented a feature that blacks the screen when the window does not have focus. This is part of a somewhat complex system to reduce flickering graphics. When left unchecked, the graphics will flicker at annoying rates.

Please download this game and enjoy…

Download Chess_Game

Game project in C++

This project is based on game connect. It is single player game. The code is very simple. Please follow the instruction to run this game properly..

it hase two files : COMM.h and Game.cpp

COMM.h should be place in include directory to make this code work. After copying just compile this and enjoy the game…

The keypads are as follows:
-the LEFT AND RIGHT arrows to move on the board;
-and the ENTER key to launch a CHIP;

COMM.H

#define ENTER 13
#define SPACE 32
#define BACK 8
#define ESC 0×1b
#define UP 0×48
#define DOWN 0×50
#define LEFT 0×4b
#define RIGHT 0×4d
#define F1 0×3b
#define F2 0×3c
#define F3 0×3d
#define F4 0×3e
#define F5 0×3f
#define F6 0×40
#define F7 0×41
#define F8 0×42
#define F9 0×43
#define F10 0×44

GAME.CPP

#include <iostream.h>
#include <graphics.h>
#include <stdlib.h>0
#include <conio.h>
#include <comm.h>
#include <dos.h>
#define DEL 15

int a[20][10],xcerc=1,ycerc=0,ray=20,player=1;

void init()
{
int graphdriver =DETECT, graphmode;
initgraph(&graphdriver, &graphmode, “..\\bgi”);
}

void intro()
{
cleardevice();
setcolor(2);
settextstyle(1,0,9);
outtextxy(75,125,”CONNECT 4″);
getch();
cleardevice();
}

void drawround(int x, int y, int ray, int col)
{
setcolor(col);
setfillstyle(1,col);
fillellipse((x-1)*50+45,(y-1)*50+90,ray,ray);
}

void drawrect()
{
setcolor(2);
setfillstyle(1,1);
rectangle(10,1,635,470);
floodfill(15,60,2);
}

void slide(int f,int m,int dir,int h)
{
if(dir==1)
for(int i=((f-1)*50+45);i<((m-1)*50+45);i+=5)
{
setcolor(1);
setfillstyle(1,1);
fillellipse(i,40,ray,ray);
setcolor(h);
setfillstyle(1,h);
fillellipse(i+5,40,ray,ray);
delay(DEL);
}
if(dir==2)
for(i=((f-1)*50+45);i>((m-1)*50+45);i-=5)
{
setcolor(1);
setfillstyle(1,1);
fillellipse(i,40,ray,ray);
setcolor(h);
setfillstyle(1,h);
fillellipse(i-5,40,ray,ray);
delay(DEL);
}
}

void animate()
{
for(int ycerc=0;ycerc<=6;ycerc++)
{
if(a[xcerc][ycerc+2]!=0)
{
if(player==1) a[xcerc][ycerc+1]=1;
if(player==2) a[xcerc][ycerc+1]=5;
return;
}
if(ycerc==0)
drawround(xcerc,ycerc,ray,1);
else
drawround(xcerc,ycerc,ray,0);
if(player==1) { drawround(xcerc,ycerc+1,ray,14); delay(35); }
if(player==2) { drawround(xcerc,ycerc+1,ray,2);  delay(35); }
}
if(player==1) a[xcerc][ycerc+1]=1;
if(player==2) a[xcerc][ycerc+1]=5;
}

inline void aux1()
{
delay(2000);
cleardevice();
setcolor(14);
settextstyle(1,0,7);
outtextxy(30,150,”YELLOW Chip WINS!!!”);
getch();
exit(0);
}

inline void aux2()
{
delay(2000);
cleardevice();
setcolor(2);
settextstyle(1,0,7);
outtextxy(30,150,”GREEN Chip WINS!!!”);
getch();
exit(0);
}

inline void aux3()
{
cleardevice();
setcolor(9);
settextstyle(1,0,11);
outtextxy(200,125,”TIE!!!”);
getch();
exit(0);
}

void win()
{
int s=0;
for(int i=1;i<=12;i++)
for(int j=1;j<=8;j++)
{
s+=a[i][j];
if(a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j]==4) aux1();
if(a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j]==20) aux2();
if(a[i][j]+a[i][j+1]+a[i][j+2]+a[i][j+3]==4) aux1();
if(a[i][j]+a[i][j+1]+a[i][j+2]+a[i][j+3]==20) aux2();
if(a[i][j]+a[i+1][j+1]+a[i+2][j+2]+a[i+3][j+3]==4) aux1();
if(a[i][j]+a[i+1][j+1]+a[i+2][j+2]+a[i+3][j+3]==20) aux2();
if(a[i][j]+a[i-1][j-1]+a[i-2][j-2]+a[i-3][j-3]==4) aux1();
if(a[i][j]+a[i-1][j-1]+a[i-2][j-2]+a[i-3][j-3]==20) aux2();
if(a[i][j]+a[i-1][j+1]+a[i-2][j+2]+a[i-3][j+3]==4) aux1();
if(a[i][j]+a[i-1][j+1]+a[i-2][j+2]+a[i-3][j+3]==20) aux2();
if(a[i][j]+a[i+1][j-1]+a[i+2][j-2]+a[i+3][j-3]==4) aux1();
if(a[i][j]+a[i+1][j-1]+a[i+2][j-2]+a[i+3][j-3]==20) aux2();
if(s==252) aux3();
}
}

void movecircle()
{
char t;
while(t!=ESC)
{
t=0;
t=getch();
if(t==RIGHT && xcerc!=12)
{
if(player==1) { slide(xcerc,xcerc+1,1,14); }
if(player==2) { slide(xcerc,xcerc+1,1,2); }
xcerc++;
}
if(t==LEFT && xcerc!=1)
{
if(player==1) { slide(xcerc,xcerc-1,2,14); }
if(player==2) { slide(xcerc,xcerc-1,2,2); }
xcerc–;
}
if((t==ENTER) && (a[xcerc][2]==0))
{
animate();
if(player==1) { player=2; xcerc=12; ycerc=0; drawround(xcerc,ycerc,ray,2); win();}
else         { player=1; xcerc=1; ycerc=0; drawround(xcerc,ycerc,ray,14); win();}

}
}
}

void drawmap()
{
int i,j,k,l;
drawrect();
setcolor(9);
setfillstyle(1,4);
rectangle(20,65,625,415);
floodfill(200,200,9);
for(i=1;i<=12;i++)
for(j=1;j<=7;j++)
{
drawround(i,j,ray,0);
}
for(k=70;k<=570;k=k+50)
{
setcolor(0);
line(k,65,k,415);
}
for(l=115;l<=365;l=l+50)
{
setcolor(0);
line(20,l,625,l);
}
}

main()
{
clrscr();
init();
intro();
drawmap();
drawround(xcerc,ycerc,ray,14);
movecircle();
}

FCFS CPU scheduling CPP Code

This code implements the First come First Served Scheduling Algorithm. It gets the number of processes and the process name, burst time and arrival time for each user from the user. It then sorts the processes depending upon the arrival time. Calculates and displays the average turn around time and waiting time.Then it will display the Gantt chart.

// Name: FCFS Scheduling Algorithm
// Description:This code implements the
//     First come First Served Scheduling Algor
//     ithm. It gets the number of processes an
//     d the process name, burst time and arriv
//     al time for each user from the user. It
//     then sorts the processes depending upon
//     the arrival time. Calculates and display
//     s the average turn around time and waiti
//     ng time.Then it will display the Gantt c
//     hart.

//
// Inputs:1. Number of processes

/*

2. Process name
3. Burst Time
4. Arrival Time

*/

/* FIRST COME FIRST SERVED SCHEDULING ALGORITHM*/
//PREPROCESSOR DIRECTIVES
#include<stdio.h>
#include<conio.h>
#include<string.h>
//GLOBAL VARIABLES - DECLARATION
int n,Bu[20],Twt,Ttt,A[10],Wt[10],w;
float Awt,Att;
char pname[20][20],c[20][20];
//FUNCTION DECLARATIONS
void Getdata();
void Gantt_chart();
void Calculate();
void fcfs();
//GETTING THE NUMBER OF PROCESSES AND TH
//     E BURST TIME AND ARRIVAL TIME FOR EACH P
//     ROCESS
void Getdata()

    {
    	int i;
    	printf("\n Enter the number of processes: ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)

        	{
        		fflush(stdin);
        		printf("\n\n Enter the process name: ");
        		scanf("%s",&pname[i]);
        		printf("\n Enter The BurstTime for Process %s =	",pname[i]);
        		scanf("%d",&Bu[i]);
        		printf("\n Enter the Arrival Time for Process %s =	",pname[i]);
        		scanf("%d",&A[i]);
        	}
    }

    //DISPLAYING THE GANTT CHART
    void Gantt_chart()

        {
        	int i;
        	printf("\n\n\t\t\tGANTT CHART\n");
        	printf("\n-----------------------------------------------------------\n");
        	for(i=1;i<=n;i++)
        		printf("|\t%s\t",pname[i]);
        	printf("|\t\n");
        	printf("\n-----------------------------------------------------------\n");
        	printf("\n");
        	for(i=1;i<=n;i++)
        		printf("%d\t\t",Wt[i]);
        	printf("%d",Wt[n]+Bu[n]);
        	printf("\n-----------------------------------------------------------\n");
        	printf("\n");
    }

    //CALCULATING AVERAGE WAITING TIME AND A
    //     VERAGE TURN AROUND TIME
    void Calculate()

        {
        	int i;
        	//For the 1st process
        	Wt[1]=0;
        	for(i=2;i<=n;i++)

            	{
            		Wt[i]=Bu[i-1]+Wt[i-1];
            	}
            	for(i=1;i<=n;i++)

                	{
                		Twt=Twt+(Wt[i]-A[i]);
                		Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
                	}
                	Att=(float)Ttt/n;
                	Awt=(float)Twt/n;
                	printf("\n\n Average Turn around time=%3.2f ms ",Att);
                	printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
            }

            //FCFS Algorithm
            void fcfs()

                {
                	int i,j,temp, temp1;
                	Twt=0;
                	Ttt=0;
                	Printf(“\n\n FIRST COME FIRST SERVED ALGORITHM\n\n”);
                	for(i=1;i<=n;i++)

                    	{
                    		for(j=i+1;j<=n;j++)

                        		{
                        			if(A[i]>A[j])

                            			{
                            				temp=Bu[i];
                            				temp1=A[i];
                            				Bu[i]=Bu[j];
                            				A[i]=A[j];
                            				Bu[j]=temp;
                            				A[j]=temp1;
                            				strcpy(c[i],pname[i]);
                            				strcpy(pname[i],pname[j]);
                            				strcpy(pname[j],c[i]);
                            			}
                            		}
                            	}
                            	Calculate();
                            	Gantt_chart();
                        }

                        void main()

                            {
                            	int ch;
                            	clrscr();
                            	Getdata();
                            	fcfs();
                            	getch();
                        }

CPU SCHEDULING ALGORITHMS (SJF)

A perfect representation of the First Come, First Served, Shortest Job First and Non-preemptive Priority Scheduling in CPU Scheduling Algorithms. It includes The Gantt, Waiting Time and Turnaround Time Chart. For other codes please use search..

#include <stdio.h>
#include <conio.h>
#include <dos.h>
main()
{
_setcursortype(_NOCURSOR);
clrscr();
int min, nodup, nodup2[10]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, noduparr=0;
int z=1, y=5, y2=3, x, xg=2, xgv=7;
int ganttval=0, wtnttchart[100], wtnttchart2[100];
float processno, burtym[100], burtymarr=1, wtadd=0, ttadd=0;
int lo, di;
char menu, proc;

sleep(2);
textcolor(15);
gotoxy(44,13);cprintf(”X”);
delay(100);
textcolor(0);
gotoxy(44,13);cprintf(” “);
textcolor(15);
gotoxy(42,13);cprintf(”O”);
delay(100);
textcolor(0);
gotoxy(42,13);cprintf(” “);
textcolor(15);
gotoxy(41,13);cprintf(”C”);
delay(100);
textcolor(0);
gotoxy(41,13);cprintf(” “);
textcolor(15);
gotoxy(40,13);cprintf(”I”);
delay(100);
textcolor(0);
gotoxy(40,13);cprintf(” “);
textcolor(15);
gotoxy(39,13);cprintf(”R”);
delay(100);
textcolor(0);
gotoxy(39,13);cprintf(” “);
textcolor(15);
gotoxy(38,13);cprintf(”N”);
delay(100);
textcolor(0);
gotoxy(38,13);cprintf(” “);
textcolor(15);
gotoxy(37,13);cprintf(”E”);
delay(100);
textcolor(0);
gotoxy(37,13);cprintf(” “);
delay(100);
textcolor(15);
gotoxy(37,13);cprintf(”E”);
delay(100);
textcolor(8);
gotoxy(37,13);cprintf(”E”);
textcolor(15);
gotoxy(38,13);cprintf(”N”);
delay(100);
textcolor(8);
gotoxy(38,13);cprintf(”N”);
textcolor(15);
gotoxy(39,13);cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(39,13);cprintf(”R”);
textcolor(15);
gotoxy(40,13);cprintf(”I”);
delay(100);
textcolor(8);
gotoxy(40,13);cprintf(”I”);
textcolor(15);
gotoxy(41,13);cprintf(”C”);
delay(100);
textcolor(8);
gotoxy(41,13);cprintf(”C”);
textcolor(15);
gotoxy(42,13);cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(42,13);cprintf(”O”);
textcolor(15);
gotoxy(44,13);cprintf(”X”);
delay(100);
textcolor(8);
gotoxy(44,13);cprintf(”X”);
sleep(2);
clrscr();
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
gotoxy(28, 13); printf(” “);
gotoxy(52, 13); printf(” “);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
gotoxy(29, 13); printf(” “);
gotoxy(51, 13); printf(” “);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
gotoxy(30, 13); printf(” “);
gotoxy(50, 13); printf(” “);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
gotoxy(32, 13); printf(” “);
gotoxy(49, 13); printf(” “);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
gotoxy(33, 13); printf(” “);
gotoxy(48, 13); printf(” “);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
gotoxy(34, 13); printf(” “);
gotoxy(47, 13); printf(” “);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
gotoxy(35, 13); printf(” “);
gotoxy(46, 13); printf(” “);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
gotoxy(36, 13); printf(” “);
gotoxy(45, 13); printf(” “);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
gotoxy(37, 13); printf(” “);
gotoxy(44, 13); printf(” “);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
gotoxy(38, 13); printf(” “);
gotoxy(43, 13); printf(” “);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
gotoxy(39, 13); printf(” “);
gotoxy(41, 13); printf(” “);
gotoxy(40, 13); cprintf(”N”);
delay(100);
gotoxy(40, 13); printf(” “);
textcolor(8);
gotoxy(40, 13); cprintf(”N”);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
textcolor(8);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
textcolor(8);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
textcolor(8);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
textcolor(8);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
textcolor(8);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
textcolor(8);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
textcolor(8);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
sleep(2);

for(x=28; x<=52; x++)
{
gotoxy(x, 13); printf(” “);
delay(100);
}
clrscr();

menu_back:
textcolor(8);
gotoxy(1, 25); cprintf(”Best viewed in maximize window”);
gotoxy(77, 1); cprintf(”SJF”);
gotoxy(1, 1); cprintf(”ÚÄÄÄÄÄÄÄ¿”);
gotoxy(1, 2); cprintf(”³  nter ³”);
gotoxy(1, 3); cprintf(”ÀÄÄÄÄÄÄÄÙ”);
gotoxy(69, 23); cprintf(”ÚÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(69, 24); cprintf(”³  redits ³”);
gotoxy(69, 25); cprintf(”ÀÄÄÄÄÄÄÄÄÄÙ”);
textcolor(15);
gotoxy(3, 2); cprintf(”E”);
gotoxy(71, 24); cprintf(”C”);
menu=getch();

if(menu==’E’ || menu==’e')
{
clrscr();
for(lo=28; lo<=53; lo++)
{
textcolor(8);
gotoxy(lo,14);cprintf(”ÛÛ”);
}

for(lo=0, di=28; lo<=100; lo+=4, di++)
{
textcolor(15);
gotoxy(28,12);cprintf(”initializing schedule…”);
gotoxy(28,13);cprintf(”%d%”, lo);
gotoxy(di,14);cprintf(”ÛÛ”);
delay(200);
}
sleep(2);
goto go;
}
else if(menu==’C’ || menu==’c')
goto credits;
else
goto menu_back;

go:
clrscr();
_setcursortype(_SOLIDCURSOR);
back:
textcolor(8);
gotoxy(16, 13); cprintf(”ow many processes would you like to put? (1-9)”);
textcolor(15);
gotoxy(15, 13); cprintf(”H”);
gotoxy(62, 13); cprintf(”:”);
gotoxy(64,13); cscanf(”%c”, &proc);
clrscr();

if(proc==’1′)
processno=1;
else if(proc==’2′)
processno=2;
else if(proc==’3′)
processno=3;
else if(proc==’4′)
processno=4;
else if(proc==’5′)
processno=5;
else if(proc==’6′)
processno=6;
else if(proc==’7′)
processno=7;
else if(proc==’8′)
processno=8;
else if(proc==’9′)
processno=9;
else
goto back;

_setcursortype(_SOLIDCURSOR);
textcolor(8);
gotoxy(28, 2); cprintf(”ÚÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(28, 3); cprintf(”³ Process ³            ³”);
gotoxy(28, 4); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(40, 3); cprintf(”Burst Time”);

for(x=1; x<=processno; x++)
{
textcolor(8);
gotoxy(28, y); cprintf(”³         ³            ³”);
gotoxy(28, y+1); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(32, y); cprintf(”P%d”, x);
y+=2;
}

textcolor(8);
gotoxy(28, y-1); cprintf(”ÀÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÙ”);

y=5;
limits:
do
{
textcolor(15);
gotoxy(44, y); cscanf(”%c”, &proc);

if(proc==’1′)
burtym[burtymarr]=1;
else if(proc==’2′)
burtym[burtymarr]=2;
else if(proc==’3′)
burtym[burtymarr]=3;
else if(proc==’4′)
burtym[burtymarr]=4;
else if(proc==’5′)
burtym[burtymarr]=5;
else if(proc==’6′)
burtym[burtymarr]=6;
else if(proc==’7′)
burtym[burtymarr]=7;
else if(proc==’8′)
burtym[burtymarr]=8;
else if(proc==’9′)
burtym[burtymarr]=9;
else
{
gotoxy(44, y); printf(” “);
goto limits;
}

burtymarr++;
y+=2;
z++;
}
while(z<=processno);

_setcursortype(_NOCURSOR);
gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(40, 3); cprintf(”Burst Time”);
gotoxy(2, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

y=4;
for(x=1; x<=processno; x++)
{
min=10000;
for(burtymarr=1; burtymarr<=processno; burtymarr++)
{
if(min>burtym[burtymarr] && burtymarr!=nodup2[0] && burtymarr!=nodup2[1]
&& burtymarr!=nodup2[2] && burtymarr!=nodup2[3] && burtymarr!=nodup2[4]
&& burtymarr!=nodup2[5] && burtymarr!=nodup2[6] && burtymarr!=nodup2[7]
&& burtymarr!=nodup2[8] && burtymarr!=nodup2[9])
{
nodup=burtymarr;
min=burtym[burtymarr];
textcolor(8);
gotoxy(1, 3); cprintf(”Ú”);
gotoxy(1, 4); cprintf(”³”);
gotoxy(1, 5); cprintf(”À”);

gotoxy(xg, 3); cprintf(”ÄÄÄÄÄÄ”);
gotoxy(xg, 4); cprintf(”      ³”);
gotoxy(xg, 5); cprintf(”ÄÄÄÄÄÄÁ”);

textcolor(15);
gotoxy(y2, y); cprintf(”P%g”, burtymarr);
}
}
wtnttchart2[nodup]=ganttval;
wtadd+=wtnttchart2[nodup];
ganttval+=min;
textcolor(15);
gotoxy(1, 6); cprintf(”0″);
gotoxy(xgv, 6); cprintf(”%d”, ganttval);
y2+=7;
xg+=7;
xgv+=7;
nodup2[noduparr]=nodup;
wtnttchart[nodup]=ganttval;
ttadd+=wtnttchart[nodup];
noduparr++;
}

textcolor(8);
gotoxy(xg-1, 3); cprintf(”¿”);
gotoxy(xg-1, 4); cprintf(”³”);
gotoxy(xg-1, 5); cprintf(”Ù”);

textcolor(8);
gotoxy(2, 1); cprintf(”he Gantt Char”);
textcolor(15);
gotoxy(1, 1); cprintf(”T”);
gotoxy(15, 1); cprintf(”t”);

gotoxy(52, 25); cprintf(”P”);
gotoxy(77, 25);cprintf(”…”);
textcolor(8);
gotoxy(53, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

y=5;
gotoxy(20, 2); cprintf(”ÚÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(20, 3); cprintf(”³     ³ Waiting Time ³ Turnaround Time ³”);
gotoxy(20, 4); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);

for(x=1; x<=processno; x++)
{
textcolor(8);
gotoxy(20, y); cprintf(”³     ³              ³                 ³”);
gotoxy(20, y+1); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(22, y); cprintf(”P%d”, x);
y+=2;
}

textcolor(8);
gotoxy(20, y-1); cprintf(”ÀÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”);

textcolor(15);
y=5;
for(x=1; x<=processno; x++)
{
gotoxy(28, y); cprintf(”%d – 0 = %d”, wtnttchart2[x], wtnttchart2[x]);
y+=2;
}

y=5;
for(x=1; x<=processno; x++)
{
gotoxy(44, y); cprintf(”%d – 0 = %d”, wtnttchart[x], wtnttchart[x]);
y+=2;
}

gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(2, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

textcolor(8);
gotoxy(2, 1); cprintf(”verage Waiting Tim”);
gotoxy(2, 5); cprintf(”verage Turnaround Tim”);
gotoxy(1, 3); cprintf(”(”);
textcolor(15);
gotoxy(1, 1); cprintf(”A”);
gotoxy(20, 1); cprintf(”e”);
gotoxy(1, 5); cprintf(”A”);
gotoxy(23, 5); cprintf(”e”);

gotoxy(1, 3); cprintf(”%g”, wtadd/(processno));
gotoxy(1, 7); cprintf(”%g”, ttadd/(processno));

gotoxy(56, 25); cprintf(”P”);
gotoxy(77,25);cprintf(”…”);
textcolor(8);
gotoxy(57, 25); cprintf(”ress any key to exit”);
getch();
goto end;

credits:
clrscr();
textcolor(8);
gotoxy(2, 1); cprintf(”rogrammer:”);
gotoxy(14, 1); cprintf(”Shahid Siddiuqe”);
gotoxy(2, 3);cprintf(”pecial thanks to:  My friends”);
gotoxy(2, 25); cprintf(”ress any key to exit”);
textcolor(15);
gotoxy(1, 1); cprintf(”P”);
gotoxy(13, 1); cprintf(”E”);
gotoxy(20, 1); cprintf(”S”);
gotoxy(1, 3); cprintf(”S”);
gotoxy(20, 3); cprintf(”J”);
gotoxy(25, 3); cprintf(”J”);
gotoxy(1, 25); cprintf(”P”);
gotoxy(22, 25); cprintf(”…”);
getch();
clrscr();
goto menu_back;

end:
clrscr();
textcolor(4);
gotoxy(37,13);cprintf(”THE END!”);
return 0;
}

FCFS CPU SCHEDULING ALGORITHMS

This is the example  code for First come first serve CPU SCHEDULING ALGORITHMS. This is so simple to use and its fully optimized. If need any help please do not hesitate to contact me…

#include <stdio.h>
#include <conio.h>
#include <dos.h>
main()
{
_setcursortype(_NOCURSOR);
clrscr();
int x, y=5, z=1;
float pro, burst[100], burst2=1, gantt=0, gantt2=0, gantt3=0;
int lo, di;
char menu, proc;

sleep(2);
textcolor(15);
gotoxy(43, 13); cprintf(”2″);
delay(100);
gotoxy(43, 13); cprintf(” “);
gotoxy(41, 13); cprintf(”P”);
delay(100);
gotoxy(41, 13); cprintf(” “);
gotoxy(40, 13); cprintf(”U”);
delay(100);
gotoxy(40, 13); cprintf(” “);
gotoxy(39, 13); cprintf(”O”);
delay(100);
gotoxy(39, 13); cprintf(” “);
gotoxy(38, 13); cprintf(”R”);
delay(100);
gotoxy(38, 13); cprintf(” “);
gotoxy(37, 13); cprintf(”G”);
delay(100);
gotoxy(37, 13); cprintf(” “);
delay(100);
textcolor(15);
gotoxy(37, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(37, 13); cprintf(”G”);
textcolor(15);
gotoxy(38, 13); cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(38, 13); cprintf(”R”);
textcolor(15);
gotoxy(39, 13); cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(39, 13); cprintf(”O”);
textcolor(15);
gotoxy(40, 13); cprintf(”U”);
delay(100);
textcolor(8);
gotoxy(40, 13); cprintf(”U”);
textcolor(15);
gotoxy(41, 13); cprintf(”P”);
delay(100);
textcolor(8);
gotoxy(41, 13); cprintf(”P”);
textcolor(15);
gotoxy(43, 13); cprintf(”2″);
delay(100);
textcolor(8);
gotoxy(43, 13); cprintf(”2″);
sleep(2);
clrscr();
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
gotoxy(28, 13); printf(” “);
gotoxy(52, 13); printf(” “);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
gotoxy(29, 13); printf(” “);
gotoxy(51, 13); printf(” “);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
gotoxy(30, 13); printf(” “);
gotoxy(50, 13); printf(” “);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
gotoxy(32, 13); printf(” “);
gotoxy(49, 13); printf(” “);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
gotoxy(33, 13); printf(” “);
gotoxy(48, 13); printf(” “);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
gotoxy(34, 13); printf(” “);
gotoxy(47, 13); printf(” “);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
gotoxy(35, 13); printf(” “);
gotoxy(46, 13); printf(” “);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
gotoxy(36, 13); printf(” “);
gotoxy(45, 13); printf(” “);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
gotoxy(37, 13); printf(” “);
gotoxy(44, 13); printf(” “);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
gotoxy(38, 13); printf(” “);
gotoxy(43, 13); printf(” “);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
gotoxy(39, 13); printf(” “);
gotoxy(41, 13); printf(” “);
gotoxy(40, 13); cprintf(”N”);
delay(100);
gotoxy(40, 13); printf(” “);
textcolor(8);
gotoxy(40, 13); cprintf(”N”);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
textcolor(8);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
textcolor(8);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
textcolor(8);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
textcolor(8);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
textcolor(8);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
textcolor(8);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
textcolor(8);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
sleep(2);

for(x=28; x<=52; x++)
{
gotoxy(x, 13); printf(” “);
delay(100);
}
clrscr();

menu_back:
textcolor(8);
gotoxy(1, 25); cprintf(”Best viewed in maximize window”);
gotoxy(76, 1); cprintf(”FCFS”);
gotoxy(1, 1); cprintf(”ÚÄÄÄÄÄÄÄ¿”);
gotoxy(1, 2); cprintf(”³  nter ³”);
gotoxy(1, 3); cprintf(”ÀÄÄÄÄÄÄÄÙ”);
gotoxy(69, 23); cprintf(”ÚÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(69, 24); cprintf(”³  redits ³”);
gotoxy(69, 25); cprintf(”ÀÄÄÄÄÄÄÄÄÄÙ”);
textcolor(15);
gotoxy(3, 2); cprintf(”E”);
gotoxy(71, 24); cprintf(”C”);
menu=getch();

if(menu==’E’ || menu==’e')
{
clrscr();
for(lo=28; lo<=53; lo++)
{
textcolor(8);
gotoxy(lo,14);cprintf(”ÛÛ”);
}

for(lo=0, di=28; lo<=100; lo+=4, di++)
{
textcolor(15);
gotoxy(28,12);cprintf(”initializing schedule…”);
gotoxy(28,13);cprintf(”%d%”, lo);
gotoxy(di,14);cprintf(”ÛÛ”);
delay(200);
}
sleep(2);
goto go;
}
else if(menu==’C’ || menu==’c')
goto credits;
else
goto menu_back;

go:
clrscr();
_setcursortype(_SOLIDCURSOR);
back:
textcolor(8);
gotoxy(16, 13); cprintf(”ow many processes would you like to put? (1-9)”);
textcolor(15);
gotoxy(15, 13); cprintf(”H”);
gotoxy(62, 13); cprintf(”:”);
gotoxy(64,13); cscanf(”%c”, &proc);
clrscr();

if(proc==’1′)
pro=1;
else if(proc==’2′)
pro=2;
else if(proc==’3′)
pro=3;
else if(proc==’4′)
pro=4;
else if(proc==’5′)
pro=5;
else if(proc==’6′)
pro=6;
else if(proc==’7′)
pro=7;
else if(proc==’8′)
pro=8;
else if(proc==’9′)
pro=9;
else
goto back;

_setcursortype(_SOLIDCURSOR);
textcolor(8);
gotoxy(28, 2); cprintf(”ÚÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(28, 3); cprintf(”³ Process ³            ³”);
gotoxy(28, 4); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(40, 3); cprintf(”Burst Time”);

for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(28, y); cprintf(”³         ³            ³”);
gotoxy(28, y+1); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(32, y); cprintf(”P%d”, x);
y+=2;
}

textcolor(8);
gotoxy(28, y-1); cprintf(”ÀÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÙ”);

y=5;
limits:

do
{
textcolor(15);
gotoxy(44, y); cscanf(”%c”, &proc);

if(proc==’1′)
burst[burst2]=1;
else if(proc==’2′)
burst[burst2]=2;
else if(proc==’3′)
burst[burst2]=3;
else if(proc==’4′)
burst[burst2]=4;
else if(proc==’5′)
burst[burst2]=5;
else if(proc==’6′)
burst[burst2]=6;
else if(proc==’7′)
burst[burst2]=7;
else if(proc==’8′)
burst[burst2]=8;
else if(proc==’9′)
burst[burst2]=9;
else
{
textcolor(8);
gotoxy(44, y); printf(” “);
goto limits;
}

burst2++;
y+=2;
z++;
}
while(z<=pro);

_setcursortype(_NOCURSOR);
gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(40, 3); cprintf(”Burst Time”);
gotoxy(2, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

textcolor(8);
gotoxy(2, 1); cprintf(”he Gantt Char”);
textcolor(15);
gotoxy(1, 1); cprintf(”T”);
gotoxy(15, 1); cprintf(”t”);
textcolor(8);
gotoxy(1, 3); cprintf(”Ú”);
gotoxy(1, 4); cprintf(”³”);
gotoxy(1, 5); cprintf(”À”);

y=2;
for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(y, 3); cprintf(”ÄÄÄÄÄÄ”);
gotoxy(y, 4); cprintf(”      ³”);
gotoxy(y, 5); cprintf(”ÄÄÄÄÄÄÁ”);
textcolor(15);
gotoxy(y+1, 4); cprintf(”P%d”, x);
y+=7;
}

textcolor(8);
gotoxy(y-1, 3); cprintf(”¿”);
gotoxy(y-1, 4); cprintf(”³”);
gotoxy(y-1, 5); cprintf(”Ù”);

y=1;
burst2=1;
for(x=1; x<=pro+1; x++)
{
textcolor(15);
gotoxy(y, 6); cprintf(”%g”, gantt);
gantt+=burst[burst2];
burst2++;
y+=7;
}

gotoxy(52, 25); cprintf(”P”);
gotoxy(77, 25);cprintf(”…”);
textcolor(8);
gotoxy(53, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

y=5;
gotoxy(20, 2); cprintf(”ÚÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(20, 3); cprintf(”³     ³ Waiting Time ³ Turnaround Time ³”);
gotoxy(20, 4); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);

for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(20, y); cprintf(”³     ³              ³                 ³”);
gotoxy(20, y+1); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(22, y); cprintf(”P%d”, x);
y+=2;
}

textcolor(8);
gotoxy(20, y-1); cprintf(”ÀÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”);

y=5;
gantt=0;
burst2=1;
for(x=1; x<=pro; x++)
{
textcolor(15);
gotoxy(28, y); cprintf(”%g – 0 = %g”, gantt, gantt);
gantt2+=gantt;
gantt+=burst[burst2];
burst2++;
y+=2;
}

y=5;
burst2=1;
gantt=burst[burst2];
for(x=1; x<=pro; x++)
{
textcolor(15);
gotoxy(44, y); cprintf(”%g – 0 = %g”, gantt, gantt);
gantt3+=gantt;
burst2++;
gantt+=burst[burst2];
y+=2;
}

gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(2, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

textcolor(8);
gotoxy(2, 1); cprintf(”verage Waiting Tim”);
gotoxy(2, 5); cprintf(”verage Turnaround Tim”);
gotoxy(1, 3); cprintf(”(”);
textcolor(15);
gotoxy(1, 1); cprintf(”A”);
gotoxy(20, 1); cprintf(”e”);
gotoxy(1, 5); cprintf(”A”);
gotoxy(23, 5); cprintf(”e”);

gotoxy(1, 3); cprintf(”%g”, gantt2/pro);
gotoxy(1, 7); cprintf(”%g”, gantt3/pro);

gotoxy(56, 25); cprintf(”P”);
gotoxy(77,25);cprintf(”…”);
textcolor(8);
gotoxy(57, 25); cprintf(”ress any key to exit”);
getch();
goto end;

credits:
clrscr();
textcolor(8);
gotoxy(2, 1); cprintf(”rogrammers:”);
gotoxy(15, 1); cprintf(”iza  elarmino”);
gotoxy(15, 3); cprintf(”ovelyn  hano”);
gotoxy(15, 5); cprintf(”irgie  ungog”);
gotoxy(15, 7); cprintf(”nrico  orenzo”);
gotoxy(15, 9); cprintf(”aychel  erison”);
gotoxy(15, 11); cprintf(”ary Joy  bial”);
textcolor(15);
gotoxy(1, 1); cprintf(”P”);
gotoxy(14, 1); cprintf(”A”);
gotoxy(19, 1); cprintf(”B”);
gotoxy(14, 3); cprintf(”J”);
gotoxy(22, 3); cprintf(”C”);
gotoxy(14, 5); cprintf(”V”);
gotoxy(21, 5); cprintf(”D”);
gotoxy(14, 7); cprintf(”E”);
gotoxy(21, 7); cprintf(”L”);
gotoxy(14, 9); cprintf(”R”);
gotoxy(22, 9); cprintf(”N”);
gotoxy(14, 11); cprintf(”M”);
gotoxy(23, 11); cprintf(”O”);
gotoxy(56, 25); cprintf(”P”);
gotoxy(77,25); cprintf(”…”);
textcolor(8);
gotoxy(57, 25); cprintf(”ress any key to exit”);
getch();
clrscr();
goto menu_back;

end:
clrscr();
textcolor(4);
gotoxy(37,13);cprintf(”THE END!”);
return 0;
}

Priority CPU SCHEDULING ALGORITHMS in C++

Small sample code to demonstrate CPU Scheduling based on Priority. Please feel free to use this code in your projects. If need any help or modification do not hesitate to ask. There are many other CPU scheduling codes. Please explore the website to see more…

#include <stdio.h>
#include <conio.h>
#include <dos.h>
main()
{
_setcursortype(_NOCURSOR);
clrscr();
int x, y=5, z=1, burst[100], burst2=1, gantt[100], gantt2[100];
float pro, wt=0, tat=0;
int lo, di, priority[100], priority2=1,
nodup[100]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, max, sum,
arrival[100]={0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
char menu, proc;

sleep(2);
textcolor(15);
gotoxy(44,13);cprintf(”X”);
delay(100);
textcolor(0);
gotoxy(44,13);cprintf(” “);
textcolor(15);
gotoxy(42,13);cprintf(”O”);
delay(100);
textcolor(0);
gotoxy(42,13);cprintf(” “);
textcolor(15);
gotoxy(41,13);cprintf(”C”);
delay(100);
textcolor(0);
gotoxy(41,13);cprintf(” “);
textcolor(15);
gotoxy(40,13);cprintf(”I”);
delay(100);
textcolor(0);
gotoxy(40,13);cprintf(” “);
textcolor(15);
gotoxy(39,13);cprintf(”R”);
delay(100);
textcolor(0);
gotoxy(39,13);cprintf(” “);
textcolor(15);
gotoxy(38,13);cprintf(”N”);
delay(100);
textcolor(0);
gotoxy(38,13);cprintf(” “);
textcolor(15);
gotoxy(37,13);cprintf(”E”);
delay(100);
textcolor(0);
gotoxy(37,13);cprintf(” “);
delay(100);
textcolor(15);
gotoxy(37,13);cprintf(”E”);
delay(100);
textcolor(8);
gotoxy(37,13);cprintf(”E”);
textcolor(15);
gotoxy(38,13);cprintf(”N”);
delay(100);
textcolor(8);
gotoxy(38,13);cprintf(”N”);
textcolor(15);
gotoxy(39,13);cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(39,13);cprintf(”R”);
textcolor(15);
gotoxy(40,13);cprintf(”I”);
delay(100);
textcolor(8);
gotoxy(40,13);cprintf(”I”);
textcolor(15);
gotoxy(41,13);cprintf(”C”);
delay(100);
textcolor(8);
gotoxy(41,13);cprintf(”C”);
textcolor(15);
gotoxy(42,13);cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(42,13);cprintf(”O”);
textcolor(15);
gotoxy(44,13);cprintf(”X”);
delay(100);
textcolor(8);
gotoxy(44,13);cprintf(”X”);
sleep(2);
clrscr();
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
gotoxy(28, 13); printf(” “);
gotoxy(52, 13); printf(” “);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
gotoxy(29, 13); printf(” “);
gotoxy(51, 13); printf(” “);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
gotoxy(30, 13); printf(” “);
gotoxy(50, 13); printf(” “);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
gotoxy(32, 13); printf(” “);
gotoxy(49, 13); printf(” “);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
gotoxy(33, 13); printf(” “);
gotoxy(48, 13); printf(” “);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
gotoxy(34, 13); printf(” “);
gotoxy(47, 13); printf(” “);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
gotoxy(35, 13); printf(” “);
gotoxy(46, 13); printf(” “);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
gotoxy(36, 13); printf(” “);
gotoxy(45, 13); printf(” “);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
gotoxy(37, 13); printf(” “);
gotoxy(44, 13); printf(” “);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
gotoxy(38, 13); printf(” “);
gotoxy(43, 13); printf(” “);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
gotoxy(39, 13); printf(” “);
gotoxy(41, 13); printf(” “);
gotoxy(40, 13); cprintf(”N”);
delay(100);
gotoxy(40, 13); printf(” “);
textcolor(8);
gotoxy(40, 13); cprintf(”N”);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
textcolor(8);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
textcolor(8);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
textcolor(8);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
textcolor(8);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
textcolor(8);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
textcolor(8);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
textcolor(8);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
sleep(2);

for(x=28; x<=52; x++)
{
gotoxy(x, 13); printf(” “);
delay(100);
}
clrscr();

menu_back:
textcolor(8);
gotoxy(1, 25); cprintf(”Best viewed in maximize window”);
gotoxy(67, 1); cprintf(”Priority – NP”);
gotoxy(1, 1); cprintf(”ÚÄÄÄÄÄÄÄ¿”);
gotoxy(1, 2); cprintf(”³  nter ³”);
gotoxy(1, 3); cprintf(”ÀÄÄÄÄÄÄÄÙ”);
gotoxy(69, 23); cprintf(”ÚÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(69, 24); cprintf(”³  redits ³”);
gotoxy(69, 25); cprintf(”ÀÄÄÄÄÄÄÄÄÄÙ”);
textcolor(15);
gotoxy(3, 2); cprintf(”E”);
gotoxy(71, 24); cprintf(”C”);
menu=getch();

if(menu==’E’ || menu==’e')
{
clrscr();
for(lo=28; lo<=53; lo++)
{
textcolor(8);
gotoxy(lo,14);cprintf(”ÛÛ”);
}

for(lo=0, di=28; lo<=100; lo+=4, di++)
{
textcolor(15);
gotoxy(28,12);cprintf(”initializing schedule…”);
gotoxy(28,13);cprintf(”%d%”, lo);
gotoxy(di,14);cprintf(”ÛÛ”);
delay(200);
}
sleep(2);
goto go;
}
else if(menu==’C’ || menu==’c')
goto credits;
else
goto menu_back;

go:
clrscr();
_setcursortype(_SOLIDCURSOR);
back:
textcolor(8);
gotoxy(16, 13); cprintf(”ow many processes would you like to put? (1-9)”);
textcolor(15);
gotoxy(15, 13); cprintf(”H”);
gotoxy(62, 13); cprintf(”:”);
gotoxy(64,13); cscanf(”%c”, &proc);
clrscr();

if(proc==’1′)
pro=1;
else if(proc==’2′)
pro=2;
else if(proc==’3′)
pro=3;
else if(proc==’4′)
pro=4;
else if(proc==’5′)
pro=5;
else if(proc==’6′)
pro=6;
else if(proc==’7′)
pro=7;
else if(proc==’8′)
pro=8;
else if(proc==’9′)
pro=9;
else
goto back;

_setcursortype(_SOLIDCURSOR);
textcolor(8);
gotoxy(16, 2); cprintf(”ÚÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿        “);
gotoxy(16, 3); cprintf(”³ Process ³            ³ Arrival Time ³ Priority ³”);
gotoxy(16, 4); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(28, 3); cprintf(”Burst Time”);

for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(16, y); cprintf(”³         ³            ³              ³          ³”);
gotoxy(16, y+1); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(20, y); cprintf(”P%d”, x);
gotoxy(46, y); cprintf(”%d”, x-1);
y+=2;
}

textcolor(8);
gotoxy(16, y-1); cprintf(”ÀÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ”);

y=5;
limits:
do
{
textcolor(15);
gotoxy(32, y); cscanf(”%c”, &proc);

if(proc==’1′)
burst[burst2]=1;
else if(proc==’2′)
burst[burst2]=2;
else if(proc==’3′)
burst[burst2]=3;
else if(proc==’4′)
burst[burst2]=4;
else if(proc==’5′)
burst[burst2]=5;
else if(proc==’6′)
burst[burst2]=6;
else if(proc==’7′)
burst[burst2]=7;
else if(proc==’8′)
burst[burst2]=8;
else if(proc==’9′)
burst[burst2]=9;
else
{
textcolor(8);
gotoxy(32, y); printf(” “);
goto limits;
}

burst2++;
y+=2;
z++;
}
while(z<=pro);

textcolor(8);
gotoxy(28, 3); cprintf(”Burst Time”);
textcolor(15);
gotoxy(56, 3); cprintf(”Priority”);

z=1;
y=5;
limits2:
do
{
textcolor(15);
gotoxy(59, y); cscanf(”%c”, &proc);

if(proc==’1′)
priority[priority2]=1;
else if(proc==’2′)
priority[priority2]=2;
else if(proc==’3′)
priority[priority2]=3;
else if(proc==’4′)
priority[priority2]=4;
else if(proc==’5′)
priority[priority2]=5;
else if(proc==’6′)
priority[priority2]=6;
else if(proc==’7′)
priority[priority2]=7;
else if(proc==’8′)
priority[priority2]=8;
else if(proc==’9′)
priority[priority2]=9;
else
{
textcolor(8);
gotoxy(59, y); printf(” “);
goto limits2;
}

priority2++;
y+=2;
z++;
}
while(z<=pro);

_setcursortype(_NOCURSOR);
gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(2, 25); cprintf(”ress any key to continue”);
gotoxy(56, 3); cprintf(”Priority”);
getch();
clrscr();

textcolor(8);
gotoxy(2, 1); cprintf(”he Gantt Char”);
textcolor(15);
gotoxy(1, 1); cprintf(”T”);
gotoxy(15, 1); cprintf(”t”);
textcolor(8);
gotoxy(1, 3); cprintf(”Ú”);
gotoxy(1, 4); cprintf(”³”);
gotoxy(1, 5); cprintf(”À”);

y=2;
for(x=1; x<=pro; x++)
{
gotoxy(y, 3); cprintf(”ÄÄÄÄÄÄ”);
gotoxy(y, 4); cprintf(”      ³”);
gotoxy(y, 5); cprintf(”ÄÄÄÄÄÄÁ”);
y+=7;
}

gotoxy(y-1, 3); cprintf(”¿”);
gotoxy(y-1, 4); cprintf(”³”);
gotoxy(y-1, 5); cprintf(”Ù”);

y=4;
sum=burst[1];
textcolor(15);
gotoxy(y, 4); cprintf(”P1″);
gotoxy(y-3, 6); cprintf(”0″);
gotoxy(y+4, 6); cprintf(”%d”, burst[1]);
gantt[1]=0;
gantt2[1]=burst[1];
for(x=2; x<=pro; x++)
{
max=-1;
for(z=2; z<=pro; z++)
if(priority[z]>max && z!=nodup[1] && z!=nodup[2]
&& z!=nodup[3] && z!=nodup[4]
&& z!=nodup[5] && z!=nodup[6] && z!=nodup[7]
&& z!=nodup[8] && z!=nodup[9])
{
max=priority[z];
nodup[x]=z;
gantt[z]=sum;
gantt2[z]=sum+burst[z];
}
sum+=burst[nodup[x]];
textcolor(15);
gotoxy(y+7, 4); cprintf(”P%d”, nodup[x]);
gotoxy(y+11, 6); cprintf(”%d”, sum);
y+=7;
}

gotoxy(52, 25); cprintf(”P”);
gotoxy(77, 25);cprintf(”…”);
textcolor(8);
gotoxy(53, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

y=5;
gotoxy(20, 2); cprintf(”ÚÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(20, 3); cprintf(”³     ³ Waiting Time ³ Turnaround Time ³”);
gotoxy(20, 4); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);

for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(20, y); cprintf(”³     ³              ³                 ³”);
gotoxy(20, y+1); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(22, y); cprintf(”P%d”, x);
y+=2;
}

textcolor(8);
gotoxy(20, y-1); cprintf(”ÀÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”);

y=5;
for(x=1; x<=pro; x++)
{
textcolor(15);
gotoxy(28, y); cprintf(”%d – %d = %d”, gantt[x], arrival[x], gantt[x]-arrival[x]);
wt+=gantt[x]-arrival[x];
y+=2;
}

y=5;
for(x=1; x<=pro; x++)
{
textcolor(15);
gotoxy(44, y); cprintf(”%d – %d = %d”, gantt2[x], arrival[x], gantt2[x]-arrival[x]);
tat+=gantt2[x]-arrival[x];
y+=2;
}

gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(2, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

textcolor(8);
gotoxy(2, 1); cprintf(”verage Waiting Tim”);
gotoxy(2, 5); cprintf(”verage Turnaround Tim”);
gotoxy(1, 3); cprintf(”(”);
textcolor(15);
gotoxy(1, 1); cprintf(”A”);
gotoxy(20, 1); cprintf(”e”);
gotoxy(1, 5); cprintf(”A”);
gotoxy(23, 5); cprintf(”e”);
gotoxy(1, 3); cprintf(”%g”, wt/pro);
gotoxy(1, 7); cprintf(”%g”, tat/pro);

gotoxy(56, 25); cprintf(”P”);
gotoxy(77,25);cprintf(”…”);
textcolor(8);
gotoxy(57, 25); cprintf(”ress any key to exit”);
getch();
goto end;

credits:
clrscr();
textcolor(8);
gotoxy(1,1);cprintf(” rogrammer:  Shahid Siddique”);
gotoxy(1,3);cprintf(” pecial thanks to:  my friends for their help..”);
gotoxy(1,25);cprintf(” ress any key to exit”);
textcolor(15);
gotoxy(1,1);cprintf(”P”);
gotoxy(13,1);cprintf(”E”);
gotoxy(20,1);cprintf(”L”);
gotoxy(1,3);cprintf(”S”);
gotoxy(20,3);cprintf(”J”);
gotoxy(25,3);cprintf(”J”);
gotoxy(34,3);cprintf(”M”);
gotoxy(38,3);cprintf(”M”);
gotoxy(43,3);cprintf(”A”);
gotoxy(48,3);cprintf(”S”);
gotoxy(56,3);cprintf(”M”);
gotoxy(61,3);cprintf(”A”);
gotoxy(69,3);cprintf(”S”);
gotoxy(1,25);cprintf(”P”);
gotoxy(22,25);cprintf(”…”);
getch();
clrscr();
goto menu_back;

end:
clrscr();
textcolor(4);
gotoxy(37,13);cprintf(”THE END!”);
return 0;
}

Using 256 colors in C++ programs

This program as header class will able you to use graphics output quality of 600*800 solution with 256 color This mode is satisfatory to identify any image. This mode generally not provided by Turbo C++ Compiler Some function are also provided for the

It must be used only with MsDOS. Because some convention I have made regarding to memory model of program

Use it on WIN98 not on WinXP (if you want then on XP use only 640*480 resolution check function InitGraph() for more details……)

The program have three classes

  1. VDU
  2. Color
  3. Box with one LineStyle class use these classes as VDU for display any char at any pos with any color
  1. putChar(int row,int column ,char ch, ClassColor colorOfChar=DefaultColor)
  2. putString (int r,int c,char *ch,ClassColor clr=DefaultColor)
  3. putLong(int r,int c,long ch,ClassColor clr=DefaultColor)
  4. putDouble (int r,int c,double ch,ClassColor clr=DefaultColor)
  5. changeColor (int r, int c,ClassColor clr) –> It changes color of any position

use Color class for

  1. SetColor(unsigned char fg,unsigned char bg ,unsigned char bl);
  2. SetColor(ClassColor cc1);
  3. MakeColor(unsigned char fg,unsigned char bg ,unsigned char bl);

#include<dos.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define round(a) ((int)(a+0.5))
void drawline(int xa, int ya, int xb, int yb);
const unsigned int MINX=0,MINY=0,MAXX=799,MAXY=599;
const unsigned int MIDX=(MAXX+1)/2,MIDY=(MAXY+1)/2;
unsigned int CentreX=MIDX,CentreY=MIDY;
char huge Timg[MAXX][MAXY];
unsigned char huge ColorLUT[768];
union REGS in, out,reg ;
union SREGS inreg ;
// —-pellate setting
typedef unsigned int Word;
typedef unsigned char Byte;
typedef struct

{
Byte Red, Grn, Blu;
}RGB;

class sColor

{};
typedef RGB PaletteRegister[255];
PaletteRegister Color,colorP;
void ClearPalette(PaletteRegister Color)

{
Word i;
for(i=0; i<=255; i++)

{
Color[i].Red=0;
Color[i].Grn=0;
Color[i].Blu=0;
}
}

void SetPalette(PaletteRegister Hue)

{
reg.x.ax=0×1012;
segread(&inreg);
inreg.es=inreg.ds;
reg.x.bx=0;reg.x.cx=256;
reg.x.dx=(int)&Hue[0];
int86x(0×10,®,®,&inreg);
}

void InitPalette(PaletteRegister Color)

{
Word i=0;
for(int r=0;r<6;r++)
for(int g=0;g<6;g++)
for(int b=0;b<6;b++)

{
Color[i].Red=r*51;
Color[i].Grn=g*51;
Color[i].Blu=b*51;
i++;
}
}

//————————————–
//     ————-
class CGBase

{
int gd,gm,gr;
union REGS i, o,reg;
struct SREGS inreg;
int fc,bc;
void setsvga ( int m )

{
i.x.ax = 0×4f02 ;
i.x.bx = m ;
int86 ( 16, &i, &o ) ;
}
void set_vesa_seg ( int bank_number )

{
union REGS i, o ;
i.x.ax = 0×4F05 ;
i.x.bx = 0 ;
i.x.dx = bank_number ;
int86 ( 16, &i, &o ) ;
}
void putpixel ( int x, int y, unsigned char color )

{
unsigned short curr_vesa_seg = 0xffff ;
unsigned short vesa_seg,vesa_offset ;
unsigned long seg_size = 0xffff + 1L ;
unsigned long offset ;
offset = ( ( unsigned long ) y * ( unsigned long ) (MAXX+1) + ( unsigned long ) x ) ;
vesa_seg = offset / seg_size ;
vesa_offset = offset % seg_size ;
if ( vesa_seg != curr_vesa_seg )

{
set_vesa_seg ( vesa_seg ) ;
curr_vesa_seg = vesa_seg ;
}
pokeb ( 0xA000, ( unsigned ) vesa_offset, color) ;
}
void line(int xa, int ya, int xb, int yb,int c)

{
int dx=xb-xa,dy=yb-ya,steps,k;
float xi,yi,x=xa,y=ya;
if(abs(dx) > abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xi=dx/(float)steps;
yi=dy/(float)steps;
putpixel(round(x),round(y),c);
for(k=0;k<steps;k++)

{
x+=xi;
y+=yi;
putpixel(round(x),round(y),fc);
}
}
void InitGraph()

{
setsvga ( 0×103) ;// *** fro Win XP use 0×101 for 640*480*256
InitPalette(colorP);
SetPalette(colorP);
// 103 for 800*600*256
}
void FinishGraph()

{
setsvga (0×03) ;
}
void Reset()

{
FinishGraph();
InitGraph();
Boundary(1);
Axis(2);
}
public:
CGBase()

{
InitGraph();
ClearPort();
ClearPort();
Axis(GREEN);
fc=1;
bc=0;
}
void putImage(int left,int top,int right,int bot)

{
unsigned short curr_vesa_seg = 0xffff ;
unsigned short vesa_seg,vesa_offset ;
unsigned long seg_size = 0xffff + 1L ;
unsigned long offset ;
int t;
if(left>right)

{
t=left;
left=right;
right=t;
}
if(top>bot)

{
t=bot;
bot=top;
top=t;
}
char color;
for(int x=left ; x<=right;x++)
for(int y=top ; y<=bot;y++)

{
color=Timg[x][y];
offset = ( ( unsigned long ) y * ( unsigned long ) (MAXX+1) + ( unsigned long ) x ) ;
vesa_seg = offset / seg_size ;
vesa_offset = offset % seg_size ;
if ( vesa_seg != curr_vesa_seg )

{
set_vesa_seg ( vesa_seg ) ;
curr_vesa_seg = vesa_seg ;
}
pokeb ( 0xA000, ( unsigned ) vesa_offset, color ) ;
}
}
~CGBase()

{
FinishGraph();
}
void ClearPort()

{
for( int i=0;i<MAXX;i++)
for(int j=0;j<MAXY;j++)
Timg[i][j]=0;
putImage(MINX,MINY,MAXX,MAXY);
}
void PutPixel(int x, int y , int c ,int rx=CentreX,int ry=CentreY)

{
if( (rx+x)<MAXX &&(rx+x)>MINX && (ry-y)<MAXY &&(ry-y)>MINY )
putpixel(rx+x,ry-y,c);
}
void Axis(int c)

{
LineAbs(CentreX,MINY,CentreX,MAXY,c);
LineAbs(MINX,CentreY,MAXX,CentreY,c);
}
void Boundary(int c)

{
RectangleAbs(MINX,MINY,MAXX,MAXY,c);
}
void LineAbs(int x1,int y1,int x2,int y2,int c=WHITE)

{
SetColor(c);
line(x1,y1,x2,y2,c);
}
void RectangleAbs(int x1,int y1,int x2,int y2,int c=WHITE)

{
SetColor(c);
Line(x1,y1,x1,y2,c);
Line(x1,y2,x2,y2,c);
Line(x2,y2,x2,y1,c);
Line(x2,y1,x1,y1,c);
}
// line according to coordinates
void Line(int x1,int y1,int x2,int y2,int c)

{
SetColor(c);
line(x1+CentreX,CentreY-y1,x2+CentreX,CentreY-y2,c);
}
void Circle(int xCenter, int yCenter, int radius,unsigned char color=WHITE)

{
float x=0;
float y=radius;
float p=1-radius;//p=(x+1)^2+(y-1/2)^2-r^2
while(x <= y)

{
PutPixel(xCenter+x, yCenter+y, color);
PutPixel(xCenter-x, yCenter+y, color);
PutPixel(xCenter+x, yCenter-y, color);
PutPixel(xCenter-x, yCenter-y, color);
PutPixel(xCenter+y, yCenter+x, color);
PutPixel(xCenter-y, yCenter+x, color);
PutPixel(xCenter+y, yCenter-x, color);
PutPixel(xCenter-y, yCenter-x, color);
x+=1;
if(p < 0)
p = p + 2 * (x + 1) – 1; //x=x+1,y=y
else

{
y–;
p = p + 2 * (x – y) + 1; //x=x+1,y=y-1
}
}
}
void Rectangle(int x1,int y1,int x2,int y2,int c )

{
SetColor(c);
RectangleAbs(x1+CentreX,CentreY-y1,x2+CentreX,CentreY-y2,c);
}
void SetColor(unsigned int c)

{
fc=c;
}
void SetBkColor(unsigned int c)

{
bc=c;
}
};

// a brief example for How to intiate th
//     e code
void main()

{
int i=0,j=0,k=0,c=0;
getch();
CGBase g;
for(i=0;i<=275;i++)

{
g.Circle(0,0,i,i);
}
getch();
}

C++ Code to cound the number of words in file

A Word Statistics Tool to count and report words statistics of an input file. The output is displayed in alphabetical order of the words with the counts. Demonstrates a friend class, operator overloading, file iostream and text parsing. It processes 337KByte text file in ~3 seconds on AMD 550MHz. And there’s no limit on the length of each line – a benefit of using STL string. The zip contains a ready-to-use console app along with the source and a make file. The source code should be compilable using any compiler that supports STL, and default template parameter values.

COMPILATION

There are two ways to compile the source code in MSVC6.0. From the command
console, type:
1. nmake wordstat.mak
or
2. cl -GX wordstat.cpp

USAGES

The user can provide an input file or cin from the console. If you provide
a file name as an argument, the program will open the file and read the
input from the file.

D:\temp\wordstat>wordstat input.txt

If you do not provide any argument, the program will wait for the
user’s input. Be sure to finish the console input with Ctrl+Z followed by
two enters.

D:\temp\wordstat>wordstat
hello this is john travolta.

wordstat.cpp

#include <list>
#include <string>
#include <iostream>
#include <fstream>

class node
{
public:

    node(std::string& s) : word(s), count(1) {}; // ctor

    bool operator<(node const& n) const {
        return (word < n.word);
    };

    bool operator==(node& n) {
        if(word == n.word) {
            this->count++; // duplicate found. Increase counter.
            return true;
        }
        else
            return false;
    };

    friend std::ostream& operator<<(std::ostream& os, const node& n);

private:

    std::string word;
    int count; /* word counter */

};

std::ostream& operator<<(std::ostream& os, const node& n)
{
    os << n.count << "\t" << n.word;
    return os;
}

// retrieve text from an input stream
// and build words list.
bool retrieve_text(std::list<node>& words,  // words list
std::istream &is,  // stream to open
const std::string& delim) // delimiter; used to parse text
{

    std::string textline;
    while(is >> textline) {
        std::string::size_type pos = 0, pos_end;
        if((pos = textline.find_first_not_of(delim, pos)) != std::string::npos) {
            while((pos_end = textline.find_first_of(delim, pos)) != std::string::npos)
            {
                if( pos != pos_end)
                    words.push_back(textline.substr(pos, pos_end - pos));
                pos = textline.find_first_not_of(delim, pos_end);
            }
            if( pos != pos_end)
                words.push_back(textline.substr(pos, pos_end - pos));
        }
    }
    return true;
}

int main(int argc, char **argv)
{
    const std::string delim = " \t\n\r.,;:()\"";
    std::list<node> words;

    if(argc >= 2) {
        std::ifstream file;
        file.open(argv[1]);
        if(file)
            retrieve_text(words, file, delim);
        else{
            std::cerr<< "File not found.\n";
            exit(-1);
        }
    }
    else{
        retrieve_text(words, std::cin, delim);
    }

    words.sort();
    words.unique(); // remove duplicates

    // display word statistics
    std::list<node>::iterator iter=words.begin(), iter_end = words.end();
    for(; iter != iter_end; ++iter)
        std::cout << (*iter) << std::endl;

    return 0;
}

Makefile – wordstat.mak

#-------------------------------------------------------------
# Makefile - nmake wordstat.mak
#-------------------------------------------------------------
CC=cl
LINKER=link
CFLAGS=-c -GX
OUTEXE=wordstat.exe
OBJS=wordstat.obj

$(OUTEXE): $(OBJS)
	$(LINKER) -OUT:$(OUTEXE) $(OBJS)

wordstat.obj: wordstat.cpp
	$(CC) $(CFLAGS) wordstat.cpp

lexical analyzer using C++

lexical analysis is the process of converting a sequence of characters into a sequence of tokens. Programs performing lexical analysis are called lexical analyzers or lexers. A lexer is often organized as separate scanner and tokenizer functions, though the boundaries may not be clearly defined.

This c++ program is based on first step for a compiler which is LEXICAL ANALYZER.Show you total no of digit, small,capital,space,other symbol used in your program as well as any file.

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<dos.h>
#include<ctype.h>
void main()
{
clrscr();

char a,l;
char w[100];
cout<<”Enter any  file path\n”;
gets(w);
int i,countd=0,counts=0,countc=0,j=0,p=0;
ifstream m(w);
for(int g=0;g<=100;g++)
{
delay(50);
clrscr();
cprintf(”Please wait while buffering “);cout<<g<<”%”;
}
clrscr();
cout<<”\n*************** BOF *******************\n”;
while(m)
{
m.get(a);
cout<<a;
i=a;
if(ispunct(a))
{    p++;
}

if(i>=48 && i<=57)
{   countd++;
}
if(i>=65 && i<=90)
{   countc++;
}
if(i>=97 && i<=122)
{
counts++;
}
if(i==32)
{
j++;
}
if(m.eof())
{
cout<<”\n*************** EOF ******************8\n”;
}
}
highvideo();
cprintf(”\nTotal no. of CAPITAL letter in this file :–”);cout<<countc<<”\n”;
cprintf(”Total no. of SMALL letter in this file   :–”);cout<<counts<<”\n”;
cprintf(”Total no. of DIGIT letter in this file   :–”);cout<<countd<<”\n”;
cprintf(”Total no. of SPACE                       :– “);cout<<j<<endl;
cprintf(”Total no. of OTHER SYMBOL                :– “);cout<<p<<endl;
cprintf(”TOTAL USED TOKEN WITHOUT SPACE           :– “);cout<<counts+countd+countc+j;
getch();
}


Code for Logarithm using C++

This is a simple C++ program which calculte the Logarithem without any builtin function of the compiler header files. It will perform three calculations.

  1. Natural Log (Ln)
  2. Log with Base 10(Log10)
  3. Log of your desired Base (LogA^x)

#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
//////////////////////// Class For Logar
//     ithemic Functions \\\\\\\\\\\\\\\\\\\
class logarithem

    {
    protected:
    long ans;
    public:
    	 logarithem():ans(0)

        	 {
        	 /* cout<<"Enter the value of 'X':";
        	cin>>ans; */
        	 }
        	double loga(double);
        	double ln(double x);
        	double checking(double);
    };

    //////////////// For Log10 \\\\\\\\\\\\\
    //     \\\\\\\\\\\
    double logarithem::loga(double b)

        {
        double LOG10=0,c;
        c=ln(b);
        LOG10=c/2.30258509299404;
        return LOG10;
    }

    /////////////////// For Natural Log \\\\
    //     \\\\\\\\\\\\\\\\\\\\
    double logarithem:: ln(double x)

        {
        	double lnX=0.000001,check;

            	do{
            		lnX=lnX-(exp(lnX)-x)/exp(lnX);
            		check=checking(lnX);
            	 }while(check!=0);
            	return lnX;
        }

        ////////////// Check Function \\\\\\\\\\
        //     \\\\\
        double logarithem::checking(double x)

            {
            	static double x1=0;
            	double x2;
            	 if(x1==0)

                	{
                	 x1=x;
                	 return 1;
                	}
                	 else

                    	 {
                    		 x2=x1-x;
                    		 if(x2<0)
                    		x2*=-1;
                    		x1=x;
                    	return x2;
                    	 }
                }

                ////////////////////////////////////////
                ////////////////////////////////
                void main()

                    {
                    clrscr();
                    logarithem a;
                    double b=0,k=0;
                    int opt1;
                    char ch;
                    		clrscr();
                    		highvideo();
                    		textcolor(BLACK);
                    		textbackground(3);
                    		one:
                    		cout<<" ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß"<<endl;
                    		cout<<"\t\t\tÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ";
                    		cout<<"\n\t\t\tÛ Student Logarithem Panel Û "<<endl;
                    		cout<<"\t\t\tÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ"<<endl;
                    		cout<<"\nÉÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
                    		cout<<"\nº Option #º Logarithemic Function\t\t";
                    		cout<<"\nÌÍÍÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
                    		cout<<"\nº1.º for Natural Log (ln) ";
                    		cout<<"\nº2.º for Log of Base 10";
                    		cout<<"\nº3.º for Log of x with Base a, Log a^x = Logx/Loga";
                    		cout<<"\nº5.º for Exit";
                    		cout<<"\nº º";
                    		cout<<"\n\n\n\tEnter Your Option:- ";
                    		cin>>opt1;
                    	 switch(opt1)

                        	 {
                        	 case 2:
                        		cout<<"\n\nEnter the value of X : ";
                        		cin>>b;
                        		cout<<"\n\n\t( Log_10^"<<b<< " is "<<a.loga(b)<<" )";
                        		cout<<endl<<"\n\tPress any key to go back....";
                        		ch=getch();
                        		if(ch=='\r')
                        		clrscr();
                        		goto one;
                        	 case 1:
                        		 cout<<"\n\nEnter the value of X"<<"\t";
                        		 cin>>b;
                        		 cout<<"\n\n\t( Ln "<<b<<" : "<<a.ln(b)<<" )";
                        		 cout<<endl<<"\n\tPress any key to go back....";
                        		 ch=getch();
                        		 if(ch=='\r')
                        		 clrscr();
                        		 goto one;
                        	 case 3:
                        		 cout<<"\n\nEnter the value of a :"<<"\t";
                        		 cin>>b;
                        		 cout<<"\nEnter the value of x :"<<"\t";
                        		 cin>>k;
                        		 cout<<"\n\n\t( Log a^x = Logx/Loga "<<b<<" : "<<a.ln(k)/a.ln(b)<<" )";
                        		 cout<<endl<<"\n\tPress any key to go back....";
                        		 ch=getch();
                        		 if(ch=='\r')
                        		 clrscr();
                        		 goto one;
                        	 case 5:
                        		delay(100);
                        		exit(0);
                        	 default:
                        		cout<<"\n\n\tPlease! Enter the options shown above ...";
                        		cout<<endl<<"\n\tPress any key to go back....";
                        		ch=getch();
                        		if(ch=='\r')
                        		clrscr();
                        		goto one;
                        	 }

                    }