我寫了下面的代碼:當我運行我的C程序時,爲什麼沒有發生?
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int month;
int day;
int hour;
int minutes;
}primaries_date;
typedef struct
{
int all_members;
char *country;
primaries_date date;
}usa_primaries;
typedef struct node *ptr;
typedef struct node
{
usa_primaries up;
ptr next;
}Node;
void add(ptr *hptr, int members, char *con, int month, int day, int hour, int minutes)
{
ptr p = NULL;
ptr q = NULL;
ptr t;
t = malloc(sizeof(Node));
if(!t)
{
printf("Cannot build list");
exit(0);
}
t->up.all_members = members;
t->up.country = con;
t->up.date.month = month;
t->up.date.day = day;
t->up.date.hour = hour;
while((p))
{
if(p->up.date.month >= month || p->up.date.day >= day || p->up.date.hour >= hour || p->up.date.minutes >= minutes)
{
q = p;
p = p->next;
}
}
if(p == *hptr)
{
*hptr = t; /*Resetting head. Assigning to the head t*/
t->next = p;
}
else
{
q->next = t;
t->next = p;
}
}
void remove_dates(ptr *hptr, primaries_date date1 , primaries_date date2)
{
ptr p1 = *hptr;
ptr p2 = p1;
while((p1) && !((p1->up.date.month == date1.month) && (p1->up.date.day == date1.day) && (p1->up.date.hour == date1.hour) &&
(p1->up.date.minutes==date1.minutes)))
p1 = p1->next;
p2 = p1;
while((p2) && !((p2->up.date.month == date2.month) && (p2->up.date.day == date2.day) && (p2->up.date.hour == date2.hour) &&
(p2->up.date.minutes==date2.minutes)))
p1 = p1->next;
p1->next = p2;
}
void printlist(ptr h)
{
while(h)
{
printf("\n");
printf("%d %d %d %d %d %s\n", h->up.date.day, h->up.date.month, h->up.date.hour, h->up.date.minutes, h->up.all_members, h->up.country);
h = h->next;
}
}
void freelist(ptr *hptr)
{
ptr p;
while(*hptr)
{
p = *hptr;
*hptr = (*hptr)->next;
free(p);
}
}
int main()
{
ptr h = NULL;
int month, day, hour, minutes;
int all_members; /*Declaration of all_members*/
char country[256];
char member;
while(scanf("%d %d %d %d %c %s",&day,&month,&hour,&minutes,&member,country) == 1)
{
if(member == 'Y')
all_members = 1;
else
all_members = 0;
add(&h,all_members,country,month,day,hour,minutes);
printlist(h);
}
freelist(&h);
return 0;
}
有沒有編譯錯誤。但是,當我運行該程序時,什麼都沒有發生。 我認爲這可能是由於scanf函數引起的,因爲scanf在掃描字符(回車鍵)時存儲'\ n'字符,但我不確定。
爲什麼當我運行我的程序時什麼都沒有發生,我該如何讓它正常運行?
在此先感謝!
您介意創建[___MCVE___](http://stackoverflow.com/help/mcve)嗎? –
還有其他(與你的*當前*問題無關)問題:想一想'usa_primaries'結構中的'country'指針。你指的是什麼?你所做的'國家'的記憶會不會改變? –
我想我把它指向了一個字符串,我不認爲我的國家指向的記憶曾經改變:) @Some程序員哥們 – Tree