2018_2_2_Simple Arithmetics_模拟_大数_输出_大综合_不容易

本文介绍了一个使用C语言实现的大数运算程序,该程序能够处理加法、减法和乘法操作,并以易于阅读的格式输出结果。通过对输入的两个大整数进行逐位运算,程序能够正确地显示中间步骤及最终答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

耐心做,祝你好运
改天再写一遍
Simple Arithmetics
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2195 Accepted: 526

Description

One part of the new WAP portal is also a calculator computing expressions with very long numbers. To make the output look better, the result is formated the same way as is it usually used with manual calculations.

Your task is to write the core part of this calculator. Given two numbers and the requested operation, you are to compute the result and print it in the form specified below. With addition and subtraction, the numbers are written below each other. Multiplication is a little bit more complex: first of all, we make a partial result for every digit of one of the numbers, and then sum the results together.

Input

There is a single positive integer T on the first line of input. It stands for the number of expressions to follow. Each expression consists of a single line containing a positive integer number, an operator (one of +, - and *) and the second positive integer number. Every number has at most 500 digits. There are no spaces on the line. If the operation is subtraction, the second number is always lower than the first one. No number will begin with zero.

Output

For each expression, print two lines with two given numbers, the second number below the first one, last digits (representing unities) must be aligned in the same column. Put the operator right in front of the first digit of the second number. After the second number, there must be a horizontal line made of dashes (-).

For each addition or subtraction, put the result right below the horizontal line, with last digit aligned to the last digit of both operands.

For each multiplication, multiply the first number by each digit of the second number. Put the partial results one below the other, starting with the product of the last digit of the second number. Each partial result should be aligned with the corresponding digit. That means the last digit of the partial product must be in the same column as the digit of the second number. No product may begin with any additional zeros. If a particular digit is zero, the product has exactly one digit -- zero. If the second number has more than one digit, print another horizontal line under the partial results, and then print the sum of them.

There must be minimal number of spaces on the beginning of lines, with respect to other constraints. The horizontal line is always as long as necessary to reach the left and right end of both numbers (and operators) right below and above it. That means it begins in the same column where the leftmost digit or operator of that two lines (one below and one above) is. It ends in the column where is the rightmost digit of that two numbers. The line can be neither longer nor shorter than specified.

Print one blank line after each test case, including the last one.

Sample Input

4
12345+67890
324-111
325*4405
1234*4

Sample Output

 12345
+67890
------
 80235

 324
-111
----
 213

    325
  *4405
  -----
   1625
     0
 1300
1300
-------
1431625

1234
  *4
----
4936

Source

Central Europe 2000

#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cstring>

#define MAX_NUMBER_DIGITS	512

char PROC[MAX_NUMBER_DIGITS+10][MAX_NUMBER_DIGITS*2+10];
int PROC_LEN[MAX_NUMBER_DIGITS+10];

void reverse(char * const r,const int r_len)
{
	int a=0, b=r_len-1, bck;

	while (b>a)
	{
		bck=r[a];
		r[a++]=r[b];
		r[b--]=bck;
	}
}

int plus(char * const f, const int f_len,
		char * const s, const int s_len,
		const int null,
		char * const vysledek, int * const vysledek_len)
{
	int max_len=(s_len<(f_len+null)) ? (f_len+null) : s_len;
	int s_idx=0,f_idx=0,last=0,idx_null=null,nula;
	int n;

	while (f_idx<max_len)
	{
		n=((s_len>s_idx)?(s[s_idx]-'0'):0) + ((idx_null>0)?0:((f_len>f_idx)?(f[f_idx]-'0'):0)) + last;
		if (n>9)
		{
			last=1;
			n-=10;
		}
		else last=0;
		vysledek[s_idx]=n+'0';

		if (!n && s_idx>0)
		{
			if (nula==-1)
			 nula=s_idx;
		}
		else nula=-1;

		++s_idx;
		if (idx_null--<=0) ++f_idx;
	}

	if (last)
	{
		vysledek[s_idx++]='1';
		nula=-1;
	}

	if (nula>0)
	 s_idx=nula;
	vysledek[s_idx]='\0';
	*vysledek_len=s_idx;
	return 3;
}

int minus(char * const f, const int f_len,
		char * const s, const int s_len,
		char * const vysledek, int * const vysledek_len)
{
	int max_len=(s_len<f_len) ? f_len : s_len;
	int idx=-1,last=0,null_idx=-1;
	int n;
	while (++idx<max_len)
	{
		n=(f[idx]-'0') - ((s_len>idx)?(s[idx]-'0'):0) - last;
		if (n<0)
		{
			last=1;
			n+=10;
		}
		else last=0;
		vysledek[idx]=n+'0';
		if (!n && idx>0)
		{
			if (null_idx==-1) null_idx=idx;
		}
		else null_idx=-1;
	}

	if (null_idx>0) idx=null_idx;
	vysledek[idx]='\0';
	*vysledek_len=idx;
	return 3;
}

int mult(char * const f, const int f_len,
		char * const s, const int s_len)
{
	int ret=2;
	int s_idx,f_idx;
	int n;
	int n_low,n_high;
	int ss;

	n_low=0;
	for(s_idx=0;s_idx<s_len;s_idx++)
	{
		ss=s[s_idx]-'0';
		f_idx=0;
		switch (ss)
		{
			case 0: PROC[ret][0]='0'; f_idx=1; break;
			case 1: strncpy(PROC[ret],f,f_len); f_idx=f_len; break;
			default:
				for(;f_idx<f_len;f_idx++)
				{
					n=ss * (f[f_idx]-'0') + n_low;
					n_low=n/10;
					n_high=n-n_low*10;
					PROC[ret][f_idx]=n_high+'0';
				}
				if (n_low)
				{
					PROC[ret][f_idx++]=n_low+'0';
					n_low=0;
				}
		}
		PROC[ret][f_idx]='\0';
		PROC_LEN[ret]=f_idx;
		ret++;
	}
	return ret;
}

void put_char(const int n,const char what)
{
	int a;
	for (a=0; a<n; a++)	putchar(what);
}

int main()
{
	int n,a,ll,dash,dashm,bl1,bl2;
	char op;
	char * f;
	char * s;

	scanf("%d",&n);
	//getchar ();
	while (n-->0)
	{
		scanf("%s",PROC[0]);

		a=1;
		f=PROC[0];
		while (isdigit(PROC[0][a++]));
		s=&PROC[0][a];
		op=PROC[0][a-1];
		PROC[0][a-1]='\0';

		PROC_LEN[0]=a-1;
		PROC_LEN[1]=strlen(s);
		strncpy(PROC[1],s,PROC_LEN[1]+1);

		reverse(PROC[0],PROC_LEN[0]);
		reverse(PROC[1],PROC_LEN[1]);

		if (op=='+')
			ll=plus(PROC[0],PROC_LEN[0],PROC[1],PROC_LEN[1],0,PROC[2],&PROC_LEN[2]);
		else
		if (op=='-')
			ll=minus(PROC[0],PROC_LEN[0],PROC[1],PROC_LEN[1],PROC[2],&PROC_LEN[2]);
		else
		if (op=='*')
			ll=mult(PROC[0],PROC_LEN[0],PROC[1],PROC_LEN[1]);

		reverse(PROC[0],PROC_LEN[0]);
		reverse(PROC[1],PROC_LEN[1]);

		dash=PROC_LEN[0]>(PROC_LEN[1]+1)?PROC_LEN[0]:PROC_LEN[1]+1;

		if (ll==3)
		{
			if (dash<PROC_LEN[2]) dash=PROC_LEN[2];
			reverse(PROC[2],PROC_LEN[2]);
			put_char(dash-PROC_LEN[0],' ');
			printf("%s\n",PROC[0]);

			put_char(dash-PROC_LEN[1]-1,' ');
			printf("%c%s\n",op,PROC[1]);

			dashm=(PROC_LEN[1]+1>PROC_LEN[2]?PROC_LEN[1]+1:PROC_LEN[2]);
			put_char(dash-dashm,' ');
			put_char(dashm,'-');
			putchar('\n');

			put_char(dash-PROC_LEN[2],' ');
			printf("%s\n",PROC[2]);
		}
		else			
		{
			PROC[ll][0]='0'; PROC[ll][1]='\0'; PROC_LEN[ll]=1;
			bl1=ll; bl2=ll+1;
			for (a=2;a<ll;a++)
			{
				plus(PROC[a],PROC_LEN[a],PROC[bl1],PROC_LEN[bl1],a-2,PROC[bl2],&PROC_LEN[bl2]);
				dashm=bl2; bl2=bl1; bl1=dashm;
			}

			dashm=dash;
			if (dash<PROC_LEN[bl1]) dash=PROC_LEN[bl1];

			put_char(dash-PROC_LEN[0],' ');
			printf("%s\n",PROC[0]);

			put_char(dash-PROC_LEN[1]-1,' ');
			printf("%c%s\n",op,PROC[1]);

			dashm=(PROC_LEN[1]+1>PROC_LEN[2]?PROC_LEN[1]+1:PROC_LEN[2]);
			
			put_char(dash-dashm,' ');
			put_char(dashm,'-');
			putchar('\n');

			for (a=2;a<ll;a++)
			{
				reverse(PROC[a],PROC_LEN[a]);
				if (PROC_LEN[a])
				{
					put_char(dash-PROC_LEN[a]-(a-2),' ');
					printf("%s\n",PROC[a]);
				}
				else
				{
					put_char(dash-(a-2)-1,' ');
					printf("0\n");
				}
			}

			put_char(dash-PROC_LEN[bl1],' ');
			put_char(PROC_LEN[bl1],'-');
			putchar('\n');
			reverse(PROC[bl1],PROC_LEN[bl1]);
			put_char(dash-PROC_LEN[bl1],' ');
			printf("%s\n",PROC[bl1]);
		}
		putchar('\n');
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值