I am at my wits end on a graphics error I get on my program. It is the last thing I need to do to finish the code, but it is also the biggest problem.
I am using Borland C++ ver 3.1 (Dos mode). The program bombs when it returns to the menu after do some stuff. The error occurs at this point in the code:
/******* GRAPHICS FUNCTIONS ********/
void graphicsOn(void)
{
int graphDriver,graphMode,errorCode;
graphDriver = DETECT;
initgraph(&graphDriver, &graphMode, "C:\\BORLANDC\\BGI");
errorCode=graphresult();
if(errorCode!=grOk)
{
cout<<"Graphics Problem";
getch();
*************************
errorCode =-3.
I've tried everything I can think of, but I still get the error. Below is the code for the entire program. If anyone can figure out what the problem is, I would very much appreciate it.....
//Mbestb.cpp
//April 24, 2002
//includes improvements with 3d bar
//includes lift() and vertex[0] height improvments
//includes drawWeight() function
//includes updates to saturate()
//initial values for the vertices are done correctly.
//and move with the vertices
//This version works best for the 3 graphs
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
//#include <strstrea.h>
#include <iomanip.h>
#include <stdio.h>
#include <string.h>
struct vertex_struct
{
int prevVertex;
int nextVertex;
int excess;
int height;
int x3; //For Graphics
int y3;
int x4;
int y4;
}vertex[10];
struct side_struct
{
int startVertex;
int endVertex;
int capacity;
char saturated;
}side[10];
int list[10];
int theList[10];
int num_sides;
int num_vertices;
int xMax; //For Graphics
int yMax;
int colW;
int colH;
int preVal[10];
char sidePreVal[10];
void initialize();
void checkHeight(int);
void saturate(int,int,int,int); //new function to redraw node values
void desaturate(int);
void flow(int);
void lift(int);
void backflow(int);
void discharge(int, int);
void backDischarge(int, int);
void liftToFront();
void reArrangeList(int);
void display(char, int);
void drawReservoir(int, int, int); //For Graphics
void drawScale();
void calibrate();
void displayLift(int, int, int, int, int, int, int, int);
void drawSide(int, char);
char checkSide(int, int);
void drawWeight(int, int, int); //draw vertex weight
void displayList();
/******* GRAPHICS FUNCTIONS ********/
void graphicsOn(void)
{
int graphDriver,graphMode,errorCode;
graphDriver = DETECT;
initgraph(&graphDriver, &graphMode, "C:\\BORLANDC\\BGI");
errorCode=graphresult();
if(errorCode!=grOk)
{
cout<<"Graphics Problem";
getch();
};
};
void graphicsOff()
{
getch();
closegraph();
};
void calibrate()
{
xMax = getmaxx(); // 639 getmaxx()returns max x value (screen relative)
yMax = getmaxy()-80; //439 increased from 40 to 80
colW = xMax/6; //6
colH = yMax/8; //8
}
//draw height bar
void drawScale()
{
int colWidth = colW/2;
int thickness = 10;
int x1 = (colWidth/2)-(thickness/2);
int y1 = yMax;
int x2 = (colWidth/2)+(thickness/2);
int y2 = colH;
int numCol = 6;
setfillstyle(SOLID_FILL,CYAN);
bar3d(x1-2, y1, x2, y2, 10, 2); //3dimentional bar
int k = 7;
char * s;
//draw numbers on vertical height bar
for (int i=y2; i<=y1; i+=colH)
{
itoa(k, s, 10);
outtextxy((colWidth/2)-5, i, s);
k--;
}
//Legend for the sides
setfillstyle(SOLID_FILL, WHITE);
bar(10, 20, 30, 10);
int points[10];
points[0]=10; points[1]=37; points[2]=30; points[3]=37; points[4]=30;
points[5]=27; points[6]=10; points[7]=27; points[8]=10; points[9]=37;
setfillstyle(EMPTY_FILL, DARKGRAY);
setcolor(DARKGRAY);
fillpoly(5, points);
setcolor(WHITE);
settextstyle(SMALL_FONT, HORIZ_DIR, 5);
outtextxy(35, 8, "- Unsaturated Side");
outtextxy(35, 25, "- Saturated Side");
/** End of Legend **/
gotoxy(35, 2); cout<<"Press any key... ";
}
void drawReservoir(int pos, int height, int liftTo)
{
int thickness; //thickness of tank pole is greater for source/sink
if (pos==0) thickness=20; else thickness=10;
int tankW;
int tankH;
//if we are the first vertex (source pos==0) or the sink
//(pos==num_vertices-1) make tank bigger
if ((pos==0)||(pos==num_vertices-1)) { tankW=35; tankH=40; }
else { tankW=25; tankH=30; } //if not source or sink....
int x1 = (colW/2)-(thickness/2)+(colW/4)+(colW*pos);
int y1 = yMax;
int x2 = (colW/2)+(thickness/2)+(colW/4)+(colW*pos);
int y2 = y1-(colH*height);
int x3 = (colW/2)-(tankW)+(colW/4)+(colW*pos);
int y3 = y2-tankH;
int x4 = (colW/2)+(tankW)+(colW/4)+(colW*pos);
int y4 = y2;
if (height != liftTo)
{
//Erase the previous image if there is a change in the height else just
//overwrite the previous image.
setfillstyle(SOLID_FILL,BLACK);
bar(x3, y1+5, x4, y2); //100
}
//Draw the new Image
setfillstyle(SOLID_FILL,RED);
bar(x1, y1, x2, y2); //draw red bar w/o tank
setfillstyle(SOLID_FILL,LIGHTGRAY); //draw tank
bar(x3, y3, x4, y4);
//Erase the sides
if ((liftTo-height)!=0) //liftTo = the height of the vertex as
//defined in the struct
{
for (int i=0; i<num_sides; i++)
if ( (side[i].startVertex == pos) ||
(side[i].endVertex == pos) )
drawSide(i, 'E');
}
displayLift(x1, y2, x2, y2-(colH*(liftTo-height)), x3, x4, tankH, pos);
//Store the x and y co-ordinates
vertex[pos].x3 = x3;
vertex[pos].y3 = y2-(colH*(liftTo-height));
vertex[pos].x4 = x4;
vertex[pos].y4 = vertex[pos].y3-tankH;
// drawWeight(pos, x3, y2); //new function. Initial
//Re-draw the sides
if ((liftTo-height)!=0) //liftTo minus height
//if height has increased
{
for (int i=0; i<num_sides; i++)
if ( (side[i].startVertex == pos) ||
(side[i].endVertex == pos) )
drawSide(i, 'N');
}
}
void displayLift(int x1, int y1, int x2, int y2, int x3, int x4, int tH, int pos)
{
int i;
for (i=y1; i>=y2; i--)
{
setfillstyle(SOLID_FILL,RED);
bar(x1, y1, x2, i); //draw red bar on top of tank
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x3, i-tH, x4, i); //redraw tank over red bar
delay(10);
setfillstyle(SOLID_FILL,BLACK);
bar(x1, y1, x2, i); //erase red bar
setfillstyle(SOLID_FILL,BLACK);
bar(x3, i-tH, x4, i); //erase tank
//delay(20);
}
setfillstyle(SOLID_FILL,RED);
bar(x1, y1, x2, i); //draw red bar
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x3, i-tH, x4, i); //draw tank
//Store the x and y co-ordinates
vertex[pos].x3 = x3;
vertex[pos].y3 = i-tH;
vertex[pos].x4 = x4;
vertex[pos].y4 = i;
//Draw the top and bottom Ellipses
int xAxis = (x3+x4)/2;
int xRad = (x4-x3)/2;
int yRad = 5;
setfillstyle(SOLID_FILL,LIGHTGRAY); setcolor(BLACK);
fillellipse(xAxis, i-tH, xRad, yRad);
setcolor(LIGHTGRAY);
fillellipse(xAxis, i, xRad, yRad);
drawWeight(pos, x3, y2); //Display the weights after lifting
}
void drawWeight(int m, int n, int o)
{
char * string;
// int n, o;
// drawWeight(v, vertex[v].x3, vertex[v].y3); //display weight after flow
//for (int m=0; m<num_vertices; m++)
//{
// n=vertex[m].x3;
// o=vertex[m].y3;
// Erase the previous value
setcolor(LIGHTGRAY);
settextstyle(SMALL_FONT, HORIZ_DIR, 5);
itoa (preVal[m], string, 10);
outtextxy (n+26, o-18, string);
setcolor(RED);
settextstyle(SMALL_FONT, HORIZ_DIR, 5);
itoa (vertex[m].excess, string, 10);
outtextxy (n+26, o-18, string);
preVal[m] = vertex[m].excess;
//}
}
void drawSide(int s, char erase)
{
int theColor;
int theFill = SOLID_FILL;
int coOrd[10];
if(erase=='E')
theColor=BLACK;
else if(side[s].saturated=='y')
{ theColor=DARKGRAY; theFill=EMPTY_FILL; }
else
theColor=WHITE;
setfillstyle(theFill,theColor);
setcolor(theColor);
int stVert = side[s].startVertex;
int endVert = side[s].endVertex;
// int cap = side[s].capacity;
//If the vertices are in decending order
if (stVert > endVert)
{
stVert = side[s].endVertex;
endVert = side[s].startVertex;
}
char sideOK = checkSide(stVert, endVert);
// First check for the previous position of the side. Based on the position
// of their neighbours, some sides may have been drawn previously below the
// X-axis and now they have to be drawn above the X-axis. In that case when
// erasing the previous value have to erased first.
// sideOK=='Y' denotes that the side can be drawn above the axis. But based
// on the above conditions some times we have to set sideOK to 'N'.
if ((erase=='E') && (sidePreVal[s]!='A'))
{ sideOK = 'N'; }
if ((endVert-stVert)==1 || sideOK=='Y')
{
coOrd[0] = vertex[stVert].x4;
coOrd[1] = vertex[stVert].y3;
coOrd[2] = vertex[stVert].x4;
// coOrd[3] = vertex[stVert].y3-(10);
coOrd[4] = vertex[endVert].x3;
// coOrd[5] = vertex[endVert].y3-(10);
coOrd[6] = vertex[endVert].x3;
coOrd[7] = vertex[endVert].y3;
if (erase=='E')
{
coOrd[7] = vertex[endVert].y3+2;
coOrd[1] = vertex[stVert].y3+2;
coOrd[5] = vertex[endVert].y3-13;
coOrd[3] = vertex[stVert].y3-13;
}
else
{
coOrd[5] = vertex[endVert].y3-10;
coOrd[3] = vertex[stVert].y3-10;
}
coOrd[8] = coOrd[0];
coOrd[9] = coOrd[1];
fillpoly(5, coOrd);
//Store the current position of the side.
sidePreVal[s] = 'A'; //A denotes the side is above the x-axis
//Display the side's capacity
char * string;
settextstyle(SMALL_FONT, HORIZ_DIR, 5);
itoa(side[s].capacity, string, 10);
int xAdj=10;
int yAdj=10;
if( vertex[stVert].y3==vertex[endVert].y3 ) //if the side is straight
{ xAdj = -15; yAdj = 20; }
if(erase=='E')
{ coOrd[1]-=2; coOrd[5]+=3; }
outtextxy(((coOrd[0]+coOrd[4])/2)+xAdj, ((coOrd[1]+coOrd[5])/2)-yAdj, string);
}
else
{
int yAxis = getmaxy();
int diff = endVert-stVert;
int sx1 = (vertex[stVert].x4-((diff-2)*10))-6;
int sy1 = yAxis-(114/diff);
int sx2 = vertex[stVert].x4-((diff-2)*10);
int sy2 = vertex[stVert].y3;
int ex1 = vertex[endVert].x3+((diff-2)*10);
int ey1 = yAxis-(114/diff);
int ex2 = vertex[endVert].x3+((diff-2)*10)+6;
int ey2 = vertex[endVert].y3;
bar(sx1, sy1, sx2, sy2);
bar(ex1, ey1, ex2, ey2);
bar(sx1, ey1, ex1, ey1-6);
//Store the current position of the side.
sidePreVal[s] = 'B'; //B denotes the side is below the x-axis
//Display the side's capacity
char * string;
settextstyle(SMALL_FONT, HORIZ_DIR, 5);
itoa(side[s].capacity, string, 10);
outtextxy(((sx1+ex1)/2), ey1-20, string);
}
}
char checkSide(int stVert, int endVert)
{
char check = 'N';
//if(stVert==1 && endVert==3) cout<<"YES";
if ((endVert-stVert)>1)
{
for (int i=stVert+1; i<endVert; i++)
{
if ((abs(vertex[stVert].height-vertex[i].height) > 1) &&
abs(vertex[endVert].height-vertex[i].height) >=0 )
{ check='Y'; }
else
{ check='N'; }
}
}
return check;
}
void displayCap(int s, char erase, int theColor)
{
}
void displayList()
{
int j=0;
/* char * ht;
char * wt;
char * string;
settextstyle(SMALL_FONT, HORIZ_DIR, 5);
itoa(vertex[j].excess, wt, 10);
strcat(string, "Vertex[");
strcat(string, wt);
outtextxy(380, 40, "Vertex[" );
outtextxy(420, 40, wt);
*/
//gotoxy(49,4);
//cout <<" ";
//gotoxy(49,4);
//cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
// printf("VERTEX[%1d] = %3d Ht: %1d", j, vertex[j].excess, vertex[j].height);
// printf("VERTEX[%1d] = %3d Ht: %1d", j, 99, 0);
/* j++;
gotoxy(49,5);
cout <<" ";
gotoxy(49,5);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
j++;
gotoxy(49,6);
cout <<" ";
gotoxy(49,6);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
j++;
gotoxy(49,7);
cout <<" ";
gotoxy(49,7);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
j++;
gotoxy(49,8);
cout <<" ";
gotoxy(49,8);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
*/
gotoxy(49,4);
cout <<" ";
gotoxy(49,4);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
// printf("VERTEX[%1d] = %3d Ht: %1d", j, vertex[j].excess, vertex[j].height);
// printf("VERTEX[%1d] = %3d Ht: %1d", j, 99, 0);
j++;
gotoxy(49,5);
cout <<" ";
gotoxy(49,5);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
j++;
gotoxy(49,6);
cout <<" ";
gotoxy(49,6);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
j++;
gotoxy(49,7);
cout <<" ";
gotoxy(49,7);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
j++;
gotoxy(49,8);
cout <<" ";
gotoxy(49,8);
cout<<"Vertex["<<j<<"] = "<<vertex[j].excess<<" Ht: "<<vertex[j].height;
}
/*for(int j=0; j<num_vertices-2; j++)
{
if (c==j)
{
gotoxy(57, 2+j);
cout<<" "; //Erase the previous value
gotoxy(57, 2+j);
cout<<"-->List["<<j<<"] = "<<theList[j];
}
else
{
gotoxy(57, 2+j);
cout<<" ";
gotoxy(60, 2+j);
cout<<"List["<<j<<"] = "<<theList[j];
}
}
*/
//}
/******* End of Graphics Functions ********/
void initialize()
{
//m_sides = 7;
//m_vertices = 5;
//rtex[0].nextVertex = 1; // s
//rtex[0].excess = 26;
//vertex[0].height = 5;
//rtex[0].height=num_vertices;
//rtex[1].prevVertex = 0; // x
//rtex[1].nextVertex = 3;
//rtex[1].excess = 0;
//rtex[1].height = 0;
//rtex[2].prevVertex = 1; // y
//rtex[2].nextVertex = 3;
//rtex[2].excess = 0;
//rtex[2].height = 0;
//rtex[3].prevVertex = 2; // z
//rtex[3].nextVertex = 4;
//rtex[3].excess = 0;
//rtex[3].height = 0;
//rtex[4].prevVertex = 3; // t
//rtex[4].excess = 0;
//rtex[4].height = 0;
//de[0].startVertex = 0; // s - x
//de[0].endVertex = 1;
//de[0].capacity = 12;
//de[0].saturated = 'n';
//de[1].startVertex = 0; // s - y
//de[1].endVertex = 3;
//de[1].capacity = 14;
//de[1].saturated = 'n';
//de[2].startVertex = 1; // x - y
//de[2].endVertex = 2;
//de[2].capacity = 5;
//de[2].saturated = 'n';
//se[3].startVertex = 2; // x - t
//de[3].endVertex = 3;
//de[3].capacity = 8;
//side[3].saturated = 'n';
//side[4].startVertex = 2; // y - z
//side[4].endVertex = 4;
//side[4].capacity = 16;
//side[4].saturated = 'n';
//side[5].startVertex = 3; // z - t
//side[5].endVertex = 4;
//side[5].capacity = 10;
//side[5].saturated = 'n';
//side[6].startVertex = 3; // z - x
//side[6].endVertex = 1;
//side[6].capacity = 7;
//side[6].saturated = 'n';
//for (int i=1; i<4; i++) // initialize the lists
//{
//list[i-1] = i;
//theList[i-1] = i;
//}
//list[3]=4; list[4]=0;
//Draw the Reservoirs
int i;
//num_vertices=7;
for(i=0; i<num_vertices; i++) //Initially draw the reservoirs as height=0
drawReservoir(i, 0, 0);
getch();
displayList();
for(i=0; i<num_vertices; i++) //Then increase the height
drawReservoir(i, 0, vertex[i].height);
for(i=0; i<num_sides; i++) //Draw the Sides
drawSide(i,'N');
getch();
displayList();
for(i=0; i<num_vertices; i++)
preVal[i] = vertex[i].excess;
} // End of initialize
void starta()
{
num_sides = 7;
num_vertices = 5;
vertex[0].nextVertex = 1; // s
vertex[0].excess = 26;
//vertex[0].height = 5;
vertex[0].height=num_vertices;
vertex[1].prevVertex = 0; // x
vertex[1].nextVertex = 2;
vertex[1].excess = 0;
vertex[1].height = 0;
vertex[2].prevVertex = 1; // y
vertex[2].nextVertex = 3;
vertex[2].excess = 0;
vertex[2].height = 0;
vertex[3].prevVertex = 2; // z
vertex[3].nextVertex = 4;
vertex[3].excess = 0;
vertex[3].height = 0;
vertex[4].prevVertex = 3; // t
vertex[4].excess = 0;
vertex[4].height = 0;
side[0].startVertex = 0; // s - x
side[0].endVertex = 1;
side[0].capacity = 12;
side[0].saturated = 'n';
side[1].startVertex = 0; // s - y
side[1].endVertex = 2;
side[1].capacity = 14;
side[1].saturated = 'n';
side[2].startVertex = 1; // x - y
side[2].endVertex = 2;
side[2].capacity = 5;
side[2].saturated = 'n';
side[3].startVertex = 1; // x - t
side[3].endVertex = 4;
side[3].capacity = 16;
side[3].saturated = 'n';
side[4].startVertex = 2; // y - z
side[4].endVertex = 3;
side[4].capacity = 8;
side[4].saturated = 'n';
side[5].startVertex = 3; // z - t
side[5].endVertex = 4;
side[5].capacity = 10;
side[5].saturated = 'n';
side[6].startVertex = 3; // z - x
side[6].endVertex = 1;
side[6].capacity = 7;
side[6].saturated = 'n';
for (int i=1; i<4; i++) // initialize the lists
{
list[i-1] = i;
theList[i-1] = i;
}
list[3]=4; list[4]=0;
}
void startb()
{
num_sides = 5;
num_vertices = 4;
vertex[0].nextVertex = 1; // s
vertex[0].excess = 34;
//vertex[0].height = 2;
vertex[0].height=num_vertices;
vertex[1].prevVertex = 0; // x
vertex[1].nextVertex = 2;
vertex[1].excess = 0;
vertex[1].height = 0;
vertex[2].prevVertex = 1; // y
vertex[2].nextVertex = 3;
vertex[2].excess = 0;
vertex[2].height = 0;
/*
vertex[3].prevVertex = 2; // z
vertex[3].nextVertex = 4;
vertex[3].excess = 0;
vertex[3].height = 0;
*/
vertex[4].prevVertex = 3; // t
vertex[4].excess = 0;
vertex[4].height = 0;
side[0].startVertex = 0; // s - x
side[0].endVertex = 1;
side[0].capacity = 14;
side[0].saturated = 'n';
side[1].startVertex = 0; // s - y
side[1].endVertex = 2;
side[1].capacity = 20;
side[1].saturated = 'n';
side[2].startVertex = 1; // x - y
side[2].endVertex = 2;
side[2].capacity = 6;
side[2].saturated = 'n';
side[3].startVertex = 2; // x - t
side[3].endVertex = 3;
side[3].capacity = 11;
side[3].saturated = 'n';
/*
side[4].startVertex = 3; // y - z
side[4].endVertex = 4;
side[4].capacity = 12;
side[4].saturated = 'n';
side[5].startVertex = 1; // z - t
side[5].endVertex = 4;
side[5].capacity = 19;
side[5].saturated = 'n';
*/
side[4].startVertex = 3; // z - x
side[4].endVertex = 1;
side[4].capacity = 9;
side[4].saturated = 'n';
for (int i=1; i<3; i++) // initialize the lists
{
list[i-1] = i;
theList[i-1] = i;
}
list[2]=3; list[3]=0;
}
void startc()
{
num_sides = 7;
num_vertices = 5;
vertex[0].nextVertex = 1; // s
vertex[0].excess = 26;
//vertex[0].height = 5;
vertex[0].height=num_vertices;
vertex[1].prevVertex = 0; // x
vertex[1].nextVertex = 2;
vertex[1].excess = 0;
vertex[1].height = 0;
vertex[2].prevVertex = 1; // y
vertex[2].nextVertex = 3;
vertex[2].excess = 0;
vertex[2].height = 0;
vertex[3].prevVertex = 2; // z
vertex[3].nextVertex = 4;
vertex[3].excess = 0;
vertex[3].height = 0;
vertex[4].prevVertex = 3; // t
vertex[4].excess = 0;
vertex[4].height = 0;
side[0].startVertex = 0; // s - x
side[0].endVertex = 1;
side[0].capacity = 12;
side[0].saturated = 'n';
side[1].startVertex = 0; // s - y
side[1].endVertex = 3;
side[1].capacity = 14;
side[1].saturated = 'n';
side[2].startVertex = 1; // x - y
side[2].endVertex = 2;
side[2].capacity = 5;
side[2].saturated = 'n';
side[3].startVertex = 2; // x - t
side[3].endVertex = 3;
side[3].capacity = 8;
side[3].saturated = 'n';
side[4].startVertex = 1; // y - z
side[4].endVertex = 4;
side[4].capacity = 16;
side[4].saturated = 'n';
side[5].startVertex = 3; // z - t
side[5].endVertex = 4;
side[5].capacity = 10;
side[5].saturated = 'n';
side[6].startVertex = 3; // z - x
side[6].endVertex = 1;
side[6].capacity = 7;
side[6].saturated = 'n';
for (int i=1; i<4; i++) // initialize the lists
{
list[i-1] = i;
theList[i-1] = i;
}
list[3]=4; list[4]=0;
}
void checkHeight(int v)
{
char height = 'n';
for (int i=0; i<num_vertices; i++)
{
if (vertex[v].height > vertex[i].height)
for (int j=0; j<num_sides; j++)
if ( ((side[j].startVertex == v) && (side[j].endVertex == i) &&
(side[j].saturated == 'n')) ||
((side[j].endVertex == v) && (side[j].startVertex == i) &&
(side[j].saturated == 'n'))
)
height = 'Y';
}
if (height != 'Y')
{
// cout<<"\nIncreasing Height... V:"<<v<< " "<<vertex[v].height;
//vertex[v].height += 1;
lift(v);
//Draw the new height
drawReservoir(v, vertex[v].height-1, vertex[v].height);
/***********************************************/
// setfillstyle(SOLID_FILL, BLACK);
// bar(35, getmaxy(), 450, colH+25);
for(int a=0; a<num_vertices; a++)
{
// drawSide(a, 'E');
drawReservoir(a, vertex[a].height, vertex[a].height);
// drawSide(a, 'N');
}
/************************************************/
getch();
displayList();
}
}
void saturate(int s)
{
side[s].saturated = 'y';
for (int i=0; i<(num_vertices-2); i++)
{
int v=i;
int x=vertex[i].x3-10;
int y=vertex[i].y4+25;
// drawWeight(v,x,y);
}
}
void desaturate()
{
for (int s=0; s <num_sides; s++)
side[s].saturated = 'n';
}
void lift(int v)
{
vertex[v].height += 1;
}
void flow(int v)
{
if (vertex[v].excess == 0)
{ cout << "The source is empty."; }
else
{
checkHeight(v);
for (int l=0; l<num_vertices; l++)
{
for (int j=0; j<num_sides; j++)
{
if ((side[j].endVertex == list[l]) &&
(side[j].startVertex == v) &&
(vertex[v].height > vertex[side[j].endVertex].height)) //Check to see if the side belongs to this node
{
discharge(v, j);
// cout << "\n Vertex[" <<side[j].endVertex<<"] = " << vertex[side[j].endVertex].excess;
} //End of side-check
}
} //End of FOR "l" loop
}
//cout << "\n vertex[" <<v<< "] = " << vertex[v].excess;
} //End of Flow
void backflow(int u)
{
if (vertex[u].excess == 0)
{ cout << "The source is empty."; }
else
{
checkHeight(u);
for (int l=0; l<num_vertices; l++)
{
for (int j=0; j<num_sides; j++)
{
if ((side[j].startVertex == list[l]) &&
(side[j].endVertex == u) &&
(vertex[u].height > vertex[side[j].startVertex].height)) //Check to see if the side belongs to this node
{
backDischarge(u, j);
// cout << "\n Vertex[" <<side[j].endVertex<<"] = " << vertex[side[j].endVertex].excess;
}
}
}
}
}
void discharge(int v1, int j1)
{
if (side[j1].saturated == 'n')
{
if (vertex[v1].excess > side[j1].capacity)
{
//cout<<"\nDischarging " <<side[j1].capacity<< " From Vertex(" <<v1<< ") ==> Vertex(" <<side[j1].endVertex<< ")";
vertex[side[j1].endVertex].excess += side[j1].capacity;
vertex[v1].excess = vertex[v1].excess - side[j1].capacity;
}
else
{
//cout<<"\nDischarging " <<vertex[v1].excess<< " From Vertex(" <<v1<< ") ==> Vertex(" <<side[j1].endVertex<< ")";
vertex[side[j1].endVertex].excess += vertex[v1].excess;
vertex[v1].excess = 0;
}
saturate(j1);
}
for(int a=0; a<num_vertices; a++)
drawWeight(a, vertex[a].x3, vertex[a].y3); //display weight after flow
getch();
displayList();
}
void backDischarge(int v1, int j1)
{
if (side[j1].saturated == 'n')
{
if (vertex[v1].excess > side[j1].capacity)
{
//cout<<"\nDischarging " <<side[j1].capacity<< " From Vertex(" <<v1<< ") ==> Vertex(" <<side[j1].startVertex<< ")";
vertex[side[j1].startVertex].excess += side[j1].capacity;
vertex[v1].excess = vertex[v1].excess - side[j1].capacity;
}
else
{
//cout<<"\nDischarging " <<vertex[v1].excess<< " From Vertex(" <<v1<< ") ==> Vertex(" <<side[j1].startVertex<< ")";
vertex[side[j1].startVertex].excess += vertex[v1].excess;
vertex[v1].excess = 0;
}
saturate(j1);
}
for(int a=0; a<num_vertices; a++)
drawWeight(a, vertex[a].x3, vertex[a].y3); //display weight after flow
getch();
displayList();
}
void display(char typ, int n)
{
if (typ == 'P')
{ cout<<"\n Pre-Flow";
cout<<"\n --------";
}
else if (typ == 'F')
{ cout<<"\n\n Flow(" <<n<< ")";
cout<<"\n ---------";
}
else if (typ == 'B')
{ cout<<"\n\n BackFlow(" <<n<< ")";
cout<<"\n -------------";
}
for (int z=0; z<num_vertices; z++)
cout << "\n vertex["<<z<<"] = " << vertex[z].excess <<" Ht: "<<vertex[z].height;
cout<<"\n";
getch();
displayList();
}
void reArrangeList(int l)
{
int temp;
temp = theList[l];
for (int k=l; k>0; k--)
{ theList[k] = theList[k-1]; }
theList[0] = temp;
}
int prevVertex=0;
void liftToFront()
{
int height;
int j;
for (int i=0; i<(num_vertices-2); i++)
{
displayList();
j = theList[i];
if (vertex[j].excess > 0)
{
height = vertex[j].height;
if (prevVertex != j) // De-saturate if "theList" order changes
{ desaturate(); prevVertex = j; }
flow(j);
// display('F', j);
// for(int a=0; a<num_vertices; a++)
// drawWeight(a, vertex[a].x3, vertex[a].y3); //display weight after flow
if (vertex[j].excess > 0) {
backflow(j);
// display('B', j);
// for(int a=0; a<num_vertices; a++)
// drawWeight(a, vertex[a].x3, vertex[a].y3); //display weight after flow
}
if (vertex[j].height > height)
{
reArrangeList(i); //i is used since the position is passed
liftToFront();
}
}
}
}
void menu()
{
clrscr();
graphicsOn();
int c;
setcolor(LIGHTBLUE);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
outtextxy(145,50,"The Lift-To-Front");
outtextxy(50,85,"Maximum Flow Algorithm");
gotoxy(30,10);
cout<<"Choose a Max Flow Graph"<<endl<<endl;
gotoxy(25,13);
cout<<"1...5 Vertices, 7 Sides, Source Tank=26"<<endl;
gotoxy(25,14);
cout<<"2...4 Vertices, 5 Sides, Source Tank=34"<<endl;
gotoxy(25,15);
cout<<"3...5 Vertices, 7 Sides, Source Tank=26"<<endl;
gotoxy(25,16);
cout<<"4...Exit"<<endl;
cin>>c;
switch(c)
{
case 1:;
starta();
break;
case 2:;
startb();
break;
case 3:;
startc();
break;
case 4:;
{
graphicsOff();
exit(0);
break;
}
}
}
void main()
{
while (1==1) {
menu();
graphicsOff();
graphicsOn();
calibrate(); //determine screen display attributes
drawScale(); //draw height bar
initialize();
int temp = vertex[0].excess;
flow(0); //This is actually Pre-Flow
vertex[0].excess -= temp;
liftToFront();
for(int i=0; i<num_sides-1; i++)
drawSide(i, 'N');
/*
for(int i=0; i<13; i++)
{ setfillstyle(i, i);
bar(100, ((i+1)*20)+10, 200, (i+1)*20);
}
*/
graphicsOff();
}
} //End of main