Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C Structure Programming: Data Processing and Sorting Algorithms

Tech 1

Book Invenotry Management

This implementation tracks publication sales using structured records, providing sorting by volume sold and revenue calculation.

#include <stdio.h>
#define MAX_ITEMS 10

typedef struct {
    char identifier[20];
    char title[80];
    char creator[80];
    double unit_price;
    int units_sold;
} Publication;

void print_catalog(Publication items[], int count);
void sort_by_volume(Publication items[], int count);
double compute_revenue(Publication items[], int count);

int main() {
    Publication inventory[MAX_ITEMS] = {
        {"978-7-5327-6082-4", "The Goalkeeper's Death", "Ronald Reng", 42.0, 51},
        {"978-7-308-17047-5", "Land of Freedom and Love", "Yun Yetui", 49.0, 30},
        {"978-7-5404-9344-8", "Londoners", "Craig Taylor", 68.0, 27},
        {"978-7-5447-5246-6", "The Lifecycle of Software Objects", "Ted Chiang", 35.0, 90},
        {"978-7-5722-5475-8", "Chip History", "Wang Bo", 74.9, 49},
        {"978-7-5133-5750-0", "Console Wars", "Blake J. Harris", 128.0, 42},
        {"978-7-2011-4617-1", "The Cafe at the Edge of the World", "John Strelecky", 22.5, 44},
        {"978-7-5133-5109-6", "Hello Aliens", "Future Publishing UK", 118.0, 42},
        {"978-7-1155-0509-5", "The Beginning of Infinity", "David Deutsch", 37.5, 55},
        {"978-7-229-14156-1", "The Fountainhead", "Ayn Rand", 84.0, 59}
    };
    
    printf("Sales Rankings (by units sold):\n");
    sort_by_volume(inventory, MAX_ITEMS);
    print_catalog(inventory, MAX_ITEMS);
    printf("\nTotal Revenue: %.2f\n", compute_revenue(inventory, MAX_ITEMS));
    
    return 0;
}

void print_catalog(Publication items[], int count) {
    printf("%-28s%-28s%-18s%-12s%-10s\n", "ISBN", "Title", "Author", "Price", "Sold");
    for (int idx = 0; idx < count; idx++) {
        printf("%-28s%-28s%-18s%-12.2f%-10d\n",
               items[idx].identifier, items[idx].title, items[idx].creator,
               items[idx].unit_price, items[idx].units_sold);
    }
}

void sort_by_volume(Publication items[], int count) {
    for (int outer = 0; outer < count - 1; outer++) {
        for (int inner = 0; inner < count - outer - 1; inner++) {
            if (items[inner].units_sold < items[inner + 1].units_sold) {
                Publication buffer = items[inner];
                items[inner] = items[inner + 1];
                items[inner + 1] = buffer;
            }
        }
    }
}

double compute_revenue(Publication items[], int count) {
    double total = 0.0;
    for (int idx = 0; idx < count; idx++) {
        total += items[idx].unit_price * items[idx].units_sold;
    }
    return total;
}

Calendar Date Operations

Utility functions for parsing dates, calculating ordinal days, and chronological comparison.

#include <stdio.h>

typedef struct {
    int year;
    int month;
    int day;
} CalendarDate;

void parse_date(CalendarDate *dt);
int get_ordinal_day(CalendarDate dt);
int compare_chronological(CalendarDate first, CalendarDate second);

void demo_ordinal() {
    CalendarDate dt;
    printf("Enter date (YYYY-MM-DD format):\n");
    for (int iter = 0; iter < 3; iter++) {
        parse_date(&dt);
        printf("%d-%02d-%02d is day %d of the year\n\n",
               dt.year, dt.month, dt.day, get_ordinal_day(dt));
    }
}

void demo_comparison() {
    CalendarDate birth_a, birth_b;
    int result;
    printf("Enter birth dates for comparison (YYYY-MM-DD):\n");
    for (int iter = 0; iter < 3; iter++) {
        parse_date(&birth_a);
        parse_date(&birth_b);
        result = compare_chronological(birth_a, birth_b);
        
        if (result == 0)
            printf("Same age\n\n");
        else if (result < 0)
            printf("First person is older\n\n");
        else
            printf("First person is younger\n\n");
    }
}

int main() {
    printf("=== Ordinal Day Calculation ===\n");
    demo_ordinal();
    printf("=== Date Comparison ===\n");
    demo_comparison();
    return 0;
}

void parse_date(CalendarDate *dt) {
    scanf("%d-%d-%d", &dt->year, &dt->month, &dt->day);
}

int get_ordinal_day(CalendarDate dt) {
    int month_lengths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int ordinal = dt.day;
    
    int is_leap = (dt.year % 4 == 0 && dt.year % 100 != 0) || (dt.year % 400 == 0);
    if (is_leap) month_lengths[1] = 29;
    
    for (int m = 0; m < dt.month - 1; m++) {
        ordinal += month_lengths[m];
    }
    return ordinal;
}

int compare_chronological(CalendarDate first, CalendarDate second) {
    if (first.year != second.year)
        return (first.year < second.year) ? -1 : 1;
    if (first.month != second.month)
        return (first.month < second.month) ? -1 : 1;
    if (first.day != second.day)
        return (first.day < second.day) ? -1 : 1;
    return 0;
}

Secure Account Display

Demonstrates enumeration types and secure output masking sensitive credential fields.

#include <stdio.h>
#include <string.h>

enum AccessLevel { LEVEL_ADMIN, LEVEL_STUDENT, LEVEL_TEACHER };

typedef struct {
    char user_id[20];
    char secret[20];
    enum AccessLevel clearance;
} UserProfile;

void display_roster(UserProfile roster[], int size);

int main() {
    UserProfile accounts[] = {
        {"A1001", "123456", LEVEL_STUDENT},
        {"A1002", "123abcdef", LEVEL_STUDENT},
        {"A1009", "xyz12121", LEVEL_STUDENT},
        {"X1009", "9213071x", LEVEL_ADMIN},
        {"C11553", "129dfg32k", LEVEL_TEACHER},
        {"X3005", "921kfmg917", LEVEL_STUDENT}
    };
    int total = sizeof(accounts) / sizeof(UserProfile);
    display_roster(accounts, total);
    return 0;
}

void display_roster(UserProfile roster[], int size) {
    for (int idx = 0; idx < size; idx++) {
        printf("%-18s", roster[idx].user_id);
        
        int secret_len = strlen(roster[idx].secret);
        for (int c = 0; c < secret_len; c++) putchar('*');
        printf("     ");
        
        switch (roster[idx].clearance) {
            case LEVEL_ADMIN:   printf("%-8s\n", "admin"); break;
            case LEVEL_STUDENT: printf("%-8s\n", "student"); break;
            case LEVEL_TEACHER: printf("%-8s\n", "teacher"); break;
        }
    }
}

Contact Priority Management

Implements a contact list with emergency flagging, partitioning priority contacts before lexiocgraphical sorting.

#include <stdio.h>
#include <string.h>
#define CONTACT_LIMIT 10

typedef struct {
    char full_name[20];
    char mobile[12];
    int is_emergency;
} ContactEntry;

void mark_emergency(ContactEntry list[], int size, char target[]);
void show_sorted(ContactEntry list[], int size);
void basic_listing(ContactEntry list[], int size);

int main() {
    ContactEntry directory[CONTACT_LIMIT] = {
        {"Liu Yi", "15510846604", 0},
        {"Chen Er", "18038747351", 0},
        {"Zhang San", "18853253914", 0},
        {"Li Si", "13230584477", 0},
        {"Wang Wu", "15547571923", 0},
        {"Zhao Liu", "18856659351", 0},
        {"Zhou Qi", "17705843215", 0},
        {"Sun Ba", "15552933732", 0},
        {"Wu Jiu", "18077702405", 0},
        {"Zheng Shi", "18820725036", 0}
    };
    char name_buffer[20];
    int emergency_count;
    
    printf("Original Directory:\n");
    basic_listing(directory, CONTACT_LIMIT);
    
    printf("\nNumber of emergency contacts to set: ");
    scanf("%d", &emergency_count);
    printf("Enter %d names:\n", emergency_count);
    for (int i = 0; i < emergency_count; i++) {
        scanf("%s", name_buffer);
        mark_emergency(directory, CONTACT_LIMIT, name_buffer);
    }
    
    printf("\nSorted Directory (Emergency first, then alphabetical):\n");
    show_sorted(directory, CONTACT_LIMIT);
    return 0;
}

void mark_emergency(ContactEntry list[], int size, char target[]) {
    for (int i = 0; i < size; i++) {
        if (strcmp(list[i].full_name, target) == 0) {
            list[i].is_emergency = 1;
            return;
        }
    }
}

void show_sorted(ContactEntry list[], int size) {
    int priority_count = 0;
    
    for (int i = 0; i < size; i++) {
        if (list[i].is_emergency) {
            ContactEntry buffer = list[i];
            list[i] = list[priority_count];
            list[priority_count] = buffer;
            priority_count++;
        }
    }
    
    for (int i = 0; i < priority_count - 1; i++) {
        for (int j = 0; j < priority_count - i - 1; j++) {
            if (strcmp(list[j].full_name, list[j + 1].full_name) > 0) {
                ContactEntry buffer = list[j];
                list[j] = list[j + 1];
                list[j + 1] = buffer;
            }
        }
    }
    
    for (int i = priority_count; i < size - 1; i++) {
        for (int j = priority_count; j < size - (i - priority_count) - 1; j++) {
            if (strcmp(list[j].full_name, list[j + 1].full_name) > 0) {
                ContactEntry buffer = list[j];
                list[j] = list[j + 1];
                list[j + 1] = buffer;
            }
        }
    }
    
    for (int i = 0; i < size; i++) {
        printf("%-10s%-15s", list[i].full_name, list[i].mobile);
        if (list[i].is_emergency) printf("%5s", "*");
        printf("\n");
    }
}

void basic_listing(ContactEntry list[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%-10s%-15s", list[i].full_name, list[i].mobile);
        if (list[i].is_emergency) printf("%5s", "*");
        printf("\n");
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.