// William Orem
// 6/18/12
// Solution to COP 3502 (Computer Science I)
// Assignment #2: Matching
//includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//defines
#define MAX 10
#define DEBUG 0
//data
struct person
{
char name[20];
int score[MAX];
};
struct session
{
struct person boyList[MAX];
struct person girlList[MAX];
struct person winning[MAX];
int score;
int numCouples;
};
void proccessSession(struct session* mySession);
struct session* readSession(FILE* ifp,struct session* mySession);
void exchangePerson(struct person girlList[MAX],struct person boylList[MAX], int i, int j,int len);
void copyPerson(struct person winning[MAX],struct person girlList[MAX],int numCouples);
void RPerm(struct session* mySession, int k);
void printWinning(struct session* mySession,int numS);
int main()
{
FILE* ifp = fopen("matching.txt", "r");
int loop, numcases;
fscanf(ifp, "%d", &numcases);
if(DEBUG)
printf("Number of sessions: %d\n",numcases);
// Go through each case.
for (loop=1; loop<=numcases; loop++)
{
//use pointer so we pass by ref
struct session* mySession = (struct session*)malloc(sizeof(struct session));
mySession->score=0;
mySession = readSession(ifp,mySession);
RPerm(mySession,0);
printWinning(mySession,loop);
}
fclose(ifp);
return 0;
}
void copyPerson(struct person winning[MAX],struct person girlList[MAX],int numCouples)
{
int loop;
for(loop=0; loopnumCouples; loop++)
{
if(mySession->boyList[loop].score[loop]girlList[loop].score[loop])
tScore += mySession->boyList[loop].score[loop];
else
tScore += mySession->girlList[loop].score[loop];
}
if(tScore > mySession->score)
{
// set as winning
copyPerson(mySession->winning,mySession->girlList,mySession->numCouples);
mySession->score = tScore;
}
else if(tScore == mySession->score)
{
//check differences
int i,diffwinning=0,diffnew=0;
for(i=0; inumCouples; i++)
{
diffwinning += abs(mySession->boyList[i].score[i]-mySession->winning[i].score[i]);
diffnew += abs(mySession->boyList[i].score[i]-mySession->girlList[i].score[i]);
}
if(diffnewwinning,mySession->girlList,mySession->numCouples);
mySession->score = tScore;
}
}
}
void RPerm(struct session* mySession, int k)
{
int j;
// Base-case
if (k == mySession->numCouples){
proccessSession(mySession);
}
else
{
// Loop through to get each possible arrangment of girls
for (j=k; jnumCouples; j++)
{
exchangePerson(mySession->girlList, mySession->boyList, k, j,mySession->numCouples);
RPerm(mySession, k+1);
exchangePerson(mySession->girlList, mySession->boyList, j, k,mySession->numCouples);
}
}
}
void printWinning(struct session* mySession,int numS)
{
int loop;
printf("Matching #%d: Maximum Score = %d.\n\n",numS,mySession->score);
for(loop=0; loopnumCouples; loop++)
{
printf("%s ",mySession->boyList[loop].name);
printf("%s \n",mySession->winning[loop].name);
}
printf("\n\n");
}
struct session* readSession(FILE* ifp,struct session *mySession)
{
int numCouples = 0, loop;
fscanf(ifp, "%d", &numCouples);
mySession->numCouples= numCouples;
if(DEBUG)
printf("Number of Couples: %d\n",numCouples);
//read boy names
for (loop=0; loopboyList[loop].name);
}
//read girlnames
for (loop=0; loopgirlList[loop].name);
}
int i,j;
//load scores
for (i=0; iboyList[i].score[j]);
}
for (i=0; igirlList[i].score[j]);
}
return mySession;
}
Match Making assignment #2 (COP3502)
Subscribe to:
Posts (Atom)
No comments:
Post a Comment