0% found this document useful (0 votes)
5 views17 pages

Prey-Predator Simulation in C++

The document describes a program that simulates prey and predator movement in a grid. It defines a PreyPredator class with private variables to track the row and column locations of the prey and predator. The class contains a fill method that initializes the grid and a run method that updates the locations each time based on different conditions comparing the relative positions of the prey and predator. The main function creates a PreyPredator object and runs its run method in a loop to continuously simulate their movement.

Uploaded by

aaqsa ansari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Prey-Predator Simulation in C++

The document describes a program that simulates prey and predator movement in a grid. It defines a PreyPredator class with private variables to track the row and column locations of the prey and predator. The class contains a fill method that initializes the grid and a run method that updates the locations each time based on different conditions comparing the relative positions of the prey and predator. The main function creates a PreyPredator object and runs its run method in a loop to continuously simulate their movement.

Uploaded by

aaqsa ansari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Prey and Predator

#include"stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<iostream>
#include<time.h>
#include<Windows.h>

using namespace std;


class PreyPredator
{
private:
int preyRowLocation, preyColumnLocation, predatorRowLocation,
predatorColumnLocation;
char arr[12][80];
int rowDifference, columnDifference;
public:
PreyPredator()
{

/*
cout << "Enter row location of predator: ";
cin >> predatorRowLocation;
cout << "Enter row location of prdator: ";
cin >> predatorColumnLocation;
cout << "Enter row location of prey: ";
cin >> preyRowLocation;
cout << "Enter column location of prey: ";
cin >> preyColumnLocation;*/
predatorColumnLocation = 2;
predatorRowLocation = 2;
preyColumnLocation = 70;
preyRowLocation = 10;
fill();

void fill()
{
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 80; j++)
arr[i][j] = '-';

}
arr[preyRowLocation][preyColumnLocation] = '+';
arr[predatorRowLocation][predatorColumnLocation] = 'C';

system("CLS");

for (int i = 0; i < 12; i++)


{
for (int j = 0; j < 80; j++)
cout<<arr[i][j];
cout << endl;
}
Sleep(500);
}

/*void computeDifference()
{

}*/

bool run()
{
if ((preyColumnLocation == predatorColumnLocation) && (predatorRowLocation
== preyRowLocation))
return false;

columnDifference = predatorColumnLocation - preyColumnLocation ;


rowDifference = predatorRowLocation - preyRowLocation ;
if (rowDifference < 0 && columnDifference < 0)
{
int tmpr = rowDifference, tmpc = columnDifference;
tmpr = -tmpr; tmpc = -tmpc;
if (tmpr > tmpc)
{
if (tmpr < 3 && tmpc < 5)
{
predatorRowLocation += 2;
preyRowLocation += 3;
}
else
{
predatorRowLocation += 2;
preyRowLocation += 1;
}
}
else
{
if (tmpr < 3 && tmpc < 5)
{
predatorColumnLocation += 5;
preyColumnLocation += 7;
}
else
{
predatorColumnLocation += 5;
preyColumnLocation += 2;
}
}
}
else if (rowDifference < 0 && columnDifference > 0)
{
int tmpr = rowDifference;
tmpr = -tmpr;
if (tmpr < columnDifference)
{
if (tmpr < 3 && columnDifference < 5)
{
predatorRowLocation += 2;
preyRowLocation += 3;
}
else
{
predatorRowLocation += 2;
preyRowLocation += 1;
}
}
else
{
if (tmpr < 3 && columnDifference < 5)
{
predatorColumnLocation += 5;
preyColumnLocation += 7;
}
else
{
predatorColumnLocation += 5;
preyColumnLocation += 2;
}
}
}
else if (rowDifference>0 && columnDifference > 0)
{
if (rowDifference < columnDifference)
{
if (rowDifference < 3 && columnDifference < 5)
{
predatorColumnLocation -= 5;
preyColumnLocation -= 7;
}
else
{
predatorColumnLocation -= 5;
preyColumnLocation -= 2;
}
}
else
{
if (rowDifference < 3 && columnDifference < 5)
{
predatorRowLocation -= 2;
preyRowLocation -= 3;
}
else
{
predatorRowLocation -= 2;
preyRowLocation -= 1;
}
}
}
else if (rowDifference>0 && columnDifference < 0)
{
int tmpc = columnDifference;
tmpc = -tmpc;
if (tmpc < rowDifference)
{
if (rowDifference < 3 && tmpc < 5)
{
predatorRowLocation -= 2;
preyRowLocation -= 3;
}
else
{
predatorRowLocation -= 2;
preyRowLocation -= 1;
}
}
else
{
if (rowDifference < 3 && tmpc < 5)
{
predatorColumnLocation += 5;
preyColumnLocation += 7;
}
else
{
predatorColumnLocation += 5;
preyColumnLocation += 2;
}
}
}
else if (rowDifference == 0 && columnDifference < 0)
{
int tmpc = columnDifference;
tmpc = -tmpc;
if (tmpc < 5)
{
predatorColumnLocation += 5;
preyColumnLocation += 7;
preyRowLocation -= 2;
}
else
{
predatorColumnLocation += 5;
preyColumnLocation += 2;
}
}
else if (rowDifference == 0 && columnDifference > 0)
{
if (columnDifference < 5)
{
predatorColumnLocation -= 5;
preyColumnLocation -= 7;
preyRowLocation += 2;
}
else
{
predatorColumnLocation -= 5;
preyColumnLocation -= 2;
}
}
else if (rowDifference < 0 && columnDifference == 0)
{
int tmpr = rowDifference;
tmpr = -tmpr;
if (tmpr<3)
{
predatorRowLocation += 2;
preyRowLocation += 3;
preyColumnLocation -= 3;
}
else
{
predatorRowLocation += 2;
preyRowLocation += 1;
}
}
else if (rowDifference > 0 && columnDifference == 0)
{
if (rowDifference < 5)
{
predatorRowLocation -= 2;
preyRowLocation -= 3;
preyColumnLocation += 3;
}
else
{
predatorRowLocation -= 2;
preyRowLocation -= 1;
}
}

////lower boundry
if (predatorColumnLocation >= 80 )
predatorColumnLocation = 1;
if (preyColumnLocation >= 80)
preyColumnLocation = 1;
if (preyRowLocation >= 12)
preyRowLocation = 1;
if (predatorRowLocation >= 12)
predatorRowLocation = 1;

///uper boundry
if (predatorColumnLocation < 1)
predatorColumnLocation = 40;
if (preyColumnLocation < 1)
preyColumnLocation = 50;
if (preyRowLocation < 1)
preyRowLocation = 4;
if (predatorRowLocation < 1)
predatorRowLocation = 6;
fill();
return true;
}
};

void main()
{
PreyPredator obj;
bool con = true;
while (con)
{
con = [Link]();
}}
Expression Evaluator
// Stack by using [Link] : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<iostream>
#define MAXSTACKSIZE 50
using namespace std;
template <class T>
class Stack
{
public:
Stack()
{
top = -1;
nodes = new T[MAXSTACKSIZE];
}
int empty()
{
if (top < 0)
return 1;
return 0;
}
T pop(void)
{
T x = NULL;
if (!empty())
{
x = nodes[top--];
return x;
}
cout << "Stack is underflow...";
return x;
}
int push(T x)
{
if (top < MAXSTACKSIZE)
{
nodes[++top] = x;
return 1;
}
cout << "Stack overflow in push...";
return 0;
}
T peek()
{
T x = NULL;
if (!empty())
{
x = nodes[top];
return x;
}
cout << "Stack underflow...";
return x;
}
~Stack()
{
delete nodes;
}
private:
int top;
T *nodes;
};

#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<iostream>
class ExpressionEvaluation
{
public:

//here user input prifix expression


void InputPrifix()
{
cout << "Enter Pri fix expression: ";
fgets(infixexp, 50, stdin);
}

//input postfix
void InputPostFix()
{

cout << "Enter Post Fix expression: ";


//cin >> postfixexp;
fgets(postfixexp, 50, stdin); // get a whole input line
/*
for (int i = 0; i < 50; i++)
{
char c = postfixexp[i];
if (!(c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || (c>'0' &&
c < '9')||(c==' ')||(c=='.')))
{
postfixexp[++i] = '$';
break;
}
}*/
}

int isdigitin(int i)
{
char x = infixexp[i];
if (x >= '0' && x <= '9')
return 1;
else
return 0;
}

int getNext(int n)
{
while (postfixexp[n] == ' ') n++;
return n;
}
int getNextin(int n)
{
while (infixexp[n] == ' ') n++;
return n;
}

int pre(char c)
{
switch (c)
{
case'^': return 0;
case'*':return 1;
case'/':return 1;
case'+':return 2;
case'-':return 2;

int prcd(char c1, char c2)


{
if (c1 == '(' || c2 == '(')
return 0;
int a = pre(c1);
int b = pre(c2);
if (a <= b)
return 1;
else
return 0;
}

void ConvertInfixToPostfix()
{
int i = 0, j = 0;
while (1)
{
i = getNextin(i);
if (infixexp[i] == '\n')
break;
else if (isdigitin(i) || (infixexp[i] == '-' && isdigitin(i + 1)) ||
infixexp[i] == '.')
{
requiredexp[j] = infixexp[i];
j++;
i++;
}
else
{
if (infixexp[i] == ')')
{
while ([Link]() != '(')
{
char c = [Link]();
requiredexp[j] = ' ';
j++;
requiredexp[j] = c;
j++;
}
[Link]();
i++;
}
if (infixexp[i] == '\n')
break;
while (![Link]() && prcd([Link](), infixexp[i]))
{
char c = [Link]();
requiredexp[j] = ' ';
j++;
requiredexp[j] = c;
j++;
i++;
}
[Link](infixexp[i]);
i++;
requiredexp[j] = ' ';
j++;

}
}
while (![Link]())
{
char c = [Link]();
requiredexp[j] = ' ';
j++;
requiredexp[j] = c;
j++;
}
cout << requiredexp;
}

int pushFullOperand(int n)
{
int neg = 0;
float operandbeforepoint = 0, operandafterpoint = 0, fulloperand = 0;
if (postfixexp[n] == '-')
{
neg = 1;
n++;
}
int count = 0;
while ((postfixexp[n] >= '0' && postfixexp[n] <= '9') || (postfixexp[n] ==
'.'))
{
if (postfixexp[n] == '.' || count > 0)
{
if (count > 0)
{
operandafterpoint *= 10;
operandafterpoint += postfixexp[n] - '0';
n++;
count++;
}
else
{
count++;
n++;
}
}
else
{
operandbeforepoint *= 10;
operandbeforepoint += postfixexp[n] - '0';
n++;
}
}

float tmp = 0;
for (; count > 0; count--)
{
tmp = operandafterpoint / 10;
}
fulloperand = operandbeforepoint + tmp;
if (neg == 1)
fulloperand = -fulloperand;
[Link](fulloperand);
n++;
return n;
}

int isdigit(int i)
{
char x = postfixexp[i];
if (x >= '0' && x <= '9')
return 1;
else
return 0;
}

//evaluate postfix
void EvaluatePostfix()
{

int i = 0;

while (1)
{
i = getNext(i);
if (postfixexp[i] == '\n')
break;
else if (isdigit(i) || (postfixexp[i]=='-'&&isdigit(i+1)))
{
i = pushFullOperand(i);
}
else
{
float v1, v2;
float res;
v2 = [Link]();
v1 = [Link]();
switch (postfixexp[i])
{
case '+':
{
res = v1 + v2;
break;
}
case '-':
{
res = v1 - v2;
break;
}
case '*':
{
res = v1 * v2;
break;
}
case '/':
{
res = v1 / v2;
break;
}
case '^':
{
res = pow(v1, v2);
break;
}
}
[Link](res);
i++;
}

}
cout << "Result is: " << [Link]() << "\n\n";
}

private:
char postfixexp[50], infixexp[50], requiredexp[50];
Stack<float> s1;
Stack<char> s2;
};
#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
void main()
{
ExpressionEvaluation exp;
[Link]();
[Link]();
//[Link]();
//[Link]();

}
Dynamic Class
// pure dynamically [Link] : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class dynamicarray
{
public:
dynamicarray()
{
cout << "Enter initial size of array: ";
cin >> size;
arr = new int[size];
}

void check()
{
int overflow, underflow;
overflow = (size * 95) / 100;
underflow = (size * 40) / 100;
if (noOfElements >= overflow)
{
int tmpsize = size;
int *tmparr = new int[size];
for (int i = 0; i < tmpsize; i++)
{
tmparr[i] = arr[i];
}
size *= 2;
delete arr;
arr = new int[size];
for (int i = 0; i < tmpsize; i++)
{
arr[i] = tmparr[i];
}
}

if (noOfElements <= underflow)


{
int tmpsize = size;
int *tmparr = new int[size];
for (int i = 0; i < tmpsize; i++)
{
tmparr[i] = arr[i];
}
size /= 2;
delete arr;
arr = new int[size];
for (int i = 0; i < tmpsize; i++)
{
arr[i] = tmparr[i];
}
}
}

void insertlast(int x)
{
check();
arr[noOfElements] = x;
noOfElements++;
cout << "Array after insertion:";
Traverse();
}

void insertAt(int element, int index)


{
check();
if (index >= noOfElements)
{
insertlast(element);
}
else
{
int i;
noOfElements++;
for (i = noOfElements; i > index; i--)
{
arr[i] = arr[i - 1];
}
arr[index] = element;
cout << "Array after insertion:";
Traverse();
}

int totalElements()
{
return noOfElements;
}

void fill()
{
for (int i = 0; i < size; i++)
{
arr[i] = i;
noOfElements++;
}
}

int isEmpty()
{
if (noOfElements <= 0)
return 1;
else
return 0;
}

void deletelast()
{
if (!isEmpty())
{
noOfElements--;
cout << "Array after deletion:";
Traverse();
}
else
{
cout << "Array is empty...\n";
}
}

void deleteAt(int index)


{
if (!isEmpty())
{
if (index < noOfElements)
{
for (int i = index; i < noOfElements; i++)
{
arr[i] = arr[i + 1];
}
noOfElements--;
cout << "Array after deletion:";
Traverse();
check();
}
else
{
cout << "you are trying to delete at higher index...\n";
}
}
else
{
cout << "Array is empty...\n";
}
}

void searchAndDelete(int x)
{
int i;
for (i = 0; i < noOfElements; i++)
{
if (arr[i] == x)
break;
}
if (i == noOfElements)
{
cout << "Element not found...\n";
}
else
deleteAt(i);
}
void inOrderinsertion(int x)
{
int i = 0;
for (; i < noOfElements; i++)
{
if (arr[i] > x)
break;
}
if (i < noOfElements-1)
{
insertAt(x, i);
}
else
insertlast(x);
}

void Traverse()
{
for (int i = 0; i <noOfElements; i++)
cout << arr[i] << " ";
cout << endl;
cout << "total elements: " << totalElements() << endl;
}
private:
int size, noOfElements = 0;
int *arr;
};
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
void main()
{
int x=0;
dynamicarray dynamic;
[Link]();
[Link]();
while (x != -1)
{
cout << "Enter index for following method such as (1):fill() . You have to
enter: 1\n";
cout << "\n(1):insertAT\t(2):InOrderInsertion\t(3):InsertLast\n";
cout << "(4):deleteAT\t(5):deleteLast\t(6):Search and Delete\n";
cout << "(7):Traverse\t(8):fill\t\t...Enter '-1' to exit...\n";
cout << "Your Choice: "; cin >> x;
switch (x)
{
case(1) :
{
int x, y;
cout << "Enter Element: ";
cin >> x;
cout << "Enter Index: "; cin >> y;
[Link](x, y);
break;
}
case(2) :
{
int x;
cout << "Enter Element: ";
cin >> x;
[Link](x);
break;
}
case(3) :
{
int x;
cout << "Enter Element: ";
cin >> x;
[Link](x);
break;
}
case(4) :
{
int y;
cout << "Enter Index: "; cin >> y;
[Link](y);
break;
}
case(5) :
{
[Link]();
break;
}
case(6) :
{
int x;
cout << "Enter Element: ";
cin >> x;
[Link](x);
break;
}
case(7) :
{
[Link]();
break;
}
case(8) :
{
[Link]();
break;
}
default:
cout << "Invalid input...";

}
}
}

You might also like