#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Nlen 26
typedef struct person_t {
char FName[Nlen];
char LName[Nlen];
char Sex;
char Grade;
struct person_t *Next;
} PERSON;
/*********************************************************************
Funstion to Ordered linked list
This is where i will call the Last name and put it into the linked
list ordered by last name.
********************************************************************/
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
PERSON *Here, /* Node being compared to new node */
*Prev; /* Node in front of Here node */
if (Top == NULL)/* List is empty – Place new node at top of list */
Top = Node;
else { /* Top != NULL *//* Start at Top of list */
Here = Top;
if (Here->LName > Node->LName) {
Node->Next = Top;
Top = Node;
}
else {/* Search for place to insert new node */
Prev = Top;
Here = Prev->Next;
while ( (Here != NULL) &&
(Here->LName < Node->LName) ) {
Prev = Here;
Here = Here->Next;
}/* Insert into list at found spot */
Node->Next = Prev->Next;
Prev->Next = Node;
}
} /* end of Top != NULL */
return Top;
}
/*************************************************************
end of Funstion to Ordered linked list
***************************************************************/
void amend_file( char file[], PERSON *STU );
void show_file( char file[], PERSON *STU );
int main (void)
{
PERSON *STU;
int Ans;
char file[] = "data.txt";
int Records [100];
do
{
printf("\n\n"
"1. Enter new record\n"
"2. Display List\n"
"3. Quit\n\n");
printf("Prompt: ");
fflush(stdout);
if ( scanf("%d", &Ans) == 1 ) /* get answer */
{
STU = (PERSON*) malloc (sizeof (PERSON));
if (STU == NULL)
{
perror("PERSON");
exit(EXIT_FAILURE);
}
switch(Ans)
{
case 1:
amend_file(file, STU); //calls the amend file funcation
InsertNode; //calls the insert Node funcation which it is not getting into becuase
//Im haveing issues with getting the file to print.
break;
case 2:
show_file(file, STU); //calls teh show file funcation
break;
case 3: default:
Ans = 3;
break;
}
} /* /get answer */
}
while ( Ans != 3 );
fflush(stdout);
free(STU);
return EXIT_SUCCESS;
}
/*************************************************************************
* amend_file: Enter stuff into the working file.
*
**************************************************************************/
void amend_file( char file[], PERSON *STU )
{
assert(STU != NULL);
FILE *fin = fopen(file, "a+");
if ( fin == NULL )
{
perror(file);
exit(EXIT_FAILURE);
}
printf("Enter new record: ");
printf ("\nEnter students First name ( up to %d letters):", Nlen);
printf ("\nEnter students Last name ( up to %d letters):", Nlen);
printf ("\nPlease enter Sex");
printf ("\nPlease enter Grade\n");
scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade);
fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
fflush(fin);
fclose(fin);
}
/******************************************************************************
* show_file: Reads the working file.
* Bare bones again...
******************************************************************************/
void show_file ( char file[], PERSON *STU )
{
assert(STU != NULL);
int b = 1;
FILE *fout = fopen(file, "r");
if (file == NULL)
{
perror(file);
exit(EXIT_FAILURE);
}
while (fscanf(fout, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade) == 4)
{
printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
STU->Sex, STU->Grade);
b++;
// }
fclose(fout);
}
fgets(STU->FName,sizeof(STU->FName),stdin);
#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define Nlen 26 typedef struct person_t { char FName[Nlen]; char LName[Nlen]; char Sex; char Grade; struct person_t *Next; } PERSON; struct person_t *Top = NULL; struct person_t *Node = NULL; /********************************************************************* Funstion to Ordered linked list This is where i will call the Last name and put it into the linked list ordered by last name. ********************************************************************/ PERSON* InsertNode (PERSON *Top, PERSON *Node ) { PERSON *Here, /* Node being compared to new node */ *Prev; /* Node in front of Here node */ if (Top == NULL)/* List is empty – Place new node at top of list */ Top = Node; else { /* Top != NULL *//* Start at Top of list */ Here = Top; if (Here->LName > Node->LName) { Node->Next = Top; Top = Node; } else {/* Search for place to insert new node */ Prev = Top; Here = Prev->Next; while ( (Here != NULL) && (Here->LName < Node->LName) ) { Prev = Here; Here = Here->Next; }/* Insert into list at found spot */ Node->Next = Prev->Next; Prev->Next = Node; } } /* end of Top != NULL */ return Top; } /************************************************************* end of Funstion to Ordered linked list ***************************************************************/ void amend_file( char file[], PERSON *STU ); void show_file( char file[], PERSON *STU ); int main (void) { PERSON *STU; int Ans; char file[] = "data.txt"; int Records [100]; FILE *fin = fopen(file, "a+"); struct person_t *Top = NULL; struct person_t *Node = NULL; do { printf("\n\n" "1. Enter new record\n" "2. Display List\n" "3. Quit\n\n"); printf("Prompt: "); fflush(stdout); if ( scanf("%d", &Ans) == 1 ) /* get answer */ { fgets(STU->FName,sizeof(STU->FName),stdin); if (STU == NULL) { perror("PERSON"); exit(EXIT_FAILURE); } switch(Ans) { case 1: amend_file(file, STU); //calls the amend file funcation break; case 2: show_file(file, STU); //calls teh show file funcation break; case 3: default: Ans = 3; break; } } /* /get answer */ } while ( Ans != 3 ); fflush(stdout); free(STU); getchar (); return EXIT_SUCCESS; } /************************************************************************* * amend_file: Enter stuff into the working file. * **************************************************************************/ void amend_file( char file[], PERSON *STU ) { assert(STU != NULL); FILE *fin = fopen(file, "a+"); if ( fin == NULL ) { perror(file); exit(EXIT_FAILURE); } printf("Enter new record: "); printf ("\nEnter students First name ( up to %d letters):", Nlen); printf ("\nEnter students Last name ( up to %d letters):", Nlen); printf ("\nPlease enter Sex"); printf ("\nPlease enter Grade\n"); scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex, &STU->Grade); Top = InsertNode (Top, Node); //calls the insert Node funcation which it is not getting into becuase //Im haveing issues with getting the file to print. fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade); fflush(fin); fclose(fin); } /****************************************************************************** * show_file: Reads the working file. * Bare bones again... ******************************************************************************/ void show_file ( char file[], PERSON *STU ) { assert(STU != NULL); int b = 1; FILE *fin = fopen(file, "r"); if (file == NULL) { perror(file); exit(EXIT_FAILURE); } while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex, &STU->Grade)== 4) { Top = InsertNode (Top, Node); //read insernode function b++; printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName, STU->Sex, STU->Grade); } fclose(fin); }
#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define Nlen 26 typedef struct person_t { char FName[Nlen]; char LName[Nlen]; char Sex; char Grade; struct person_t *Next; } PERSON; struct person_t *Top = NULL; struct person_t *Node = NULL; /********************************************************************* Funstion to Ordered linked list This is where i will call the Last name and put it into the linked list ordered by last name. ********************************************************************/ PERSON* InsertNode (PERSON *Top, PERSON *Node ) { PERSON *Here, /* Node being compared to new node */ *Prev; /* Node in front of Here node */ if (Top == NULL)/* List is empty – Place new node at top of list */ Top = Node; else { /* Top != NULL *//* Start at Top of list */ Here = Top; if (Here->LName > Node->LName) { Node->Next = Top; Top = Node; } else {/* Search for place to insert new node */ Prev = Top; Here = Prev->Next; while ( (Here != NULL) && (Here->LName < Node->LName) ) { Prev = Here; Here = Here->Next; }/* Insert into list at found spot */ Node->Next = Prev->Next; Prev->Next = Node; } } /* end of Top != NULL */ return Top; } /************************************************************* end of Funstion to Ordered linked list ***************************************************************/ void amend_file( char file[], PERSON *STU ); void show_file( char file[], PERSON *STU ); int main (void) { PERSON *STU; int Ans; char file[] = "e:data.txt"; int Records [100]; FILE *fin = fopen(file, "a+"); struct person_t *Top = NULL; struct person_t *Node = NULL; do { printf("\n\n" "1. Enter new record\n" "2. Display List\n" "3. Quit\n\n"); printf("Prompt: "); fflush(stdout); if ( scanf("%d", &Ans) == 1 ) /* get answer */ { fgets(STU->FName,sizeof(STU->FName),stdin); if (STU == NULL) { perror("PERSON"); exit(EXIT_FAILURE); } switch(Ans) { case 1: amend_file(file, STU); //calls the amend file funcation break; case 2: show_file(file, STU); //calls teh show file funcation break; case 3: default: Ans = 3; break; } } /* /get answer */ } while ( Ans != 3 ); fflush(stdout); free(STU); getchar (); return EXIT_SUCCESS; } /************************************************************************* * amend_file: Enter stuff into the working file. * **************************************************************************/ void amend_file( char file[], PERSON *STU ) { assert(STU != NULL); FILE *fin = fopen(file, "a+"); if ( fin == NULL ) { perror(file); exit(EXIT_FAILURE); } printf("Enter new record: "); printf ("\nEnter students First name ( up to %d letters):", Nlen); printf ("\nEnter students Last name ( up to %d letters):", Nlen); printf ("\nPlease enter Sex"); printf ("\nPlease enter Grade\n"); scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex, &STU->Grade); Top = InsertNode (Top, Node); //calls the insert Node funcation which it is not getting into becuase //Im haveing issues with getting the file to print. fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade); fflush(fin); fclose(fin); } /****************************************************************************** * show_file: Reads the working file. * Bare bones again... ******************************************************************************/ void show_file ( char file[], PERSON *STU ) { assert(STU != NULL); int b = 1; FILE *fin = fopen(file, "r"); if (file == NULL) { perror(file); exit(EXIT_FAILURE); } while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex, &STU->Grade)== 4) { Top = InsertNode (Top, Node); //read insernode function b++; printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName, STU->Sex, STU->Grade); } fclose(fin); }
fgets(STU->FName,sizeof(STU->FName),stdin);
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> #include <math.h> #define ROWNO 11 #define COLNO 11 #define XVAL 40 #define YVAL 40 #define SIZE 20 struct Node { int x, y, points,state; struct Node *n,*p,*u,*d; }; struct Node *createLList(struct Node *hPtr,int gold, int trap) { struct Node *newptr, *curr,*top,*left,*above; int x = 0,y = 0,j; newptr = (struct Node *) malloc (sizeof(struct Node)); newptr->x = XVAL + x * SIZE; newptr->y = YVAL + y * SIZE; newptr->state = 0; newptr->points = 0; newptr->n = newptr; newptr->p = newptr; hPtr = curr = left = above = newptr; j=rand() %10; if((j==1)&&(gold)) {curr->points=5; gold--;} if((j==2)&&(trap)) {curr->points=-3; trap--;} for(y=1; y<=11; y++) { for(x=1; x<=10; x++) { j=rand() %10; if((j==1)&&(gold)) {curr->points=5; gold--;} if((j==2)&&(trap)) {curr->points=-3; trap--;} newptr = (struct Node *) malloc (sizeof(struct Node)); newptr->x = XVAL + x * SIZE; newptr->y = YVAL + ((y-1) * SIZE); newptr->state = 0; newptr->points = 0; newptr->p = curr; curr->n = newptr; newptr->n = left; left->p = newptr; curr = newptr; above = above->n; top=top->n; newptr->u = above; above->d = newptr; newptr->d = top; top->u = newptr; if((y==11)&&(x==10)) goto endd; } x=0; top = hPtr; above = left; j=rand() %10; if((j==1)&&(gold)) {curr->points=5; gold--;} if((j==2)&&(trap)) {curr->points=-3; trap--;} newptr = (struct Node *) malloc (sizeof(struct Node)); newptr->x = XVAL + x * SIZE; newptr->y = YVAL + y * SIZE; newptr->state = 0; newptr->points = 0; newptr->u = above; newptr->d = top; top->u = newptr; above->d = newptr; left = newptr; curr = left; } endd: return hPtr; } void drawAll(struct Node *hptr,struct Node *curr) { struct Node *temp; int x, y, row, col, color,statt; temp = curr; curr = hptr; for(y=1;y<=11;y++) { for(x=1;x<=11;x++) { switch(curr->state) { case 0:color=15; statt=4; break; case 1:color=0; statt=1; break; } setfillstyle(statt, color); bar(curr->x,curr->y,curr->x+15,curr->y+15); curr = curr->n; } curr = curr->n->d; } curr = temp; setfillstyle(1, 15); bar(curr->x,curr->y,curr->x+15,curr->y+15); } int main(int argc,char *argv[]) { int gd = DETECT, gm; char key; int i , j,gold = 0,trap = 0,points = 0; struct Node *curr, *head; FILE *fPtr; char string[100][50]; // initgraph(&gd, &gm, "e:\\prog\\TC\\bgi\\"); initgraph(&gd, &gm, "c:\\TC\\bgi\\"); head=createLList(head,atoi(argv[1]),atoi(argv[2])); curr = head; fPtr = fopen("e:\\test.dat", "r"); if(fPtr == NULL) { cprintf("Error, File could not be opened\n\r"); getch(); goto end;} else { i=1; while(1) { fscanf(fPtr, "%s", string[i]); if(feof(fPtr)) break; if(atoi(string[i]) == 1) curr->state = 1; else if(atoi(string[i]) == 0) curr->state = 0; if(i==12) {curr = curr->n->d; i=1;} else curr = curr->n; i++; } fclose(fPtr); } drawAll(head,curr); while(1) {key = getch(); if(key == 0) {key = getch();} if(key == 0x1b) break; if(points<0) break; switch(key) { case 0x4b:if(curr->p->state == 0) curr = curr->p; break; case 0x4d:if(curr->n->state == 0) curr = curr->n; break; case 0x48:if(curr->u->state == 0) curr = curr->u; break; case 0x50:if(curr->d->state == 0) curr = curr->d; break; } drawAll(head,curr); if(key == 13) {points = points + curr->points; switch(curr->points) { case 5:gold++; setfillstyle(1, 14); bar(curr->x,curr->y,curr->x+15,curr->y+15); break; case -3:trap++; setfillstyle(1, 4); bar(curr->x,curr->y,curr->x+15,curr->y+15); break; } sprinf(points, "No of points: %f\n", M_PI); } } end: return 0; }
| DaniWeb Message | |
| Cancel Changes | |