目前不太懂的输出问题已解决

来源:互联网 发布:python requests的功能 编辑:程序博客网 时间:2024/05/18 02:46
C PRIMER PLUS 第8章编程练习第8题遇到的问题
#include<stdio.h>
char memu(void);
float shuzi(void);
void add(float a,float b);
void s(float a,float b);
void m(float a,float b);
void d(float a,float b);
int main(void)
{
  float a,b;
  char ch;
  while((ch=memu())!='q')
  {
      printf("Enter first number:");
      a=shuzi();
printf("Enter second number:");
      b=shuzi();
      if(ch=='a')
        add(a,b);
      if(ch=='s')
        s(a,b);
      if(ch=='m')
        m(a,b);
      if(ch=='d')
        d(a,b);
    printf("%c",ch);
  }
  printf("bye\n");
  return 0;
}
char memu(void)
{
    char ch;
    ch='l';
    while(ch!='a'||ch!='s'||ch!='m'||ch!='d'||ch!='q')
    {
      printf("Enter the operation of your choice:\n");
      printf("a.add       s.subtract\n");
      printf("m.multiply  d.divide\n");
      printf("q.quit\n");
      ch=getchar();
      while(getchar()!='\n')
        continue;
          switch(ch)
         {
           case 'a':break;
           case 's':break;
           case 'm':break;
           case 'd':break;
           case 'q':break;
           default:printf("what?%c\n",ch);break;
         }
      return ch;
   }
}
float shuzi(void)
{
    float q;
    char ch;
    while((scanf("%f",&q))!=1)
    {
       while((ch=getchar())!='\n')
        putchar(ch);
        printf(" is not an number\n");
        printf("Please enter a number,such as 2.5,-1.78E8,or3:");
    }
    return q;
}
void add(float a,float b)
{
    printf("%f+%f=%f\n",a,b,a+b);
}
void s(float a,float b)
{
    printf("%f+%f=%f\n",a,b,a-b);
}
void m(float a,float b)
{
    printf("%f*%f=%f\n",a,b,a*b);
}
void d(float a,float b)
{
    printf("%f/%f=%f",a,b,a/b);
}
解决方法1+2:
1、在scanf前面加rewind(stdin);

2、

char memu(void)

char ch;  ch='l';

while(ch!='a'||ch!='s'||ch!='m'||ch!='d'||ch!='q')

  {  printf("Enter the operation of your choice:\n");

  printf("a.add s.subtract\n");  printf("m.multiply d.divide\n"); 

printf("q.quit\n");  ch=getchar(); if(ch=='\n')ch=getchar();//加上这行 

printf("get:%c\n",ch);

while(getchar()!='\n'continue;

switch(ch)

  {  case'a':break;

case's':break

case'm':break

case 'd':break

case 'q':break

default:printf("what?%c\n",ch);break;

  } 

return ch; 

}

  }

问题提问地址:http://topic.csdn.net/u/20120307/16/6dbc46ba-b4cf-4bdc-ad07-c7213e093fd4.html?seed=377171222&r=77808417#r_77808417