Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
typedef char string20[21];
typedef struct {
string20 relation;
string20 relationvalue;
/* other members here */
}relationtype;
struct nodeTag {
struct nodeTag *pNext;
struct nodeTag *pPrev;
string20 term;
relationtype trivia[10];
int ctrtriv;
};
typedef struct nodeTag nodestructtype;
typedef nodestructtype *ptrnode;
/*
ptrnode * arrange(ptrnode *pFirst)
{
ptrnode pNew, pTail, pRun;
pNew=
//arrange alphabeticall
if(pFirst == NULL)
return(pNew);
else if(strcmp(pNew->term, (*pFirst)->term) < 0)
{
pNew -> pNext = *pFirst;
return(pNew);
}
else {
pTail = *pFirst;
while(strcmp(pNew->term, pTail->term) >0 && pTail -> pNext != NULL)
{
pRun = pTail;
pTail = pTail-> pNext;
}
if(strcmp(pNew->term, pTail->term) < 0)
{
pRun->pNext = pNew;
pNew->pNext = pTail;
pNew->pPrev = pRun;
pTail->pPrev = pNew;
}
else
{
pTail->pNext = pNew;
pNew->pPrev = pTail;
}
return(pFirst);
}
}
void viewterm(ptrnode pFirst)
{
ptrnode pTail, pRun = pFirst,pTemp;
pTail = pRun;
do{
printf("%s",pFirst -> term);system("pause");
pRun = pRun -> pNext;
}while(pFirst -> pNext != NULL);
}
void addtrivia (ptrnode *pFirst)
{
int temp=1,y=0;
char extra;
ptrnode pNew = *pFirst, pRun, pTail;
printf("Enter Trivia:");
fgets(pNew -> trivia[y].relation,sizeof(string20),stdin);
printf("Enter Clue:");
fgets(pNew -> trivia[y].relationvalue,sizeof(string20),stdin);
// insert node at front of list
pNew->pNext = *pFirst;
*pFirst = pNew;
printf("Trivia = %s\nClue = %s", pNew -> trivia[y].relation, pNew -> trivia[y].relationvalue);
y++;
system("pause");
}*/
ptrnode addterm (ptrnode pFirst)
{
ptrnode pNew, pRun, pTail;
string20 stemp;
int ctr1 = 0,ctr2=0;
char opt,temp;
int found = 0;
pRun = pFirst;
pNew = malloc(sizeof(nodestructtype));
if(pNew != NULL)
{ pNew->pNext=NULL;
pNew->pPrev=NULL;
pNew->ctrtriv = 0;
do{
pRun = pFirst;
printf("\n\n Enter term: ");
fgets(stemp,sizeof(string20),stdin);
//check if word is unique
while(pRun != NULL)
{ if(strcmp(stemp, pRun -> term) == 0)
strcpy(stemp, "another");
pRun = pRun -> pNext;
}
printf("\nWord is unique!\n ");
}while(strcmp(stemp, "another") == 0 || strlen(stemp) < 1 || strlen(stemp) > 20);
// (pNew->ctrterm)++;
strcpy(pNew -> term, stemp);
//pTemp = arrange(pRun);
printf("Enter maximum of 10 trivias\n\n\n");
do { printf("%dst/nd/rd/th trivia\n",ctr1+1);
do{
printf("Enter trivia:");
fgets(stemp,sizeof(string20),stdin);//fgets(pNew->trivia[ctr-1].relation,sizeof(string20),stdin);
if(strlen(stemp) > 0 && strlen(stemp) < 20)
{
strcpy(pNew -> trivia[ctr1].relation, stemp);
( pNew -> ctrtriv)++;
}
}while(strlen(stemp) < 1 || strlen(stemp) > 20);
do{
printf("Enter clue to trivia:");
fgets(stemp,sizeof(string20),stdin);
// fgets(pNew->trivia[ctr1-1].relationvalue,sizeof(string20),stdin);
while(ctr2 < pNew->ctrtriv)
{ if(strcmp(pNew -> trivia[ctr2].relationvalue, stemp)== 0)//check if clue already entered
{ printf("\n Clue already used. Enter another.\n");
found = 1;
}
ctr2++;
}
if(found != 1)
strcpy(pNew -> trivia[ctr1].relationvalue, stemp);
}while(strlen(stemp) < 1 || strlen(stemp) > 20 || found == 1);
ctr1++;
printf("\nDo you want to enter another trivia?[Y/N]\n");
scanf("%c%c", &opt, &temp);
} while((opt != 'n' || opt != 'N' )&& ctr1 < 10 );
/* //Sort alphabetically
if(pFirst == NULL)
return(pNew);
else if(strcmp(pNew->term, pFirst->term) < 0)
{
pNew -> pNext = pFirst;
return(pNew);
}
else {
pTail = pFirst;
while(strcmp(pNew->term, pTail->term) >0 && pTail -> pNext != NULL)
{
pRun = pTail;
pTail = pTail-> pNext;
}
if(strcmp(pNew->term, pTail->term) < 0)
{
pRun->pNext = pNew;
pNew->pNext = pTail;
pNew->pPrev = pRun;
pTail->pPrev = pNew;
}
else
{
pTail->pNext = pNew;
pNew->pPrev = pTail;
}
return(pFirst);
}
*/
}
}
ptrnode addtrivia(ptrnode pFirst)
{
int ctr1 = 0,ctr2=0;
string20 stemp;
ptrnode pNew ,pRun,pTail;
char opt,temp;
int found = 0;
pRun = pFirst;
printf("\nTerms: \n");
while(pRun != NULL)
{ printf("- %s \n", pRun -> term);
pRun = pRun -> pNext;
}
do{
pRun = pFirst;
printf("\n Enter term you want to add trivia to: ");
fgets(stemp,sizeof(string20),stdin);
while(pRun != NULL && strcmp(pRun->term, stemp) != 0)
pRun = pRun -> pNext;
if(pRun == NULL)
printf("\nNot available\n");
}while(pRun == NULL && strcmp(stemp, "exit") != 0);
if(strcmp(stemp, "exit") != 0)
{ ctr1 = pRun -> ctrtriv;
printf(" Type exit in trivia to stop. Note: max is 10 trivia. \n\n");
do {
printf("%dst/nd/rd/th trivia\n",ctr1 + 1);
printf(" Enter trivia: ");
fgets(stemp,sizeof(string20),stdin);
if(strcmp(stemp, "exit") != 0 && strlen(stemp) > 1 && strlen(stemp) < 20)
{ strcpy(pRun -> trivia[ctr1].relation, stemp);
( pRun -> ctrtriv )++;
do {
printf(" Enter clue to trivia: ");
fgets(stemp,sizeof(string20),stdin);
while(ctr2 < pRun->ctrtriv)
{ if(strcmp(pRun -> trivia[ctr2].relationvalue, stemp)== 0)
{ printf("\n Clue already in use. Input again.\n");
found = 1;
}
ctr2++;
}
if(found != 1)
strcpy(pRun -> trivia[ctr1].relationvalue, stemp);
}while(strlen(stemp) < 1 || strlen(stemp) > 20 || found == 1);
}
ctr1++;
} while(strcmp(pRun -> trivia[ctr1-1].relation, "exit") != 0 && pRun -> ctrtriv < 10 || strlen(stemp) < 1 || strlen(stemp) > 20);
}
return(pFirst);
}
void maintenance(ptrnode pFirst)
{ char temp;
int opt;
do{
system("cls");
printf("Choose what to do:\n");
printf("1. Add term\n");
printf("2. Add trivia\n");
printf("3. View term\n");
printf("4. View trivia\n");
printf("5. Back to main\n");
scanf("%d%c",&opt,&temp);
switch(opt){
case 1: pFirst = addterm(pFirst);printf("check3 "); break;
case 2: pFirst = addtrivia(pFirst);printf("check3 ");break;
//case 3: viewterm(pFirst);break;
//case 4: viewtrivia(pFirst);break;
case 5: system("cls");break;
}
}while(opt != 5);
}
void freelist(ptrnode *pFirst)
{
ptrnode pRun;
pRun = *pFirst;
while(pFirst != NULL)
{
pRun = *pFirst;
pRun = pRun -> pNext;
free(pRun);
}
}
main()
{
char temp;
int opt;
ptrnode pFirst;
pFirst = NULL;
printf("\t\tWelcome to Word Trivia 2.0\n");
do{
printf("Enter what phase?\n");
printf("1. Maintenance Phase.\n");
printf("2. Application Phase.\n");
printf("3. Exit Program\n");
scanf("%d%c",&opt,&temp);
switch(opt)
{
case 1:system("cls");
maintenance(pFirst);
system("cls");
break;
case 2:system("cls");
//application();break;
case 3:system("cls");printf("\n\t\tGoodbye!\n");system("pause");break;
}
}while(opt != 3);
freelist(&pFirst);
}