【发布时间】:2010-10-27 01:49:42
【问题描述】:
我之前有一个关于按名称、ID 和金额组织一些输入的问题。现在我已经弄清楚如何组织它们,我需要有 3 个输出;第一个按名称,第二个按 ID,最后一个按金额。我可以使用 case 和 switch 语句吗?每次我尝试时,所有三个输出都是按名称命名的。
这是我目前所拥有的:
void GeneralSort(const int SortItem, const int count, CustomerProfile c[])
{
string tempname;
string tempid;
float tempamount;
for(int iteration = 1; iteration < count; iteration ++)
{
for(int n = 0; n < (count-iteration); n++)
{
if(c[n].CustomerName > c[n+1].CustomerName)
{
tempname = c[n].CustomerName;
c[n].CustomerName = c[n+1].CustomerName;
c[n+1].CustomerName = tempname;
}
if(c[n].CustomerId > c[n+1].CustomerId)
{
tempid = c[n].CustomerId;
c[n].CustomerId = c[n+1].CustomerId;
c[n+1].CustomerId = tempid;
}
if(c[n].AmountDue > c[n+1].AmountDue)
{
tempamount = c[n].AmountDue;
c[n].AmountDue = c[n+1].AmountDue;
c[n+1].AmountDue = tempamount
}
我如何获取其余数据,因此它将按 ID 输出第二个输出,按金额输出第三个输出。我认为您可以添加 switch 语句,但是当我累了时,所有三个输出都是按名称的第一组。任何帮助表示赞赏。我不指望任何人为我解决这一切,只是提示我指向正确的方向。
输出示例:
//by name
name id amount
able b2 24
bob g3 68
carry a4 12
//by id
name id amount
carry a4 12
able b2 24
bob g3 68
//by amount
name id amount
carry a4 12
able b2 24
bob g3 68
【问题讨论】: