1b1n1m 发表于 2017-3-30 17:48:04

还是c primer plus上面的练习题

/*使用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++;
          }
          if else (ch == '!')
          {
            putchar(ch);
            flag++;
          }
          putchar(ch);
      }
      printf("\nThere is %d changs in this program.",falg);

      return 0;
}

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



PS:怎么从vim里面复制代码的时候不选取旁边的行号?我用v模式复制了以后又不能全局粘贴到网上。求助啊啊 啊

人造人 发表于 2017-3-30 18:25:05

是 else if
不是if else

^_^

直接在shell下输入

cat test.c

1b1n1m 发表于 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调试,能不能教教我?

人造人 发表于 2017-3-30 18:52:47

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


/*使用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;
}


Please enter some words:
your. your!#
your! your!!
There is 2 changs in this program.请按任意键继续. . .

人造人 发表于 2017-3-30 18:54:41

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


gdb调试,我认为应该由小甲鱼老师来讲^_^
@小甲鱼

小甲鱼 发表于 2017-4-1 12:32:50

人造人 发表于 2017-3-30 18:54
gdb调试,我认为应该由小甲鱼老师来讲^_^
@小甲鱼

{:10_323:}后面我安排几节课讲讲gdb调试~

人造人 发表于 2017-4-1 12:52:17

小甲鱼 发表于 2017-4-1 12:32
后面我安排几节课讲讲gdb调试~

嗯,很期待^_^

轩~ 发表于 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

轩~ 发表于 2017-4-12 14:46:04

运行截图:

Hacker_Jack 发表于 2017-4-12 17:22:12

回答下楼主关于linux环境下将 有问题的代码发到FishC论坛上的问题。
如果你跟我一样用的小甲鱼的环境的话,建议设置一个共享文件夹。不要点自动挂载(具体教程网上有)。
使用mount -t vboxsf windows下的目录名 /mnt/linux下的目标文件夹名 这个语句挂载。
如果觉得每次开机都要挂载很麻烦的话那么就切到root 用vim /etc/profile ,把上一行的那个语句粘到文件最后,每次一开机就可以实现与windows的文件共享,直接把你想要的问题代码文件cp到共享文件夹,到
windows里复制粘贴就好啦!

1b1n1m 发表于 2017-4-15 09:47:18

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

我是在电脑里面直接装的ubuntu。。。现在是通过cat命令在shell里面输出代码然后复制到论坛
页: [1]
查看完整版本: 还是c primer plus上面的练习题