Return to Snippet

Revision: 55028
at January 23, 2012 07:57 by AlexanderRavikovich


Initial Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>

/* Initiliazing structures */
typedef struct
{
	char name[30];
	char id[10];
	char address[30];
	int adults, children;
	double fee;
} subscriber;

/* Functions prototype */
void menu();
subscriber * add_subscribers(subscriber * club, int * size); /* Add subscribers to array */
subscriber * del_subscriber(subscriber * out_subscriber , int * size, char * id); /* Remove subscriber from array */
void print_subscribers (subscriber * club, int size); /* Print subscribers from array */
void id_sorting(subscriber * club, int size); /* Sort subscribers by id */
void fee_sorting(subscriber * club, int size); /* Sort subscribers by fee  */

void main()
{
	int option, size = 0, i, j, removed = 0;
	char id[10];

	/* Database of subscribers */
	subscriber * real_db = NULL;

	while(1)
	{
		/* Call for menu func */
		menu();

		/* Set option var */
		scanf("%d", &option);
		getchar();

		/* Run programm by option */
		switch(option)
		{
			case 1:
				/* Call function, that takes pointer "club" & address of variable "size" */
				real_db = add_subscribers(real_db, &size);
			break;

			case 2:
				if (size)
				{
					printf("Enter ID subscriber for delete:\n");
					gets(id);

					for (i=0; i < size; i++)
					{
						if (strcmp(real_db[i].id, id) == 0)
						{
							real_db = del_subscriber(real_db, &size, id);
							removed = 1;
							printf("The subscriber is DELETED!\n");
						}
					}
					if (!removed)
					{
						printf("The subscriber not exist in the array!\n");
					}
				}
				else
				{
					printf("No subscribers!\n\n");
				}
			break;

			case 3:
				if (size)
				{
					print_subscribers (real_db, size);
				}
				else
				{
					printf("No subscribers!\n\n");
				}
			break;

			case 4:
				if (size)
				{
					id_sorting(real_db, size);
				}
				else
				{
					printf("No subscribers!\n\n");
				}

			break;

			case 5:
				if (size)
				{
					fee_sorting(real_db, size);
				}
				else
				{
					printf("No subscribers!\n\n");
				}
			break;

			case 6:
				exit(1);
			break;
		}
	}
}

/* Bubble sort array of structs by id */
void id_sorting(subscriber * club, int size)
{
	int i, j;
	subscriber temp_member;

	for (i = 0; i < size; i++)
	{
		for(j = i; j < size; j++)
		{
			if (strcmp(club[i].id, club[j].id) > 0)
			{
				temp_member = club[i];
				club[i] = club[j];
				club[j] = temp_member;
			}
		}
	}
}

/* Bubble sort array of structs by fee */
void fee_sorting(subscriber * club, int size)
{
	int i, j;
	subscriber temp_member;

	for (i = 0; i < size; i++)
	{
		for(j = i; j < size; j++)
		{
			if (club[i].fee > club[j].fee)
			{
				temp_member = club[i];
				club[i] = club[j];
				club[j] = temp_member;
			}
		}
	}
}

/* Print subscribers */
void print_subscribers (subscriber * club, int size)
{
	int i;

	for (i = 0; i < size; i++)
	{
		printf("%-20s\t", club[i].name);
		printf("%-20s\t", club[i].address);
		printf("%-10s\t", club[i].id);
		printf("%.0f", club[i].fee);
		printf("\n");
	}
}

/* Delete item from subscribers */
subscriber * del_subscriber(subscriber * out_subscriber , int * size, char * id) 
{
	int i, j, z;

	/* Temp database */
	subscriber * temp_db;

	/* Create temp array of structs the same size like real db array is */
	temp_db = (subscriber *) malloc(sizeof(subscriber)*((*size)-1));

	if (temp_db == NULL)
	{
		printf("No memory! Program is exiting...");
		exit(1);
	}

	/* Copy array of subscriber to temp array, but without subscriber with id = blablabla */
	z = 0;
	for (j = 0; j < *size; j++)
	{
		if (strcmp(out_subscriber[j].id, id) != 0)
		{
			temp_db[z] = out_subscriber[j];
			z++;
		}
	}
	/* Delete original array of subscribers */
	free(out_subscriber);
	/* Create new array of subscriber with new size-1 */
	out_subscriber = (subscriber *) malloc(sizeof(subscriber)*((*size)-1));

	if (out_subscriber == NULL)
	{
		printf("No memory! Program is exiting...");
		exit(1);
	}

	/* Copying old subscribers to original array */
	for (j = 0; j < (*size)-1; j++)
	{
		out_subscriber[j] = temp_db[j];
	}

	/* Delete temp array */
	free(temp_db);

	/* -1 to size of our subscribers */
	*size = (*size)-1;

	/* We must return new adress of our array beause we deleted him and create again, and adress changed */
	return out_subscriber;
}

/* Function that insert new subscribers to the club array */
subscriber * add_subscribers(subscriber * club, int * size)
{
	int i, new_size = 0;
	/* Set pointer to array, that have type "subscriber" */
	subscriber * temp_db;

	printf("Enter subscribers number:\n");

	scanf("%d", &new_size);
	getchar();

	/* If we have an subscribers in database, create "temp" array and copy them to it */
	if (* size > 0)
	{
		temp_db = (subscriber *) malloc(sizeof(subscriber)*(*size));

		if (temp_db == NULL)
		{
			printf("No memory! Program is exiting...");
			exit(1);
		}

		for (i = 0; i < *size; i++)
		{
			temp_db[i] = club[i];
		}
		/* Delete old array of subscribers */
		free(club);
	}

	/* Create new club array with size of "num of old subscribers + num of new subscribers" */
	club = (subscriber *) malloc(sizeof(subscriber)* (*size+new_size));

	if (club == NULL)
	{
		printf("No memory! Program is exiting...");
		exit(1);
	}

	/* If we have old subscribers in temp, so copying them back to club array */
	if (* size > 0)
	{
		for (i = 00; i < *size; i++)
		{
			club[i] = temp_db[i];
		}
		free(temp_db);
	}

	/* Add to array new subscribers */
	for (i = *size; i < (*size+new_size); i++)
	{
		printf("Enter name of subscriber:\n");
		gets(club[i].name);

		printf("Enter address of subscriber:\n");
		gets(club[i].address);

		printf("Enter ID of subscriber:\n");
		gets(club[i].id);

		printf("Enter number of adults of subscriber:\n");
		scanf("%d", &club[i].adults);
		getchar();

		printf("Enter number of children of subscriber:\n");
		scanf("%d", &club[i].children);
		getchar();

		/* Calculate and set fee by children & adults */
		club[i].fee = club[i].children*900+club[i].adults*2000;

		printf("The fee of subscriber is %.0f\n\n", club[i].fee);
	}

	/* Set "size" our new subscribers number */
	* size += new_size;

	return club;
}

/* Function that prints menu */
void menu()
{
	printf("Choose function:\n"
	"1-Add subscribers\n"
	"2-Delete subscriber\n"
	"3-Print all subscribers\n"
	"4-Sort subscribers by ID\n"
	"5-Sort subscribers by fee\n"
	"6-Exit\n");
}

Initial URL


Initial Description
Make database of subscribers that you can add, remove and sort.

Initial Title
Work 5 in C - Subscribers database by array of structs

Initial Tags


Initial Language
C