编程语言
首页 > 编程语言> > C#培训2019-9-5第五课(课堂练习) 利用数组实现加法运算

C#培训2019-9-5第五课(课堂练习) 利用数组实现加法运算

作者:互联网

要求:创建固定长度的数组(eg:128),将输入的字符串转存到Int数组中,实现两个数组相加。

提示:
1、由于两个数组的有效长度不一定一样,例如123+1234,若正向存储(1234→Array[0]存1;Array[1]存2;Array[2]存3;Array[3]存4),则相加时会出现错位相加(eg:十位和个位相加),因此需要反向存储(1234→Array[3]存1;Array[2]存2;Array[1]存3;Array[0]存4),这样的话两个数组的Array[0]永远是个位,可以实现直接相加。
2、当相加后数组内的值大于等于10时需存入个位值,且进位。
3、当最高位相加后的值大于等于10,需要增加一个有效位,且增加的有效位置1。
4、打印数组时,128的长度不都打出来,只打印有效长度。
5、判断是否转换 Int[ ] 数组成功,若不成功用标志位 isNumble 来判断抛出异常。

完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayPractice9_5
{
	class Program
	{
		const int LENGTH_Max = 128;
		static bool isNumble = true;
		static int nInputValue1Length;
		static int nInputValue2Length;
		static int MoveBit;
		static void Main( string[] args )
		{
			while( true ) {
				Console.WriteLine( "Please InputValue1" );
				int[] nInputValue1 = GetUserInput();
				bool isNumble1 = isNumble;
				Console.WriteLine( "Please InputValue2" );
				int[] nInputValue2 = GetUserInput();
				bool isNumble2 = isNumble;
				if( isNumble1 == false || isNumble2 ==false ) {
					Console.WriteLine( "Wrong Input" );
					isNumble2 = true;
					isNumble1 = true;
				}
				else {
					int[] nInputValue3 = Add( nInputValue1, nInputValue2 );
					Console.WriteLine( "InputValue1 + InputValue1 =" );
					PrintArray( nInputValue3 );
				}

			}

		}

		private static int[] Add( int[] nInputValue1, int[] nInputValue2 )
		{
			 nInputValue1Length=0;
			 nInputValue2Length=0;
			for( int i = nInputValue1.Length-1; i >= 0; i-- ) {
				if( nInputValue1[i] != 0 ) {
					nInputValue1Length = i;
					break;
				}
			}
			for( int i = nInputValue2.Length-1; i >= 0; i-- ) {
				if( nInputValue2[ i ] != 0 ) {
					nInputValue2Length = i;
					break;
				}
			}
			int[] nInputValue3= new int[nInputValue1.Length];
			
			for( int i = 0; i<= Math.Max ( nInputValue1Length,nInputValue2Length); i++ ) {
					nInputValue3[ i ] = nInputValue1[ i ] + nInputValue2[ i ] + nInputValue3[ i ];
					if( nInputValue3[ i ] >= 10 ) {
						nInputValue3[ i ] = nInputValue3[ i ] % 10;
						if( i != Math.Max( nInputValue1Length, nInputValue2Length )  ) {
							nInputValue3[ i + 1 ]++;
							continue;
						}
						if( i != Math.Max( nInputValue1Length, nInputValue2Length )+1  ) {
							nInputValue3[ i + 1 ]++;
							MoveBit = 1;
						}
						else {
							 MoveBit = 1;
						}
					}
				}
			return nInputValue3;
		}

		static int[] MoveArray( int[] nInputValue3 )
		{
			for( int i = Math.Max( nInputValue1Length,nInputValue2Length) ; i >= 0; i-- ) {
				nInputValue3[ i + 1 ] = nInputValue3[ i ];
			}
			nInputValue3[ nInputValue2Length+1 ] = 1;
			return nInputValue3;
		}

		static int[] GetUserInput()
		{
			
			string szInputValue = Console.ReadLine();
			char[] szInputValueArray = new char[ LENGTH_Max ];
			for( int i = szInputValue.Length-1; i >=0; i-- ) {
				szInputValueArray[ i ] = szInputValue[ i ];
			}
			int[] nInputValue = new int[ LENGTH_Max ];
			for( int i = szInputValue.Length-1; i >=0; i-- ) {
				isNumble = int.TryParse( szInputValueArray[ i ].ToString(), out nInputValue[ szInputValue.Length - 1-i ] );
				if( isNumble==false ) {
					for( int j = nInputValue.Length - 1; j >= 0; j-- ) {
					    nInputValue[ j ] = 0;
					}
					return nInputValue;
				}
			}

			return nInputValue;
		}


		static void PrintArray( int[] nInputValue1 )
		{
			int nInputValue1Length1 = Math.Max( nInputValue1Length,nInputValue2Length);

			//all "0" →not print 
			int nSum = 0;
			for( int i = 0; i <= nInputValue1Length1; i++ ) {
				nSum = nInputValue1[ i ] + nSum;
			}
			if( nSum == 0 ) {
				return;
			}

			//Already Move Array
			if(  MoveBit == 1 ) {
				nInputValue1Length1 = nInputValue1Length1 + 1;
			}
				for( int i = nInputValue1Length1; i >=0 ; i-- ) {
						Console.Write( nInputValue1[ i ] );
				}
				MoveBit = 0;
				Console.WriteLine( );
		}
	}
}

最终实现效果如下
实现正整数相加,无法实现负数相加
同时可以实现对前置0的判断:
存取数组为倒序存储,容易实现此功能
错误判断如下:
输入的字符串中只要有一个字符,就抛出异常(使用isNumble实现)

标签:nInputValue3,nInputValue1,课堂练习,C#,第五课,int,static,Array,nInputValue2Length
来源: https://blog.csdn.net/qq_41617697/article/details/100574040