计算机1.0

该博客展示了如何使用C语言编写基础的数学运算函数,包括加、减、乘、除和取余,并构建了一个简单的命令行计算器程序。用户通过输入操作符进行计算,程序能够根据操作符调用相应的函数执行计算。文章还包含了一个主函数示例,演示了如何整合这些函数并处理用户输入。
摘要由CSDN通过智能技术生成

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int Add(int x, int y)
{
    return x + y;
}

int Mul(int x, int y)
{
    return x * y;
}

int Div(int x, int y)
{
    return 1.0 * x / y;
}

int Sub(int x, int y)
{
    return x - y;
}

int Qum(int x, int y)
{
    return x % y;
}

void calc(int(*p)(int, int), int x, int y)
{
    int ret = 0;
    ret = p(x, y);
    printf("%d\n", ret);
}

void All(int* n, int* x, int* y)
{
    while (1)
    {
        
        if (n == 0)
            calc(Add, x, y);
        else if (n == 1)
            calc(Sub, x, y);
        else if (n == 2)
            calc(Mul, x, y);
        else if (n == 3)
            calc(Div, x, y);
        else if (n == 4)
            calc(Qum, x, y);
        else
            printf("error");
        break;

    }

}

int main()
{

    int x = 0;
    int y = 0;

    char mark[5] = "+-*/%";
    int n = 0;
    char s;

begin:;

    scanf("%d%c%d", &x, &s, &y);

    for (n;n < 5;)
    {
        if (mark[n] == s)
        {
            goto home;
        }
        else
            n++;
    }

home:;
    int* tn = n;

    int* tx = x;
    int* ty = y;

    All(tn, tx, ty);

    printf("输入1继续计算:");
    int put = 0;
    scanf("%d", &put);

    if (put == 1)
        goto begin;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值