作者liu2007 (薯)
看板C_and_CPP
标题[问题] 请乡民救救我的指标....
时间Thu Sep 10 12:34:37 2009
我想要写一个程式
这个程式的动作是
void main()
{
FirstNode();
ReadCheater();
SortCheater();
//WriteCheater();
}
这是节点结构
struct Node
{
char Str[50];
unsigned int Length;
struct Node *next;
} *First, *Point, *SortedFirst;
函式内容:
建立一个新的节点
struct Node* NewNode(
void)
{
struct Node *New;
New = (
struct Node* )malloc(
sizeof(
struct Node));
New->next = NULL;
return New;
}
先建立两个空节点
一个是为排序的起始节点
一个是以排序的起始节点
void FirstNode(
void)
{
First = NewNode();
SortedFirst = NewNode();
Point = First;
}
读txt档里面的资料
每读一笔资料(字串)
就make一个新的节点出来储存
void ReadCheater(
void)
{
FILE *fSource = fopen("
Path\\Cheater.txt","r");
struct Node *temp;
char Str[100];
do{
temp = NewNode();
fscanf(fSource, "%s\n", &Str);
if ((strcmp(Str, " ")==0)||(strcmp(Str, " ")==0)||
(strcmp(Str, " "))==0)
{
fscanf(fSource, "%s", &Str);
}
strcpy( temp->Str, Str);
temp->Length = strlen(Str);
Point->next = temp;
Point = Point->next;
}
while (!feof(fSource));
}
其中do while 里面的if 功能是在於:
如果读到程式认为是字串,但其实不是(换行、非中文或英文的"乱码")
就再读下一个字串。
这段我测试过了没什麽问题
问题在於下一个函式
之後作长度的排序(不作内容的排序)
void SortCheater(
void)
{
struct Node *temp;
unsigned int Length = 0;
FILE *fOutput = fopen("
Path\\Sorted.txt","w");
temp = SortedFirst;
Point = First;
while (First->next == NULL){
Length++;
while (Point->next != NULL){
if ( Point->next->Length == Length){
temp->next = Point->next;
Point->next = Point->next->next;
temp = temp->next;
}
else
Point = Point->next;
}
Point = First;
}
temp = SortedFirst;
do
{
fprintf(fOutput, "%s\n", temp->Str);
temp = temp->next;
}
while (temp != NULL);
fclose(fOutput);
}
这里
我看了印出来的结果
是空字串(不像中文字的中文字,而且只有一行)
代表temp似乎没有指向SortedFirst这个空节点.....
为什麽会这样呢?
这里是我的原始资料:
http://www.badongo.com/file/17096488
这是原始资料的来源:
http://tw.csonline.gamania.com/event/20090901/090908_2.htm
因为我还不会读网路
所以先将就复制贴上在笔记本里
请乡民救救我的指标.....
除虫除了三天了.....
感激 <(_._)>
--
圣露西亚捐款10万美金
该国人口数仅17万
→ silverache:换算过来 平均每个国民捐给我们20几万台币~~啾感心
推
LIONDODO:哇,一个人捐20几万…
推
m7cord:好感动,一个人二十万耶。Q_Q
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.232.32.116
※ 编辑: liu2007 来自: 118.232.32.116 (09/10 12:37)
1F:推 holymars:while (First->next == NULL) 09/10 12:57
2F:→ holymars:这个while loop根本不会进去吧 09/10 12:58
\(" ̄口 ̄)/ 改成!=就可以了~ 我又耍笨了Orz.....感谢圣大!!
下面的图就不用看了XDDDDDDD (难得画图)
******************************************************************************
经过ReadCheater()函式後
First Point
↓ ↓
◎→█→█→█→█→█→█→█→█→█→█→█→NULL
******************************************************************************
SortCheater中的while开始前:
First
↓
◎→█→█→█→█→█→█→█→█→█→█→█→NULL
↑
Point
SortedFirst
↓
◎→NULL
↑
temp
******************************************************************************
※ 编辑: liu2007 来自: 118.232.32.116 (09/10 13:15)
3F:推 holymars:对啊 First->next不是NULL 那while loop怎麽会跑呢 09/10 13:12
4F:推 Peruheru:While的()内条件「成立」才会「进入」,你搞颠倒了 09/10 13:13
5F:→ holymars:你用什麽IDE啊 学一下基本的debugger怎麽用吧XD 09/10 13:13
我是用VC++ 6.0
debugger是下面视窗的讯息吗?
6F:→ holymars:为这种bug花三天真是不值得XDDD 09/10 13:13
7F:→ Peruheru:要进入的条件要反过来写,=Null时离开就写成!=Null 09/10 13:14
※ 编辑: liu2007 来自: 118.232.32.116 (09/10 13:20)
8F:推 VictorTom:debugger, 就是环境给你的debug用的工具泛称吧.... 09/10 13:47
9F:→ VictorTom:比如用step by step一行一行trace, 搭配watch看变数, 09/10 13:47
10F:→ VictorTom:加入code break或data break point等等.... 09/10 13:47