创建在C简单的计算器 [英] Creating a simple calculator in C

查看:132
本文介绍了创建在C简单的计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的C计算器脚本,只使用基本的+, - ,/,*。我有以下,但我不知道为什么它不能正常打印。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{    //介绍瓦尔
    双数字1,数字,结果;
    字符符号; //操作*, - ,+ /    //允许用户交互
    的printf(请输入您的公式\\ n);
    scanf函数(%F%C%F,&安培;数字1,和放大器;符号,&安培;数字2);    开关(符号){
        案例'+':
            结果=数字1 +数字2;
            打破;
        默认:
            的printf(别的事我不知道);
            打破;
    }    的getchar();
    返回0;
}

为什么结果不被打印?我在这里做得不对,

 结果=数字1 +数字2;


解决方案

  

为什么不被打印结果呢?


您正确地算出答案,的但不随地打印

您需要有这样的:

 的printf(答:%F +%F =%F \\ N,数字1,数字,结果);

如果没有打印语句,没有被打印出来。


修改回应评论:

你做的printf 您计算的结果?
就个人而言,我会放的printf就在之前的getchar();

有关更多的调试,只是你以后scanf函数,我会写:

 的printf(输入所接收:数字1是%F \\ n编号2为%F \\ nsymbol是%C \\ N,数字1,数字,符号);

如果没有显示您所键入的输入,那么什么是错的与您如何收集输入。

I am trying to write a simple C calculator script, using only the basic +, -, /, *. I have the following, but I'm not sure why it's not printing correctly.

#include<stdio.h>
#include<stdlib.h>

int main (void)
{

    //introduce vars
    double number1, number2, result;
    char symbol; //the operator *, -, +, /

    //allow user interaction
    printf("Enter your formula \n");
    scanf("%f %c %f", &number1, &symbol, &number2);

    switch (symbol) {
        case '+':
            result = number1 + number2;
            break;
        default:
            printf("something else happened i am not aware of");
            break;
    }

    getchar();
    return 0;
}

Why is the result not being printed? Am I doing something wrong here,

result = number1 + number2;

解决方案

"Why is the result not being printed?"

You calculate the answer properly, but do not print it anywhere.

You need to have something like:

printf("Answer: %f + %f = %f\n", number1, number2, result);

Without a print statement, nothing gets printed.


EDIT Responding to comment:

Did you do the printf after you calculate the result? Personally, I would put the printf just before the getchar();

For more debugging, just after your scanf, I would write:

printf("Input as received: number1 is %f\n number2 is %f\nsymbol is %c\n", number1, number2, symbol);

If that does not show the input that you typed, then something is wrong with how you gather input.

这篇关于创建在C简单的计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆