gpt4 book ai didi

c - 如何在 vector 范围内实现排序?

转载 作者:太空宇宙 更新时间:2023-11-04 06:48:25 25 4
gpt4 key购买 nike

<分区>

我的任务是用 C 语言编写一个带有动态内存分配的伪 vector 。我大部分都成功了,但还有一个额外的部分要求我们实现我们选择的排序函数。只是为了看看我是否可以正确地获得索引等,我使用了冒泡排序,但是我在这方面完全没有成功。我认为这是由于我将 vector 结构传递给函数的方式所致,但似乎我正在访问错误的数据。我将添加我的(不成功的)尝试使用 vector 类型的交换函数。我希望有人告诉我我做错了什么。

这是家庭作业,如果还没有明确说明的话。我尝试添加正确的标签,但它似乎不存在于此处。

vec.c

#include "vec.h"
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>

#define GOOD 0;
#define BAD -1;

struct SVec VecInit(void) {
struct SVec vec;
vec.pData = 0;
vec.uiCount = 0;

return vec;
}

int VecAdd(struct SVec * const vec, int const iVal) {
int newsz = vec->uiCount + 1;
if (vec->pData == 0)
vec->pData = (int*)malloc(newsz * sizeof(int));
else
vec->pData = (int*)realloc(vec->pData, newsz * sizeof(int));

*(vec->pData + vec->uiCount) = iVal;
vec->uiCount++;

return 1;
}

int VecContains(struct SVec const * const vec, int const iVal) {
int index;
if (iVal < 0) return BAD;
for (index = 0; index < vec->uiCount; index++) {
if (*(vec->pData + index) == iVal) {
return GOOD; //success, change later
}
}
return BAD; //fail, change later
}

int VecDie(struct SVec * const vec) {
free(vec->pData);
vec->pData = NULL;
vec->uiCount = 0;
//free(vec); //not sure if this is necessary
}

int VecGet(struct SVec const * const vec, int const index) {
if (index > vec->uiCount || vec->uiCount <= 0 || index < 0)
return BAD; //failure
return vec->pData[index];
}

int VecIndexAt(struct SVec const * const vec, int const iVal) {
int index;
if (!VecContains(&vec, iVal))
return BAD; //value wasn't found
for (index = 0; index < vec->uiCount; index++) {
if (*(vec->pData + index) == iVal)
return index;
}

return BAD; //value wasn't found, should not be able to get here but let's be explicit
}

int VecRemove2(struct SVec * const vec, int const iVal) {
int newsize = vec->uiCount - 1;
int start;

for (size_t i = 0; i < vec->uiCount; i++) {
if (*(vec->pData + i) == iVal) {
*(vec->pData + i) = NULL;
start = i;
for (start; start < vec->uiCount; start++) {
if (start + 1 >= vec->uiCount) break; //bound check the end of vector
*(vec->pData + start) = *(vec->pData + start + 1);
}
vec->pData = (int*)realloc(vec->pData, newsize * sizeof(int));
vec->uiCount--;
return GOOD;
}
}

return BAD;
}

int VecRemove(struct SVec * const vec, int const iVal) {
int newsz = vec->uiCount - 1;
int indexToRemove;
int index;

if (vec->uiCount == 0) { return BAD; }

if (!VecContains(&vec, iVal)) { return BAD; }
else {
indexToRemove = VecIndexAt(&vec, iVal);
*(vec->pData + indexToRemove + 1) = NULL;

for (index = 0; index < sizeof(vec[0]) / sizeof(vec); index++) {
if (index == 0) continue;
vec->pData[index] = vec->pData[index + 1];
}

//vec->pData = (int*)realloc(vec->pData, newsz * sizeof(int));
vec->uiCount--;
return GOOD;
}
}

int VecSet(struct SVec * const vec, int const index, int const iVal) {
if (!VecContains(&vec, iVal) || index > vec->uiCount || index < 0) return BAD;
vec->pData[index] = iVal;
}

int VecShow(struct SVec const * const vec) {
int count = vec->uiCount;
int index;

if (vec->uiCount == 0) return -1;

for (index = 0; index <= count; index++) {
if (index == count - 1) {
printf("%d", *(vec->pData + index));
break;
}
printf("%d, ", *(vec->pData + index));
}

printf("\n");
}

//does not work
void VecShuffle(struct SVec * const vec, int const size) {
int i, j, tmp;

for (i = size - 1; i > 0; i--) {
j = rand() % (i + 1);
tmp = VecGet(&vec, j);
VecSet(&vec, j, VecGet(&vec, i));
VecSet(&vec, i, tmp);
}
}

int VecSize(struct SVec const * const vec) {
return vec->uiCount;
}

//does not work
void VecSort(struct SVec * vec) {
int i;
int j;
for (i = 0; i < vec->uiCount - 1; i++) {
for (j = 0; j < vec->uiCount - i - 1; j++) {
if (VecGet(&vec, j) > VecGet(&vec, j + 1)) {
int temp = VecGet(&vec, i);
int iValIndex = VecIndexAt(&vec, i);
int jValIndex = VecIndexAt(&vec, j);
VecSet(&vec, iValIndex, i);
VecSet(&vec, jValIndex, temp);
}
}
}
}

//REMOVE LATER
int VecValueAt(struct SVec const * const vec, int const index) {
if (index > vec->uiCount || vec->uiCount <= 0 || index < 0)
return BAD; //failure
return *(vec->pData + index);
}

//does not work
void VecSwap(struct SVec * vec, int const iVal, int const jVal) {
int temp = VecGet(&vec, iVal);
int iValIndex = VecIndexAt(&vec, iVal);
int jValIndex = VecIndexAt(&vec, jVal);
VecSet(&vec, iValIndex, iVal);
VecSet(&vec, jValIndex, temp);

}

void main() {
struct SVec vec = VecInit();
for (size_t i = 0; i < 5 ; i++)
{
VecAdd(&vec, i);
}

VecShow(&vec);

//VecSort(&vec);

VecShow(&vec);

getchar();
}

vec.h

typedef unsigned int uint;

struct SVec
{
int *pData;
uint uiCount;
};

struct SVec VecInit(void);

int VecAdd(struct SVec const * vec, int const iVal);

int VecContains(struct SVec const const * vec, int const iVal);

int VecDie(struct SVec const * vec);

int VecGet(struct SVec const * const vec, int const index);

int VecIndexAt(struct SVec const * const vec, int const iVal);

int VecRemove(struct SVec const * vec, int const iVal);

int VecRemove2(struct SVec const * vec, int const iVal);

int VecSet(struct SVec * const vec, int const index, int const iVal);

int VecShow(struct SVec const * const vec);

void VecShuffle(struct SVec * const vec, int const size);

int VecSize(struct SVec const * const vec);

void VecSort(struct SVec * const vec);

int VecValueAt(struct SVec const * const vec, int const index);

当我可以让函数工作时,它不会对 vector 产生任何影响,这就是让我想到结构传递问题的原因。现在,如果我使用 VecSort() 程序将无法编译,而且我不记得我更改了什么。

更新了代码以反射(reflect)提到的一些更改以及完整性、原型(prototype)和头文件。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com