鱼C论坛

 找回密码
 立即注册
查看: 1602|回复: 3

[已解决]求助!

[复制链接]
发表于 2022-7-3 18:06:38 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>

  2. #define MAX 1024

  3. int main()
  4. {
  5.         char str1[MAX],str2[MAX];
  6.         char *target1 = str1,*target2=str2;
  7.         char cum;
  8.         int ch;
  9.        
  10.         printf("请输入一个字符串到str1:");
  11.         fgets(str1, MAX, stdin);
  12.        
  13.         printf("请输入需要拷贝的字符数量:");
  14.         scanf("%d",&ch);
  15.        
  16.         printf("开始拷贝 str1 的内容到 str2 中...\n");
  17.        
  18.         while(cum=*target1 && (*target2++ = *target1++)!='\0' && ch>0)
  19.         {
  20.                 ch--;
  21.                 if((int)cum<0)
  22.                 {
  23.                         *target2++ = *target1++;
  24.                 }
  25.         }
  26.        
  27.         printf("拷贝完毕!\n现在str2中的内容是:%s",str2);
  28.        
  29.         return 0;
  30. }
复制代码

无标题.png
结果是错的
但是怎么想都不知道代码哪里有问题。。
最佳答案
2022-7-3 19:13:02
  1. while( ch>0&&(cum=*target2++ = *target1++)!='\0' )
  2.         {
  3.                 ch--;
  4.                 if(cum<0)
  5.                 {
  6.                         *target2++ = *target1++;
  7.                 }
  8.         }
  9.         if(*target2)*target2='\0';
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-7-3 19:13:02 | 显示全部楼层    本楼为最佳答案   
  1. while( ch>0&&(cum=*target2++ = *target1++)!='\0' )
  2.         {
  3.                 ch--;
  4.                 if(cum<0)
  5.                 {
  6.                         *target2++ = *target1++;
  7.                 }
  8.         }
  9.         if(*target2)*target2='\0';
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-4 12:16:39 | 显示全部楼层

为什么while( ch>0&&(cum=*target2++ = *target1++)!='\0' )就可以,而while(cum=*target1 && (*target2++ = *target1++)!='\0' && ch>0)就不行?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-4 18:17:44 | 显示全部楼层
gandixiwang 发表于 2022-7-4 12:16
为什么while( ch>0&&(cum=*target2++ = *target1++)!='\0' )就可以,而while(cum=*target1 && (*target2+ ...

了解一下逻辑短路
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 01:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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