SOme time
unknown
c_cpp
3 years ago
16 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
// single Node that contains:
// double mileage, int returnDate (YYMMDD), and int flag that checks if the car is new or the car is returned,
// and double charge.
struct Node {
int isNew;
int statusFlag; // sets the status which it is in
// 0 == available, 1 == rented, 2 == is repair
char plateNumber[30];
double mileage;
int returnDate;
double charge;
// * to assign key to memory position;
struct Node *next;
};
// function that asks to add a new car to the available-for-rent list
// rent cars int value == 1
// new cars int value == 0
void addNewCarInList(struct Node **header, int isNew, int flagType) {
// initializing mileages, and return date values and scanning user
// values for mileages and return date
// temporary node current created to check if value is null
struct Node *temp, *current;
temp = (struct Node *) malloc(sizeof(struct Node));
printf("\nEnter Plate Number");
scanf("%s", temp->plateNumber);
printf("\nEnter Mileage ");
scanf("%lf", &temp->mileage);
printf("\nYou Entered %0.2lf for Mileage, Enter Return Date in the following Format\nYYMMDD\n", temp->mileage);
scanf("%d", &temp->returnDate);
printf("\nYou Entered %d for return date\n the following will be saved in Available to Rent ", temp->returnDate);
temp->statusFlag = flagType; // sets as whatever type of list of returned car
temp->next = NULL;
if (isNew == 0) {
temp->charge = 0;
}
// checks flag, returned card being 1
else if (isNew == 1) {
// checks if the mileage is less than or equal to 200
if (temp->mileage <= 200) {
printf("\nYou are going to be Charged $80\n");
}
// checks if the mileage is greater, than 200 and charging 0.15 dollars per mileage.
else {
temp->charge = (((temp->mileage) - 200) * 0.15) + 80;
printf("\nYou are going to be charged %0.2f\n", (((temp->mileage) - 200) * 0.15) + 80);
}
}
if (*header == NULL) {
// if the header node is not there,
// ie meaning that it's the first variable in the Node, then it will be assigned to header.
*header = temp;
}
// this is set so that if the there is a linked List, it will check where the list ends
// ie the next == NULL and place the that value as the NEXT memory data.
else {
// set the current node to temp
current = *header;
// checks which node memory allocation is null
while (current->next != NULL) {
// if it's not null, its sets the current node to that node.
current = current->next;
}
current->next = temp;
// if it is null, then it assigns next memory allocation to temp
}
}
// the following uses bruteforce to sort Nodes, based off of Mileage
void sortMileage(struct Node **mileageHeader) {
struct Node *current = NULL, *tempHeader;
// temp variable to store variable
double currentVar;
tempHeader = *mileageHeader;
while (tempHeader != NULL) {
current = tempHeader;// setting temp current value to node
while (current->next != NULL) {
if (current->mileage > current->next->mileage) {// compare data from node smaller
currentVar = current->mileage;// setting the current Variable to current mileage
current->mileage = current->next->mileage; // swap data
current->next->mileage = currentVar; // reassigning value
}
current = current->next; // move to the next element
}
tempHeader = current->next; // move to the next node
}
}
// the following uses Bruteforce to sort Nodes, based on the Return Date
void sortReturnDate(struct Node **returnDateHeader) {
struct Node *current = NULL, *tempHeader;
// temp variable to store variable
int currentVar;
tempHeader = *returnDateHeader;
while (tempHeader != NULL) {
current = tempHeader;// setting temp current value to node
while (current->next != NULL) {
if (current->returnDate > current->next->returnDate) {// compare data from node greater
currentVar = current->returnDate;// setting the current Variable to current mileage
current->returnDate = current->next->returnDate; // swap data
current->next->returnDate = currentVar; // reassigning value
}
current = current->next; // move to the next element
}
tempHeader = current->next; // move to the next node
}
}
//Function takes the transfer to repair
void transferToRepair(struct Node **availableToRentHeader, struct Node **isRepairHeader) {
// number plate
char numberPlate[30];
printf("\nEnter the NumberPlate to check\n");
scanf("%s", numberPlate);
struct Node *transferringNode, *currentNode; // temp value to store cache Node
if (availableToRentHeader != NULL) {// check if loop is not equal to null
struct Node *temporaryNode;
temporaryNode = *availableToRentHeader;// sets currentNode node to availableToRentHead
while (temporaryNode != NULL) {// checks if the node next is null, checks till last node
if (temporaryNode->plateNumber == numberPlate) {// checks if the node is equavilent to number
transferringNode->statusFlag = 2; //setting the status to 2, to indicate in repair
transferringNode = temporaryNode;// sets that node to temp to the node
// deletes the node
break;
}
if (temporaryNode->plateNumber != numberPlate) { // if it's not connected to the first Node
while (temporaryNode->plateNumber != numberPlate) { // while loop that iterates and checks each node
temporaryNode = currentNode;
currentNode = currentNode->next;
}
} // moves to the next list
else {
printf("Number Plate is not found.");
transferringNode->next = currentNode->next;
break;
}
}
}
// goes to the repair header
if (isRepairHeader != NULL) {// checks if rentedHeader == null
struct Node *temporaryNode;
// creates a temporary
temporaryNode = *isRepairHeader; // sets current is rentedHeader;
while (temporaryNode->next != NULL) {// checks if next value is
temporaryNode = temporaryNode->next; // traverse till the end of the line
}
temporaryNode->next = transferringNode; // sets the end value to temp.
}
}
void firstAvailable(struct Node **rentedHeader, struct Node **availableToRentHead) {// both nodes
// enter Return Date
printf("\nEnter your Expected Return Date: ");
// initialize Return Date
int expectedReturnDate;
scanf("%d", &expectedReturnDate);
// struct Node
struct Node *transferingNode, *currentNode; // temp value to store cache Node
if (availableToRentHead != NULL) {// check if rentedHeader is not null
struct Node *temporaryNode;
temporaryNode = *availableToRentHead;
// adds the available
if (temporaryNode != NULL) {// checks if rentedHeader == null
*rentedHeader = temporaryNode->next; // checks if the rented Header
transferingNode = temporaryNode;
transferingNode->statusFlag = 1; // setting the flag from 0 to 1 indicating being rented
free(temporaryNode);
}
if (rentedHeader != NULL) {// checks if rentedHeader == null
struct Node *temporaryNodeRented;
// creates a temporary
temporaryNodeRented = *rentedHeader; // sets current is rentedHeader;
while (temporaryNodeRented->next != NULL) {// checks if next value is
temporaryNodeRented = temporaryNodeRented->next; // traverse till the end of the line
}
temporaryNodeRented->next = transferingNode; // sets the end value to temp.
}
}
}
// store the values on disk
void storeDisk(struct Node **rent, struct Node **available, struct Node **isRepair) {
FILE *Ptr;
struct Node *rentTemp, *availableTemp, *isRepairTemp;
rentTemp = *rent;
availableTemp = *available;
isRepairTemp = *isRepair;
Ptr = fopen("Disk.txt", "w");
if (Ptr == NULL) {
printf("Error");
} else {
// checks while the Ptr,
while (Ptr != NULL && rentTemp != NULL) {
// stored in the following format plate number, mileage, return date, charge,
fprintf(Ptr, "%s,%lf,%d,%lf,%d,%d", rentTemp->plateNumber, rentTemp->mileage, rentTemp->returnDate,
rentTemp->charge, rentTemp->isNew, rentTemp->statusFlag);
// move to the next line
rentTemp = rentTemp->next;
}
while (Ptr != NULL && availableTemp != NULL) {
fprintf(Ptr, "%s,%lf,%d,%lf,%d,%d", availableTemp->plateNumber, availableTemp->mileage,
availableTemp->returnDate, availableTemp->charge, availableTemp->isNew, availableTemp->statusFlag);
// move to the next line
availableTemp = availableTemp->next;
}
// checks if pointer is null and or Repair is Null
// if not adds it to the file
while (Ptr != NULL && isRepairTemp != NULL) {
fprintf(Ptr, "%s,%lf,%d,%lf,%d,%d", isRepairTemp->plateNumber, isRepairTemp->mileage,
isRepairTemp->returnDate, isRepairTemp->charge, isRepairTemp->isNew, isRepairTemp->statusFlag);
// move to the next line
isRepairTemp = isRepairTemp->next;
}
}
}
void readDisk(struct Node **rent, struct Node **available, struct Node **isRepair) {
struct Node *currentNode, *tempNode;
FILE *File;
char line[1024];
File = fopen("Disk.txt", "r");
if(File == NULL) {
printf("\nThe Disk is NULL\nContinuing to the End\n");
}
else {
while(fgets(line, 1024, File))
{
// printf("hello\n");
currentNode = malloc(sizeof(struct Node));
sscanf(line, "%s,%lf,%d,%lf,%d,%d\n", currentNode->plateNumber, ¤tNode->mileage, ¤tNode->returnDate, ¤tNode->charge, ¤tNode->isNew, ¤tNode->statusFlag);
printf("hello\n");
if(currentNode->statusFlag == 0)
{
tempNode = *available;
if(*available == NULL)
{
*available = currentNode;
}
else {
while (tempNode->next != NULL) {
tempNode = tempNode->next;
}
tempNode->next = currentNode;
}
}
if(currentNode->statusFlag == 1)
{
tempNode = *rent;
if(*rent == NULL)
{
*rent = currentNode;
}
else {
while (tempNode->next != NULL) {
tempNode = tempNode->next;
}
tempNode->next = currentNode;
}
}
if(currentNode->statusFlag == 2)
{
tempNode = *isRepair;
if(*isRepair == NULL)
{
*isRepair = currentNode;
}
else {
while (tempNode->next != NULL) {
tempNode = tempNode->next;
}
tempNode->next = currentNode;
}
}
}
}
//
};
// the following prints out the given Linked Lists, and the following
void printList(struct Node *header, char name[30]) {
// assign temp Node that checks if
struct Node *current;
//checks if the node is empty, if it is it outputs that the value is null
if (header == NULL) {
printf("\nThe List (%s) does not have any values\n", name);
} else {
// traverses through each value and print its it out
printf("\n\n%s\n\n", name);
current = header;
while (current != NULL) {
// if it's not null, its sets the current node to that node.
printf("\n\nMileage: %0.2lf\nPlate Number: %s\nReturnDate:%d", header->mileage, header->plateNumber,
header->returnDate);
current = current->next;
}
}
}
void questionAsker() {
// availableForRent, rented[], inRepair;
struct Node *availableForRent = NULL;
struct Node *rented = NULL;
struct Node *inRepair = NULL;
int i = 0;
// the following will initialize and set the Nodes based off of TXT file.
readDisk(&rented, &availableForRent, &inRepair);
while (i != 8) {
printf("Enter a number: \n"
"(1) add a new car to the available-for-rent list,\n"
"(2) add a returned car to the available-for-rent list,\n"
"(3) add a returned car to the repair list,\n"
"(4) transfer a car from the repair list to the available-for-rent list,\n"
"(5) rent the first available car,\n"
"(6) print all the lists,\n"
"(7) quit.\n");
scanf("%d", &i);
// 0 is available, 1 is rented, 2 is repaired
switch (i) {
// Case calls add new car to the available-for-rent list Function
case 1:
addNewCarInList(&availableForRent, 0, 0);
printf("\nThe following Car has been added to the List");
break;
// Case calls returned car to the available-for-rent list Function,
case 2:
addNewCarInList(&availableForRent, 1, 0);
printf("\nThe following Car has been added to the List");
break;
// Case calls returned car to the repair list Function
case 3:
addNewCarInList(&inRepair, 1, 2);
break;
// Case calls transfer a car from the repair list to the available-for-rent list,
case 4:
transferToRepair(&availableForRent, &inRepair);// transfers from Repair to available to rent
sortReturnDate(&availableForRent);// sorts the nodes based of Return Dates
break;
// Case calls rent the first available car Function
case 5:
sortMileage(&availableForRent); // sorts the cars based of mileage
firstAvailable(&rented, &availableForRent);// chooses the first available car.
break;
// Case calls print all the list Function
case 6:
printList(availableForRent, "Available For Rent");
sortReturnDate(&rented),// sorts the cars before printing out list
printList(rented, "Rented");
sortMileage(&availableForRent);// sorts the cars based off of mileage.
printList(inRepair, "In Repair");
break;
// 7 Breaks The Code and Ends function
case 7:
storeDisk(&rented, &availableForRent, &inRepair);
// while loop that adds all increments of total profit made
double totalCost;
while (availableForRent != NULL) {
totalCost += availableForRent->charge;
}
printf("\nTotal Made today was %0.2lf\n", totalCost);
// checks the total made in available to rend at the end.
printf("\nStoring On Disk\n");
exit(0);
}
}
}
// main
int main(int argc, char *argv[]) {
// calls main function
questionAsker();
return 0;
}
Editor is loading...