Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. // head.h
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. int current_num_tables = 0;
  7. const int N = 10000;
  8. int comp(const void *, const void *);
  9. struct table
  10. {
  11.     char code[9];
  12.     char name[255];
  13.     int numb;
  14. };
  15. void add_table(struct table* t, const char* _code, const char* _name, const int _numb)
  16. {
  17.     for (int i = 0; i < current_num_tables; ++i)
  18.     {
  19.         if (!strcmp(_code, t[i].code)) {
  20.             t[i].numb += _numb;
  21.             return;
  22.         }
  23.     }
  24.     strcpy(t[current_num_tables].code, _code);
  25.     strcpy(t[current_num_tables].name, _name);
  26.     t[current_num_tables].numb = _numb;
  27.     current_num_tables++;
  28.     return;
  29. }
  30.  
  31. void swap_table(struct table* t, const int num1, const int num2)
  32. {
  33.     char buf_code[9];
  34.     char buf_name[255];
  35.     int  buf_numb;
  36.  
  37.     strcpy(buf_code, t[num2].code);
  38.     strcpy(buf_name, t[num2].name);
  39.     buf_numb = t[num2].numb;
  40.  
  41.     strcpy(t[num2].code, t[num1].code);
  42.     strcpy(t[num2].name, t[num1].name);
  43.     t[num2].numb = t[num1].numb;
  44.  
  45.     strcpy(t[num1].code, buf_code);
  46.     strcpy(t[num1].name, buf_name);
  47.     t[num1].numb = buf_numb;
  48. }
  49. void bubbleSort(struct table *t, size_t size) {
  50.     for (size_t i = 1; i < size; i++)
  51.     {
  52.         for (size_t j = 1; j < size; j++)
  53.         {
  54.             if (t[j].numb > t[j - 1].numb) {
  55.                 swap_table(t, j, j - 1);
  56.             }
  57.         }
  58.     }
  59. }
  60. void print(struct table q[])
  61. {
  62.     printf("The table looks like this:\n\n");
  63.     printf("Product code\t Name\t Quantity\n\n");
  64.     for (int i = 0; i < N; i++)
  65.     {
  66.         if (q[i].code[0] != '\0')
  67.         {
  68.             printf("%s\t %s\t %d\n", q[i].code, q[i].name, q[i].numb);
  69.         }
  70.     }
  71. }
  72. //qq.cpp
  73. #define _CRT_SECURE_NO_WARNINGS
  74. #include <stdio.h>
  75. #include <string.h>
  76. #include <conio.h>
  77. #include <Windows.h>
  78. #include "head.h"
  79.  
  80. int main()
  81. {
  82.     system("color 0A");
  83.     int i;
  84.     struct table *t;
  85.     t = (struct table*)malloc(N * sizeof(struct table));
  86.     for (i = 0; i < N; i++)
  87.     {
  88.         t[i].code[0] = '\0';
  89.         t[i].name[0] = '\0';
  90.         t[i].numb = 0;
  91.     }
  92.     FILE *f = fopen("work.txt", "r");
  93.     int numb;
  94.     char name[255], code[9];
  95.     while (fscanf(f, "%s %s %i", code, name, &numb) != EOF && current_num_tables < N)
  96.     {
  97.         add_table(t, code, name, numb);
  98.     }
  99.     fclose(f);
  100.     bubbleSort(t, current_num_tables);
  101.     print(t);
  102.     f = fopen("table.txt", "w");
  103.     fprintf(f,"Product code\tName\tQuantity\n\n");
  104.     for (i = 0; i < N; i++)
  105.     {
  106.         if (t[i].code[0] != '\0')
  107.  
  108.         {
  109.             fprintf(f, "%s\t%s\t%i\n", t[i].code, t[i].name, t[i].numb);
  110.         }
  111.     }
  112.     fclose(f);
  113.     free(t);
  114.         printf("\n\nData is written to a file. Press any key.\n\n\n\n");
  115.     _getch();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement