大佬教程收集整理的这篇文章主要介绍了如何输入带有空格的名称,作为c中结构数组的成员变量?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用快速排序来实现对结构数组的排序,但它甚至没有正确获取输入。 确切的问题是:我们必须按照商品名称和销售价格按升序排列杂货商品的名称。
#include <stdio.h>
#include <stdlib.h>
#include<String.h>
struct comp
{
int SALEp;
char pname[50];
};
voID swap(struct comp *x,struct comp *y)
{
struct comp temp=*x;
*x=*y;
*y=temp;
}
int partition(struct comp item[50],int lb,int ub)
{
char pivot[50];
strcpy(pivot,item[lb].pName);
int start=lb;
int end=ub;
while(start<end)
{
while(item[start].pname<=pivot)
start++;
while(item[end].pname>pivot)
end--;
if(start<end)
swap(&item[start],&item[end]);
}
}
quickSort(struct comp item[50],int ub)
{
if(lb<ub)
{
int loc=partition(item,lb,ub);
quickSort(item,loc-1);
quickSort(item,loc+1,ub);
}
}
int main()
{
struct comp item[50];
int n;
printf("Enter number of items:\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter product %d name:\n",i);
scanf("%[^\n]",item[i].pName);
printf("Enter SALEs percentage:\n");
scanf("%d",&item[i].SALEp);
}
quickSort(item,n);
printf("Sorted List:\n");
for(int i=0;i<n;i++)
{
puts(item[i].pName);
printf(item[i].SALEp);
}
return 0;
}
我得到以下结果
Enter number of items:
2
Enter product 0 name:
Enter SALEs percentage:
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的如何输入带有空格的名称,作为c中结构数组的成员变量?全部内容,希望文章能够帮你解决如何输入带有空格的名称,作为c中结构数组的成员变量?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。