Book Sales Analysis
The following program demonstrates how to manage and analyze book sales data using C structs. It includes functions to sort books by sales volume and calculate total revenue.
#include <stdio.h>
#define MAX_BOOKS 10
typedef struct {
char isbn[20];
char title[80];
char author[80];
double price;
int copies_sold;
} Book;
void display_books(Book books[], int count);
void sort_by_sales(Book books[], int count);
double calculate_revenue(Book books[], int count);
int main() {
Book library[MAX_BOOKS] = {
{"978-7-5327-6082-4", "The Death of the Goalkeeper", "Ronald Lunn", 42.00, 51},
{"978-7-308-17047-5", "Land of Freedom and Love: Journey to Israel", "Yun Yetai", 49.00, 30},
{"978-7-5404-9344-8", "Londoners", "Craig Taylor", 68.00, 27},
{"978-7-5447-5246-6", "Software Lifecycle", "Ted Chiang", 35.00, 90},
{"978-7-5722-5475-8", "Brief History of Chips", "Wang Bo", 74.90, 49},
{"978-7-5133-5750-0", "Console Wars", "Blake J. Harris", 128.00, 42},
{"978-7-2011-4617-1", "The Cafe at the End of the World", "John Strelecky", 22.50, 44},
{"978-7-5133-5109-6", "Hello Alien", "Future Publishing UK", 118.00, 42},
{"978-7-1155-0509-5", "The Beginning of Infinity", "David Deutsch", 37.50, 55},
{"978-7-229-14156-1", "The Fountainhead", "Ayn Rand", 84.00, 59}
};
printf("Book Sales Ranking (by copies sold):\n");
sort_by_sales(library, MAX_BOOKS);
display_books(library, MAX_BOOKS);
printf("\nTotal Sales Revenue: %.2f\n", calculate_revenue(library, MAX_BOOKS));
return 0;
}
void display_books(Book books[], int count) {
for (int i = 0; i < count; i++) {
printf("%s\t%s\t%s\t%.2f\t%d\n",
books[i].isbn, books[i].title, books[i].author,
books[i].price, books[i].copies_sold);
}
}
void sort_by_sales(Book books[], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (books[j].copies_sold < books[j + 1].copies_sold) {
Book temp = books[j];
books[j] = books[j + 1];
books[j + 1] = temp;
}
}
}
}
double calculate_revenue(Book books[], int count) {
double total = 0.0;
for (int i = 0; i < count; i++) {
total += books[i].price * books[i].copies_sold;
}
return total;
}
</stdio.h>
Date Operations
This program handles date manipulations, including determining the day of the year and comparing dates.
#include <stdio.h>
typedef struct {
int year;
int month;
int day;
} Date;
void read_date(Date* date_ptr);
int day_number(Date date);
int compare_dates(Date date1, Date date2);
void test_day_of_year() {
Date test_date;
for (int i = 0; i < 3; i++) {
printf("Enter date (format: YYYY-MM-DD): ");
read_date(&test_date);
printf("%04d-%02d-%02d is day %d of the year\n\n",
test_date.year, test_date.month, test_date.day,
day_number(test_date));
}
}
void test_date_comparison() {
Date date1, date2;
for (int i = 0; i < 3; i++) {
printf("Enter two dates (format: YYYY-MM-DD):\n");
read_date(&date1);
read_date(&date2);
int result = compare_dates(date1, date2);
if (result == 0) printf("Both dates are equal\n\n");
else if (result < 0) printf("First date is earlier\n\n");
else printf("First date is later\n\n");
}
}
int main() {
printf("Test 1: Determine day of year\n");
test_day_of_year();
printf("\nTest 2: Compare dates\n");
test_date_comparison();
return 0;
}
void read_date(Date* date_ptr) {
scanf("%d-%d-%d", &date_ptr->year, &date_ptr->month, &date_ptr->day);
}
int day_number(Date date) {
int days_per_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int total = 0;
if ((date.year % 4 == 0 && date.year % 100 != 0) || (date.year % 400 == 0)) {
days_per_month[1] = 29;
}
for (int i = 0; i < date.month - 1; i++) {
total += days_per_month[i];
}
total += date.day;
return total;
}
int compare_dates(Date date1, Date date2) {
if (date1.year < date2.year) return -1;
if (date1.year > date2.year) return 1;
if (date1.month < date2.month) return -1;
if (date1.month > date2.month) return 1;
if (date1.day < date2.day) return -1;
if (date1.day > date2.day) return 1;
return 0;
}
</stdio.h>
User Account Management
This program manages user accounts with different roles and displays password information securely.
#include <stdio.h>
#include <string.h>
enum UserType { admin, student, teacher };
typedef struct {
char username[20];
char password[20];
enum UserType role;
} UserAccount;
void display_accounts(UserAccount accounts[], int count);
int main() {
UserAccount users[] = {
{"A1001", "123456", student},
{"A1002", "123abcdef", student},
{"A1009", "xyz12121", student},
{"X1009", "9213071x", admin},
{"C11553", "129dfg32k", teacher},
{"X3005", "921kfmg917", student}
};
int user_count = sizeof(users) / sizeof(UserAccount);
display_accounts(users, user_count);
return 0;
}
void display_accounts(UserAccount accounts[], int count) {
for (int i = 0; i < count; i++) {
int pwd_length = strlen(accounts[i].password);
printf("Username: %s, ", accounts[i].username);
printf("Password: ");
for (int j = 0; j < pwd_length; j++) {
printf("*");
}
printf(", Role: ");
switch (accounts[i].role) {
case admin: printf("Administrator"); break;
case student: printf("Student"); break;
case teacher: printf("Teacher"); break;
default: printf("Unknown"); break;
}
printf("\n");
}
}
</string.h></stdio.h>
Contact List Management
This program manages a contact list with VIP contacts, allowing users to mark important contacts and sort the list accordingly.
#include <stdio.h>
#include <string.h>
typedef struct {
char name[20];
char phone[12];
int is_vip;
} Contact;
void mark_vip(Contact contacts[], int count, char name[]);
void display_contacts(Contact contacts[], int count);
void sort_contacts(Contact contacts[], int count);
#define MAX_CONTACTS 10
int main() {
Contact phonebook[MAX_CONTACTS] = {
{"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}
};
int vip_count, i;
char name[20];
printf("Original contact list:\n");
display_contacts(phonebook, MAX_CONTACTS);
printf("\nEnter number of VIP contacts to set: ");
if (scanf("%d", &vip_count) != 1) {
printf("Invalid input!\n");
return 1;
}
printf("Enter %d VIP contact names:\n", vip_count);
for (i = 0; i < vip_count; i++) {
if (scanf("%s", name) != 1) {
printf("Error reading name!\n");
return 1;
}
mark_vip(phonebook, MAX_CONTACTS, name);
}
printf("\nSorted contact list (VIPs first, then alphabetical):\n");
sort_contacts(phonebook, MAX_CONTACTS);
display_contacts(phonebook, MAX_CONTACTS);
return 0;
}
void mark_vip(Contact contacts[], int count, char name[]) {
for (int i = 0; i < count; i++) {
if (strcmp(contacts[i].name, name) == 0) {
contacts[i].is_vip = 1;
break;
}
}
}
void sort_contacts(Contact contacts[], int count) {
Contact temp;
// First sort by VIP status (descending)
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (contacts[j].is_vip < contacts[j + 1].is_vip) {
temp = contacts[j];
contacts[j] = contacts[j + 1];
contacts[j + 1] = temp;
}
}
}
// Then sort VIP contacts alphabetically
int vip_end = 0;
for (int i = 0; i < count; i++) {
if (contacts[i].is_vip == 1) vip_end++;
}
for (int i = 0; i < vip_end - 1; i++) {
for (int j = 0; j < vip_end - 1 - i; j++) {
if (strcmp(contacts[j].name, contacts[j + 1].name) > 0) {
temp = contacts[j];
contacts[j] = contacts[j + 1];
contacts[j + 1] = temp;
}
}
}
// Finally sort non-VIP contacts alphabetically
for (int i = vip_end; i < count - 1; i++) {
for (int j = vip_end; j < count - 1 - (i - vip_end); j++) {
if (strcmp(contacts[j].name, contacts[j + 1].name) > 0) {
temp = contacts[j];
contacts[j] = contacts[j + 1];
contacts[j + 1] = temp;
}
}
}
}
void display_contacts(Contact contacts[], int count) {
for (int i = 0; i < count; i++) {
printf("%-10s%-15s", contacts[i].name, contacts[i].phone);
if (contacts[i].is_vip) printf(" *");
printf("\n");
}
}
</string.h></stdio.h>