C语言字符串加减计算器

#pragma warning(disable:4996)
#include 

void calculator(bool isAdd,char *a, char *b);//数组计算函数
int getLength(char *a);						 //获得数组长度
void getInt(char *a, char *b);				 //获得整数部分
void getFloat(char *a, char *b);			 //获得小数部分
void getNumber(char *a, char *b,char *c);	 //获得数组拼接
bool isBigger();							 //判断哪个更大

char *firstNumber = new char[32];			 //操作数I
char *secondNumber = new char[32];			 //操作数II
char *resultNumber = new char[33];			 //结果数字
char *firstInt = new char[21];				 //操作数I整数部分
char *secondInt = new char[21];				 //操作数II整数部分
char *firstFloat = new char[11];			 //操作数I小数部分
char *secondFloat = new char[11];			 //操作数II小数部分

int main(int argc,char*args[])
{
	char response;
	int select=0;
	char *tempA = new char[32];
	char *tempB = new char[32];
	while (true)
	{
		printf("输入第一个数字:\n");
		scanf("%s", tempA);
		fflush(stdin);
		printf("输入第二个数字:\n");
		scanf("%s", tempB);
		fflush(stdin);
		printf("要执行的功能:\n1.加法运算\n2.减法运算\n请选择:");
		scanf("%d", &select);
		fflush(stdin);
		switch (select)
		{
			case 1:
				printf("你选择了加法运算....\n");
				calculator(true, tempA, tempB);
				break;
			case 2:
				printf("你选择了减法运算....\n");
				calculator(false, tempA, tempB);
				break;
			default:
				break;
		}
		printf("还要继续计算吗?(Y/N)");
		scanf("%c", &response);
		fflush(stdin);
		if (response != 'Y'&&response != 'y')
			break;
		else
			printf("\n");
	}
	return 0;
}

//加法函数
void calculator(bool isAdd,char *a, char *b)
{
	int temp,tempA,tempB;
	//两个数字转换为规格字符串
	getInt(a, firstInt);
	getInt(b, secondInt);
	getFloat(a, firstFloat);
	getFloat(b, secondFloat);
	getNumber(firstInt,firstFloat, firstNumber);
	getNumber(secondInt, secondFloat, secondNumber);
	//如果相加
	if (isAdd)
	{
		temp = 0;
		for (int i = 30; i > -1; i--)
		{
			if (i == 20)
			{
				resultNumber[21] = '.';
				continue;
			}
			tempA = firstNumber[i] - 48;
			tempB = secondNumber[i] - 48;
			if (tempA + tempB + temp > 9)
			{
				resultNumber[i + 1] = tempA + tempB + temp - 10 + 48;
				temp = 1;
			}
			else
			{
				resultNumber[i + 1] = tempA + tempB + temp + 48;
				temp = 0;
			}
		}
		resultNumber[0] = temp > 0 ? '1' : '0';

	}
	//如果相减
	else
	{
		temp = 0;
		for (int i = 30; i > -1; i--)
		{
			if (i == 20)
			{
				resultNumber[21] = '.';
				continue;
			}
			if (isBigger())
			{
				tempA = firstNumber[i] - 48;
				tempB = secondNumber[i] - 48;
			}
			else
			{
				//后一个更大,交换次序
				tempA = secondNumber[i] - 48;
				tempB = firstNumber[i] - 48;
			}
			if (tempA + temp - tempB < 0)
			{
				resultNumber[i + 1] = 10 + tempA + temp - tempB + 48;
				temp = -1;
			}
			else
			{
				resultNumber[i + 1] = tempA + temp - tempB + 48;
				temp = 0;
			}
		}
		resultNumber[0] =isBigger()?'0':'-';	//正负的处理
	}
	resultNumber[32] = '\0';//字符末尾处理
	printf("第一个数字:\n%s\n", firstNumber);
	printf("第二个数字:\n%s\n", secondNumber);
	printf("计算结果为:\n%s\n", resultNumber);
}

//获得字符串的长度
int getLength(char *a)
{
	for (int i = 0; i < 31;i++)
	{
		if (a[i] == '\0')
			return i;
	}
	return 0;
}

//判定两个字符串整数位哪个更长
int isLonger(char *a, char *b)
{
	int lengthA=-1, lengthB=-1;
	for (int i = 0; i < 31;i++)
	{
		if (a[i] == '.')
			lengthA = i;
		if (b[i] == '.')
			lengthB = i;
	}
	if (lengthA == -1)
		lengthA = getLength(a);
	if (lengthB == -1)
		lengthB = getLength(b);
	return lengthA - lengthB;
}

//获得字符串里包含的整数
void getInt(char *a,char *b)
{
	for (int i = 0; i < 21; i++)
	{
		if (a[i] == '.'||a[i]=='\0')
		{
			b[i] = '\0';	//末尾处理
			return;
		}
		else
			b[i] = a[i];
	}

}

//获得字符串里包含的小数
void getFloat(char *a, char *b)
{
	int current = 0;
	bool isOK = false;
	for (int i = 0; i < 31; i++)
	{
		if (a[i] == '\0')
		{
			b[i-current] = '\0';	//末尾的处理
			return;
		}
		if (!isOK&&a[i] == '.')
		{
			isOK = true;
		}
		else if (isOK)
		{
			b[i-current] = a[i];
			current--;
		}
		current++;
	}
}

//拼接整数与小数成完整数
void getNumber(char *a, char *b,char *c)
{
	int intLength, floatLength;
	intLength = getLength(a);
	floatLength = getLength(b);
	for (int i = 0; i < 32; i++)
	{
		//整数部分处理
		if (i < 20)
		{
			if (i >= 20 - intLength)
				c[i] = a[i - (20 - intLength)];
			else
				c[i] = '0';
		}
		else if (i == 20)
			c[i] = '.';
		else
		{
			if (i <= 20 + floatLength)
				c[i] = b[i - 21];
			else if (i < 31)
				c[i] = '0';
			else
				c[i] = '\0';	//末尾的处理
		}
	}
}

bool isBigger()
{
	for (int i = 0; i < 32; i++)
	{
		if (firstNumber[i] != secondNumber[i])
			return firstNumber[i] > secondNumber[i];
	}
}

C语言字符串加减计算器_第1张图片


你可能感兴趣的:(C++)