| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 398 人关注过本帖
标题:求助 c语言中编写计算器问题用getchar
只看楼主 加入收藏
新手啊啊啊啊
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2015-7-27
结帖率:0
收藏
已结贴  问题点数:10 回复次数:3 
求助 c语言中编写计算器问题用getchar
#include<stdio.h>
#include<iostream>
void main()
{
    int a,b,sum;
    char c;
    printf("please input the two number:");
    scanf_s("%d,%d",&a,&b);
    printf("please input you want the oreap:");
    c=getchar();
    if(c=='+')
    {
        sum=a+b;
        printf("the result=%d",sum);
    }
    if(c=='-')
    {
        sum=a-b;
        printf("the result=%d",sum);
    }
    if(c=='*')
    {
        sum=a*b;
        printf("the result=%d",sum);
    }
    if(c=='/')
    {
        sum=a/b;
        printf("the result=%d",sum);
    }
    system("pause");
}


    这是我的代码,但为什么我到getchar后的代码运行不了,可以详细的说明用getchar来编写四则运算的计算器
2015-07-28 10:37
新手啊啊啊啊
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2015-7-27
收藏
得分:0 
还有我是用VS2010编写的
2015-07-28 10:43
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:5 
#include<stdio.h>
int main()
{
    int a,b,sum;
    char c;
    printf("please input the two number:");
    scanf("%d,%d",&a,&b);
    while(getchar() != '\n')
        continue;
    printf("please input you want the oreap:");
    scanf("%c",&c);
    while(getchar() != '\n')
        continue;  
    if(c=='+')
    {
        sum=a+b;
        printf("the result=%d",sum);
    }
    if(c=='-')
    {
        sum=a-b;
        printf("the result=%d",sum);
    }
    if(c=='*')
    {
        sum=a*b;
        printf("the result=%d",sum);
    }
    if(c=='/')
    {
        if(a == 0){
            printf("BUG!\n");
            return 0;
        }
        sum=a/b;
        printf("the result=%d",sum);
    }
    return 0;
}
2015-07-28 13:30
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:5 
程序代码:
#include <stdio.h>
#include <string.h>

int main( void )
{
    int a, b;
    char opt;

    if( scanf("%d %c%d",&a,&opt,&b)==3 && strchr("+-*/",opt) )
    {
        int result;
        switch( opt )
        {
        case '+': result=a+b; break;
        case '-': result=a-b; break;
        case '*': result=a*b; break;
        case '/': result=a/b; break;
        }
        printf( " = %d\n", result );
    }

    return 0;
}

输入 -3 - +5
输出 = -8
输入 -3--5
输出 = 2
2015-07-28 14:13
快速回复:求助 c语言中编写计算器问题用getchar
数据加载中...
 
   



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

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