Shimi 发表于 2020-3-14 15:58:54

C语言作业,急求

从键盘上输入5个字符串,对其进行升序排序并输出,不能使用strcmp函数,求大佬帮忙看看,这到底哪错了,排序结果就是不正确
#include<stdio.h>
#include<string.h>
#define MAX_LINE 5
#define MAX_LINE_LEN 81

void sortP_Str(char *pstr[]);
int Find(char a[],char b[]);
int main(void)      
{
int i;
char *pstr, str;
for (i=0; i<MAX_LINE; i++)
{
pstr = str;
}
printf("Input 5 strings:\n");
for (i=0; i<MAX_LINE; i++)
{
gets(pstr);
}
sortP_Str(pstr);
printf("---------------------------\n");
for (i=0; i<MAX_LINE; i++)
{
printf("%s\n", pstr);
}
return 0;
}

void sortP_Str(char *pstr[])
{
int i, j;
char *p;
for (i=0; i<3; i++)
{
for (j=i; j<=3; j++)
{
   
   if (Find(pstr,pstr))
   {
    p = *(pstr+i);
    *(pstr+i) = *(pstr+j);
    *(pstr+j) = p;
   }
}
}
}
int Find(char a[],char b[])
{
int i = 0, j = 0;
int ret = 0;
while ((i<strlen(a))||(j<strlen(b)))
{
if (a> b)
{
   ret = a - b;
}
else if (a< b)
{
   ret = a - b;
}
else;
i++, j++;
}
return ret;
}
示例是这样的:
Input 5 strings:
hello
My
Friend
are you ready?
help me!
---------------------------
Friend
My
are you ready?
hello
help me!

墨羽岚 发表于 2020-3-14 16:02:28

你把代码用论坛要求的格式再发一遍吧,斜着看很累的{:10_260:}

Shimi 发表于 2020-3-14 16:07:04

int main(void)      
{
        int i;
        char *pstr, str;

for (i=0; i<MAX_LINE; i++)
        {
                pstr = str;
        }
        printf("Input 5 strings:\n");
        for (i=0; i<MAX_LINE; i++)
        {
                gets(pstr);
        }
        sortP_Str(pstr);
        printf("---------------------------\n");
        for (i=0; i<MAX_LINE; i++)
        {
                printf("%s\n", pstr);
        }
        return 0;
}
void sortP_Str(char *pstr[])
{
        int i, j;
        char *p;
        for (i=0; i<3; i++)
        {
                for (j=i; j<=3; j++)
                {
                       
                        if (Find(pstr,pstr))
                        {
                                p = *(pstr+i);
                                *(pstr+i) = *(pstr+j);
                                *(pstr+j) = p;
                        }
                }
        }
}
int Find(char a[],char b[])
{
        int i = 0, j = 0;
        int ret = 0;
        while ((i<strlen(a))||(j<strlen(b)))
        {
                if (a> b)
                {
                        ret = a - b;
                }
                else if (a< b)
                {
                        ret = a - b;
                }
                else;
                i++, j++;
        }
        return ret;
}

墨羽岚 发表于 2020-3-14 16:33:31

你最后那个while里面a>b和a<b所执行的是同一个表达式{:10_260:}是不是这里出了问题

jackz007 发表于 2020-3-14 16:37:53

本帖最后由 jackz007 于 2020-3-14 16:41 编辑

#include<stdio.h>

#define MAX_LINE 5
#define MAX_LINE_LEN 81

compare(char * s1 , char * s2)
{
      int k , r                                                                         ;
      for(k = 0 , r = s1 - s2 ; s1 && s2 && ! r ; k ++ , r = s1 - s2) ;
      return r                                                                        ;
}

void sortstr(char str[])
{
      char s                                                                                    ;
      int d , i , j , k                                                                                       ;
      for(i = i ; i < MAX_LINE ; i ++) {
                for(j = i ; j > 0 && compare(str , str) > 0 ; j --) {
                        for(d = 0 , s = str ; s ; d ++ , s = str)                  ;
                        for(d = 0 , str = str ; str ; d ++ , str = str) ;
                        for(d = 0 , str = s ; str ; d ++ , str = s)                     ;
                }
      }
}

main(void)
{
      char c , s                                                                      ;
      int i , j , k                                                                                           ;
      for(i = 0 ; i < MAX_LINE ; i ++) for(j = 0 ; (c = getchar()) != '\n' ; s = c , s = '\0') ;
      sortstr(s)                                                                                              ;
      printf("\n****************************************\n\n")                                                ;
      for(i = 0 ; i < MAX_LINE ; i ++) printf("%s\n" , s)                                                ;
}
      编译、运行实况:
C:\Bin>g++ -o ss ss.c

C:\Bin>ss
hello
My
Friend
are you ready?
help me!

****************************************

Friend
My
are you ready?
hello
help me!

C:\Bin>
页: [1]
查看完整版本: C语言作业,急求