鱼C论坛

 找回密码
 立即注册
查看: 2046|回复: 4

[已解决]C语言作业,急求

[复制链接]
发表于 2020-3-14 15:58:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
从键盘上输入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[MAX_LINE], str[MAX_LINE][MAX_LINE_LEN];
for (i=0; i<MAX_LINE; i++)
{
  pstr[i] = str[i];
}
printf("Input 5 strings:\n");
for (i=0; i<MAX_LINE; i++)
{
  gets(pstr[i]);
}
sortP_Str(pstr);
printf("---------------------------\n");
for (i=0; i<MAX_LINE; i++)
{
  printf("%s\n", pstr[i]);
}
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[i],pstr[j]))
   {
    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[i]> b[j])
  {
   ret = a[i] - b[j];
  }
  else if (a[i]< b[j])
  {
   ret = a[i] - b[j];
  }
  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:37:53
本帖最后由 jackz007 于 2020-3-14 16:41 编辑
  1. #include<stdio.h>

  2. #define MAX_LINE 5
  3. #define MAX_LINE_LEN 81

  4. compare(char * s1 , char * s2)
  5. {
  6.         int k , r                                                                         ;
  7.         for(k = 0 , r = s1[k] - s2[k] ; s1[k] && s2[k] && ! r ; k ++ , r = s1[k] - s2[k]) ;
  8.         return r                                                                          ;
  9. }

  10. void sortstr(char str[][MAX_LINE_LEN])
  11. {
  12.         char s[MAX_LINE_LEN]                                                                                      ;
  13.         int d , i , j , k                                                                                         ;
  14.         for(i = i ; i < MAX_LINE ; i ++) {
  15.                 for(j = i ; j > 0 && compare(str[j - 1] , str[j]) > 0 ; j --) {
  16.                         for(d = 0 , s[d] = str[j - 1][d] ; s[d] ; d ++ , s[d] = str[j - 1][d])                    ;
  17.                         for(d = 0 , str[j - 1][d] = str[j][d] ; str[j - 1][d] ; d ++ , str[j - 1][d] = str[j][d]) ;
  18.                         for(d = 0 , str[j][d] = s[d] ; str[j][d] ; d ++ , str[j][d] = s[d])                       ;
  19.                 }
  20.         }
  21. }

  22. main(void)
  23. {
  24.         char c , s[MAX_LINE][MAX_LINE_LEN]                                                                      ;
  25.         int i , j , k                                                                                           ;
  26.         for(i = 0 ; i < MAX_LINE ; i ++) for(j = 0 ; (c = getchar()) != '\n' ; s[i][j ++] = c , s[i][j] = '\0') ;
  27.         sortstr(s)                                                                                              ;
  28.         printf("\n****************************************\n\n")                                                ;
  29.         for(i = 0 ; i < MAX_LINE ; i ++) printf("%s\n" , s[i])                                                  ;
  30. }
复制代码

        编译、运行实况:
  1. C:\Bin>g++ -o ss ss.c

  2. C:\Bin>ss
  3. hello
  4. My
  5. Friend
  6. are you ready?
  7. help me!

  8. ****************************************

  9. Friend
  10. My
  11. are you ready?
  12. hello
  13. help me!

  14. C:\Bin>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-14 16:02:28 | 显示全部楼层
你把代码用论坛要求的格式再发一遍吧,斜着看很累的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-14 16:07:04 | 显示全部楼层
  1. int main(void)        
  2. {
  3.         int i;
  4.         char *pstr[MAX_LINE], str[MAX_LINE][MAX_LINE_LEN];

  5. [p=30, 2, left]for (i=0; i<MAX_LINE; i++)
  6.         {
  7.                 pstr[i] = str[i];
  8.         }[/p]
  9. [p=30, 2, left]        printf("Input 5 strings:\n");
  10.         for (i=0; i<MAX_LINE; i++)
  11.         {
  12.                 gets(pstr[i]);
  13.         }[/p]
  14. [p=30, 2, left]        sortP_Str(pstr);
  15.         printf("---------------------------\n");
  16.         for (i=0; i<MAX_LINE; i++)
  17.         {
  18.                 printf("%s\n", pstr[i]);
  19.         }[/p]
  20. [p=30, 2, left]        return 0;
  21. }
  22. void sortP_Str(char *pstr[])
  23. {
  24.         int i, j;
  25.         char *p;[/p]
  26. [p=30, 2, left]        for (i=0; i<3; i++)
  27.         {
  28.                 for (j=i; j<=3; j++)
  29.                 {
  30.                        
  31.                         if (Find(pstr[i],pstr[j]))
  32.                         {
  33.                                 p = *(pstr+i);
  34.                                 *(pstr+i) = *(pstr+j);
  35.                                 *(pstr+j) = p;
  36.                         }
  37.                 }
  38.         }
  39. }
  40. int Find(char a[],char b[])
  41. {
  42.         int i = 0, j = 0;
  43.         int ret = 0;
  44.         while ((i<strlen(a))||(j<strlen(b)))
  45.         {
  46.                 if (a[i]> b[j])
  47.                 {
  48.                         ret = a[i] - b[j];
  49.                 }
  50.                 else if (a[i]< b[j])
  51.                 {
  52.                         ret = a[i] - b[j];
  53.                 }
  54.                 else;
  55.                 i++, j++;
  56.         }
  57.         return ret;
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 16:33:31 | 显示全部楼层
你最后那个while里面a[i]>b[j]和a[i]<b[j]所执行的是同一个表达式是不是这里出了问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 16:37:53 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-3-14 16:41 编辑
  1. #include<stdio.h>

  2. #define MAX_LINE 5
  3. #define MAX_LINE_LEN 81

  4. compare(char * s1 , char * s2)
  5. {
  6.         int k , r                                                                         ;
  7.         for(k = 0 , r = s1[k] - s2[k] ; s1[k] && s2[k] && ! r ; k ++ , r = s1[k] - s2[k]) ;
  8.         return r                                                                          ;
  9. }

  10. void sortstr(char str[][MAX_LINE_LEN])
  11. {
  12.         char s[MAX_LINE_LEN]                                                                                      ;
  13.         int d , i , j , k                                                                                         ;
  14.         for(i = i ; i < MAX_LINE ; i ++) {
  15.                 for(j = i ; j > 0 && compare(str[j - 1] , str[j]) > 0 ; j --) {
  16.                         for(d = 0 , s[d] = str[j - 1][d] ; s[d] ; d ++ , s[d] = str[j - 1][d])                    ;
  17.                         for(d = 0 , str[j - 1][d] = str[j][d] ; str[j - 1][d] ; d ++ , str[j - 1][d] = str[j][d]) ;
  18.                         for(d = 0 , str[j][d] = s[d] ; str[j][d] ; d ++ , str[j][d] = s[d])                       ;
  19.                 }
  20.         }
  21. }

  22. main(void)
  23. {
  24.         char c , s[MAX_LINE][MAX_LINE_LEN]                                                                      ;
  25.         int i , j , k                                                                                           ;
  26.         for(i = 0 ; i < MAX_LINE ; i ++) for(j = 0 ; (c = getchar()) != '\n' ; s[i][j ++] = c , s[i][j] = '\0') ;
  27.         sortstr(s)                                                                                              ;
  28.         printf("\n****************************************\n\n")                                                ;
  29.         for(i = 0 ; i < MAX_LINE ; i ++) printf("%s\n" , s[i])                                                  ;
  30. }
复制代码

        编译、运行实况:
  1. C:\Bin>g++ -o ss ss.c

  2. C:\Bin>ss
  3. hello
  4. My
  5. Friend
  6. are you ready?
  7. help me!

  8. ****************************************

  9. Friend
  10. My
  11. are you ready?
  12. hello
  13. help me!

  14. C:\Bin>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-27 21:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表