| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 466 人关注过本帖
标题:求一个简单的小型计算机程序
只看楼主 加入收藏
pingweiqiang
Rank: 1
来 自:兴平
等 级:新手上路
帖 子:30
专家分:9
注 册:2010-3-15
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:2 
求一个简单的小型计算机程序
小弟新手,想找个简单的能计算数字加减乘除计算器,求高手给个简单易懂的c程序

[ 本帖最后由 pingweiqiang 于 2010-3-15 15:44 编辑 ]
搜索更多相关主题的帖子: 小型计算机 
2010-03-15 15:41
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:20 
#include <stdio.h>
#include <stdlib.h>

char symbol[] = {'+', '-', '*', '/', '='};
enum {
    Add = 0,
    Dec,
    Mul,
    Div,
    Equ,
    Max
};
int Judge(char c)
{
    switch (c)
    {
        case '+':
            return Add;
        case '-':
            return Dec;
        case '*':
            return Mul;
        case '/':
            return Div;
        case '=':
            return Equ;
        default:
            return -1;
    }
}
double Calculate(double a, double b, int flag)
{
    switch (flag)
    {                                                                                                            
        case Add:                                                                                                
            return a+b;                                                                                          
        case Dec:                                                                                                
            return a-b;                                                                                          
        case Mul:                                                                                                
            return a*b;                                                                                          
        case Div:                                                                                                
            return a/b;                                                                                          
        default:                                                                                                  
            printf("Error!\n");                                                                                   
            exit(1);                                                                                             
    }                                                                                                            
}
int main(void)                                                                                                   
{                                                                                                                 
    double a, b, answer;                                                                                          
    char c;                                                                                                      
    int flag;                                                                                                     
    printf("Input format: a <symbol(+,-,*,/)> b <enter>\n");                                                        
                                                                                   
    while (1)                                                                                                     
    {                                                                                                            
        printf(">>> ");                                                                                          
        scanf("%lf", &a);                                                                                         
        while ((flag = Judge(c = getchar())) == -1);                                                              
        scanf("%lf", &b);                                                                                         
        while ((c = getchar()) != '\n');                                                                          
        answer = Calculate(a, b, flag);                                                                           
        printf(">>> %lf\n", answer);                                                                              
    }                                                                                                            
                                                                                                                  
}        
随手写了个,功能不是很强,没有加纠错

2010-03-15 16:39
pingweiqiang
Rank: 1
来 自:兴平
等 级:新手上路
帖 子:30
专家分:9
注 册:2010-3-15
收藏
得分:0 
回复 2楼 ldg628
不错,刚学有点难啊!
2010-03-15 18:21
快速回复:求一个简单的小型计算机程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021840 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved