-1

好的,以前我的代码出错,参数不匹配,现在我在重写排序和交换函数时遇到问题。我对使用哪些变量名感到困惑。我已经尝试了所有我能想到的方法,但我不断出错。我已经完成了我分配给我的两本书的所有阅读,这个任务是星期天到期的,但我正在努力工作并完成它。因此,如果有人能指出我重写排序和交换函数的正确方向,我会非常亲切。我感到困惑的确切代码行如下:

 void swap(salesTran A[], int i, int j)
    {
    int temp;
    temp =A[i];
    A[j] = A[j];
    A[j] = temp;
    return;
    }
    void sort(salesTran A[], int size)
    {
        for(int p=1; p<size; p++)
        {
            for(int c=0; c<size-p; c++)
            {
                if(A[c]>A[c+1]) A (A,c,c+1);
            }
        }
        return;
    }

我的整个程序:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct salesTran {
string name;
double quantity,price;
};

void swap(salesTran A[], int i, int j);
void sort(salesTran A[], int size);

ostream& operator << (ostream& os, salesTran A)
{os << A.name << "\t" << A.quantity << "\t" << A.price;
return os;}
istream& operator >> (istream& is, salesTran& A)
{is >> A.name >> A.quantity >> A.price;
return is;}

int main()
{
   salesTran data[250];

ifstream fin;
fin.open("sales.txt");
ofstream fout;
fout.open("results.txt");

int index = 0;
fin >> data[index];
while(!fin.eof())
{
index++;
fin >> data[index];
}

sort(data, index);

for(int j=0; j < index; j++)
{ 
cout << data[j] << endl;
}

return 0;
}

void swap(salesTran A[], int i, int j)
{
int temp;
temp =A[i];
A[j] = A[j];
A[j] = temp;
return;
}
void sort(salesTran A[], int size)
{
    for(int p=1; p<size; p++)
    {
        for(int c=0; c<size-p; c++)
        {
            if(A[c]>A[c+1]) A (A,c,c+1);
        }
    }
    return;
}
4

2 回答 2

1
A (A,c,c+1);

您正试图A在您显然想要的地方调用函数swap

std::swap另请注意,有些功能std::sort可以满足您的需求,但效率更高。

于 2013-03-05T02:47:59.477 回答
1

在您的交换函数中,temp应该是 object 类型的salesTran对象,而不是int.

于 2013-03-05T02:44:37.667 回答