You are on page 1of 6

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX_MEDICINES 100

struct medicine {
char name[50];
char manufacturer[50];
int quantity;
float price;
};

int num_medicines = 0;
struct medicine medicines[MAX_MEDICINES];

void read_medicines_from_file(char* filename);


int find_medicine(char* name);
void sort_medicines_by_quantity();
void sort_medicines_by_price();
void edit_medicine(int index);
void add_medicine();
void delete_medicine(int index);
void print_medicines();
void write_medicines_to_file(char* filename);

int main() {
int option;

printf("Welcome to Pharmacy Management System!\n");

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;
}

} while (option != 10);

return 0;
}

void read_medicines_from_file(char* filename) {


FILE* file = fopen(filename, "r");

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);
}

int find_medicine(char* name) {


for (int i = 0; i < num_medicines; i++) {
if (strcmp(name, medicines[i].name) == 0) {
return i;
}
}

return -1;
}

void sort_medicines_by_quantity() {
struct medicine temp;

for (int i = 0; i < num_medicines - 1; i++) {


for (int j = 0; j < num_medicines - i - 1; j++) {
if (medicines[j].quantity > medicines[j+1].quantity) {
temp = medicines[j];
medicines[j] = medicines[j+1];
medicines[j+1] = temp;
}
}
}
}

void sort_medicines_by_price() {
struct medicine temp;

for (int i = 0; i < num_medicines - 1; i++) {


for (int j = 0; j < num_medicines - i - 1; j++) {
if (medicines[j].price > medicines[j+1].price) {
temp = medicines[j];
medicines[j] = medicines[j+1];
medicines[j+1] = temp;
}
}
}
}

void edit_medicine(int index) {


printf("Enter the new details for the medicine:\n");

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++;
}

void delete_medicine(int index) {


for (int i = index; i < num_medicines - 1; i++) {
medicines[i] = medicines[i+1];
}

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);
}
}

void write_medicines_to_file(char* filename) {


FILE* file = fopen(filename, "w");

if (file == NULL) {
printf("Error opening file.\n");
return;
}

for (int i = 0; i < num_medicines; i++) {


fprintf(file, "%s %s %d %f\n", medicines[i].name,
medicines[i].manufacturer, medicines[i].quantity, medicines[i].price);
}

fclose(file);
}

You might also like