Untitled

Ass
mail@pastecode.io avatar
unknown
c_cpp
2 years ago
8.4 kB
7
Indexable
Never
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <time.h>

using namespace std;

class Data   // Using class to store our data structure
{
    public:
    int totalSubject;
    int miscFee;
    string name;
    string ID;
    double subjectFee;
    double totalFee;
    Data *next;
};

void Insertion(Data **head, Data **tail)   // Queue Linked List implementation
{
    Data *n = new Data();
    srand((unsigned)time(NULL));

    // Requesting data from user

    cout << "Please enter your full name : ";
    cin.ignore();
    getline(cin, n->name);

    cout << "Please enter your student ID : ";
    cin >> n->ID;

    cout << "Please enter your total subject taken for the upcoming semester : ";
    cin >> n->totalSubject;

    n->miscFee = 500 + (rand() % 300);
    n->subjectFee = n->totalSubject * 400;

    // Formula for the above : offset + (rand() % range)
    // - offset: Defines the starting point of the range
    /* - range: The number of possible values between the start and end of the range
         with both numbers inclusive. For instance, for the range between 100 to 300, the offset will be 100, and the range will be 201. */

    n->totalFee = n->subjectFee + n->miscFee;
    n->next = NULL;

    if(*head == NULL)
    {
        *head = n;
        *tail = n;
    }

    else
    {
        (*tail)->next = n;
        *tail = n;
        (*tail)->next = NULL;
    }

    cout << endl;
}

void Display(Data *head)
{
    int counter = 1;

    cout << "The information stored inside our database are as follows :-" << endl;

    while(head != NULL)
    {
        cout << endl << counter << ". Student Name : " << head->name << endl;
        cout << "   Student ID   : " << head->ID << endl;
        cout << "   Total subject taken : " << head->totalSubject << endl;
        cout << "   Subject fees : RM " << setprecision(2) << fixed << head->subjectFee << endl;
        cout << "   Miscellaneous fees : RM " << setprecision(2) << fixed << head->miscFee << endl;
        cout << "   Total fees incurred : RM " << setprecision(2) << fixed << head->totalFee << endl;
        counter++;

        head = head->next;
    }

    cout << endl;
}

void Update(Data *head)
{
    string tempID;
    int confirmation = 1;

    cout << "Please enter your student ID : ";
    cin >> tempID;

    while(head->ID != tempID)
    {
        if(head->next == NULL)
        {
            confirmation = 0;
            break;
        }

        head = head->next;
    }

    if(confirmation == 0)
    {
        cout << "ERROR! No matching student ID found ... Returning to main menu ..." << endl << endl;
    }
    else
    {
        cout << "Please enter your new total subject taken : ";
        cin >> head->totalSubject;

        head->subjectFee = head->totalSubject * 400;
        head->totalFee = head->subjectFee + head->miscFee;

        cout << endl << "Congratulations! Your information has been updated in the system ..." << endl << endl;
    }
}

void Delete(Data **head, Data **tail)
{
    string tempID;
    int confirmation = 1;

    Data *p = *head;
    Data *temp;

    cout << "Please enter the student ID of the information log that you want to delete : ";
    cin >> tempID;

    if((*head)->ID == tempID)
    {
        *head = (*head)->next;
        free(p);
        cout << "Congratulations! Your specified information log has been deleted from our system ..." << endl << endl;
    }

    else if(p->next == NULL)   // Error handling : If student ID not found at head, and linked list contains only the head
    {
        cout << "ERROR! No matching student ID found ... Returning to main menu ..." << endl << endl;
    }

    else
    {
        while(p->next->ID != tempID)
        {
            if(p->next == NULL)   // Error handling : If reached the end of linked list and student ID was still not found
            {
                confirmation = 0;
                break;
            }

            p = p->next;
        }

        if(confirmation == 1)
        {
            temp = p->next;
            p->next = temp->next;

            if(temp == *tail)
            {
                *tail = p;
                p->next = NULL;
            }

            free(temp);
            cout << "Congratulations! Your specified information log has been deleted from our system ..." << endl << endl;
        }

        if(confirmation == 0)
        {
            cout << "ERROR! No matching student ID found ... Returning to main menu ..." << endl << endl;
        }
    }
}

// Start of sorting algorithm (merge sort, taken from https://codeforgeek.com/sort-linked-lists-cpp/)

Data *mid_point(Data *head)
{
    // base case
    if(head == NULL || head->next == NULL)
        return head;

    // recursive case
    Data *fast = head;
    Data *slow = head;

    while(fast != NULL && fast->next != NULL)
    {
        fast = fast->next;

        if(fast->next == NULL)
            break;

        fast = fast->next;
        slow = slow->next;
    }

    return slow;
}

Data *merge(Data *a, Data *b)
{
    // base case
    if(a == NULL)
        return b;

    if(b == NULL)
        return a;

    // recursive case
    // take a head pointer
    Data *c;

    if(a->totalFee < b->totalFee)
    {
        c = a;
        c->next = merge(a->next, b);
    }
    else
    {
        c = b;
        c->next = merge(a, b->next);
    }

    return c;
}

Data *mergeSort(Data *head)
{
    // base case
    if(head == NULL || head->next == NULL)
        return head;

    // recursive case
    // Step 1: divide the linked list into
    // two equal linked lists

    Data *mid = mid_point(head);
    Data *a = head;
    Data *b = mid->next;

    mid->next = NULL;

    // Step 2: recursively sort the smaller
    // linked lists
    a = mergeSort(a);
    b = mergeSort(b);

    // Step 3: merge the sorted linked lists
    Data *c = merge(a, b);

    return c;
}

// End of sorting algorithm (merge sort, taken from https://codeforgeek.com/sort-linked-lists-cpp/)

int main()
{
    Data *head = NULL;
    Data *tail = NULL;

    int choice;

    cout << "~~ Welcome to the UNITEN Fees Calculation System! ~~" << endl << endl;

    while(true)
    {
        cout << "Please choose what do you want to do from the below menu :-" << endl;
        cout << "[1] Add new information into system" << endl;
        cout << "[2] Update readily available information in the system" << endl;
        cout << "[3] Display all information in the system" << endl;
        cout << "[4] Sort all information in the system (in ascending order)" << endl;
        cout << "[5] Delete a specified information in the system" << endl;
        cout << "[6] Exit the program" << endl;
        cout << "Enter your choice : ";
        cin >> choice;

        if(choice == 1)
        {
            cout << endl;
            Insertion(&head, &tail);
            cout << "Congratulations! Your information has been added into our system ..." << endl << endl;
        }

        else if(choice == 2)
        {
            cout << endl;
            Update(head);
        }

        else if(choice == 3)
        {
            cout << endl;
            Display(head);
        }

        else if(choice == 4)
        {
            cout << endl;
            head = mergeSort(head);
            cout << "Congratulations! Our system's database has been sorted in ascending order ..." << endl << endl;

            // Resetting the the tail pointer

            Data *p = head;
            while(p->next != NULL)
            {
                p = p->next;
            }
            tail = p;
        }

        else if(choice == 5)
        {
            cout << endl;
            Delete(&head, &tail);
        }

        else if(choice == 6)
        {
            cout << "\nThank you for using this program!" << endl;
            cout << "We hope to hear from you again soon!" << endl;
            break;
        }

        else
        {
            cout << "\nERROR! Unrecognized input detected ... Returning to main menu ..." << endl << endl;
        }
    }
}