-
Need help with code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
/*****************************************************************************/
#define BOOKS 1000
#define STUDENTS 500
/*****************************************************************************/
// DATA TYPE CONFIGURING
typedef struct
{
long BookCode;
char BookName[16];
char BookAuther[21];
int BookTotalNumber;
int BookNotLend;
}_Books;
/*****************************************************************************/
typedef struct
{
long StudentID;
char StudentName[11];
char StudentLastName[21];
char StudentAddress[31];
long StudentPhone;
}_Students;
/*****************************************************************************/
typedef struct
{
int Day;
int Month;
}_Date;
/*****************************************************************************/
typedef struct
{
long BookCode;
long StudentID;
_Date Date;
struct _Lend * NextLend; // Linked list;
}_Lend;
/*****************************************************************************/
//GLOBAL define
_Lend *LendList = NULL; // Linked list
_Students Students[STUDENTS];
_Books Books[BOOKS];
/*****************************************************************************/
// FUNCTIONS
/*****************************************************************************/
/*****************************************************************************/
void FillBooks() // Function for filling up array of books
{
int i;
for (i = 0; i < BOOKS; i++)
{
printf("Enter %d Book Code\n", i + 1);
_flushall;
scanf("%ld", &Books[i].BookCode);
printf("Enter %d Book Name\n", i + 1);
_flushall;
gets(Books[i].BookName);
printf("Enter %d bookAuther",i+1);
_flushall;
gets(Books[i].BookAuther);
_flushall;
scanf("%ld",&Books[i].BookTotalNumber);
printf("Enter %d Book availbel Copys\n", i + 1);
_flushall;
scanf("%ld", &Books[i].BookNotLend);
}
}
/*****************************************************************************/
/*****************************************************************************/
void FillStudents()
{
int i;
for (i = 0; i < STUDENTS; i++)
{
printf("Enter %d Students ID \n", i + 1);
_flushall;
scanf("%ld", &Students[i].StudentID);
printf("Enter %d Student Name\n", i + 1);
_flushall;
gets(Students[i].StudentName);
printf("Enter %d Student Last Name\n", i + 1);
_flushall;
gets(Students[i].StudentLastName);
printf("Enter %d StudentAddress \n");
_flushall;
gets(Students[i].StudentAddress);
printf("Enter %d student Phone \n", i + 1);
_flushall;
scanf("%ld", &Students[i].StudentPhone);
}
}
/*****************************************************************************/
/*****************************************************************************/
void FillLendings() // Function for filling up list of questions
{
int i;
_Lend *NewItem; //Define new objective
_Lend *move = LendList; //Additional pointer for the list
for (i = 0; i < BOOKS; i++)
{
NewItem = (_Lend *)(malloc(sizeof(_Lend))); //allocating dynamic memory
move = LendList; // Starter pointer pointing to start of list
printf("Enter Book Code\n");
_flushall;
scanf("%ld", &NewItem->BookCode); // -> Useful for linked list most of the time
printf("Enter Student ID \n");
scanf("%ld", &NewItem->StudentID);
_flushall;
printf("%d", &NewItem->Date.Day); //INTERESTING PART Date.Day ****
_flushall;
scanf("%d", &NewItem->Date.Day);
printf("Enter The Month of Lending \n");
_flushall;
scanf("%d", &NewItem->Date.Month);
if (LendList == NULL) //If new objective is first in the list
{
NewItem->NextLend = LendList; //part will point on pointer what already pointed in past on head of list
LendList = NewItem;
}
else
{
while (move->NextLend != NULL) // Processing pointer in the list untile end
move = move->NextLend;
NewItem->NextLend = move->NextLend; // New part
move->NextLend = NewItem; //Part before new part will point on new part
}
}
}
/*****************************************************************************/
/*****************************************************************************/
void print() //Function for printing linked list
{
_Lend *move = LendList;
while (move != NULL) // Loop working untile its gets to end
{
printf("%ld ", move->BookCode);
printf("%ld ", move->StudentID);
printf("%d ", move->Date.Day);
printf("%d ", move->Date.Month);
printf("\n");
move = move->NextLend; // Moving pointer to next element
}
}
/*****************************************************************************/
/*****************************************************************************/
FindMostPopularBook()
{
int iTmp, i;
char cTmpName[16];
iTmp = Books[0].BookTotalNumber;
for (i = 1; i < BOOKS; i++)
{
if (Books[i].BookTotalNumber>iTmp)
iTmp = Books[i].BookTotalNumber; //if number of copy of books correct equal to number copies maximum
}
for (i = 1; i < BOOKS; i++)
{
if (Books[i].BookTotalNumber == iTmp)
printf("\n\nThe Biggest Number of coppies has the book %d, %s End\n", Books[i].BookTotalNumber, Books[i].BookName); //Print number of book and name of book
}
}
/*****************************************************************************/
/*****************************************************************************/
FindStudentWithMaxLend(_Lend *hp) // Function for finding students for holding most of books
{
int i, iBig, iTmp[STUDENTS] = { 0 }; //Array of size number students loop working until reach end of line
while (hp != NULL)
{
for (i = 0; i < STUDENTS; i++)
{
if (hp->StudentID == Students[i].StudentID)
iTmp[i]++;//If found student hold the book, the sie of element in array what represent the student
hp = hp->NextLend; // Moving pointer to next linked list element
}
}
iBig = iTmp[0];
for (i = 1; i < STUDENTS; i++)// Loop for finding biggest number in array
{
if (iTmp[i] > iBig)
iBig = iTmp[i];
}
for (i = 0; i < STUDENTS; i++)//Loop for inspecting data of student
{
if (iTmp[i] == iBig) //if number of current students equal to number of student holders maximum
{
printf("\n\n The biggest Number of Books Has Student s,%s\n", Students[i].StudentName, Students[i].StudentLastName); // Print data of student
}
}
}
/*****************************************************************************/
/*****************************************************************************/
FindNotReturnedBook(_Lend *hp, int Month, int Day)//Function to find students thats not returned books in time
{
int i, iTmp[STUDENTS] = { 0 };
while (hp != NULL)//Loop working untile it reach end of list
{
for (i = 0; i < STUDENTS; i++)
if ((hp->StudentID == Students[i].StudentID) && ((hp->Date.Month < Month) || ((hp->Date.Day < Day) && (hp->Date.Month == Month))))
iTmp[i]++;//If found student who not returned book in time, increase element in array what represent student
hp = hp->NextLend; // forwarding pointer to list to next element
}
for (i = 0; i < STUDENTS; i++)
{
if (iTmp[i] > 0)
{
printf("\n\n The Biggest Number Of Books Has Student %s, %s \n", Students[i].StudentName, Students[i].StudentLastName);//Print information of student
}
}
}
/*****************************************************************************/
/*****************************************************************************/
CreateNewLendList(_Lend **hp, int BookCode, int StudentID, int Month, int Day)
{
int i;
_Lend *NewItem; //Defining new element
_Lend *move = (*hp); //Defining a temporary pointer
for (i = 0; i < BOOKS;i++)
{
if(Books[i].BookCode==BookCode)
Books[i].BookNotLend--; // Decreasing number copies of holders
NewItem = (_Lend *)malloc(sizeof(_Lend)); //Alocation of dynamic memory for new element in list
NewItem->BookCode = BookCode;
NewItem->StudentID = StudentID;
NewItem->Date.Day = Day;
NewItem->Date.Month = Month;
move = LendList;
while (move->NextLend != NULL)
move = move->NextLend;
NewItem->NextLend = move->NextLend;//The new element will point on element what was poiting behind it
move->NextLend = NewItem; //Element before new element will point on a new element
return; // Exit from function
}
}
/*****************************************************************************/
/*****************************************************************************/
UpdateLendListReturn(_Lend **hp, int BookCode, int StudentID, int Month, int Day)
{
int i, j;
_Lend *first = (*hp);
_Lend *sec = first->NextLend;
_Lend *tmp;
for (i = 0; i < BOOKS; i++)
if (BookCode == Books[i].BookCode)
Books[i].BookNotLend++;
while (sec != NULL)
{
if ((first->BookCode == BookCode) && (first->StudentID == StudentID))//If code of book and student are fit so element will be deleted
{
if ((first->Date.Month < Month) || ((first->Date.Day < Day) && (first->Date.Month == Month)))
{
printf("\n This Book Was Not Returned Late\n");
}
else
{
printf("This Book Was Returned on Time\n");
}
//Deleting element
tmp = first;
(*hp) = sec;
free(tmp);
first = (*hp);
sec = first->NextLend;
}
else if ((sec->BookCode == BookCode) && (sec->StudentID == StudentID))// If code of book and code of student fit so element for deletion
{
if ((sec->Date.Month < Month) || ((sec->Date.Day < Day) && (sec->Date.Month == Month))) // Check if book returned in time
{
printf("\n This Book Was Not Returned Late\n");
}
else
{
printf("\n This Book Was Returned on Time\n");
}
// Deleting element
tmp = sec;
sec = sec->NextLend;
first->NextLend = sec;
free(tmp);
}
else
{
first = first->NextLend; // Forwarding first pointer
sec = sec->NextLend;
}
}
}
/*****************************************************************************/
/*****************************************************************************/
void FreeNumeric(_Lend *hp)
{
if (hp == NULL)
return;
//Free(hp-> next);
free(hp);
}
/*****************************************************************************/
/*****************************************************************************/
void main()
{
time_t timer;
struct tm *tblock;
FillBooks();
FillStudents();
FillLendings();
FindMostPopularBook();
FindStudentWithMaxLend(LendList);
timer = time(NULL);//gets time of day
tblock = localtime(&timer);//Converts date /time to a structure
tblock = localtime(&timer);//converts date/time to a structure
FindNotReturnedBook(LendList,tblock->tm_mon+1,tblock->tm_mday);
printf("list 2\n");
getchar();
//Free(LendList, LendList);
}
FindNotReturnedBook(LendList,tblock->tm_mon+1,tblock->tm_mday); // Can't figure out whats the problem
-
What's the problem? You haven't said.
BTW your identifiers are illegal. Names in the global scope starting with an underscore are reserved for the implementation. Names at function scope starting with an underscore and followed by a capital letter are also reserved for the implementation. Names containing a double underscore are also reserved.
-
Here are some things that need to be fixed.
1. void main is wrong
FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
2. Over-use of global variables. You should be passing parameters and returning results.
3. gets() is dangerous.
FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
4. What is _flushall;
No doubt it's something in the obsolete dos.h header file.
For 3 and 4, what you should do is something along the lines of
Code:
char buff[BUFSIZ];
fgets(buff,BUFSIZ,stdin);
// now use sscanf / strtol / strtod / strcpy / whatever
// to validate buff and move the data to your data structure.
5. Weird loop
Code:
for (i = 0; i < STUDENTS; i++)
{
if (hp->StudentID == Students[i].StudentID)
iTmp[i]++;//If found student hold the book, the sie of element in array what represent the student
hp = hp->NextLend; // Moving pointer to next linked list element
}
You're stepping through the list one node at a time for each iteration of student.
What's more, there is no end-of-list check going on.
Perhaps you meant this
Code:
for (i = 0; i < STUDENTS; i++)
{
if (hp->StudentID == Students[i].StudentID)
iTmp[i]++;//If found student hold the book, the sie of element in array what represent the student
}
hp = hp->NextLend; // Moving pointer to next linked list element