Sie sind auf Seite 1von 6

#include <conio.

h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>

#define UP 72
#define DOWN 80

int choice;
int e[]={1,2,3,4,5,6,7,8,9}, c[9];
int n, r;
long int total=1, str[1000][9];
int temp, last, loc, p;
int x, y;

main()
{
int page, curp;
int m, start;
char key='\0';

clrscr();

printf("\n\n\n\n\t\t\tProject in Combinatorics with Graph Theory");


printf("\n\n\n\t\t\tCreated by: Elgin Jan D. Padura");
printf("\n\t\t\t\t Jose Antonio Perez");

getch();
clrscr();
printf("Permutation and Combination");

printf("\n\n1 Permutation\n(maximum is 8 taken 3!)");


printf("\n2 Combination\n(maximum is 9 taken 9!)");
printf("\n3 Exit");

printf("\n\nEnter number of choice: ");


scanf("%d",&choice);

clrscr();

if(choice==1)
permute();
else if(choice==2)
combine();
else if(choice==3)
exit(0);
else
{
gotoxy(25,7);
scanf("%d",&choice);
}

clrscr();

key='\0';
start=0;
curp=1;

if((total%120)==0)
page=total/120;
else
page=(total/120)+1;

for(x=1;x<=61;x+=15)
{
for(y=1;y<=24;y++)
{
if(start<total)
{
gotoxy(x,y);
for(m=0;m<r;m++)
printf("%d ",str[start][m]);
start++;
}
}
}

while(key!='\r')
{
if(kbhit())
{
key=(char)getch();
if(key==UP)
{
if(curp>1)
{
clrscr();
curp--;
}
}
else if(key==DOWN)
{
if(curp<page)
{
clrscr();
curp++;
}
}

start=(curp*120)-120;

for(x=1;x<=61;x+=15)
{
for(y=1;y<=24;y++)
{
if(start<total)
{
gotoxy(x,y);
for(m=0;m<r;m++)
printf("%d ",str[start][m]);
start++;
}
}
}
}
}
}

permute()
{
int m;
clrscr();
datafeed();
loc=r-1;

for(m=0;m<total;m++)
{
for(x=0;x<r;x++)
str[m][x]=e[x];

if(e[loc]!=last)
swaplast();
else if(e[loc]==last)
{
while(e[loc]==last)
{
loc--;
findlast();
}
loc++;
sortfirst();
loc--;
findnext();
loc=r-1;
}
}
}

combine()
{
int m;
clrscr();
datafeed();
loc=r-1;
p=n-1;

for(m=0;m<total;m++)
{
for(x=0;x<r;x++)
str[m][x]=e[x];

if(e[loc]!=c[p])
findnext();
else if(e[loc]==c[p])
{
while(e[loc]==c[p])
{
loc--;
p--;
}
sortremain();
moveall();
loc=r-1;
p=n-1;
}
}
}

datafeed()
{
/*1*/
gotoxy(1,1);
printf("Input value of n: ");
scanf("%d",&n);
/*2*/
gotoxy(1,2);
printf("Input value of r: ");
scanf("%d",&r);
/*3*/
if(choice==1)
{
if(n==r)
{
for(x=1;x<=n;x++)
total*=x;

gotoxy(1,4);
printf("Total permutation: %ld\n",total);
}
else if(n>r)
{
for(x=n;x!=(n-r);x--)
total*=x;

gotoxy(1,4);
printf("Total permutation: %ld\n",total);
}
else if(r>n)
{
while(n<r)
{
gotoxy(1,4);
printf("Invalid Input");

gotoxy(1,1);
printf("Input value of n: ");
scanf("%d",&n);

gotoxy(1,2);
printf("Input value of r: ");
scanf("%d",&r);
}
}

}
else if(choice==2)
{
if(n>r)
{
for(x=n;x!=(n-r);x--)
total*=x;
for(x=r;x>0;x--)
total/=x;

gotoxy(1,4);
printf("Total combination: %d\n",total);
}
else if(r>n)
{
while(r>n)
{
gotoxy(1,4);
printf("Invalid Input");

gotoxy(1,1);
printf("Input value of n: ");
scanf("%d",&n);

gotoxy(1,2);
printf("Input value of r: ");
scanf("%d",&r);
}
}
}

for(x=0;x<n;x++)
c[x]=e[x];

last=e[n-1];
}

swaplast()
{
temp=e[loc];

for(x=loc;x<(n-1);x++)
e[x]=e[x+1];

e[n-1]=temp;
}

findlast()
{
for(x=loc;x<n;x++)
c[x]=e[x];

for(y=loc;y<n;y++)
{
for(x=(y+1);x<n;x++)
{
if(c[y]>c[x])
{
temp=c[y];
c[y]=c[x];
c[x]=temp;
}
}
}

last=c[n-1];
}

sortfirst()
{
for(y=loc;y<n;y++)
{
for(x=(y+1);x<n;x++)
{
if(e[y]>e[x])
{
temp=e[y];
e[y]=e[x];
e[x]=temp;
}
}
}
}

findnext()
{
int ctr=0;

if(choice==1)
{
temp=e[loc];

for(x=(loc+1);x<n;x++)
{
if(e[x]>e[loc] && ctr==0)
{
e[loc]=e[x];
e[x]=temp;
ctr++;
}
}

last=e[n-1];
}
else if(choice==2)
{
for(x=(loc+1);x<n;x++)
{
if(e[loc]<e[x] && ctr==0)
{
temp=e[loc];
e[loc]=e[x];
e[x]=temp;
ctr++;
}
}
}
}

sortremain()
{
for(y=(loc+1);y<n;y++)
{
for(x=(y+1);x<n;x++)
{
if(e[y]>e[x])
{
temp=e[y];
e[y]=e[x];
e[x]=temp;
}
}
}
}

moveall()
{
int z, ctr=0;

for(x=(loc+1);x<n;x++)
{
if(e[loc]<e[x] && ctr==0)
{
z=x;
ctr++;
}
}

for(x=loc;x<n;x++)
{
if(z<n)
{
temp=e[x];
e[x]=e[z];
e[z]=temp;
z++;
}
}
}

Das könnte Ihnen auch gefallen