鱼C论坛

 找回密码
 立即注册
查看: 1733|回复: 7

[已解决]C语言S1E23动动手2,代码清单无法真正实现转置

[复制链接]
发表于 2019-8-18 11:39:56 | 显示全部楼层 |阅读模式

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

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

x
小甲鱼的代码是只符合特例的,它默认字符串的长度必须是递减的,这样它一旦发现一个将要越界就直接break出去(因为后面的也肯定不用了);作为验证,我调换了样例代码中FishC和Five的位置,竖着打印时FishC只打印了Fish,因为前面的Five已经把这行break出去了。
想问一下有人能实现真正的转置打印吗?
最佳答案
2019-8-18 14:02:22
这个代码可以吗?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>

  5. size_t search(const uint8_t *data, uint8_t n, size_t size)
  6. {
  7.         size_t res = 0;
  8.         for(size_t i = 0; i < size; ++i)
  9.         {
  10.                 if(data[i] == n)
  11.                         ++res;
  12.         }
  13.         return res;
  14. }

  15. void print(const char *string[], size_t size)
  16. {
  17.         uint8_t *flag = malloc(size);
  18.         memset(flag, 0, size);
  19.         for(size_t i = 0; ;++i)
  20.         {
  21.                 for(size_t j = 0; j < size; ++j)
  22.                 {
  23.                         if(!flag[j] && !string[j][i])
  24.                                 flag[j] = 1;
  25.                 }
  26.                 if(!search(flag, 0, size))
  27.                         break;

  28.                 for(size_t j = 0; j < size; ++j)
  29.                         printf("%c ", flag[j] ? ' ' : string[j][i]);
  30.                 printf("\n");
  31.         }
  32.         free(flag);
  33. }

  34. int main(void)
  35. {
  36.         const char *string[] = {
  37.                 "homework",
  38.                 "fishc",
  39.                 "localhost",
  40.                 "workspace",
  41.                 "accessed",
  42.                 "description"
  43.         };
  44.         const size_t size = sizeof(string) / sizeof(string[0]);
  45.         print(string, size);
  46.         return 0;
  47. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-18 12:05:24 | 显示全部楼层
贴下代码吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-18 12:19:05 | 显示全部楼层
你闭眼发问题,那我也张眼说瞎话,不能,没有
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-18 14:01:22 | 显示全部楼层
要学会提问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-18 14:02:22 | 显示全部楼层    本楼为最佳答案   
这个代码可以吗?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>

  5. size_t search(const uint8_t *data, uint8_t n, size_t size)
  6. {
  7.         size_t res = 0;
  8.         for(size_t i = 0; i < size; ++i)
  9.         {
  10.                 if(data[i] == n)
  11.                         ++res;
  12.         }
  13.         return res;
  14. }

  15. void print(const char *string[], size_t size)
  16. {
  17.         uint8_t *flag = malloc(size);
  18.         memset(flag, 0, size);
  19.         for(size_t i = 0; ;++i)
  20.         {
  21.                 for(size_t j = 0; j < size; ++j)
  22.                 {
  23.                         if(!flag[j] && !string[j][i])
  24.                                 flag[j] = 1;
  25.                 }
  26.                 if(!search(flag, 0, size))
  27.                         break;

  28.                 for(size_t j = 0; j < size; ++j)
  29.                         printf("%c ", flag[j] ? ' ' : string[j][i]);
  30.                 printf("\n");
  31.         }
  32.         free(flag);
  33. }

  34. int main(void)
  35. {
  36.         const char *string[] = {
  37.                 "homework",
  38.                 "fishc",
  39.                 "localhost",
  40.                 "workspace",
  41.                 "accessed",
  42.                 "description"
  43.         };
  44.         const size_t size = sizeof(string) / sizeof(string[0]);
  45.         print(string, size);
  46.         return 0;
  47. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-18 15:16:20 | 显示全部楼层

因为我问的不是自己写的代码,所以想当然的就没有贴代码,确实是我没有描述清楚问题给回答问题的大家造成了不便。您的代码对现在的我还有点复杂,不过谢谢您的回复!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-18 15:18:34 | 显示全部楼层
bin554385863 发表于 2019-8-18 12:19
你闭眼发问题,那我也张眼说瞎话,不能,没有

确实是我脑残了,因为我问的是小甲鱼的代码,不是自己写的,所以没想着把代码和本来的问题写出来。以后我会把问题描述得完整的,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-18 23:40:05 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 18:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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