Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // head.h
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int current_num_tables = 0;
- const int N = 10000;
- int comp(const void *, const void *);
- struct table
- {
- char code[9];
- char name[255];
- int numb;
- };
- void add_table(struct table* t, const char* _code, const char* _name, const int _numb)
- {
- for (int i = 0; i < current_num_tables; ++i)
- {
- if (!strcmp(_code, t[i].code)) {
- t[i].numb += _numb;
- return;
- }
- }
- strcpy(t[current_num_tables].code, _code);
- strcpy(t[current_num_tables].name, _name);
- t[current_num_tables].numb = _numb;
- current_num_tables++;
- return;
- }
- void swap_table(struct table* t, const int num1, const int num2)
- {
- char buf_code[9];
- char buf_name[255];
- int buf_numb;
- strcpy(buf_code, t[num2].code);
- strcpy(buf_name, t[num2].name);
- buf_numb = t[num2].numb;
- strcpy(t[num2].code, t[num1].code);
- strcpy(t[num2].name, t[num1].name);
- t[num2].numb = t[num1].numb;
- strcpy(t[num1].code, buf_code);
- strcpy(t[num1].name, buf_name);
- t[num1].numb = buf_numb;
- }
- void bubbleSort(struct table *t, size_t size) {
- for (size_t i = 1; i < size; i++)
- {
- for (size_t j = 1; j < size; j++)
- {
- if (t[j].numb > t[j - 1].numb) {
- swap_table(t, j, j - 1);
- }
- }
- }
- }
- void print(struct table q[])
- {
- printf("The table looks like this:\n\n");
- printf("Product code\t Name\t Quantity\n\n");
- for (int i = 0; i < N; i++)
- {
- if (q[i].code[0] != '\0')
- {
- printf("%s\t %s\t %d\n", q[i].code, q[i].name, q[i].numb);
- }
- }
- }
- //qq.cpp
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #include <Windows.h>
- #include "head.h"
- int main()
- {
- system("color 0A");
- int i;
- struct table *t;
- t = (struct table*)malloc(N * sizeof(struct table));
- for (i = 0; i < N; i++)
- {
- t[i].code[0] = '\0';
- t[i].name[0] = '\0';
- t[i].numb = 0;
- }
- FILE *f = fopen("work.txt", "r");
- int numb;
- char name[255], code[9];
- while (fscanf(f, "%s %s %i", code, name, &numb) != EOF && current_num_tables < N)
- {
- add_table(t, code, name, numb);
- }
- fclose(f);
- bubbleSort(t, current_num_tables);
- print(t);
- f = fopen("table.txt", "w");
- fprintf(f,"Product code\tName\tQuantity\n\n");
- for (i = 0; i < N; i++)
- {
- if (t[i].code[0] != '\0')
- {
- fprintf(f, "%s\t%s\t%i\n", t[i].code, t[i].name, t[i].numb);
- }
- }
- fclose(f);
- free(t);
- printf("\n\nData is written to a file. Press any key.\n\n\n\n");
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement