C语言函数小练习——C语言实现加、减、乘、除四则运算

用函数加减乘除四则运算,首先打印初始界面,然后输入要进行的算法在输入两个数据,最后给出结果。

#include 
#include 
//1.初始界面打印
void scron()
{
	printf("*************************************\n");
	printf("*************************************\n");
	printf("*************************************\n");
	printf("*************************************\n");
	printf("*********加、减、乘、除四则运算********\n");
	printf("***************作者:Ken**************\n");
	printf("*************************************\n");
	printf("*************************************\n");
	printf("*************************************\n");
	printf("*************************************\n");
	printf("*************************************\n");
	printf("*************************************\n");

}
//2.加减乘除四种算法定义
int add(int a,int b)
{
	int z;
    z = a+b;
	return z;
}
int min(int a,int b)
{
	int z;
    z = a-b;
	return z;
}
int cheng(int a,int b)
{
	int z;
    z = a*b;
	return z;
}
float chu(int a,int b)
{
	float z;
    z = (float)a/b;
	return z;
}

//3.算法输入反馈
int cal(char suanFa)
{
	int mark;
	switch(suanFa)
    {
		case '+':
			printf("您输入了加法!!!\n");
        break;
        case '-':
			printf("您输入了减法!!!\n");
        break;
        case '*':
			printf("您输入了乘法!!!\n");
        break;
        case '/':
			printf("您输入了除法!!!\n");
        break;
        default:
			printf("您输入的算法有误!!!\n");
            return mark = 0;
            break;
    }

}
//数据暂存,多层函数调用
void cun(char suanFa)
{
	int data1;
    int data2;
    int mark;
	mark = cal(suanFa);
    while(1){
    if(mark == 0){
		break;
    }
    
	printf("请输入第一个数据:\n");
    scanf("%d",&data1);
    printf("请输入第二个数据:\n");
    scanf("%d",&data2);
	real(data1,data2,suanFa);
   }
}

//4.算法执行9
void real(int data1,int data2,char suanFa)
{
	switch(suanFa)
    {
		case '+':
			printf("%d + %d = %d\n",data1,data2,add(data1,data2));
        break;
        case '-':
			printf("%d - %d = %d\n",data1,data2,min(data1,data2));
        break;
        case '*':
			printf("%d * %d = %d\n",data1,data2,cheng(data1,data2));
        break;
        case '/':
			printf("%d / %d = %.2f\n",data1,data2,chu(data1,data2));
        break;
        default:
			
            break;
    }

}
int main()
{	
    int data1;
    int data2;
    char suanFa;
    char mark = 0;
    scron();
    while(1)
    {
    if(mark != 0)
		getchar();
	mark = 1;
    printf("请输入算法:\n");
    scanf("%c",&suanFa);
		getchar();
	cun(suanFa);
        }
	system("pause");
	return 0;
}

你可能感兴趣的:(C语言,c语言)