问题描述
我得到了以下结构:
struct data
{
string code;
string description;
float price;
};
在用户将数据插入data a[]
中之后,该程序应该查找并打印具有用户要求价格的产品(使用二进制搜索)。
这是我写的(n是用户插入的产品数):
void binary(int n,data a[])
{
int last = 0,first = n-1,med,p;
bool found = false;
cout<<"\nInsert the price of the products you'd like to see: ";
cin>>p;
while(last <= first && !found)
{
med = (last + first)/2;
if( a[med].price == p )
{
print(med,a);
found = true;
}
else if( a[med].price > p )
first = med - 1;
else
last = med + 1;
}
if(found)
{
int pos = med;
while(a[++pos].price == p)
{
print(pos,a);
}
while(a[--med].price == p)
{
print(pos,a);
}
}
else
cout<<"\nThere are no products that have the price you've inserted";
}
打印功能如下:
void print(int x,data a[])
{
cout<<"\nCode: " << a[x].code;
cout<<"\nDescription: " << a[x].description;
cout<<"\nPrice: " << a[x].price;
}
问题在于,当没有产品具有插入价格时,cout
不会打印任何内容。 (因此它确实知道何时有符合标准的产品,但不会打印它们)。提前非常感谢您!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)