鱼C论坛

 找回密码
 立即注册
查看: 1741|回复: 10

[已解决]还是c primer plus上面的练习题

[复制链接]
发表于 2017-3-30 17:48:04 | 显示全部楼层 |阅读模式

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

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

x
  1. /*使用if else语句编写一个程序读入输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换*/                                                                                                

  2.   #include<stdio.h>
  3.   int main(void)
  4.   {
  5.       char ch;
  6.       int flag = 0;
  7.   
  8.       printf("Please enter some words:\n");
  9.       while((ch = getchar()) != '#')
  10.       {
  11.           if(ch == '.')
  12.           {
  13.               ch = '!';
  14.               flag++;
  15.           }
  16.           if else (ch == '!')
  17.           {
  18.               putchar(ch);
  19.               flag++;
  20.           }
  21.           putchar(ch);
  22.       }
  23.       printf("\nThere is %d changs in this program.",falg);
  24.   
  25.       return 0;
  26.   }
复制代码

这个代码为什么报以下错误?
ex4.c: In function ‘main’:
ex4.c:17:6: error: expected ‘(’ before ‘else’
   if else (ch == '!')
      ^



PS:怎么从vim里面复制代码的时候不选取旁边的行号?我用v模式复制了以后又不能全局粘贴到网上。求助啊啊 啊
最佳答案
2017-3-30 18:52:47
1b1n1m 发表于 2017-3-30 18:38
现在问题来了,最后输出的flag值是35不管怎么输入。
Please enter some words:
your. your!#
  1. /*使用if else语句编写一个程序读入输入,读到#停止。
  2. 用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换*/

  3. #include<stdio.h>
  4. int main(void)
  5. {
  6.         char ch;
  7.         int flag = 0;

  8.         printf("Please enter some words:\n");
  9.         while((ch = getchar()) != '#')
  10.         {
  11.                 if(ch == '.')
  12.                 {
  13.                         ch = '!';
  14.                         flag++;
  15.                 }
  16.                 else if(ch == '!')
  17.                 {
  18.                         putchar(ch);
  19.                         flag++;
  20.                 }
  21.                 putchar(ch);
  22.         }
  23.         printf("\nThere is %d changs in this program.", flag);

  24.         return 0;
  25. }
复制代码

  1. Please enter some words:
  2. your. your!#
  3. your! your!!
  4. There is 2 changs in this program.请按任意键继续. . .
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-30 18:25:05 | 显示全部楼层
是 else if
不是if else

^_^

直接在shell下输入

cat test.c
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-30 18:38:01 | 显示全部楼层
人造人 发表于 2017-3-30 18:25
是 else if
不是if else

现在问题来了,最后输出的flag值是35不管怎么输入。
Please enter some words:
your. your!#
your your!
There is 35 changs in this program.
还不会用gdb调试,能不能教教我?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-30 18:52:47 | 显示全部楼层    本楼为最佳答案   
1b1n1m 发表于 2017-3-30 18:38
现在问题来了,最后输出的flag值是35不管怎么输入。
Please enter some words:
your. your!#
  1. /*使用if else语句编写一个程序读入输入,读到#停止。
  2. 用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换*/

  3. #include<stdio.h>
  4. int main(void)
  5. {
  6.         char ch;
  7.         int flag = 0;

  8.         printf("Please enter some words:\n");
  9.         while((ch = getchar()) != '#')
  10.         {
  11.                 if(ch == '.')
  12.                 {
  13.                         ch = '!';
  14.                         flag++;
  15.                 }
  16.                 else if(ch == '!')
  17.                 {
  18.                         putchar(ch);
  19.                         flag++;
  20.                 }
  21.                 putchar(ch);
  22.         }
  23.         printf("\nThere is %d changs in this program.", flag);

  24.         return 0;
  25. }
复制代码

  1. Please enter some words:
  2. your. your!#
  3. your! your!!
  4. There is 2 changs in this program.请按任意键继续. . .
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-30 18:54:41 | 显示全部楼层
1b1n1m 发表于 2017-3-30 18:38
现在问题来了,最后输出的flag值是35不管怎么输入。
Please enter some words:
your. your!#

gdb调试,我认为应该由小甲鱼老师来讲^_^
@小甲鱼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-1 12:32:50 | 显示全部楼层
人造人 发表于 2017-3-30 18:54
gdb调试,我认为应该由小甲鱼老师来讲^_^
@小甲鱼

后面我安排几节课讲讲gdb调试~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-1 12:52:17 | 显示全部楼层
小甲鱼 发表于 2017-4-1 12:32
后面我安排几节课讲讲gdb调试~

嗯,很期待^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-12 14:45:06 | 显示全部楼层
/*使用if else语句编写一个程序读入输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换*/

#include<stdio.h>
int main(void)
{
        char ch;
        int flag = 0;

        printf("Please enter some words:\n");
        while ((ch = getchar()) != '#')
        {
                if (ch == '.')
                {
                        ch = '!';
                        flag++;
                }
                else if (ch == '!')
                {
                        putchar(ch);
                        flag++;
                }
                putchar(ch);
        }
        printf("\nThere is %d changs in this program.", flag);

        return 0;
}


两个错误:
1.是else if  不是if else
2.定义的是flag不是falg

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-12 14:46:04 | 显示全部楼层
运行截图:
{]]0W}C3(XQG77MJ$0A]73Y.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-12 17:22:12 | 显示全部楼层
回答下楼主关于linux环境下将 有问题的代码发到FishC论坛上的问题。
如果你跟我一样用的小甲鱼的环境的话,建议设置一个共享文件夹。不要点自动挂载(具体教程网上有)。
使用mount -t vboxsf windows下的目录名 /mnt/linux下的目标文件夹名 这个语句挂载。
如果觉得每次开机都要挂载很麻烦的话那么就切到root 用vim /etc/profile ,把上一行的那个语句粘到文件最后,每次一开机就可以实现与windows的文件共享,直接把你想要的问题代码文件cp到共享文件夹,到
windows里复制粘贴就好啦!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-15 09:47:18 | 显示全部楼层
Hacker_Jack 发表于 2017-4-12 17:22
回答下楼主关于linux环境下将 有问题的代码发到FishC论坛上的问题。
如果你跟我一样用的小甲鱼的环境的话 ...

我是在电脑里面直接装的ubuntu。。。现在是通过cat命令在shell里面输出代码然后复制到论坛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-24 21:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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