通过switch case语句实现简单的计算器功能,说明它的使用方法

功能说明:1.通过三个文件case.c,main.c,case.h来实现,case.c是计算功能的函数,main.c是调用函数,case.h用来存放头文件声明。

2.实现两个数的+-*/运算,进一步验证switch case语句使用

3.指针传参与一般传参的不同

 

case.h文件

#ifndef __CASE_H__
#define __CASE_H__



int func1_Add(int *a,int *b);
int func2_Subtraction(int *a,int *b);
int func3_Multiplication(int *a,int *b);
double func4_Division(int *a,int *b);

//int FUNC1(void);
int FUNC2(void);


#endif

case.c文件


#include"case.h"



int func1_Add(int *a,int *b)
{
	int sum=0;
	sum = (*a)+(*b);
	
	return sum;
} 


int func2_Subtraction(int *a,int *b)
{
	int sum=0;
	sum =(*a)-(*b);
	
	return sum;
} 


int func3_Multiplication(int *a,int *b)
{
	int sum=0;
	sum =(*a)*(*b);
	
	return sum;
} 


double func4_Division(int *a,int *b)
{
	double sum=0;
	sum = (*a)/(*b);
	
	return sum;
} 

main.c文件


#include"case.h"
#include<stdio.h>

int main(void)
{
	
	//FUNC1();
	FUNC2();
	
	return 0;
}



int FUNC2(void)
{
	int a,a1,a2;
	int i,i1,i2;
	double i3;
	int temp=0,temp1=0,temp2=0;
b:	
	printf("please input your choose:\n");
	printf("1 represion + \n");
	printf("2 represion - \n");
	printf("3 represion * \n");
	printf("4 represion / \n");
	scanf("%d",&a);
	printf("please input tow number as the source\n");
	scanf("%d,%d",&a1,&a2);
	temp = a;
	temp1=a1;
	temp2=a2;
		while(1)
		{
			switch(temp)
			{
				case 1:
				 i=func1_Add(&temp1,&temp2);
				printf("func1_Add = %d\n",i);
				goto b;
				
				case 2:
				 i1=func2_Subtraction(&temp1,&temp2);
				printf("func2_Add = %d\n",i1);
				goto b;
				
				case 3:
				 i2=func3_Multiplication(&temp1,&temp2);
				printf("func3_Add = %d\n",i2);
				goto b;
				
				case 4:
				 i3=func4_Division(&temp1,&temp2);
				printf("func4_Add = %lf\n",i3);
				goto b;
				
				case 5:
				goto end;
			}
			
		}
end:		
	printf("the end\n");
		
	return 0;
}

在gcc环境下,输入以下命令:gcc case.c main.c -lm得到a.out可执行文件,./a.out就可以得到想执行的结果。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值