我的代码将从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++相关问答推荐

海湾合作委员会是否保证大小匹配的访问?

C strlen on char array

你能用自己的地址声明一个C指针吗?

如何避免重新分配指针数组时,我们从一开始就不知道确切的大小

为什么这个C程序代码会产生以下结果?

什么C代码将确定打开的套接字正在使用的网络适配器?

在C语言中使用scanf()时我无法理解的警告

当打印字符串时,为什么在c中没有使用常量限定符时我会收到警告?

C中的指针增量和减量(*--*++p)

Setenv在c编程中的用法?

Flose()在Docker容器中抛出段错误

GDB输出ARM助记符

什么是.c.h文件?

C:Assignment中的链表赋值从指针目标类型中丢弃‘const’限定符

为什么会导致分段故障?(C语言中的一个程序,统计文件中某个单词的出现次数)

将某些内容添加到链接列表时,列表中的其他项将使用最后添加的项的名称

如何在C中计算包含递增和递减运算符的逻辑表达式?

在C中交换字符串和数组的通用交换函数

C 错误:对 int 数组使用 typedef 时出现不兼容的指针类型问题

在链表中插入一个值