calc.c

来自「一款收款机C源代码!因为是几年前的代码了」· C语言 代码 · 共 709 行 · 第 1/2 页

C
709
字号
					{
						In_Buff[numcnt ++] = 0;
						if(dec_flag)
							dec_cnt ++;
					}
					if(numcnt != 9)
					{
						In_Buff[numcnt ++] = 0;
						if(dec_flag)
							dec_cnt ++;
					}
					Calc_Disp(numcnt);
				}
				break;

			case CALC_DEC:
				if(dec_flag == 0)
				{
					dec_flag = 1;
					if(numcnt == 0)
					{
						In_Buff[numcnt ++] = 0;
						Clr_Dsp_Data();
						RightDisp(0,0);
						Insert_Period(9);
					}
//					Calc_Disp(numcnt);
				}
				break;

			case CALC_CL:			/* Clear all status */
				Clr_Status();
				break;

			case CALC_CE:
				dec_flag = 0;
				dec_cnt = 0;
				Calc_Sign = 0;
				numcnt = 1;
				In_Buff[0] = 0;			/* Equal input 0 */
				Calc_Disp(numcnt);
				break;

			case CALC_BS:
				if(numcnt == 0)			/* Did't input other key, do nothing */
					break;
				if((dec_flag == 1)&&(dec_cnt == 0))
					dec_flag = 0;
				else
				{
					if(numcnt > 1)
						numcnt --;
					else // numcnt == 1 		/* Equal input 0 */
					{
						In_Buff[0] = 0;
						Calc_Sign = POSITIVE;
					}
					if(dec_cnt)
						dec_cnt --;
				}
				Calc_Disp(numcnt);
				break;

			case CALC_FD:
//				Key_Feed();
				break;

			case CALC_PLUS:
			case CALC_MINUS:
			case CALC_MUL:
			case CALC_DIV:
//			case CALC_XCH:
			case CALC_EQU:
			case CALC_EQU1:
			case CALC_ST:
				asc2long();
				lastincmd = InCmd;
				InCmd = Logi_Code;
				return;

			case CALC_XCH:
				if(numcnt == 0)
				{
//					if((InCmd == CALC_EQU) || (InCmd == CALC_EQU1))		/* Indicate reverse the sign of the flag */
					if((InCmd == CALC_EQU) || (InCmd == CALC_EQU1) || (InCmd == CALC_XCH))
									/* Indicate reverse the sign of the flag */
					{
						lastincmd = InCmd;
						InCmd = CALC_XCH;
						return;
					}
					else			/* Equal to input 0 */
					{
						numcnt = 1;
						In_Buff[0] = 0;
					}
				}
				else
				{
					if((In_Buff[0] == 0) && (dec_flag == 0))
						break;
					Calc_Sign = (Calc_Sign == POSITIVE)?NEGATIVE:POSITIVE;
				}
				Calc_Disp(numcnt);
				break;

			#ifdef SCANNER 	/* Support the SCANNER function */
			case SPLU:			/* Do nothing */	
				break;
			#endif /* End SCANNER */

			default:
				TenKey = Isdigit(Logi_Code, Calc_Digit_Table);
				if(TenKey != 0xFF)
				{
					if((In_Buff[0] == 0) && (dec_flag == 0))
						numcnt = 0;
					if(numcnt != 9)
					{
						In_Buff[numcnt ++] = TenKey;
						if(dec_flag)
							dec_cnt ++;
						Calc_Disp(numcnt);
					}
				}
				else					/* Input other key, error */
				{
					errorType(ERR_CALC_KEY);
					Clr_Status();
				}
				break;
		}
	}
}


const word Calc_Digit_Table[MAX_DIGIT_NUM] =
{
	CALC_0, CALC_1, CALC_2, CALC_3, CALC_4, CALC_5, CALC_6, CALC_7, CALC_8, CALC_9
};

/* Dispaly the tenkey input */
void Calc_Disp(byte Cnt)
{
	byte i;

   mode_disp_flag = 0;
	if(Cnt == 0)
	{
		Clr_Dsp_Data();
		RightDisp(0,0);
		Insert_Period(9);
		return;
	}
	Clr_Dsp_Data();
	for(i = 0; i < Cnt; i ++)
		Insert_Num(In_Buff[i], MAX_LCD_NUM - Cnt + i);
	if(Calc_Sign == NEGATIVE)
		Insert_Char('-',MAX_LCD_NUM - Cnt - 1);
	Insert_Period(MAX_LCD_NUM - dec_cnt - 1);
}

/* Calculate the result */
void Calc_Update(void)
{
	byte Ret_Vlu;		/* The return value */
	
	switch(Calc_Mode)
	{
		case CALCM_NULL:		/* Normal mode, converse the input data to result */
			L2DL(lnum,&Calc_Result,dec_cnt);
			break;

		case CALCM_ADD:		/* Addition mode */
			Ret_Vlu = DL_Add(&Calc_Result,lnum,dec_cnt);			/* Not overflow */
			if(Ret_Vlu == NOTOK)
			{
				errorMsg(Disp_Err_Calc);
				Clr_Status();
				return;
			}
			break;

		case CALCM_SUB:		/* Subtraction mode */
			Ret_Vlu = DL_Add(&Calc_Result,-lnum,dec_cnt);			/* Not overflow */
			if(Ret_Vlu == NOTOK)
			{
				errorMsg(Disp_Err_Calc);
				Clr_Status();
				return;
			}
			break;

		case CALCM_MUL:		/* Multiplication mode */
			Ret_Vlu = DL_Mul(&Calc_Result,lnum,dec_cnt);
			if(Ret_Vlu == NOTOK)
			{
				errorMsg(Disp_Err_Calc);
				Clr_Status();
				return;
			}
			break;

		case CALCM_DIV:		/* Division mode */
//			DL_Div(&Calc_Result,lnum,dec_cnt,1,10);			/* Remain 11 dots */
			Ret_Vlu = DL_Exact_Div(&Calc_Result,lnum,dec_cnt);
			if(Ret_Vlu == NOTOK)
			{
				errorMsg(Disp_Err_Calc);
				Clr_Status();
				return;
			}
			break;

		case CALCM_XCH:
			if(InCmd == CALC_XCH)
			{
				Calc_Result.sign = (Calc_Result.sign == POSITIVE)?NEGATIVE:POSITIVE;
			}
			break;
			
		case CALCM_PRI:		/* The operations PRI mode */
			if(bak_mode == CALCM_SUB)		/* The operation is subtraction, reverse the sign */
				Calc_Result.sign = (Calc_Result.sign == POSITIVE)?NEGATIVE:POSITIVE;
			Ret_Vlu = DL_Add2(&Calc_Result,bak_result);
			if(Ret_Vlu == NOTOK)
			{
				errorMsg(Disp_Err_Calc);
				Clr_Status();
				return;
			}
			break;
	}

	Calc_Result_Adjust(&Calc_Result);			/* Adjust the result let it store the data max 10 decimal digits,
																and adjust the decimal last digit not 0 */
	Calc_Result_Disp();
}

/* Adjust the result, max 10 decimal digits and the last decimal digits not 0 */
void Calc_Result_Adjust(DoubleLong *ll)
{
	DoubleLong dl;
	long round;

	while(ll->dots > 11)
		DL_Div(ll, 10, 1, 0, 0);
	if(ll->dots == 11)
	{
		if(ll->sign == NEGATIVE)
			round = -5;
		else
			round = 5;
		DL_Add(ll, round, 11);
		DL_Div(ll, 10 , 1, 0, 0);
	}

	DL_Eva(&dl, ll);
	DL_Div(&dl, 10, 0, 0, 0);
	DL_Mul(&dl, 10, 0);
	while((ll->lowlong == dl.lowlong) && (ll->dots > 0))
	{
		DL_Div(ll, 10, 1, 0, 0);
		DL_Eva(&dl, ll);
		DL_Div(&dl, 10, 0, 0, 0);
		DL_Mul(&dl, 10, 0);
	}
}

/* Display the new result */
void Calc_Result_Disp(void)
{
	long disp_data;
	long round;
	byte dots;
	byte i;
	DoubleLong dl;

	i = 0;
	DL_Eva(&dl, &Calc_Result);
	
	while((dl.lowlong != 0) || (dl.highlong != 0))		/* Calculate the result length */
	{
		DL_Div(&dl, 10, 0, 0, 0);
		i ++;				/* Get the total length */
	}

	DL_Eva(&dl, &Calc_Result);
	if((i > dl.dots) && ((i - dl.dots) > 9))		/* The result is larger than 1000000000 */
	{
		errorMsg(Disp_Err_Calc);
		Clr_Status();
		return;
	}

	if(i > 9)
	{
		while(i > 10)
		{
			DL_Div(&dl, 10, 1, 0, 0);
			i --;
		}
		if(dl.sign == NEGATIVE)
			round = -5;
		else
			round = 5;
		DL_Add(&dl, round, dl.dots);
		DL_Div(&dl, 10, 1, 0, 0);
		if((dl.lowlong == 1000000000) && (dl.dots == 0))		/* When the result's value is from 999999999.5000... 
																					to 999999999.9999... , error. */
		{
			errorMsg(Disp_Err_Calc);
			Clr_Status();
			return;
		}

		Calc_Result_Adjust(&dl);			/* When the decimal last digit is 0, adjust it */
	}
	if(dl.dots > 8)
	{
		while(dl.dots > 9)
		{
			DL_Div(&dl, 10, 1, 0, 0);
		}
		if(dl.sign == NEGATIVE)
			round = -5;
		else
			round = 5;
		DL_Add(&dl, round, dl.dots);
		DL_Div(&dl, 10, 1, 0, 0);

		Calc_Result_Adjust(&dl);			/* When the decimal last digit is 0, adjust it */
		if(dl.lowlong == 0)
			DL_Clr(&Calc_Result);
	}

	if(dl.sign == POSITIVE)
		disp_data = dl.lowlong;
	else
		disp_data = -dl.lowlong;
	bak_lnum1 = disp_data;
	dots = dl.dots;
	bak_dots1 = dots;
	if((bak_mode == CALCM_NULL) || ((Calc_Mode != CALCM_MUL) && (Calc_Mode != CALCM_DIV)))
												/* When not the operations PRI mode, can dispaly the result */
	{
		Clr_Dsp_Data();
		RightDisp(disp_data,dots);
	}
	if(dots == 0)
		Insert_Period(9);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?