我的代码将从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;
}