Convert a String to long without using C#’s built in function?

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

namespace StringToLong
{
class StringToLong
{
private static long ConvertString(string str)
{
int len = str.Length;
bool blNegative = false;

int index = blNegative? 1:0;

if (str[0].Equals("-"))
{
blNegative = true;
}

long lnResult = 0;

do {
lnResult *= 10;
int i = str[index] - 48; //ASCII zero
if (i < 0 | i > 9)
throw new FormatException();

lnResult += i;
index++;
} while(len > index);

return blNegative? -lnResult:lnResult;
}

static void Main(string[] args)
{
string str1 = "35111111111";

Console.WriteLine(ConvertString(str1).ToString());

Console.ReadLine();

}
}
}

In Java

public static void main(String[] args) {
String s = "35";
long ans = parse(s);
}

public final static long parse(CharSequence d) {
int l = d.length();
boolean n= d.charAt(0) == '-';
long v = 0;
int p = n?1:0;
do {
v *= 10;
int i = d.charAt(p) - 48;
if (i < 0 | i > 9)
throw new NumberFormatException(d+"");
v += i;
p++;
} while (l > p);
return n?-v:v;
}

Advertisements

Trinary Tree in Java

Implement insert and delete in a tri-nary tree. Much like a binary-tree but with 3 child nodes for each parent instead of two — with the left node being values < parent, the right node values > parent, and the middle node values == parent. For example, if I added the following nodes to the tree in this
order: 5, 4, 9, 5, 7, 2, 2

package TrinaryTree;

public class TrinaryTree {

    //Main function
  public static void main(String[] args)
  {
      //Sample tree values rooted at 5
      int Values[] = {5, 4, 9, 5, 7, 2, 2};

      //Create Tree
      TrinaryTree tree = new TrinaryTree();

      //Populate tree
      tree.Populate(Values, 7);

  }

  //Define a tree node
  static class Node
  {
      //Left is less than node value
      Node left;
      //Middle is equal to node value
      Node middle;
      //Right is greater than node value
      Node right;

      //Node value
      int value;

      //Constructor
      public Node(int value)
      {
          this.value = value;
      }
  }

  //Populate method: receives an
  //array of integers along with
  //its size (n)
  public void Populate(int A[], int n)
  {
      //Tree rooted at the first
      //element of the array
      Node root = new Node(A[0]);

      //Insert values into the tree
      for (int i = 1; i < n; i++)
      {
          Insert(root, A[i]);
      }

      //Print tree
      Print(root);

  }

  //Insert a node into the tree
  public void Insert(Node node, int value)
  {
      //If the value to be inserted
      //is less than node value then
      //we either inserts it as the
      //left child if it does not exist
      //or recursively call the insert
      //on the left child if it does exist
      if (value < node.value)
      {
          //Left child exist
          if (node.left != null)
          {
              Insert(node.left, value);
          }
          //Left child does not exist
          //so create it
          else
          {
              node.left = new Node(value);
          }
      }
      //Same reasoning as above but for right
      else if (value > node.value)
      {
          if (node.right != null)
          {
              Insert(node.right, value);
          }
          else
          {
              node.right = new Node(value);
          }
      }
      //Same reasoning as above but for middle
      else
      {
          if (node.middle != null)
          {
              Insert(node.middle, value);
          }
          else
          {
              node.middle = new Node(value);
          }
      }
  }

  public Node Delete(Node node, int value)
  {
      if (node.value > value)
      {
          node.left = Delete(node.left, value);
      }
      else if(node.value < value)
      {
          node.right = Delete(node.right, value);
      }
      else
      {
          if (node.middle != null)
          {
              node.middle = Delete(node.middle, value);
          }
          else if(node.right != null)
          {
              int min = minimum(node.right).value;
              node.value = min;
              node.right = Delete(node.right, min);
          }
          else
          {
              node = node.left;
          }
      }
      return node;
  }

  protected Node minimum(Node node)
  {
      if(node != null)
      {
          while (node.left != null)
          {
              return minimum(node.left);
          }
      }

      return node;
  }

  //Recursive method to print the
  //whole tree
  public void Print(Node root)
  {
      if (root != null)
      {
          System.out.println("Node value : " + root.value);
          Print(root.left);
          Print(root.middle);
          Print(root.right);
      }
  }
}

String to Long in Java

package Question1;

public class StringToLong {

public static void main(String[] args) {

StringToLong _StringToLong = new StringToLong();
_StringToLong.Test();
}

public void Test()
{
long i = 0;
String[] stringArray = {"123", "-123", "9223372036854775807", "-9223372036854775808", "a12" };

for (String obj : stringArray)
{
try
{
i = this.StringToLong(obj);

if (i == Long.parseLong(obj))
{
System.out.println(obj + ": ... success ...");
} else {
System.out.println(obj + ":... failure ...");
}

} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}

public long StringToLong(String str) throws NumberFormatException
{
long result = 0L, digit = 1L;

// Check Negative Number
boolean negativeNumber = false;

if(str.substring(0, 1).equals("-"))
{
negativeNumber = true;
str = str.substring(1);
}

// Check Number or not
if(!str.matches("^[0-9]+$"))
{
throw new NumberFormatException(str + " has Invalid Character");
}

for (int i = str.length() - 1; i >= 0; i--)
{
// number zero = ASCII code 48
result += ((long) str.charAt(i) - 48L) * digit;
digit *= 10;
}

// If Negative Number, go negative
if(negativeNumber)
{
result = -result;
}

return result;
}
}