Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Входные данные имеют вид: 123dh678 iphone 100
- 8символов - шифр, название продукта и количество произведенного продукта. Все это в main читается из файла и добавляется в таблицу с помощью функции table_add. current_num_tables - количество элементов в таблице.
- 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)
- {
- size_t i, index = 0;
- if (index == 0)
- {
- strcpy(t[index].code, _code);
- strcpy(t[index].name, _name);
- t[index].numb = _numb;
- index++;
- current_num_tables++;
- }
- binarysearch(_numb, t, current_num_tables);
- index = binarysearch(_numb, t, current_num_tables);
- if (t[index].numb != NULL)
- for (i = 1; i < index; i++)
- {
- memcpy(&t[i], &t[i - 1], sizeof(*t));
- }
- else
- {
- strcpy(t[index].code, _code);
- strcpy(t[index].name, _name);
- t[index].numb = _numb;
- }
- current_num_tables++;
- return;
- }
- int binarysearch(size_t a, struct table* t, size_t n)
- {
- int low, high, middle;
- low = 0;
- high = n - 1;
- while (low <= high)
- {
- middle = (low + high) / 2;
- if (a < t[middle].numb)
- high = middle - 1;
- else if (a > t[middle].numb)
- low = middle + 1;
- else
- return middle;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement