【问题标题】:switch structures with structs and arrays?用结构和数组切换结构?
【发布时间】: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

【问题讨论】:

    标签: c++ sorting struct


    【解决方案1】:

    您现在对函数所做的是混合数据的属性而不是对其进行排序。您应该使用一个标志来区分要排序的列或 3 个函数 sortByName、sortByID 和 sortByAmout。而且你的ifs是错误的。而不是

    if(c[n].CustomerName > c[n+1].CustomerName)
    {
       tempname = c[n].CustomerName;
       c[n].CustomerName = c[n+1].CustomerName;
       c[n+1].CustomerName = tempname;
    }
    

    应该是这样的

    CustomerProfile tempItem;
    
    if(c[n].CustomerName > c[n+1].CustomerName)
    {
        tempItem = c[n];
        c[n] = c[n+1];
        c[n+1] = tempItem;
    }
    

    【讨论】:

      【解决方案2】:

      您之前接受的questions 答案是使用 std::sort 函数。

      要在这里做你想做的事,你应该有 3 个不同的排序函数

      1. 按名称排序
      2. 按 ID 排序
      3. 按金额排序

      然后你只需调用 std::sort 相关函数

      std::sort(customers.begin(), customers.end(), &sortByName);
      //print your collection here
      std::sort(customers.begin(), customers.end(), &sortById);
      //print your collection here
      std::sort(customers.begin(), customers.end(), &sortByAmount);
      //print your collection here
      

      【讨论】:

      • 谢谢,但不幸的是,我不打算对这个作业使用排序功能。
      • ahhhh,你应该在你的问题中提到这一点。
      【解决方案3】:

      您应该为此使用 std::sort 而不是编写自己的排序代码,除非您有非常好的理由编写自己的排序代码。

      在这种情况下,编写自己的代码不太清晰、效率较低,并且包含重复的代码。

      编辑:好的,不允许使用排序是一个很好的理由。

      在这种情况下,我的建议是尝试将代码重构为函数,并尝试使每个函数只做一件事。这样一来,重复就会变得明显并且可以消除。

      您发布的代码试图在一大块中执行至少四件事。尝试将您的排序代码与您正在排序的代码分开。然后,您应该能够将三种排序方式中的每一种分开。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-04
        • 2012-01-27
        相关资源
        最近更新 更多