关于对输入字符串排序程序的问题
#include <stdio.h>#include <string.h>
typedef struct wrd{
char wrd[20];
struct wrd *next;
}WRD;
void main(){
char c;
int i=0,n=1,ct;
WRD *head,*ptrT,*ptrP,*mark;
printf("Please input the word(use space to divide)\n");
head = (WRD*)malloc(sizeof(WRD));
ptrP = (WRD*)malloc(sizeof(WRD));
head->next = ptrP;
while((c=getchar())!=EOF){
if(c==' '){
ptrP->next = (WRD*)malloc(sizeof(WRD));
if(ptrP == NULL){
printf("error!\n");
exit(1);
}
ptrP = ptrP->next;
n++;
i=0; /*Q */
}
else{
ptrP->wrd[i] = c;
i++;
ptrP->wrd[i] = '\0';
}
}
ct = n;
ptrT = head;
mark = head;
while(n--){
ptrP = mark;
ptrT = mark;
for(i=0; i<n+1; i++){
if(strcmp(ptrT->next->wrd,ptrP->next->wrd) > 0){
ptrT = ptrP;
}
ptrP = ptrP->next;
}
ptrP = ptrT->next;
ptrT->next = ptrP->next;
ptrP->next = mark->next;
mark->next = ptrP;
mark = ptrP;
}
ptrP = head;
for(i=0; i<ct; i++){
printf("%s\t",ptrP->next->wrd);
ptrP = ptrP->next;
}
}
求问哪里出错?VS可以完成排序但是输出结果有乱码,turboC运行完全不符合要求
乱码问题解决了、、红字那边输错了
但是还有一个问题,为神马Ctrl+Z要输两次才认?
[ 本帖最后由 ke3470 于 2011-5-19 17:58 编辑 ]