zoukankan      html  css  js  c++  java
  • C Primer Plus汇总 (二)错误笔记2

    特别注意可恶的\n啦,为什么最后ch的值会变?求助!!!

    可恶可恶啊http://tieba.baidu.com/p/1429836706

    include<stdio.h>
    #include<stdbool.h>
    int main(void)
    {
     int w=1;
     float s;
     char ch;
     bool is,z; while(w)
     {
     for(is=true;is==true;)
     {
     z=true;
     printf("***************\n");
     printf("请输入下列字母\n");
     printf("a)朝鲜蓟 1.25$/磅\n");
     printf("b)甜菜 0.65$/磅\n");
     printf("c)胡萝卜 0.89$/磅\n");
     printf("q)退出\n");
     while((ch=getchar())&&z==true)
     {
     switch(ch)
     {
     case 'a':s=1.25;z=is=false;break;
     case 'b':s=0.65;z=is=false;break;
     case 'c':s=0.89;z=is=false;break;
     case 'q':z=s=false;break;
     default:z=false;printf("DONE,请重新输入\n");break;
     }
     }
     }
     //if(ch=='a')
     // printf("q");
     printf("1111111111%c",ch);
     }
     printf("循环结束");
    return 0;
    }

    程序能正常输出1111111,DONE也能输出 可是去掉注释符//ch的值却不能显示

     

    我错了,果然是\n

    include<stdio.h>
    #include<stdbool.h>
    int main(void)
    {
     int w=1;
     float s;
     char ch;
     bool is,z; while(w)
     {
     for(is=true;is==true;)
     {
     z=true;
     printf("***************\n");
     printf("请输入下列字母\n");
     printf("a)朝鲜蓟 1.25$/磅\n");
     printf("b)甜菜 0.65$/磅\n");
     printf("c)胡萝卜 0.89$/磅\n");
     printf("q)退出\n");
     while(z==true&&(ch=getchar()))
     {
     if(ch!='\n')
     {
     switch(ch)
     {
     case 'a':s=1.25;z=is=false;break;
     case 'b':s=0.65;z=is=false;break;
     case 'c':s=0.89;z=is=false;break;
     case 'q':z=s=false;break;
     default:z=false;printf("DONE,请重新输入\n");break;
     }
     printf("1%c",ch);
     }
     printf("2%c",ch);
     }
     printf("3%c",ch);
     }
     //if(ch=='a')
     // printf("q");
     printf("4%c",ch);
     }
     printf("循环结束");
    return 0;
    }

    被除数为0的悲剧

    http://tieba.baidu.com/p/1441525681

    include<stdio.h>
    #include<ctype.h>
    int put(char x,int y,int z);
    int main(void)
    {
     int a,b;
     char ch;
     a=b=0;
     while((ch=getchar())!='#')
     {
     put(ch,a,b);
     }
     printf("共%d个字母,%d个单词,平均每个单词%d个字母.\n",a,b,a/b);
     return 0;
    }
    int put(char x,int y,int z)
    {
     if(isalpha(x))
     y++;
     else if(isblank(x))
     z++;
     return 0;
    }

    啊啊啊,我又2了
    除以0了

  • 相关阅读:
    mybatis 对string类型判断比较 group case when then 综合
    利用Apache shiro SimpleHash 加密字符串
    linux下用rpm 安装jdk
    运行java程序的时候出现Exception in thread "main" java.lang.UnsupportedClassVersionError:
    linux如何查看端口被哪个进程占用?
    Linux如何查看JDK的安装路径
    Linux中查看jdk版本
    linux修改/etc/profile权限
    db2,oracle,mysql ,sqlserver限制返回的行数
    什么是T-SQL
  • 原文地址:https://www.cnblogs.com/zero5/p/2602548.html
Copyright © 2011-2022 走看看