Professional Documents
Culture Documents
Документ Microsoft Word
Документ Microsoft Word
h>
#include <stdlib.h>
#include <string.h>
struct medicine {
char name[50];
char manufacturer[50];
int quantity;
float price;
};
int num_medicines = 0;
struct medicine medicines[MAX_MEDICINES];
int main() {
int option;
do {
printf("\nChoose an option:\n");
printf("1. Read medicines from file\n");
printf("2. Find a medicine\n");
printf("3. Sort medicines by quantity\n");
printf("4. Sort medicines by price\n");
printf("5. Edit a medicine\n");
printf("6. Add a medicine\n");
printf("7. Delete a medicine\n");
printf("8. Print all medicines\n");
printf("9. Write medicines to file\n");
printf("10. Exit\n");
scanf("%d", &option);
switch (option) {
case 1:
read_medicines_from_file("medicines.txt");
break;
case 2:
printf("Enter the name of the medicine: ");
char name[50];
scanf("%s", name);
int index = find_medicine(name);
if (index == -1) {
printf("Medicine not found.\n");
} else {
printf("Name: %s\n", medicines[index].name);
printf("Manufacturer: %s\n", medicines[index].manufacturer);
printf("Quantity: %d\n", medicines[index].quantity);
printf("Price: %.2f\n", medicines[index].price);
}
break;
case 3:
sort_medicines_by_quantity();
printf("Medicines sorted by quantity.\n");
break;
case 4:
sort_medicines_by_price();
printf("Medicines sorted by price.\n");
break;
case 5:
printf("Enter the name of the medicine to edit: ");
scanf("%s", name);
index = find_medicine(name);
if (index == -1) {
printf("Medicine not found.\n");
} else {
edit_medicine(index);
printf("Medicine edited successfully.\n");
}
break;
case 6:
add_medicine();
printf("Medicine added successfully.\n");
break;
case 7:
printf("Enter the name of the medicine to delete: ");
scanf("%s", name);
index = find_medicine(name);
if (index == -1) {
printf("Medicine not found.\n");
} else {
delete_medicine(index);
printf("Medicine deleted successfully.\n");
}
break;
case 8:
print_medicines();
break;
case 9:
write_medicines_to_file("medicines.txt");
printf("Medicines written to file.\n");
break;
case 10:
printf("Goodbye!\n");
break;
default:
printf("Invalid option.\n");
break;
}
return 0;
}
if (file == NULL) {
printf("Error opening file.\n");
return;
}
num_medicines = 0;
while (!feof(file)) {
fscanf(file, "%s %s %d %f\n", medicines[num_medicines].name,
medicines[num_medicines].manufacturer, &medicines[num_medicines].quantity,
&medicines[num_medicines].price);
num_medicines++;
}
fclose(file);
}
return -1;
}
void sort_medicines_by_quantity() {
struct medicine temp;
void sort_medicines_by_price() {
struct medicine temp;
printf("Name: ");
scanf("%s", medicines[index].name);
printf("Manufacturer: ");
scanf("%s", medicines[index].manufacturer);
printf("Quantity: ");
scanf("%d", &medicines[index].quantity);
printf("Price: ");
scanf("%f", &medicines[index].price);
}
void add_medicine() {
printf("Enter the details for the new medicine:\n");
printf("Name: ");
scanf("%s", medicines[num_medicines].name);
printf("Manufacturer: ");
scanf("%s", medicines[num_medicines].manufacturer);
printf("Quantity: ");
scanf("%d", &medicines[num_medicines].quantity);
printf("Price: ");
scanf("%f", &medicines[num_medicines].price);
num_medicines++;
}
num_medicines--;
}
void print_medicines() {
printf("List of medicines:\n");
for (int i = 0; i < num_medicines; i++) {
printf("Name: %s\n", medicines[i].name);
printf("Manufacturer: %s\n", medicines[i].manufacturer);
printf("Quantity:%d\n", medicines[i].quantity);
printf("Price: %f\n\n", medicines[i].price);
}
}
if (file == NULL) {
printf("Error opening file.\n");
return;
}
fclose(file);
}