comp.lang.ada
 help / color / mirror / Atom feed
From: mike.martelli@gmail.com
Subject: Re: New to Ada, noticing something strange.
Date: 29 Sep 2005 16:58:33 -0700
Date: 2005-09-29T16:58:33-07:00	[thread overview]
Message-ID: <1128038313.717692.268490@z14g2000cwz.googlegroups.com> (raw)
In-Reply-To: <D5Sdnd4bntpo9aHeRVn-tw@megapath.net>

Ok, Randy sounds good here is my code.  I also tried compiling in on
Laptop with the latest GNAT compilier and I get the same results.

==================================================

with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Command_Line;
with Ada.Strings.Unbounded;
with Ada.Characters.Handling;

use Ada.Text_IO;
use Ada.Integer_Text_IO;
use Ada.Command_Line;
use Ada.Strings.Unbounded;

	procedure BigInts is
		base, operand1, operand2: Unbounded_String :=
To_Unbounded_String("");
		charMap: array (Character) of Integer;
		NumOfArgs, size, carry: Integer := 0;
		charBase, digit, charDigit: Character;

		procedure PopulateArray is
		begin
			charMap('0') := 0;
			charMap('1') := 1;
			charMap('2') := 2;
			charMap('3') := 3;
			charMap('4') := 4;
			charMap('5') := 5;
			charMap('6') := 6;
			charMap('7') := 7;
			charMap('8') := 8;
			charMap('9') := 9;
			charMap('A') := 10;
			charMap('B') := 11;
			charMap('C') := 12;
			charMap('D') := 13;
			charMap('E') := 14;
			charMap('F') := 15;
		end PopulateArray;

		function ResultSize(operand1, operand2: Unbounded_String) return
Integer is
		begin
			if Length(operand1) > Length(operand2)  then
				size := Length(operand1);
			else
				size := Length(operand2);
			end if;

			return size;
		end ResultSize;

		function Convert2Character(strChar: String) return Character is
		begin
		--Put_Line(strChar);
			case Integer'Value(strChar) is
				when 10 => return 'A';
				when 11 => return 'B';
				when 12 => return 'C';
				when 13 => return 'D';
				when 14 => return 'E';
				when 15 => return 'F';
				when others => return strChar(1);
			end case;
		end Convert2Character;

		function AddDigits(charOp1, charOp2: Character) return Character is
		begin
			charDigit := Convert2Character(Integer'Image(charMap(charOp1) +
charMap(charOp2) + carry));
			Put("Print1: ");
			Put(Convert2Character(Integer'Image(charMap(charOp1) +
charMap(charOp2) + carry)));
			if (charMap(charOp1) + charMap(charOp2) + carry) >
(charMap(charBase) - 1) then
				Put_Line("here1: ");
				charDigit := Convert2Character(Integer'Image(charMap(charBase) -
1)); --Set the digit to the highest possible values
				carry := (charMap(charOp1) + charMap(charOp2) + carry) -
(charMap(charBase) - 1); --carry is all of the rest
			else
				Put_Line("here2: ");
				carry := 0; --there is no carry
			end if;
			Put("Digit: ");
			Put(charDigit);
			New_Line;
			Put("Carry: ");
			Put(carry);
			New_Line;
			return charDigit;
		end AddDigits;

	begin
		NumOfArgs := ARGUMENT_COUNT;

		if NumOfArgs = 3 then
			base := To_Unbounded_String(Argument(1));
			operand1 := To_Unbounded_String(Argument(2));
			operand2 := To_Unbounded_String(Argument(3));

			if not (Integer'Value(To_String(base)) > 1) or not
(Integer'Value(To_String(base)) < 16) then
					Put("The base entered is not valid.");
					return;
			end if;

			charBase := Convert2Character(To_String(base));

			--converts all lowercase characters to uppercase
			for i in 1 .. Length(operand1) loop
				case Element(operand1, i) is
					when 'a' => Replace_Element(operand1, i, 'A');
					when 'b' => Replace_Element(operand1, i, 'B');
					when 'c' => Replace_Element(operand1, i, 'C');
					when 'd' => Replace_Element(operand1, i, 'D');
					when 'e' => Replace_Element(operand1, i, 'E');
					when 'f' => Replace_Element(operand1, i, 'F');
					when others => Replace_Element(operand1, i, Element(operand1, i));
				end case;
			end loop;

			--converts all lowercase characters to uppercase
			for i in 1 .. Length(operand2) loop
				case Element(operand2, i) is
					when 'a' => Replace_Element(operand2, i, 'A');
					when 'b' => Replace_Element(operand2, i, 'B');
					when 'c' => Replace_Element(operand2, i, 'C');
					when 'd' => Replace_Element(operand2, i, 'D');
					when 'e' => Replace_Element(operand2, i, 'E');
					when 'f' => Replace_Element(operand2, i, 'F');
					when others => Replace_Element(operand2, i, Element(operand2, i));
				end case;
			end loop;

			PopulateArray;

			for i in 1 .. Length(operand1) loop
				if charMap(To_String(operand1)(i)) >= charMap(charBase) then
						Put_Line("Operand 1 is not valid with the base entered");
						return;
				end if;
			end loop;

			for i in 1 .. Length(operand2) loop
				if charMap(To_String(operand2)(i)) >= charMap(charBase) then
						Put_Line("Operand 2 is not valid with the base entered");
						return;
				end if;
			end loop;

			for i in reverse 1 .. ResultSize(operand1,operand2) loop
				digit := AddDigits(To_String(operand1)(i), To_String(operand2)(i));
				Put(digit);
				New_Line;
			end loop;
			--Print final carry digit
			Put("Final Carry: ");
			Put(carry);
			New_Line;
		else
			Put("Not enough args");
			return;
		end if;
	end BigInts;




  reply	other threads:[~2005-09-29 23:58 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-29 17:20 New to Ada, noticing something strange mike.martelli
2005-09-29 18:39 ` Jeffrey R. Carter
2005-09-29 19:05   ` mike.martelli
2005-09-29 22:25     ` Randy Brukardt
2005-09-29 23:58       ` mike.martelli [this message]
2005-09-30  0:28         ` mike.martelli
2005-09-30  6:06         ` Jeffrey R. Carter
2005-09-30  6:28         ` Jeffrey R. Carter
2005-09-30 10:19         ` Georg Bauhaus
2005-09-30 16:43           ` mike.martelli
2005-09-30  6:23     ` Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox