免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1809 | 回复: 9
打印 上一主题 下一主题

[C] 新手 写的《c prime plus》第八章的一个习题,大神指点下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-27 11:13 |只看该作者 |倒序浏览
/* author: xiaowh00 */
/* date: 2012-7-27 */

#include <stdio.h>

void menu(void); // print munu
void get_input(char); // get operation and operand
float get_float(void); // get float number
void operate(float,float,char); // operate (+-*/)
void flush(void); // flush keyboard

int main(void)
{
        char ch;   // choice

        menu();
        while(scanf("%c",&ch)==1 && ch!='q')
        {
                if(ch=='a' || ch=='s' || ch=='m' || ch=='d')
                {
                        get_input(ch);
                        menu();
                        flush();
                }
                else
                {
                        printf("wrong operation\n");
                        menu();
                        flush();
                }
        }

        printf("Bye.\n");

        return 0;
}

void menu(void)
{
        printf("Enter the operation of your choice:\n");
        printf("a. add          s. subtract\n");
        printf("m. multiply     d. divide\n");
        printf("q. quit\n");
}

void flush(void)
{
        int ch;
        while((ch=getchar())!=EOF && ch!='\n')
                ;
}
void get_input(char op)
{
        float op1,op2;

        printf("Enter first number: ");
        op1=get_float();
        printf("Enter second number: ");
        op2=get_float();

        while((op=='d' || op=='s') && op2==0)
        {
                printf("Enter a number other than 0: ");
                op2=get_float();
        }
        operate(op1,op2,op);

}

float get_float(void)
{
        float f;
        int ch;

        while(scanf("%f",&f)!=1)
        {
                while((ch=getchar())!='\n')
                        putchar(ch);
                printf(" is not an number.\n");
                printf("Please enter a number,such as 2.5,-1.78E8,or 3: ");
        }
        return f;
}

void operate(float op1,float op2,char op)
{
        switch(op)
        {
                case 'a' : printf("%.2f + %.2f = %.2f\n",op1,op2,op1+op2);
                           break;
                case 's' : printf("%.2f - %.2f = %.2f\n",op1,op2,op1-op2);
                           break;
                case 'm' : printf("%.2f * %.2f = %.2f\n",op1,op2,op1*op2);
                           break;
                case 'd' : printf("%.2f / %.2f = %.2f\n",op1,op2,op1/op2);
                           break;
                default : printf("there must be some error!\n");
                          break;
        }
}

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
2 [报告]
发表于 2012-07-27 11:28 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
3 [报告]
发表于 2012-07-27 11:29 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2012-07-27 11:29 |只看该作者
Write a program that shows you a menu offering you the choice of addition, subtraction, multiplication, or division. After getting your choice, the program asks for two numbers, then performs the requested operation. The program should accept only the offered menu choices. It should use type float for the numbers and allow the user to try again if he or she fails to enter a number. In the case of subtraction, the program should prompt the user to enter a new value if 0 is entered as the value for the second number. A typical program run should look like this:








Enter the operation of your choice:

a. add           s. subtract

m. multiply      d. divide

q. quit

a

Enter first number: 22.4

Enter second number: one

one is not an number.

Please enter a number, such as 2.5, -1.78E8, or 3: 1

22.4 + 1 = 23.4

Enter the operation of your choice:

a. add           s. subtract

m. multiply      d. divide

q. quit

d

Enter first number: 18.4

Enter second number: 0

Enter a number other than 0: 0.2

18.4 / 0.2 = 92

Enter the operation of your choice:

a. add           s. subtract

m. multiply      d. divide

q. quit

q

Bye.





回复 2# pmerofc


   

论坛徽章:
0
5 [报告]
发表于 2012-07-27 11:32 |只看该作者
首先谢谢你的恢复哈!
就是清空键盘缓冲区,本来使用fflush(),但是我用fflush()发现在gcc下没效果,查了scanf()的百度百科,这个在gcc下不能用,就自己写了一个flush()

回复 3# pmerofc


   

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
6 [报告]
发表于 2012-07-27 11:39 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
7 [报告]
发表于 2012-07-27 11:44 |只看该作者
恩,确实可以简化成这样
pmerofc 发表于 2012-07-27 11:39
回复 1# xiaowh00 明显应该写为

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
8 [报告]
发表于 2012-07-27 11:47 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
9 [报告]
发表于 2012-07-27 11:52 |只看该作者
逗号表达式的用法,我怎么没想到呢,不过这样连起来写是简洁了,while这一句有点复杂
还有一个问题就是我的格式化输出的问题,比如输入1.2+1  怎么样实现输出1.2+1   我用的固定的.2%f ,如何根据输入的数据自动判断如何输出啊?
回复 8# pmerofc


   

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
10 [报告]
发表于 2012-07-27 12:03 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP