Advertisement
Guest User

Untitled

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