用c语言写一个混合四则运算的计算器

发布于 2022-05-24  60 次阅读


#include 
typedef unsigned int u16;	
typedef unsigned char u8;

struct Config
{
	int symbol:4;//加法1,减法2,乘法3,除法4
	u16 decimal:1; //小数点位 0为整数 1位小数部分
};
struct Config config;



/****
函数名称: char_num(u8 chr)
功能:字符串转浮点型 

***/
float char_num(u8 chr){
	u8 t = '\0';
	switch(chr){
		case '0': t =  0;break;
		case '1': t =  1;break;
		case '2': t =  2;break;
		case '3': t =  3;break;
		case '4': t =  4;break;
		case '5': t =  5;break;
		case '6': t =  6;break;
		case '7': t =  7;break;
		case '8': t =  8;break;
		case '9': t =  9;break;
	}
	return t;
}

float power(float a,int n)
{
	u8 i = 0;
	float num = a; 
	for (i = 0;i<n-1;i++) { num = num * a; } return num; } float analy(u8 *str) { u8 i = 0; float temp_num = 0; u16 decimal_places = 1;//小数位 float add_dat[40];//加法存储器 u8 add_n = 0;//个数 float multipli_temp = 1;//乘法临时变量 u8 multipli_n = 0;//个数 float num = 0; while (1) { u8 n = 0; if(str[i]>='0' && str[i]<='9' )  
		{
			
			if (config.decimal == 0)
				temp_num = temp_num*10+char_num(str[i]);
			if (config.decimal == 1)
			{	temp_num = temp_num + char_num(str[i])*power(0.1,decimal_places); 
				decimal_places++; 

			}
			 
		}
		
		if(str[i] == '.')
		{
			config.decimal = 1;
			decimal_places = 1;
		}
		 
		if(str[i] == '*')  // 2+5*n //3*5+2
		{
			if (config.symbol == 1 || config.symbol ==0){
				multipli_temp	= temp_num;
				temp_num = 0; }
			if 	(config.symbol == 2){ //2-5*n
				multipli_temp	= -temp_num;
				temp_num = 0; }	
			if 	(config.symbol == 3){ // 2*5*n
				multipli_temp	= multipli_temp * temp_num;
				temp_num = 0; }	
			if (config.symbol== 4){
				multipli_temp	= multipli_temp / temp_num;
			}
			config.symbol = 3;
			config.decimal = 0;
			decimal_places = 1;
			temp_num = 0;
		} 
		
		if(str[i] == '/') // 2+5/n 
		{
			if (config.symbol == 1|| config.symbol ==0){
				multipli_temp	= temp_num;
				temp_num = 0; }
			if 	(config.symbol == 2){ //2-5*n
				multipli_temp	= -temp_num;
				temp_num = 0; }	
			if 	(config.symbol == 3){ // 2*5*n
				multipli_temp	= multipli_temp * temp_num;
				temp_num = 0; }	
			if (config.symbol == 4){
				multipli_temp	= multipli_temp/ temp_num;
			}
			config.symbol = 4;
			config.decimal = 0;
			decimal_places = 1;
			temp_num = 0;
		}
		
		
		if(str[i] == '+')
		{	if (config.symbol == 1 || config.symbol ==0){
				add_dat[add_n] = temp_num; 
				add_n++;}
			if (config.symbol == 2){
				add_dat[add_n] = -temp_num; 
				add_n++;}
			if (config.symbol == 3){//*  2*5+n
				add_dat[add_n] = multipli_temp * temp_num;
				add_n++;
			}
			if (config.symbol == 4){
				add_dat[add_n] = multipli_temp / temp_num;//2/5+n
				add_n++;
			}
			temp_num = 0;
			config.decimal = 0;
			decimal_places = 1;
			config.symbol = 1;
			}
		
		if(str[i] == '-')
		{
			if (config.symbol == 1 || config.symbol ==0){
				add_dat[add_n] = temp_num; 
				add_n++;}
			if (config.symbol == 2){
				add_dat[add_n] = -temp_num; 
				add_n++;}
			if (config.symbol == 3){//*  2*5+n
				add_dat[add_n] = multipli_temp * temp_num;
				add_n ++;
			}
			if (config.symbol == 4){
				add_dat[add_n] = multipli_temp / temp_num;//2/5+n 
				add_n ++;
			}
			config.symbol = 2;
			config.decimal = 0;
			decimal_places = 1;
			temp_num = 0;
		}
		if(str[i] == '=')
		{
			if (config.symbol == 1){
				add_dat[add_n] = temp_num; 
				add_n++;}
			if (config.symbol == 2){
				add_dat[add_n] = -temp_num; 
				add_n++;}
			if (config.symbol == 3){//*  2*5+n
				add_dat[add_n] = multipli_temp * temp_num;
				add_n ++;
			}
			if (config.symbol == 4){
				add_dat[add_n] = multipli_temp / temp_num;//2/5+n
				
				add_n ++;
			}
			config.decimal = 0;
			decimal_places = 1;
			config.symbol == 0;
	
			for (n = 0;n<add_n;n++)
			{
				num = num +add_dat[n];
				
			}
			return num;
		}
		i++;
	}
}




void main(){
	
	float num;
	config.decimal == 0;
	char chr[30];
	


	while (1)
	{
		scanf("%s",chr);
		num = analy(chr);
		printf("%f\n",num);
				
	 } 
 
}