Дан список, содержащий 10 записей, каждая из которых имеет структуру:
Шифр товара: тип строка(5 символов)
Наименование товара: тип строка(20 символов)
Цена (грн.): вещественное
Признак наличия или отсутствия: логическое
Список упорядочен по убыванию цены товара.Разработать алгоритмы и программы линейного и двоичного поиска всех товаров, цена которых не превышает 1500 грн., с выводом найденных записей на экран.
В написаной программе не корректно работает сортировка : цена не соответствует товару, и двоичный поиск тоже видно из-за этого тоже не работает должным образом
Вот код программы:
Код: Выделить всё
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <math.h>
struct shop
{
char code[6];
char name[20];
float price;
bool is;
};
int _tmain(int argc, _TCHAR* argv[])
{
shop a[]={
{"12345","Printer",400,true},
{"54321","holodilnik",5000,true} ,
{"52316","noytbyk",7500,true},
{"15632","tv",8700,true},
{"86423","pilesos",450,true},
{"79563","MFY",1650,true},
{"33256","hlebopesh",1200,true},
{"99854","monitor",1400,true},
{"98648","telefon",500,true},
{"36978","radio",150,true}
};
for(int i=0; i<10; i++) printf("%s %s %f %i\n ", a[i].code, a[i].name, a[i].price, a[i].is);
printf("Seache method #1...\n");
for(int i=0; i<10;i++)
{
if(a[i].price<1500)
{
printf("%s %s %f %i\n ", a[i].code, a[i].name, a[i].price, a[i].is);
}
}
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9-i; j++)
{
if(a[j].price>a[j+1].price)
{
float tmp = a[j].price;
a[j].price = a[j+1].price;
a[j+1].price = tmp;
}
}
}
printf("Sort...\n");
for(int i=0; i<10; i++) printf("%s %s %f %i\n ", a[i].code, a[i].name, a[i].price, a[i].is);
printf("Seache method #2...\n");
int k=0,max=0,min=10,b;
do
{
k=(max+min)/2;
if(a[k].price<1500)
min=k;
else
max=k-1;
} while(!(abs(max-min)<=1));
if(a[max].price<1500) b=max;
if(a[min].price<1500) b=min;
for(int i=0; i<b; i++) printf("%s %s %f %i\n ", a[i].code, a[i].name, a[i].price, a[i].is);
while(!_kbhit());
return 0;
}