Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- #include <stdlib.h>
- #include <string.h>
- const int MAXNAMESIZE = 25; // 25 символов для имени и фирмы будет достаточно, чтобы не мучаться с динамическими массивами
- const int MAXFIRMSIZE = 25;
- const int MAXARRSIZE = 10000;
- struct accessories {
- char name[MAXNAMESIZE];
- char firm[MAXFIRMSIZE];
- int cost, count;
- };
- void Append(struct accessories*, int);
- int menu() {
- int option = 0;
- int k = 0;
- do
- {
- printf("\n|------|\n\n");
- printf("Меню: \n");
- printf("1.Создать массив структур. \n");
- printf("2.Просмотреть. \n");
- printf("3.Упорядочить. \n");
- printf("4. Добавить. \n");
- printf("5. Найти. \n");
- printf("6. Удалить. \n");
- printf("7. Удалить всё. \n");
- printf("8.Выход \n");
- scanf("%d",&option);
- printf("\n");
- if ((option>0) && (option < 9))
- k=1;
- }
- while(!k);
- return option;
- }
- void View(struct accessories *array, int FLAG) {
- if (FLAG == 0)
- printf("Массив структур пуст!\n");
- else {
- printf ("----------------------------------------------------------------------\n");
- printf ("Name Firm Cost Count\n");
- for (int i = 0; i < FLAG; i++) {
- printf ("%-25s %-25s %-9d %-9d\n", array[i].name, array[i].firm, array[i].cost, array[i].count);
- }
- printf ("----------------------------------------------------------------------\n");
- }
- }
- void Sort(struct accessories *array, int FLAG) {
- for (int i = 0; i < FLAG - 1; i++) {
- for (int j = 0; j < FLAG - i - 1; j++) {
- if (strcmp(array[j].name, array[j+1].name) > 0) {
- // тут меняем местами структуру
- int t = array[j].cost;
- array[j].cost = array[j+1].cost;
- array[j+1].cost = t;
- t = array[j].count;
- array[j].count = array[j+1].count;
- array[j+1].count = t;
- char tmp1[MAXNAMESIZE];
- strcpy(tmp1, array[j].name);
- strcpy(array[j].name, array[j+1].name);
- strcpy(array[j+1].name, tmp1);
- char tmp2[MAXFIRMSIZE];
- strcpy(tmp2, array[j].firm);
- strcpy(array[j].firm, array[j+1].firm);
- strcpy(array[j+1].firm, tmp2);
- }
- }
- }
- }
- void Append(struct accessories *array, int FLAG) {
- printf("Введите название: ");
- char *name = (char*)malloc(25);
- scanf("%25s", name);
- printf("Введите фирму: ");
- char *firm = (char*)malloc(25);
- scanf("%25s", firm);
- printf("Введите стоимость: ");
- int cost, count;
- scanf("%d", &cost);
- printf("Введите кол-во: ");
- scanf("%d", &count);
- FLAG++;
- for (int i = FLAG; i > 0; i--) // сдвигаем все элементы на 1
- array[i] = array[i-1];
- strcpy(array[0].name, name); // заполняем первый(новый) элемент
- strcpy(array[0].firm, firm);
- array[0].cost = cost;
- array[0].count = count;
- }
- void Find(struct accessories *array, int FLAG) {
- if (FLAG == 0) {
- printf("Массив пуст!\n");
- return;
- }
- printf("Как искать?\n");
- printf("1. По названию\n");
- printf("2. По диапазону стоимости\n");
- int k = 0;
- scanf("%d", &k);
- if (k == 1) {
- char *name = (char*)malloc(25);
- printf("Введите название: ");
- scanf("%s", name);
- int count = 0;
- printf ("----------------------------------------------------------------------\n");
- printf ("Name Firm Cost Count\n");
- for (int i = 0; i < FLAG; i++){
- if (strcmp(array[i].name, name) == 0) {
- count += 1;
- printf ("%-25s %-25s %-9d %-9d\n", array[i].name, array[i].firm, array[i].cost, array[i].count);
- }
- }
- if (count == 0)
- printf("Ничего не найдено! \n");
- printf ("----------------------------------------------------------------------\n");
- }
- else if (k == 2) {
- int l = 0, r = 0;
- printf("Введите начало диапазона: ");
- scanf("%d", &l);
- printf("Введите конец диапазона: ");
- scanf("%d", &r);
- int count = 0;
- printf ("----------------------------------------------------------------------\n");
- printf ("Name Firm Cost Count\n");
- for (int i = 0; i < FLAG; i++){
- if ((array[i].cost >= l) && (array[i].cost <= r)) {
- count += 1;
- printf ("%-25s %-25s %-9d %-9d\n", array[i].name, array[i].firm, array[i].cost, array[i].count);
- }
- }
- if (count == 0)
- printf("Ничего не найдено! \n");
- printf ("----------------------------------------------------------------------\n");
- }
- else {
- printf ("Отмена поиска!\n");
- return;
- }
- }
- int Delete(struct accessories *array, int FLAG) {
- int deletecount = 0;
- if (FLAG == 0) {
- printf("Массив пуст!\n");
- return 0;
- }
- printf("Введите максимальную стоимость: ");
- int maxcost = 0;
- scanf("%d", &maxcost);
- int i = 0;
- while (i < FLAG) {
- if (array[i].cost > maxcost) {
- int j = i;
- while (j < FLAG) {
- strcpy(array[j].name, array[j+1].name);
- strcpy(array[j].firm, array[j+1].firm);
- array[j].cost = array[j+1].cost;
- array[j].count = array[j+1].count;
- j++;
- }
- deletecount++;
- i--;
- }
- i++;
- }
- if (deletecount == 0)
- printf("Ничего не удалено!\n");
- else
- printf("Удалено элементов: %d", deletecount);
- return deletecount;
- }
- void DeleteAll(struct accessories *array, int FLAG) {
- FLAG = 0;
- printf("Все записи удалены! \n");
- }
- int main() {
- setlocale(LC_ALL,"rus");
- struct accessories array[MAXARRSIZE];
- int k = 0;
- int FLAG = 0; // кол-во структур в массиве
- while(!k)
- {
- switch (menu())
- {
- case 1:
- if (FLAG == 0) {
- printf("Введите размер массива: ");
- int n = 0;
- scanf("%d", &n);
- FLAG = 0;
- for (int i = 0; i < n;i++) {
- Append(array, n);
- }
- FLAG = n;
- printf("Массив создан! \n");
- }
- else
- printf("Массив уже создан!!! \n");
- break;
- case 2:
- printf("Выводим массив:\n\n");
- View(array, FLAG);
- break;
- case 3:
- Sort(array, FLAG);
- printf("Массив отсортирован!\n");
- break;
- case 4:
- Append(array, FLAG);
- FLAG++;
- printf("Новый элемент добавлен!\n");
- break;
- case 5:
- Find(array, FLAG);
- break;
- case 6:
- FLAG -= Delete(array, FLAG);
- break;
- case 7:
- DeleteAll(array, FLAG);
- FLAG = 0;
- break;
- case 8:
- printf("Bye!\n");
- k = 1;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement