Book Inventory Management
Start with a structure that represents a book entry, containing fields for ISBN, title, author, unit price, and number of copies sold. An array of such records is preloaded with sample data.
Implement an output function that prints a formatted table header and then each book’s details. Align columns so that ISBNs, titles, authors, and numerical values appear neatly under their respective headings. Use fixed-width formatting with precision control for the price field.
void output(Book x[], int n) {
printf("%-20s %-25s %-20s %8s %8s\n",
"ISBN", "Title", "Author", "Price", "Copies");
for (int i = 0; i < n; ++i)
printf("%-20s %-25s %-20s %8.1f %4d\n",
x[i].isbn, x[i].name, x[i].author,
x[i].sales_price, x[i].sales_count);
}
Next, build a sorting routine using bubble sort that orders the books in descending order based on the number of copies sold. Swap entire structure instances when the current element’s sales count is smaller than the next one.
void sort(Book x[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - 1 - i; ++j) {
if (x[j].sales_count < x[j + 1].sales_count) {
Book tmp = x[j];
x[j] = x[j + 1];
x[j + 1] = tmp;
}
}
}
}
Calculate the total sales amount by iterating over the array, multiplying each book’s price by its copies sold, and accumulating the result.
double sales_amount(Book x[], int n) {
double total = 0.0;
for (int i = 0; i < n; ++i)
total += x[i].sales_price * x[i].sales_count;
return total;
}
In main, invoke the sort function, display the ranked list, and print the total sales figure.
Date Operations and Comparison
Define a Date struct with year, month, and day members. Implement the following three functions.
First, accept a date from standard input using a pointer. Read the values directly through the pointer, separating components with hyphens.
void input(Date *pd) {
scanf("%d-%d-%d", &pd->year, &pd->month, &pd->day);
}
Second, compute the day-of-year. Create a static array of days per month. Adjust February for leap years using the standard divisibility rules. Sum the days of all complete months before the given month, then add the day component.
int day_of_year(Date d) {
int days_per_month[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
days_per_month[1] = 29;
int total = 0;
for (int i = 0; i < d.month - 1; ++i)
total += days_per_month[i];
return total + d.day;
}
Third, compare two dates. Return -1 if the first date is earlier, 1 if latter, and 0 if they are identical. Perform multi-level comparison, starting with the year, then month, and final day.
int compare_dates(Date d1, Date d2) {
if (d1.year != d2.year) return d1.year < d2.year ? -1 : 1;
if (d1.month != d2.month) return d1.month < d2.month ? -1 : 1;
if (d1.day != d2.day) return d1.day < d2.day ? -1 : 1;
return 0;
}
Two test routines exercise these functions: one printts the day-of-year for three entered dates, and another compares birth dates of two individuals, outputting the relative age relationship.
Account Display with Masked Passwords
Create an enumeration for account roles: admin, student, and teacher. An Account struct stores a username, a password string, and a role.
Implement an output function that masks every character of the password with asterisks, preserving the original length. Print the username left-aligned, followed by the masked password and the role name obtained through a switch statement.
void output(Account x[], int n) {
for (int i = 0; i < n; ++i) {
printf("%-10s", x[i].username);
size_t len = strlen(x[i].password);
for (size_t j = 0; j < len; ++j)
putchar('*');
printf(" ");
switch (x[i].type) {
case 0: printf("admin"); break;
case 1: printf("student"); break;
case 2: printf("teacher"); break;
}
printf("\n");
}
}
In main, initialize a mixed-role account array and display the masked records.
VIP Contact Manager
Define a Contact structure with fields for name, phone number, and a VIP flag. Provide two operational functions besides a basic output helper.
The first function marks contacts as VIP by matching their name against a provided string. Loop through the array and set the flag to 1 when a match is found.
void set_vip_contact(Contact x[], int n, char name[]) {
for (int i = 0; i < n; ++i)
if (strcmp(x[i].name, name) == 0)
x[i].vip = 1;
}
The second function prepares a sorted display. First, sort the contacts alphabetically by name using bubble sort with strcmp. Then perform a stable sort that prioritizes VIP status: bring entries with vip == 1 before those with vip == 0. Finally, invoke the output routine to print the ordered list, appending an asterisk next to VIP contacts.
void display(Contact x[], int n) {
for (int i = 0; i < n - 1; ++i)
for (int j = 0; j < n - 1 - i; ++j)
if (strcmp(x[j].name, x[j + 1].name) > 0) {
Contact tmp = x[j];
x[j] = x[j + 1];
x[j + 1] = tmp;
}
for (int i = 0; i < n - 1; ++i)
for (int j = 0; j < n - 1 - i; ++j)
if (x[j].vip < x[j + 1].vip) {
Contact tmp = x[j];
x[j] = x[j + 1];
x[j + 1] = tmp;
}
output(x, n);
}
The driver reads an initial contact list, prompts for VIP names, sets the flags, and displays the final ordered directory.