C 在我的程序中断函数之前添加排序算法
所以我写了一个排序算法(我还在学习,所以可能有点乱) 现在,当我将它添加到我的main.c中时,它甚至没有启动就中断了它之前的函数,这毫无意义 排序功能是:C 在我的程序中断函数之前添加排序算法,c,sorting,C,Sorting,所以我写了一个排序算法(我还在学习,所以可能有点乱) 现在,当我将它添加到我的main.c中时,它甚至没有启动就中断了它之前的函数,这毫无意义 排序功能是: void ordinaAlfabeticamente(RecordSoggetto *elenco, int nElementi) { printf("avviando sorting"); int i, j, comparatore; RecordSoggetto *copia = (Recor
void ordinaAlfabeticamente(RecordSoggetto *elenco, int nElementi) {
printf("avviando sorting");
int i, j, comparatore;
RecordSoggetto *copia = (RecordSoggetto *)malloc(sizeof(RecordSoggetto));
_Bool isStillSorting = true;
while (isStillSorting) {
isStillSorting = false;
for (i = 1; i < nElementi; i++) {
memcpy(copia, &elenco[i], sizeof(RecordSoggetto));
j = i - 1;
comparatore = strcmp(elenco[j].cognome, copia->cognome);
if (comparatore == 0) {
comparatore = strcmp(elenco[j].nome, copia->nome);
}
while (j > 0 && comparatore) {
isStillSorting = true;
memcpy(&elenco[j + 1], &elenco[j], sizeof(RecordSoggetto));
j--;
memcpy(&elenco[j + 1], copia, sizeof(RecordSoggetto));
}
}
}
}
void sortAlphabetically(RecordSoggetto *elementList, int nElements) {
printf("start sorting");
int i, j, comparatore;
RecordSoggetto *copy = (RecordSoggetto *)malloc(sizeof(RecordSoggetto));
_Bool isStillSorting = true;
while (isStillSorting) {
isStillSorting = false;
for (i = 1; i < nElements; i++) {
memcpy(copy, &elementList[i], sizeof(RecordSoggetto));
j = i - 1;
comparatore = strcmp(elementList[j].surname, copy->surname);
if (comparatore == 0) {
comparatore = strcmp(elementList[j].name, copy->name);
}
while (j > 0 && comparatore) {
isStillSorting = true;
memcpy(&elementList[j + 1], &elementList[j], sizeof(RecordSoggetto));
j--;
memcpy(&elementList[j + 1], copy, sizeof(RecordSoggetto));
}
}
}
}
有趣的是,如果函数ordinalarfabeticamente
是主要的,那么函数caricaDati
在第一行就放弃了,就像字面上一样
加勒比:
void caricaDati(VD *dati, char *nomefile) {
printf("avviando carica dati\n");
FILE *fp = apriFile(nomefile, "rb"); //apriFile is just open with control
printf("file aperto");
fread(&dati->nElementi, sizeof(int), 1, fp);
printf("letto numero elementi");
dati->v = (RecordSoggetto *)malloc(dati->nElementi * sizeof(RecordSoggetto));
fread(dati->v, sizeof (RecordSoggetto), dati->nElementi, fp);
printf("letto criminali");
chiudiFile(fp);
};
printf
用于控制,只有第一个函数启动,我也尝试在函数apriFile
中放入printf,但它们没有启动,所以我假设函数在第一个printf
之后就消失了
更具体地说,一旦启动程序,它只打印avviando carica dati\n
并进入无限循环
然而
如果我只是删除了对ordinalFabeticamente
的调用,我不会遇到任何问题
我是否应该在排序功能启动之前让js等待caricaDati
完成
我尝试将所有内容翻译成英语,使其更具可读性:
所以我写了一个排序算法(我还在学习,所以可能有点乱)
现在,当我将它添加到main.c中时,它甚至没有启动就中断了它之前的函数,这毫无意义
排序功能是:
void ordinaAlfabeticamente(RecordSoggetto *elenco, int nElementi) {
printf("avviando sorting");
int i, j, comparatore;
RecordSoggetto *copia = (RecordSoggetto *)malloc(sizeof(RecordSoggetto));
_Bool isStillSorting = true;
while (isStillSorting) {
isStillSorting = false;
for (i = 1; i < nElementi; i++) {
memcpy(copia, &elenco[i], sizeof(RecordSoggetto));
j = i - 1;
comparatore = strcmp(elenco[j].cognome, copia->cognome);
if (comparatore == 0) {
comparatore = strcmp(elenco[j].nome, copia->nome);
}
while (j > 0 && comparatore) {
isStillSorting = true;
memcpy(&elenco[j + 1], &elenco[j], sizeof(RecordSoggetto));
j--;
memcpy(&elenco[j + 1], copia, sizeof(RecordSoggetto));
}
}
}
}
void sortAlphabetically(RecordSoggetto *elementList, int nElements) {
printf("start sorting");
int i, j, comparatore;
RecordSoggetto *copy = (RecordSoggetto *)malloc(sizeof(RecordSoggetto));
_Bool isStillSorting = true;
while (isStillSorting) {
isStillSorting = false;
for (i = 1; i < nElements; i++) {
memcpy(copy, &elementList[i], sizeof(RecordSoggetto));
j = i - 1;
comparatore = strcmp(elementList[j].surname, copy->surname);
if (comparatore == 0) {
comparatore = strcmp(elementList[j].name, copy->name);
}
while (j > 0 && comparatore) {
isStillSorting = true;
memcpy(&elementList[j + 1], &elementList[j], sizeof(RecordSoggetto));
j--;
memcpy(&elementList[j + 1], copy, sizeof(RecordSoggetto));
}
}
}
}
现在有趣的是,如果函数sortAlphabetically
是主要的,那么函数loadData
在第一行就放弃了,就像字面上一样
加载数据:
void loadData(VD *data, char *filename) {
printf("starting load data\n");
FILE * fp = openFile(filename, "rb"); //apriFile is just open with control
printf("file opened");
fread(&data->nElements, sizeof(int), 1, fp);
printf("read number of elements");
data->v = (RecordSoggetto *)malloc(data->nElements * sizeof(RecordSoggetto));
fread(data->v, sizeof (RecordSoggetto), data->nElements, fp);
printf("read elements");
closeFile(fp);
};
printf是用于控制的,只有第一个函数启动,我也尝试在函数apriFile
中放入printf,但是它们没有启动,所以我假设函数在第一个printf之后就死了
更具体地说,一旦启动程序,它只打印start load data\n
并进入无限循环
然而
如果我只是删除了对sortAlphabetically的调用,我就不会有问题了
我是否应该让js在排序功能启动之前等待加载数据完成?您的排序算法不正确,它无法在正确的位置交换记录 以下是修改后的版本:
void ordinaAlfabeticamente(RecordSoggetto *elenco, int nElementi) {
_Bool isStillSorting = true;
while (isStillSorting) {
isStillSorting = false;
for (int i = 1; i < nElementi; i++) {
int comparatore = strcmp(elenco[i - 1].cognome, elenco[i].cognome);
if (comparatore > 0 ||
(comparatore == 0 && strcmp(elenco[i - 1].nome, elenco[i].nome) > 0)) {
// swap records that are out of order
RecordSoggetto tmp = elenco[i - 1];
elenco[i - 1] = elenco[i];
elenco[i] = tmp;
isStillSorting = true;
}
}
}
}
void ordinalarfabeticamente(RecordSoggetto*elenco,int nElementi){
_Bool-isStillSorting=true;
while(排序){
isStillSorting=false;
对于(int i=1;i0){
//交换发生故障的记录
RecordSoggetto tmp=elenco[i-1];
elenco[i-1]=elenco[i];
elenco[i]=tmp;
isStillSorting=true;
}
}
}
}
使用英语作为标识符确实更具可读性,更容易与外国人分享:
void sortAlphabetically(RecordSoggetto *elementList, int nElements) {
_Bool isStillSorting = true;
while (isStillSorting) {
isStillSorting = false;
for (int i = 1; i < nElements; i++) {
int cmp = strcmp(elementList[i - 1].cognome, elementList[i].cognome);
if (cmp > 0 ||
(cmp == 0 && strcmp(elementList[i - 1].nome, elementList[i].nome) > 0)) {
// swap records that are out of order
RecordSoggetto tmp = elementList[i - 1];
elementList[i - 1] = elementList[i];
elementList[i] = tmp;
isStillSorting = true;
}
}
}
}
按顺序作废(RecordSoggetto*元素列表,内部元素){
_Bool-isStillSorting=true;
while(排序){
isStillSorting=false;
对于(int i=1;i0||
(cmp==0&&strcmp(elementList[i-1].nome,elementList[i].nome)>0){
//交换发生故障的记录
RecordSoggetto tmp=元素列表[i-1];
元素列表[i-1]=元素列表[i];
元素列表[i]=tmp;
isStillSorting=true;
}
}
}
}
老前辈(比如我)会争辩说,局部变量的较短名称实际上增加了可读性。我让你来评判你自己的环境:
void sortAlphabetically(RecordSoggetto *array, int n) {
int done = 0;
while (!done) {
done = 1;
for (int i = 1; i < n; i++) {
int cmp = strcmp(array[i - 1].cognome, array[i].cognome);
if (cmp > 0 || (cmp == 0 && strcmp(array[i - 1].nome, array[i].nome) > 0)) {
// swap records that are out of order
RecordSoggetto tmp = array[i - 1];
array[i - 1] = array[i];
array[i] = tmp;
done = 0;
}
}
}
}
按顺序无效(RecordSoggetto*数组,int n){
int done=0;
而(!完成){
完成=1;
对于(int i=1;i0 | |(cmp==0&&strcmp(数组[i-1].nome,数组[i].nome)>0)){
//交换发生故障的记录
RecordSoggetto tmp=数组[i-1];
数组[i-1]=数组[i];
数组[i]=tmp;
完成=0;
}
}
}
}
camelcase和\u Bool
,我第一次看到这些。另外,我对您的语言不太流利,您是否可以用英语重新提交/注释代码,恕我直言,我只是无法跟踪变量,我也只知道英语是我的第二语言。在此之前,我建议使用调试器:)您确定文件存在并且路径正确吗?注意:1)如果这是C,则不需要强制转换malloc返回的void*
。2) 你能提供一个最小的可重复的例子吗?因为我不能完全阅读代码,也许我可以运行它来找出答案。3) printf()。4) 主观的,但我发现如果缩进是if\n{
而不是if{…
,则更容易遵循缩进,并且它使遵循代码更容易。此外,您似乎要执行VD database;
然后加载数据(&database…
。在此代码中,类型为VD
的变量database
不需要存在(如定义,但未分配任何内容)当然有一个垃圾值,所以传递一个指向它的指针可能不是一个好主意,不确定。顺便说一句,谢谢你的翻译。当然,不客气。Bufferedstdout
可能是个魔鬼,在调试时使用fprintf()中的stderr