目前不太懂的输出问题已解决
来源:互联网 发布: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;
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';
{
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;
{
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);
}
{
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
- 目前不太懂的输出问题已解决
- 解决无法访问Google的问题(目前已…
- 目前未解决的问题
- 目前待解决的问题
- 我的问题已解决
- 【已解决】Android真机设备调试时LogCat的日志无法输出的问题
- 目前使用Z-Stack未解决的一些问题
- 目前遇到的问题
- 目前我的问题
- 目前的问题
- php 5.3 验证码 不输出的原因 已解决
- 关于.NET装不上的问题已解决
- 解决日志已满的问题
- sql80.col打不开的问题已解决!
- 短信订购的问题已解决
- 最近碰到的问题已解决
- 解决为'*********' 的游标已存在问题
- 解决归档日志已满的问题
- 特别注意可恶的\n啦,为什么最后ch的值会变?求助!!!
- iOS开发学习笔记——UITableview
- 使用ActivityGroup来切换Activity和Layout
- 被除数为0的悲剧
- JSTL 入门: 探讨 core
- 目前不太懂的输出问题已解决
- 关于jdbc内嵌事务(结合spring的传播属性)
- 代码细节需注意
- 浅谈 "高内聚、低耦合" 这对兄弟
- JS分页代码
- 再谈免费馅饼
- 再看阿里云
- 字体对话框
- php防止重复定义常量 .