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了

  • 相关阅读:
    430flash的操作
    430单片机之定时器A功能的大致介绍
    MSP430看门狗
    430之通用异步串口通信模块
    430的启动,I/O中断
    Msp430概述
    烦躁
    12864密码锁
    单片机的动手实践篇--51单片机玩转12864
    【CSS】font样式简写(转)- 不是很建议简写
  • 原文地址:https://www.cnblogs.com/zero5/p/2602548.html
Copyright © 2011-2022 走看看