当前位置:网站首页>C language: a simple calculator is implemented by using code many times

C language: a simple calculator is implemented by using code many times

2022-06-26 22:16:00 Nianchi ichthyology programming

#include <stdio.h>
int mul(int x , int y);
float div(float x , float y);
int yushu(int x , int y);
int add(int x , int y){
    
int z;
//printf(" Enter two integers :");
//scanf("%d%d",&x , &y);
z = x + y;
return z;
}
int sub(int x , int y){
    
int z;
z = x - y;
return z;
}
void Menu()
{
    
	printf(" Welcome to the calculator \n");
	printf(" \n");
	printf("--------------------------- \n");
	printf("1. Add  2. Subtraction  \n3. Multiplication  4. division  \n5. remainder  6. Back to main menu  \n");
	printf("--------------------------- \n");
	printf(" \n");
}
int main()
{
    
	char ch1 = 'y',ch2,a;
	printf(" Please select 1-6:"); 
	while(ch1 == 'y'||ch1 =='Y'){
    
		Menu();
		printf("== Please enter ==\n");
		printf(" Please enter your choice :\n");
		scanf("%c",&ch2);
		getchar();
		switch(ch2){
    
			case '1':{
    
			int a , b , c;
			printf(" Enter two integers :");
			scanf("%d%d",&a , &b);
			c = add(a,b);
			printf("%d + %d = %d \n", a , b , c);
			break;
		}
		case '2':{
    
			int a , b , c;
			printf(" Enter two integers :");
			scanf("%d%d",&a , &b);
			c = sub(a,b);
			printf("%d - %d = %d \n", a , b , c);
			break;
		}
		case '3':{
    
			int a , b , c;
			printf(" Enter two integers :");
			scanf("%d%d",&a , &b);
			c = mul(a,b);
			printf("%d * %d = %d \n", a , b , c);
			break;
		}
		case '4':{
    
			int a , b ;
			float c; 
			printf(" Enter two integers :");
			scanf("%d%d",&a , &b);
			c = div(a,b);
			printf("%d / %d = %g \n", a , b , c);
			break;
		}
		case '5':{
    
			int a , b , c;
			printf(" Enter two integers :");
			scanf("%d%d",&a , &b);
			c = yushu(a,b);
			printf("%d %% %d = %d \n", a , b , c);
			break;
		}
		case '6':{
    
			ch1 = 'n';
			break;
		}
		default:{
    
				printf(" Incorrect input , Please enter 0-9 Make a selection !");
				break;
			}	
		}
		if(ch2 != '\0'){
    
			printf(" Press any key to continue , Back to main menu \n");
			a = getchar();
			if(a != '\xA'){
    
				getchar();
				ch1 = 'n';
			}
		}
	}	
} 
int mul(int x , int y){
    
int z;
z = x * y;
return z;
}
float div(float x , float y){
    
float z;
z = x / y;
return z;
}
int yushu(int x , int y){
    
int z;
z = x % y;
return z;
}

原网站

版权声明
本文为[Nianchi ichthyology programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262210452113.html