Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int VOIDINT=666;/*used when theer is no int to print*/
char VOIDCHAR='z';/*used when ther is no char to print*/
int count=0;/* count is the variable that checks if the array is full*/
int exec=0;/*counts the number of executed lines*/
char cheio [10] ="vetorcheio";/*states a full array (and prints "arrayfull"*/
char vaziu [10] ="vetorvazio";/*states an empty array (and prints "emptyarray"*/
char fora [12] ="foradolimite";/*indicates out of array limits (and prints "outoflimit"*/
char nvaziu [13] ="vetornaovazio";/*indicates that the array is not empty*/
void saida(int respexec, int respint, char respchar)
{
if (respint==VOIDINT)
printf ("x%d %s",respexec, respchar);
if (respchar==VOIDCHAR)
printf ("x%d %d",respexec, respint);
if ((respint==VOIDINT) && (respchar==VOIDCHAR))
printf ("x%d",respexec);
}
void inserecomeco (int v[], int num, int n)
{
int i;
if (count==n)/*checks if array is full or not*/
{
exec=4;
saida (exec, VOIDINT, *cheio);
}
else
{
for (i=n-1; i>0; i--)/*moves the whole array one position to the right*/
{
v[i]=v[i-1];
}
v[0]=num;
count++;
exec=(2*n+6);
saida (exec, VOIDINT, VOIDCHAR);
}
}
void inserefim (int v[], int num, int n)
{
if (count==n)/*verifica se o vetor esta cheio*/
{
exec=3;
saida (exec, VOIDINT, *cheio);
}
else
{
v[count+1]=num;
count++;
exec=5;
saida (exec, VOIDINT, VOIDCHAR);
}
}
void removecomeco (int v[], int n)
{
int i;
if (count==0) /*checks if array is empty*/
{
exec=4;
saida (exec, VOIDINT, *vaziu);
}
else
{
for (i=0; i<n; i++);
{
v[i]=v[i+1];
}
count--;
exec=(2*n+5);
saida (exec, VOIDINT, VOIDCHAR);
}
}
void removefim (int v[], int n)
{
int i;
if (count==0) /*same as above*/
{
exec=4;
saida (exec, VOIDINT, *vaziu);
}
else
{
for (i=n-1; i>0; i--)/*moves the whole vector one position to the right*/
{
v[i]=v[i-1];
}
count--;
exec=(2*n+5);
saida (exec, VOIDINT, VOIDCHAR);
}
}
void removeposicao (int v[], int pos, int n)
{
int i;
if ((pos>n) || (pos<0) || (count==0))
{
exec=4;
saida (exec, VOIDINT, *fora);
}
else
{
for (i=pos; i<n; i++)
{
v[pos]=v[pos+1];
}
count--;
exec=(2*n+5);
saida (exec, VOIDINT, VOIDCHAR);
}
}
void insereposicao (int v[], int pos, int num, int n)
{
int i;
if ((pos>n) || (pos<0))
{
exec=4;
saida (exec, VOIDINT, *fora);
}
if (count==n)
{
exec=5;
saida (exec, VOIDINT, *cheio);
}
for (i=n-1; i>pos; i--)
{
v[i]=v[i-1];
}
v[pos]=num;
count++;
exec=(2*n+7);
saida (exec, VOIDINT, VOIDCHAR);
}
void elementoposicao (int v[], int pos, int n)
{
if ((pos>n) || (pos<0))
{
exec=3;
saida (exec, VOIDINT, *fora);
}
else
{
exec=3;
saida (exec, v[pos], VOIDCHAR);
}
}
void tamanho () // gets the size of the array
{
exec=2;
saida (exec, count, VOIDCHAR);
}
void ultimo (int v[]) //gets the last element of the array
{
if (count==0)
{
exec=3;
saida (exec, VOIDINT, *vaziu);
}
else
{
exec=3;
saida (exec, v[count], VOIDCHAR);
}
}
void primeiro (int v[]) //gets the first element of the array
{
if (count==0)
{
exec=3;
saida (exec, VOIDINT, *vaziu);
}
else
{
exec=3;
saida (exec, v[0], VOIDCHAR);
}
}
void vazio () // if array is empty, it prints it is, if its isnt, it prints its not :)
{
if (count==0)
{
exec=3;
saida (exec, VOIDINT, *vaziu);
}
else
{
exec=3;
saida (exec, VOIDINT, *nvaziu);
}
}
int main ()
{
int v[MAX]={0};
int n, pos, num;
char nomefunc;
char a [] ="inserecomeco", b [] = "inserefim", c [] = "removecomeco", d
[] = "removefim";
char e [] = "removeposicao", f [] = "insereposicao", g [] =
"elementoposicao", h [] = "tamanho";
char i [] = "ultimo", j [] = "primeiro", k [] = "vazio";
do
{
printf ("Insira o tamanho do vetor: ");/*"Insert the vectos size"*/
scanf ("%d", &n);
}while ((n<=0) && (n>100));
printf ("\nInsira a funcao desejada: ");/*Insert the function name*/
scanf ("%s", &nomefunc);
switch (nomefunc)
{
case 'a':
printf ("A funcao escolhida foi: InsereComeco.\nDigite o numero a
ser inserido: ");/*the chosen function was "InsertStart"*/
scanf ("%d", &num);
inserecomeco (v, num, n);
break;
case 'b':
printf ("A funcao escolhida foi: InsereFim.\nDigite o numero a ser
inserido: ");
scanf ("%d", &num);
inserefim (v, num, n);
break;
case 'c':
printf ("A funcao escolhida foi: RemoveComeco.");
removecomeco (v, n);
break;
case 'd':
printf ("A funcao escolhida foi: RemoveFim.");
removefim (v, n);
break;
case 'e':
printf ("A funcao escolhida foi: RemovePosicao.\nDigite a posicao a
ser removida: ");
scanf ("%d", &pos);
removeposicao (v, pos, n);
break;
case 'f':
printf ("A funcao escolhida foi: InserePosicao.\nDigite a posicao e
o numero a ser inserido(nao se esqueca de colocar um espaco entre eles): ");
scanf ("%d", &pos);
scanf ("%d", &num);
insereposicao (v, pos, num, n);
break;
case 'g':
printf ("A funcao escolhida foi: ElementoPosicao.\nDigite a posicao
a ser verificada: ");
scanf ("%d", &pos);
elementoposicao (v, pos, n);
break;
case 'h':
printf ("A funcao escolhida foi: Tamanho.");
tamanho ();
break;
case 'i':
printf ("A funcao escolhida foi: Ultimo.");
ultimo (v);
break;
case 'j':
printf ("A funcao escolhida foi: Primeiro.");
primeiro (v);
break;
case 'k':
printf ("A funcao escolhida foi: Vazio.");
vazio ();
break;
default:
printf ("Essa funcao nao eh valida.");
break;
}
system ("pause");
return 0;
}