【问题标题】:C/C++ program crashes on bubblesortC/C++ 程序在冒泡排序上崩溃
【发布时间】:2013-11-06 21:59:12
【问题描述】:

我有一个具有不同数据类型的结构数组,我想根据结构的浮点数对数组进行排序,从最小到最大... 我的程序运行良好,但是当我对结构数组进行冒泡排序时,程序崩溃或不再响应......这是我的代码:

#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>

using namespace std;


struct Stud{
        long matrnr;
        char vorname[30];
        char name[30];
        char datum[30];
        float note;
    };



void sortieren(Stud mystud[], int studentCounter) {
Stud tmp;   
for (int i = 0 ; i< studentCounter; ++i) {
    for (int j=0; j< studentCounter-1;  ++j) {
        if (mystud[j].note > mystud[j+1].note) 
        {
                tmp = mystud[j+1];
                mystud[j+1] = mystud[j];
                mystud[j] = tmp;
            }           
        }
    }       
}           


int main(int argc, char **argv)
{   


    const int MAX = 30;
    Stud stud;  
    Stud mystud[30]; // <<-- Array of "Stud" type
    //memset((void*)mystud,0,sizeof(mystud) * sizeof(Stud));
    int wordCounter(0);
    int i(0); //thats the charCounter or index
    int studentCounter(0);
    char wort[MAX];
    //int matrnr;
    //char vorname[MAX];
    //char name[MAX];
    //char datum[MAX];
    //float note;
        ....
....
....
....

【问题讨论】:

  • 这是一个练习,还是您对最简单的数组排序方法感兴趣?
  • 您是否意识到您并没有移动整个学生记录,而只是复制了您的note 字段?
  • 你调试了吗?它在哪里崩溃?在调试器中运行并检查ijstudentCounter 的值,当然还有数组的元素。请注意,您没有交换数组的元素,您只是交换了 mystud 数组的两个元素的 note 值。
  • 这是一个练习,但是是的,我对对数组进行排序的最简单方法感兴趣,... @StevenBurnap 哎呀,我现在看到了,哈哈...太愚蠢了..编辑:我现在在代码中更新了它,但它仍然崩溃......
  • 您需要使用strncpy 或类似的东西。您的崩溃本身很可能是由确定studentCounter 的值的方式和/或为mystud 分配内存的方式引起的。但这都是代码审查的东西,并不适合这个网站。

标签: c++ c arrays struct


【解决方案1】:

这就是它的工作原理:

void sortieren(Stud mystud[], int studentCounter) {
Stud tmp;   
for (int i = 0 ; i< studentCounter; ++i) {
    for (int j=0; j< studentCounter-1;  ++j) {
        if (mystud[j].note > mystud[j+1].note) 
        {
                tmp.matrnr = mystud[j+1].matrnr;
                strcpy(tmp.vorname,mystud[j+1].vorname);
                strcpy(tmp.name,mystud[j+1].name);
                strcpy(tmp.datum , mystud[j+1].datum);
                tmp.note = mystud[j+1].note;

                mystud[j+1].matrnr = mystud[j].matrnr;
                strcpy(mystud[j+1].vorname ,mystud[j].vorname);                 
                strcpy(mystud[j+1].name , mystud[j].name);
                strcpy(mystud[j+1].datum ,mystud[j].datum);
                mystud[j+1].note = mystud[j].note;

                mystud[j].matrnr = tmp.matrnr;
                strcpy(mystud[j].vorname , tmp.vorname);
                strcpy(mystud[j].name , tmp.name);
                strcpy(mystud[j].datum , tmp.datum);
                mystud[j].note = tmp.note;                  

            }           
        }
    }       
}
..
...
...
...

sortieren(mystud , studentCounter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2014-03-26
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多