首页 > 代码库 > 大数减法(含小数)

大数减法(含小数)

正数减法

描述: 两个任意长度的正数相减,这两个正数可以带小数点,也可以是整数,请输出结果。输入的字符串中,不会出现除了数字与小数点以外的其它字符,不会出现多个小数点以及小数点在第一个字符的位置等非法情况,所以考生的程序中无须考虑输入的数值字符串非法的情况。

详细要求以及约束:
1.输入均为正数,但输出可能为负数;
2.输入输出均为字符串形式;
3.如果输出是正数则不需要带符号,如果为负数,则输出的结果字符串需要带负号
例如:2.2-1.1 直接输出为“1.1”,1.1-2.2 则需要输出为“-1.1”
4.输出的结果字符串需要过滤掉整数位前以及小数位后无效的0,小数位为全0的,直接输出整数位
例如相减结果为11.345,此数值前后均不可以带0,“011.345”或者“0011.34500”等等前后带无效0的均视为错误 输出。例如1.1-1.1结果为0.0,则直接输出0。
要求实现函数:
void Decrease(char *input1, char*input2, char *output)
【输入】 char *iinput1 被减数
char*nput2 减数
【输出】 char *output 减法结果
【返回】 无
示例
输入:char *input1="2.2"
char *input2="1.1"
输出:char*output="1.1"
输入:char *input1="1.1"
char *input2="2.2"
输出:char *output="-1.1"

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<malloc.h>void Decrease(char *input1, char*input2, char *output){	int i, j, k;	int length1_integer, length1_decimal;	int length2_integer, length2_decimal;	int length_integer, length_decimal;	int length;	int *num1, *num2, *differ, *temp;	int neg_flag = 0, flag = 0;	//负数标志	//寻找数1的小数点,如果没有小数点,在数的末尾增加小数点。	for (i = 0; input1[i] != ‘\0‘; i++)	{		if (input1[i] == ‘.‘)		{			length1_integer = i;			break;		}	}	if (input1[i] == ‘\0‘)	{		input1[i] = ‘.‘;		length1_integer = i;		length1_decimal = 0;		input1[i + 1] = ‘\0‘;	}	else	{		while (input1[i] != ‘\0‘)		{			i++;		}		length1_decimal = i - length1_integer - 1;	}	//寻找数2的小数点,如果没有小数点,在数的末尾增加小数点。	for (i = 0; input2[i] != ‘\0‘; i++)	{		if (input2[i] == ‘.‘)		{			length2_integer = i;			break;		}	}	if (input2[i] == ‘\0‘)	{		input2[i] = ‘.‘;		length2_integer = i;		length2_decimal = 0;		input2[i + 1] = ‘\0‘;	}	else	{		while (input2[i] != ‘\0‘)		{			i++;		}		length2_decimal = i - length2_integer - 1;	}	//计算整数位和小数位的最大长度,并为最后结果分配空间	length_integer = length1_integer > length2_integer ? length1_integer : length2_integer;	length_decimal = length1_decimal > length2_decimal ? length1_decimal : length2_decimal;	num1 = (int *)malloc(sizeof(int)*(length_integer + length_decimal));	num2 = (int *)malloc(sizeof(int)*(length_integer + length_decimal));	temp = (int *)malloc(sizeof(int)*(length_integer + length_decimal));	differ = (int *)malloc(sizeof(int)*(length_integer + length_decimal + 2));	memset(num1, 0, sizeof(int)*(length_integer + length_decimal));	memset(num2, 0, sizeof(int)*(length_integer + length_decimal));	memset(temp, 0, sizeof(int)*(length_integer + length_decimal));	memset(differ, 0, sizeof(int)*(length_integer + length_decimal + 2));	//将原数据1存入数组,并按照小数点对齐	for (i = length1_integer - 1, j = length_integer - 1; i >= 0; i--, j--)	{		num1[j] = input1[i] - ‘0‘;	}	for (i = length1_integer + 1, j = length_integer; i <= length1_integer + length1_decimal; i++, j++)	{		num1[j] = input1[i] - ‘0‘;	}	//将原数据2存入数组,并按照小数点对齐	for (i = length2_integer - 1, j = length_integer - 1; i >= 0; i--, j--)	{		num2[j] = input2[i] - ‘0‘;	}	for (i = length2_integer + 1, j = length_integer; i <= length2_integer + length2_decimal; i++, j++)	{		num2[j] = input2[i] - ‘0‘;	}	//开始减法计算	for (i = length_integer + length_decimal - 1; i >= 0; i--)	{		differ[i] = differ[i] + num1[i] - num2[i];		if ((differ[i] < 0) && (i != 0))		{			differ[i - 1]--;			differ[i] = 10 + differ[i];		}	}	//如果结果为负数,重新计算差值。123-456为667,重新计算时,使用1000-667,即得最后结果333	if (differ[0] < 0)	{		differ[0] = 10 + differ[0];		neg_flag = 1;		temp[0] = 10;		for (i = length_integer + length_decimal - 1; i >= 0; i--)		{			differ[i] = flag + temp[i] - differ[i];			flag = 0;//注意借位标志位及时清零。			if ((differ[i] < 0) && (i != 0))			{				flag--;				differ[i] = 10 + differ[i];			}		}	}	//将小数位右移,以增加小数点	for (i = length_integer + length_decimal - 1; i >= length_integer; i--)	{		differ[i + 1] = differ[i];	}	//将结果存入output	for (i = 0; i<length_integer + length_decimal + 2; i++)	{		output[i] = differ[i] + ‘0‘;	}	output[length_integer] = ‘.‘;	output[length_integer + length_decimal + 1] = ‘\0‘;	//删除后缀0	for (i = length_integer + length_decimal; output[i] == ‘0‘; i--);	if (output[i] == ‘.‘)	{		output[i] = ‘\0‘;	}	else	{		output[i + 1] = ‘\0‘;	}	//删除前导零	for (i = 0; output[i] == ‘0‘; i++);	if (output[i] == ‘.‘)	{		for (j = i, k = 1; output[j] != ‘\0‘; j++, k++)		{			output[k] = output[j];		}		output[k] = ‘\0‘;	}	else	{		for (j = i, k = 0; output[j] != ‘\0‘; j++, k++)		{			output[k] = output[j];		}		output[k] = ‘\0‘;	}	//如果结果为负数,增加负号	if (neg_flag == 1)	{		length = strlen(output);		for (i = length; i>=0; i--)		{			output[i+1] = output[i];		}		output[0] = ‘-‘;	}	//如果两个数相等,则结果的所有零和小数点已被删除。故output只含‘\0‘。直接指定结果为0即可。	if (output[0] == ‘\0‘)	{		output[0] = ‘0‘;		output[1] = ‘\0‘;	}}int main(){	char input1[255], input2[255], output[255];	gets(input1);	gets(input2);	Decrease(input1, input2, output);	puts(output);	return 0;}