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