我正在参加C ++入门课程,并且在理解我的排序错误方面遇到了一些麻烦。 首先我知道我的格式不是最好的,抱歉。我正在学习并试图改进它。 对于一些上下文,我已经为之前的任务编写了相同的程序,并且在大多数情况下它运行良好。区别在于我在之前的任务中使用了一个简单的冒泡排序,如下所示:
for(int i = 1;i <totalEmployeeCount; i++)
{
for( j = 0; j < totalEmployeeCount - i;j++)
{
if(employee[j]->netPay > employee[j+1]->netPay)
{
payroll * temp;
temp = employee[j];
employee[j] = employee[j+1];
employee[j+1] = temp;
但后来我的任务是将我的冒泡排序与选择排序相结合。所以我首先在这些参数中写了一个简单的排序 -
#include<iostream>
using namespace std;
int swap (int[],int,int);
int main()
{
int slot[10] = {8,3,1,5,7,10,4,9,6,2};
int n = 10,i;
int lower,upper,sml,sortflag,scan;
lower = 0;
upper = n - 1;
sortflag = 1;
while((lower<upper)&&(sortflag==1))
{
sml = lower;
sortflag = 0;
scan = lower+1;
while (scan<=upper-lower)
{
if (slot[scan]>slot[scan+1])
{
swap(slot,scan,scan+1);
sortflag=1;
if(slot[scan]<slot[sml])
sml = scan;
}
scan++;
}
swap(slot,lower,sml);
upper = upper - 1;
lower = lower + 1;
}
cout<<"after while sort: "<<endl;
for(i = 0; i < n; i++) {
cout << slot[i] << " " << endl;
}
}
swap(int slot[],int i, int j){
int temp;
temp = slot[i];
slot[i] = slot[j];
slot[j] = temp;
}
这很好。但是,当我尝试实现我的组合排序(我的老师称之为exsel排序)时,它不再排序。我没有收到任何错误消息,也没有任何警告(使用Dev C ++),程序的其余部分按预期运行。我能想到的最好的是我没有正确地将参数传递给交换函数,但是我确实是一个新手并且我并不完全确定我知道这意味着什么,即使我输入它。如果有人愿意帮助我完全理解我的愚蠢,那就太棒了。谢谢!
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
int hrs;
class payroll{
public:
int employeeID, payStat;
string firstName, lastName;
double salary,hourlyRate, hours, otHours, regPay, otPay, grossPay,
taxRate, taxAmount, netPay;
public: void setVariables(int empID,string fName,string lName, int stat,
double rate, double hrs)
{
employeeID = empID;
firstName = fName;
lastName = lName;
payStat = stat;
if(payStat == 1){hourlyRate = rate;}
else{salary = rate;}
hours = hrs;
}
public: virtual double calculateGrossPay()=0;
double calculateTaxAmount()
{
taxRate = .30;
taxAmount = grossPay*taxRate;
return taxAmount;
}
double calculateNetPay()
{
netPay = grossPay - taxAmount;
return netPay;
} // END CALCULATENETPAY() FUNCTION.
void printHeadings()
{
cout<<setw(45)<<"--PAY ROLL REPORT--"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<" NAME ID HW OT GROSS-PAY NET-PAY OT-PAY"<< endl;
cout<<"----------------------------------------------------------------------------"<<endl;
} // end headings
void printData()
{
cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
cout<<setw(6)<<firstName<<setw(8)<<lastName<<setw(8)<<employeeID<<setw(6)<<hours<<setw(6)
<<otHours<<setw(8)<<grossPay<<setw(10)<<netPay<<setw(10)<<otPay<<endl;
}
};
class employeeSalary : public payroll
{
double calculateGrossPay()
{
double hourlyRate = (salary/2080);
double regPay = (40*hourlyRate);
if (hours > 40)
{
otHours = (hours - 40); //calculate OT hours
otPay = (otHours * hourlyRate); //calculate OT pay
grossPay = regPay;
}
else if (hours <= 40)
{
otHours = 0; otPay = 0; grossPay = regPay;
}
return grossPay;
}
};
class employeeHourly : public payroll
{
public:
double calculateGrossPay()
{
double regPay = (hrs * hourlyRate); //calculate regular hours
if (hours > 40)
{
otHours = (hours - 40); //calculate OT hours
otPay = (otHours * hourlyRate * 1.5); //calculate OT pay
int reg = hours - otHours;
regPay = reg*hourlyRate;
grossPay = (regPay + otPay); //calculate gross pay
} //end if clause for gross pay with overtime
else
{
otHours = 0; otPay = 0; grossPay = regPay;
} //end else clause for four hours
return grossPay;
} //end calculateGrossPay() function
}; //end EmployeeHourly class
swap (payroll*employee[],int i, int j)
{
payroll*temp;
temp = employee[j];
employee[j] = employee[i];
employee[i] = temp;
}
int main()
{
int employeeCounter = 0;
int totalEmployeeCount,empID,stat,m;
int lower,upper,sml,sortflag,scan;
sml = 0;
scan = 0;
upper = totalEmployeeCount - 1;
lower = 0;
sortflag = 1;
double min,max,rate;
string fName;
string lName;
min = 2000.00;
max = 0;
cout<< "enter # of employees you want to process: ";
cin>>totalEmployeeCount;
payroll* employee[100];
while(employeeCounter < totalEmployeeCount)
{
cout<<"Is employee "<<employeeCounter+1<< " hourly or salary? (enter 1 for hourly / 2 for salary):";
cin>>stat;
cout<<endl;
if (stat == 1)
{
cout<< "Instantiating a HOURLY employee object inherited from base class employee"
<<endl<<endl;
cout<<"Enter employee's ID: ";
cin>>empID;
cout<<"Enter employee's first name: ";
cin>>fName;
cout<<"Enter employee's last name: ";
cin>>lName;
cout<<"Enter employee's hourly wage: ";
cin>>rate;
cout<<"Enter employee's hours for this week: ";
cin>>hrs;
employee[employeeCounter] = new employeeHourly();
employee[employeeCounter]->setVariables(empID, fName, lName, stat, rate, hrs);
employee[employeeCounter]->calculateGrossPay();
employee[employeeCounter]->calculateTaxAmount();
employee[employeeCounter]->calculateNetPay();
cout<<endl<<endl;
employeeCounter++;
} //end if
else
{
cout<<endl<<"instantialting a SALARY employee object inherited from base class employee" <<endl<<endl;
cout<<"Enter employee's ID: ";
cin>>empID;
cout<<"Enter employee's first name: ";
cin>>fName;
cout<<"Enter employee's last name: ";
cin>>lName;
cout<<"Enter employee's Salary: ";
cin>>rate;
cout<<"Enter employee's hours for this week: ";
cin>>hrs;
employee[employeeCounter] = new employeeSalary();
employee[employeeCounter]->setVariables(empID, fName, lName, stat, rate, hrs);
employee[employeeCounter]->calculateGrossPay();
employee[employeeCounter]->calculateTaxAmount();
employee[employeeCounter]->calculateNetPay();
cout<<endl<<endl;
employeeCounter++;
}
}
employee[0]->printHeadings();
while((lower<upper)&&(sortflag==1))
{
sml = lower;
sortflag = 0;
scan = lower+1;
while (scan <= upper-lower)
{
if (employee[scan]->netPay> employee[scan+1]->netPay)
{
swap(employee,scan,scan+1);
sortflag=1;
if(employee[scan]->netPay < employee[sml]->netPay)
sml = scan;
}
scan++;
}
swap(employee,lower,sml);
upper = upper - 1;
lower = lower + 1;
}
cout<<"after sort: "<<endl;
for (m=0; m < totalEmployeeCount; m++)
{
if (employee[m]->netPay > max);
max = employee[m]->netPay;
m++;
}
for(int i = 0; i < employeeCounter; i++)
{
employee[i]->printData();
}
return 0;
}