使用字符串对基于CHAR的数组进行排序 [英] Sorting a CHAR based Array with Strings

查看:106
本文介绍了使用字符串对基于CHAR的数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在为我的学校编写此项目,并且想知道如何在4个以上的不同位置进行编辑..基本上,当我运行代码时,它将允许您正常添加新客户,但不会对其进行排序,除非您删除预定的数组客户值之一.我认为这可能与i< 7或j< 4有关,我试图弄乱这些值,但似乎这是使程序运行的仅有的两个值

Hello, I''m writing this project for my school, and was wondering how to have more than just 4 different locations to edit..basically when I run the code, it will allow you to add a new customer normally, but will not sort it, unless you delete one of the pre-determined array customer values. I think it might have something to do with the i<7, or j<4, i tried messing with the values, but it seems this is the only two values that makes the program run

//--Print All Customers
void printAllCustomers ( int accountArray[], double balanceArray[], char customerNames[][25] )
{
    int index;
    int i, j;

char temp[SIZE];

for (i=0;i<;7;i++)
{
   for (j=i+1;j<4;j++)
   {
      if (strcmp(customerNames[i],customerNames[j])<0)
       {
         strcpy(temp,customerNames[i]);
         strcpy(customerNames[i],customerNames[j]);
         strcpy(customerNames[j],temp);
        }
    }
}



    for ( index = 0; index<SIZE; index++)
    {
        if ( accountArray[index] != 0 )

            printf("\t%s\n\t%i\n\t%.2lf\n\n", customerNames[index], accountArray[index], balanceArray[index]);


}
    system("pause");
    bankerSide ( accountArray, balanceArray, customerNames );


}
//--------------------------------------------------------------------------------

推荐答案

冒泡排序需要:
The bubble sort requires:
for (i=0; i<n-1;>    for (j=i+1; j<n;>    {
    //...
  }



其中N是应排序的数组的大小(您应将N作为printAllCustomers参数).



where N is the size of the array that should be sorted (you should take N as printAllCustomers argument).


我不确定是否添加了此正确的或不,每当我运行它时,它都会为客户列表提供一堆怪异的数字.





char temp [SIZE];

对于(i = 0; i< 25-1; i ++)
{
对于(j = i + 1; j< 25; j ++)
{
如果(strcmp(customerNames [i],customerNames [j])> 0)
{
strcpy(temp,customerNames [i]);
strcpy(customerNames [i],customerNames [j]);
strcpy(customerNames [j],temp);
}
}
}
I''m not sure if added this correct or not, it comes up with a bunch of weird numbers for the list of customers whenever i go to run it.





char temp[SIZE];

for (i=0;i<25-1;i++)
{
for (j=i+1;j<25;j++)
{
if (strcmp(customerNames[i],customerNames[j])>0)
{
strcpy(temp,customerNames[i]);
strcpy(customerNames[i],customerNames[j]);
strcpy(customerNames[j],temp);
}
}
}


如果期望将索引与其他两个数组对齐,那么您将遭受重创.

我要做的第一件事是创建一个对象,以容纳每个数组中的一个元素(当然,在同一索引处).接下来,我将这些对象放入通用列表中.接下来,我将使用此提示/技巧对所需的属性进行排序:

用于收集项目的通用比较类 [ ^ ]

最后,我将以新的顺序将值放回数组中(如果需要).

您当前的设计是一场疯狂的噩梦.
If you''re expected to keep the indexes lined up with the other two arrays, you''re in a world of hurt.

The first thing I''d do is create an object to hold one element from each array (at the same index, of course). Next, I''d put thos objects into a generic list. Next, I''d use this tip/trick to sort on the desired property:

A Generic Comparison Class for Collection Items[^]

Finally, I''d put the values back into the arrays in the new order (if required).

Your current design is a freakin'' nightmare.


这篇关于使用字符串对基于CHAR的数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆