模拟计算机(函数指针数组的应用)-C语言

//模拟计算机 
//指针 *p 指针数组(*p[3])
//数组指针 (*p)[3]  数组指针数组(*p[3])[3]
//函数指针int (*p)(int, int)  函数指针数组int (*p[3])(int, int)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
double Add(double x, double y);
double Sub(double x, double y);
double Mul(double x, double y);
double Div(double x, double y);
double (*function[4])(double, double) = {Add, Sub, Mul, Div};
int main()
{
	double x = 0, y = 0, a, b, c;
	int choose = 0; 
	int Ary_10;
	char Ary_2[20], Ary_8[20], Ary_16[20];        //存储二进制的数 
	while(1)
	{
		system("cls");
		printf ("┏--------------------------------------------- ┓\n");
        printf ("┇请选择你要计算的方法:                         ┇\n");
        printf ("┣--------------------------------------------- ┫\n");
        printf ("┇  1、加法        5、进制转换                  ┇\n");
        printf ("┇  2、减法        6、求一元二次方程            ┇\n");
        printf ("┇  3、乘法                                     ┇\n");
        printf ("┇  4、除法        0、退出                      ┇\n");
        printf ("┗--------------------------------------------- ┛\n");
        printf ("Please write down the number:");
        scanf ("%d", &choose);
        switch(choose)
        {
        	case 0:
				exit(0);
				break;
        	case 1:
				printf("\n请输入两个运算符(x + y):");
				scanf("%lf + %lf", &x, &y);
				printf("\n结果为:%6.2f\n", function[0](x, y));
				system("pause");
				break;
			case 2:
				printf("\n请输入两个运算符(x - y):");
				scanf("%lf - %lf", &x, &y);
				printf("\n结果为:%6.2f\n", function[1](x, y));
				system("pause");
				break;
			case 3:
				printf("\n请输入两个运算符(x * y):");
				scanf("%lf * %lf", &x, &y);
				printf("\n结果为:%6.2f\n", function[2](x, y));
				system("pause");
				break;
			case 4:
				printf("\n请输入两个运算符(x / y):");
				scanf("%lf / %lf", &x, &y);
				printf("\n结果为:%6.2f\n", function[3](x, y));
				system("pause");
				break;
			case 5:
				printf("\n请输入要转换的数:");
				scanf("%d", &Ary_10);
				itoa (Ary_10, Ary_2 , 2);
				itoa (Ary_10, Ary_8 , 8);
				itoa (Ary_10, Ary_16 , 16);
				printf("\n二进制: %s\n", &Ary_2);
				printf("\n八进制: %s\n", &Ary_8);
				printf("\n十六进制: %s\n", &Ary_16);
				system("pause");
				break;
			case 6:
				//求一元二次方程的解的代码
				printf("请输入一元一次方程的a b c三个数:");
				scanf("%lf %lf %lf", &a, &b, &c);
				printf("a:%lf b:%lf c:%lf\n", a, b, c);
				double Rad = b*b - 4*a*c;
				printf("Rad %lf\n", Rad);
				if (Rad > 0)
				{
					double x1 = (-b + sqrt(Rad)) / (2*a);
					double x2 = (-b - sqrt(Rad)) / (2*a);
					printf("有两个解 x1 = %lf, x2 = %lf\n", x1, x2);
				}
				else if (Rad == 0)
				{
					double x1 = -b / (2*a);
					printf("只有一个解 x1 = %lf\n", x1);
				}
				else
				{
					printf("无解\n");
				}
				system("pause");
				break;
			default : 
				printf(" 请输入正确的数字。\n");
				system("pause");
				break;
		}
	}
	
	 
}
double Add(double x, double y)
{
	return x + y;
}
double Sub(double x, double y)
{
	return x - y;
}
double Mul(double x, double y)
{
	return x * y;
}
double Div(double x, double y)
{
	return x / y;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脆订壳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值