我的代码将从input.txt中获取学生信息.将它们保存在一个链接列表中,然后按出生年份排序,然后打印.当重复运行时,它打印无意义的数字,每次重复运行时,输出都会改变.但是当调试代码时,输出将是正确的.

当ı运行我的代码时,输出如下: 按生日升序排列的学生姓名: -2013265920000 M-2013265920-2013265920-2013265920-2013265920 阿里·佩赫利文;2005 -2013265920-2013265920-20132659200

但是我调试我的代码和输出: 按生日升序排列的学生姓名: 阿里·佩赫利文;2005 塞拉米·基里奇;2002 ESMA Sultan;2001 穆罕默德·阿里·萨西尔;2000

我的输入文件‘input.txt’为: 040160811;阿里·佩利文;2005年 040180224;穆罕默德·阿里·萨西尔;2000年 820190040;ESMA Sultan;2001年 150190207;塞拉米·基里奇;2002年

这似乎是关于我的'sortAndPrintByBirth'功能.但他找不到任何错误. 我认为这是一个记忆的东西.

#include <stdio.h>
#include <stdlib.h>



struct n {
    char number[9];
    char name;
    int year;
    struct n* next;
};
typedef struct n node;


struct s_node {
    char* number;
    struct s_node *next;
}*stack = NULL;





int isLetter(char character);
int isNumber(char character);
int charToInt(char character);
node** sortAndPrintByBirth(node* *list);
node** sortAndPrintByFaculty(node* *list);
int length(node** list);
void push(char* value);
char* pop();
void display();

        int main() {

    node* Student;
    Student = (node *) malloc(sizeof(node));
    node* head1;
    node* head;
    head = Student;
    head1 = head;
    node* Students[100];

    FILE* file;
    file = fopen("C:\\Users\\PC\\CLionProjects\\untitled1\\input.txt", "r");
    char fchar;
    fchar = fgetc(file);
    int switchNY = 0; // 0 for school number, 1 for birth year
    int studentCounter = 0;
    while(fchar != EOF){               /// Dosyadan veri alma
        if(isLetter(fchar)){
            head1->name = fchar;
            head1->year = (int) NULL;
            head1->next = malloc(sizeof(node*));
            head1 = head1->next;
            fchar = fgetc(file);
        }else if(isNumber(fchar)){
            if(switchNY == 0){
                for(int i = 0; i < 9; i++){
                    head1->number[i] = fchar;
                    head1->year =(int) NULL;
                    head1->number[i+1] = (char) NULL;
                    fchar = fgetc(file);
                }

            }else {
                for (int i = 0; i < 4; i++) {
                    head1->year = charToInt(fchar);
                    head1->next = malloc(sizeof(node *));
                    head1 = head1->next;
                    fchar = fgetc(file);
                }
                if (fchar == EOF) {
                    Students[studentCounter] = head;         ///en son yapmaya çalıştığın şey her \n karakterinde
                    Students[studentCounter + 1] = NULL;     ///yeni student tanımlayıp onun adresini arrayde tutmak
                    fchar = fgetc(file);
                    Student = (node *) malloc(sizeof(node));
                    head = Student;
                    head1 = head;
                    studentCounter++;
                }
                switchNY = 0;
            }

        }else if(fchar == '\n'){                         /// kod ; lerde buraya giriyo, onu düzelt
            Students[studentCounter] = head;             ///en son yapmaya çalıştığın şey her \n karakterinde
            Students[studentCounter+1] = NULL;           ///yeni student tanımlayıp onun adresini arrayde tutmak
            fchar = fgetc(file);
            Student = (node *)  malloc(sizeof(node));
            head = Student;
            head1 = head;
            studentCounter++;
        }else if(fchar == ';'){
            head1->year = (int) NULL;
            if(switchNY == 1){
                head1->name = fchar;
            }
            switchNY = 1;
            fchar = fgetc(file);
            head1->next = malloc(sizeof(node*));
            head1 = head1->next;
        }
    }
    fclose(file);


    sortAndPrintByBirth(Students);
    //sortAndPrintByFaculty(Students);


    return -1;
}


int isLetter(char character) {
    return ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character == ' '));
}
int isNumber(char character) {
    return (character >= '0' && character <= '9');
}
int charToInt(char character) {
    if (character >= '0' && character <= '9') {
        return character - '0';
    } else {
        return -1; // Eğer karakter bir sayı değilse -1 döndür
    }
}


node** sortAndPrintByBirth(node* *list) {
    int i = length(list);
    node* result[i];

    for(int a=0; a<i; a++){
        result[a] = list[a]->number;
    }



    for(int count = 0; count < i-1; count++){
        for(int step = 0; step < i-1 ; step++){
            node *iter1;
            node *iter2;
            iter1 = result[step];
            iter2 = result[step+1];
            int year1 = 0,year2 = 0;
            while (iter2 != NULL){
                while ((void *) iter1->next->year == NULL) { ///void* eklendi, derleyicinin isteği
                    iter1 = iter1->next;
                }
                while ((void *) iter2->next->year == NULL) { ///void* eklendi, derleyicinin isteği
                    iter2 = iter2->next;
                }
                for (int j = 0; j < 4; ++j) {
                    iter1 = iter1->next;
                    year1 = 10 * year1 + iter1->year;
                }

                for (int j = 0; j < 4; ++j) {
                    iter2 = iter2->next;
                    year2 = 10 * year2 + iter2->year;
                }
                break;
            }
            if(year1 < year2){
                node* temp;
                temp = result[step];
                result[step] = result[step+1];
                result[step+1] = temp;
            }else if(year2 == year1){
                continue;
            }
        }
    }
    node* iter3;
    printf("Student names in ascending order by birthday:\n");
    for (int j = 0; j < i; ++j) {
        iter3 = result[j]->next;
        while(iter3->year == (int )NULL){
            printf("%c",iter3->name);
            iter3 = iter3->next;
        }
        for (int k = 0; k < 4; ++k) {
            printf("%d",iter3->year);
            iter3 = iter3->next;
        }
        printf("\n");
    }
    return result;
}


node** sortAndPrintByFaculty(node* *list){
    int i = length(list);
    node* result[i];
    char* temp[i];

    for(int a=0; a<i; a++){
        temp[a] = list[a]->number;
    }

    /// temp  = "040160811","040180224","820190040","150190207"

    for(int count = 0; count < i-1; count++){
        for(int step = 0; step < i-1 ; step++){
            for(int digit = 0; digit < 3 ; digit ++){
                if(charToInt(temp[step][digit]) < charToInt(temp[step+1][digit])){
                    char* c;
                    c = temp[step];
                    temp[step] = temp[step+1];
                    temp[step+1] = c;
                    break;
                }else if(charToInt(temp[step][digit]) == charToInt(temp[step+1][digit])){
                    continue;
                }else{
                    break;
                }
            }
        }
    }

    for (int j = 0; j < i; ++j) {
        push(temp[j]);              ///Değerler büyükten küçüğe stack'e push'landı.
    }
    //display();
    printf("School numbers by the faculty codes in ascending order:\n");
    for (int j = 0; j < i; ++j) {
        printf("%s\n", pop());
    }

    return result;
}

int length(node** list){
    int value = 0;
    while(list[value] != NULL){
        value++;
    }
    return value;
}

void push(char* value)  {
    struct s_node *m;
    m=(struct s_node*)malloc(sizeof(struct s_node ));
    m->number= value ;
    m->next=stack;
    stack=m;
}
void display()  {
    struct s_node *temp=stack;
    while(temp!=NULL)   {
        printf("%s\t", temp->number);
        temp=temp->next;
    }
}

char* pop()  {
    struct s_node *temp ;
    if (stack == NULL) {
        printf("\nSTACK is Empty.");
    } else {
        char* i = stack->number;

        temp = stack;
        stack = stack->next;
        free(temp);  // sadece temp'i serbest bırak, number için ayrı bir free yapmaya gerek yok
        return i;

    }
    return NULL;
}




推荐答案

至少存在以下问题:

Insufficient allocation

成员.next是指向类型struct n AKA node的指针.分配应该基于引用数据的大小,而不是指针的大小.

// head1->next = malloc(sizeof(node*));
head1->next = malloc(sizeof head1->next[0]);

Save time, compiler with all warnings enabled

@Some programmer dude

C++相关问答推荐

拥有3x3二维数组并访问数组[1][3]等同于数组[2][0]?

在C23中使用_GENERIC实现带有右值的IS_POINTER(P)?

在C++中使用函数指针的正确语法

C在声明带有值的数组时,声明大小有用吗?

我怎么才能用GCC编译一个c库,让它包含另一个库呢?

是否需要包括<;errno.h>;才能使用perror?

一旦运行长度超过2,编译器是否会优化";strnlen(mystring,32)>;2";以停止循环?

为什么函数是按照定义的顺序执行的,而不是按照从avr-c中的int main()调用的顺序执行的?

C标准关于外部常量的说明

用C++高效解析HTTP请求的方法

我可以使用Windows SDK';s IN6_IS_ADDR_LOOPBACK等,尽管没有文档?

分支预测和UB(未定义的行为)

WSASocket在哪里定义?

函数指针作为函数参数 - 应该使用 const 吗?

如何找出C中分配在堆上的数组的大小?

malloc:损坏的顶部大小无法找出问题

int 与 size_t 与 long

将帧从相机 (/dev/video0) 复制到帧缓冲区 (/dev/fb0) 会产生意外结果

O_DIRECT为何比普通读取慢?

关于 OpenMP 并行 SIMD 缩减