1

不确定我是否做对了,或者即使我被允许使用指针进行排序。我制作了一个指向其他元素的结构。然后制作了这个结构的向量,并尝试对其进行一些交换。

起初我在交换操作中使用了取消引用运算符,但是当我这样做时,我的排序变得很奇怪,我在最终结果中得到了一堆重复的#(它们都应该是唯一的),从而解决了取消引用问题这个,但它没有排序(除非可能通过内存地址)。

所以这是我的代码,关注的领域是 sortSuperStruct 函数

#include <algorithm> //used for swap, would like to use for sort, but oh well
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
//#include <math.h>
#include <string>
#include <sstream>
#include <vector>
//------------------------------------------------------------------------------
//namespaces?
//------------------------------------------------------------------------------
using namespace std;
//------------------------------------------------------------------------------
//prototypes
//------------------------------------------------------------------------------

struct superStruct
{
    //probably not needed
    float *percent;

    //copy of Dwarf's role pPercent;
    float *pPercent;
    int *roleName;
    int *name;

};

//parallel array to follow along with Dwarf role
struct roles
{
    int name;
    float priority;
    int numberToAssign;

    void setRolePriorities ()
    {
        priority = (float)rand()/(float)RAND_MAX;
    }

    void setNumberToAssign(int n)
    {
        numberToAssign = rand() % n + 1;

    }
};

//a fit for a role, a Dwarf has this
struct role
{
    float percent;
    //copy of roles name
    int *roleName;
    //copy of roles priority
    float *rolePriority;
    float pPercent;

    void set_pPercent()
    {
        pPercent = percent * (*rolePriority);

    };

};

struct Dwarf
{
    vector <role> roleValues;

    int *maxLabor;

    int name;

    void setRoleValues(int n, vector <roles> *roleS)
    {
        srand ( time(NULL) );
        //rolesValues

        roleValues.resize(n);

        //set role %'s
        for (int x = 0; x < n; x++)
        {
            roleValues[x].percent = (float)rand()/(float)RAND_MAX;
            //cout << roleValues[x].percent << endl;
            //I said I'm pointing to the address value of roleS...
            roleValues[x].roleName = &roleS->at(x).name;
            roleValues[x].rolePriority = &roleS->at(x).priority;
            roleValues[x].set_pPercent();
        }
    };
};

void initializeSuperStruct(vector <superStruct> *s, vector <Dwarf> *d, int n)
{
    //for each Dwarf
    int counter = 0;
    for (int x = 0; x < d->size(); x++)
    {
        //for each role
        for (int y = 0; y < n; y++)
        {
            s->at(counter).pPercent = &d->at(x).roleValues[y].pPercent;
            s->at(counter).name = &d->at(x).name;
            //both are pointers.
            s->at(counter).roleName = d->at(x).roleValues[y].roleName;
            //cout << d->at(x).roleValues[y].percent << endl;
            s->at(counter).percent = &d->at(x).roleValues[y].percent;

            counter++;
        }

    };


};

void printSuperStruct(vector <superStruct> *s)
{
    for (int x = 0; x < s->size(); x++)
    {
        cout << "Count: " << x << endl;
        cout << "Name: " << *s->at(x).name << endl;
        cout << "Percent: " << *s->at(x).percent << endl;
        cout << "pPercent: " << *s->at(x).pPercent << endl;
        cout << "Role Name: " << *s->at(x).roleName << endl;
    }
};

void sortSuperStruct(vector <superStruct> *s)
{
    //runs through list
    for (int i = 0; i < s->size(); i++)
    {
        //checks against lower element
        //cout << *s->at(i).pPercent << endl;
        for (int j = 1; j < (s->size()-i); j++)
        {
            //using pointer dereferences messed this up, but, not using them doesn't sort right
            if (*s->at(j-1).pPercent < *s->at(j).pPercent)
            {
                //cout << *s->at(j-1).pPercent << "vs. " << *s->at(j).pPercent << endl;

                //I don't think swap is working here
                //superStruct temp;
                //temp = s->at(j-1);
                //s->at(j-1) = s->at(j);
                //s->at(j) = temp;
                swap(s->at(j-1), s->at(j));
                //cout << *s->at(j-1).pPercent << "vs. " << *s->at(j).pPercent << endl;
            }
        }
    }

}

int main()
{
    srand (time(NULL));

    int numberOfDwarves;
    int numberOfRoles;
    int maxLabors;

    vector <superStruct> allRoles;
    vector <Dwarf> myDwarves;
    vector <roles> myRoles;

    cout << "number of Dwarf's: " << endl;
    cin >> numberOfDwarves;

    cout << "max labor per dwarf" << endl;
    cin >> maxLabors;

    cout << "number of Roles: " << endl;
    cin >> numberOfRoles;

    //this will probably have to be a vector
    //Dwarf myDwarfs[numberOfDwarves];

    myDwarves.resize(numberOfDwarves);
    allRoles.resize(numberOfDwarves*numberOfRoles);
    myRoles.resize(numberOfRoles);

    //init roles first
    for (int x = 0; x < numberOfRoles; x++)
    {
        myRoles[x].name = x;
        myRoles[x].setRolePriorities();
        myRoles[x].setNumberToAssign(numberOfDwarves);
    }

    for (int x = 0; x < numberOfDwarves; x++)
    {
        myDwarves[x].setRoleValues(numberOfRoles, &myRoles);
        myDwarves[x].name = x;

        //messy having this here.
        myDwarves[x].maxLabor = &maxLabors;

    }

    initializeSuperStruct(&allRoles, &myDwarves, numberOfRoles);
    cout << "Before sort, weighted matrix" << endl;
    system("pause");
    printSuperStruct(&allRoles);
    sortSuperStruct(&allRoles);
    system("pause");
    cout << "After sort, weighted matrix" << endl;
    printSuperStruct(&allRoles);
}

输出:

    Role Name: 5
    Count: 96
    Name: 6
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5
    Count: 97
    Name: 7
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5
    Count: 98
    Name: 8
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5
    Count: 99
    Name: 9
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5
4

2 回答 2

2
srand (time(NULL));

调用一次,在开始时main。在各种方法中使用它意味着你会得到很多重复的值。

if (s->at(j-1).pPercent < s->at(j).pPercent)

这确实比较了指针值,因为指针不一定指向同一个数组,具有未定义的行为(可能会比较虚拟地址)。如果要按值排序,则需要取消引用

if (*s->at(j-1).pPercent < *s->at(j).pPercent)

而在

    for (int j = 1; j < (s->size()-1); j++)
    {
        //using pointer dereferences messed this up, but, not using them doesn't sort right
        if (s->at(j-1).pPercent < s->at(j).pPercent)

你从不考虑最后一个元素,那应该是

for(unsigned int j = 1; j < s->size() - i; ++j)

进行适当的冒泡排序。

于 2012-11-09T15:29:40.547 回答
0

你的冒泡排序对我来说似乎不正确。

它应该类似于:

void sortSuperStruct(vector <superStruct>& s)
  for(int i = 0; i < s.size()-1; i++){
    for(int j = i; j < s.size(); j++){
      if(*(s[i].pPercent) < *(s[j].pPercent)){
        swap(s.at(i),s.at(j));
      }
    }
  }
}

主要区别:

  • i 从 0 到 size-1,而不是从 0 到 size
  • j 从 i 到 size,而不是 1 到 size-1
  • if 语句测试 i 和 j,而不是 j 和 j-1
  • 交换也交换 i 和 j,而不是 j 和 j-1

细微差别:

  • 向量通过引用而不是指针传递。这可能并不重要,但至少对我来说它看起来更干净。

请注意,这将从最大到最小排序。如果你希望它从最小到最大,你的 if 语句应该有一个 > 符号。

于 2012-11-09T15:21:45.607 回答