An unusual addition.

Source: Internet
Author: User
Tags pow readline

Read two positive integers a and B, less than 100, to compute a+b. Notice that each digit of A and b is given by the corresponding English word.
The specific input and output format is provided as follows:
Input format: The test input contains a number of test cases, one for each test case, in the form "A + B =", and two adjacent
The string has a space interval. When A and B are entered at the same time for 0 o'clock, the corresponding results are not output.
Output format: Outputs 1 rows for each test case, that is, the a+b value.

Input Sample:

One + two =
Three four + five six =
Zero Seven + eight nine =
Zero + zero =

Output Sample:

3
90
96

I wrote the code (mainly through the string interception):

C # implementation (with the help of vs2005):

Using System;
Using System.Collections.Generic;
Using System.Text;

Namespace Teststringplus
{
Class Program
{
static void Main (string[] args)
{
String str = Console.ReadLine ();
string strfirst = null;
string strlast = null;
Strfirst=str. Substring (0,str. IndexOf (' + '));
Strlast = str. Substring (str. IndexOf (' + ') +2,str. IndexOf (' = ')-str. IndexOf (' + ')-2);
INT-A, last;
A-i = Int. Parse (Transfertonum (Strfirst). Trim ());
Last=int. Parse (Transfertonum (strlast). Trim ());
if (!= 0 | | | last!= 0)
Console.WriteLine (First+last);

}
static string Transfertonum (string s)
{
string strnum = null;
while (s.length!=0)
{
Strnum + = Getnum (s.substring (0,s.indexof (')));
s = s.substring (S.indexof (") +1,s.length-s.indexof (")-1);
}
return strnum;
}
static string Getnum (string s)
{
Switch (s)
{
Case "Zero":
return "0";
Case "one":
return "1";
Case "two":
Return "2";
Case "three":
Return "3";
Case "Four":
Return "4";
Case "Five":
Return "5";
Case "Six":
return "6";
Case "Seven":
return "7";
Case "Eight":
Return "8";
Case "Nine":
return "9";
Default
return null;
}
}
}
}

Java implementation:

Import java.io.*;
public class teststringplus{
public static void Main (String args[]) {
try{
BufferedReader br=new BufferedReader (New InputStreamReader (system.in));
String Str=br.readline ();
String strfirst = null;
String strlast = null;
Strfirst=str.substring (0,str.indexof (' + '));
Strlast = str.substring (str.indexof (' + ') +2,str.indexof (' = '));
INT-A, last;
A-Integer.parseint (Transfertonum (Strfirst). Trim ());
Last=integer.parseint (Transfertonum (strlast). Trim ());
if (!= 0 | | | last!= 0)
System.out.println (First+last);
}catch (IOException e) {}
}
public static string Transfertonum (string s)
{
String strnum = "";
while (S.length ()!=0)
{
Strnum + = Getnum (s.substring (0,s.indexof (')));
s = s.substring (S.indexof (") +1,s.length ());
}
return strnum;
}
public static string Getnum (string s)
{
if (S.equals ("Zero"))
{
return "0";
}
else if (s.equals ("one")
{
return "1";
}
else if (s.equals ("two"))
{
Return "2";
}
else if (S.equals ("three"))
{
Return "3";
}
else if (S.equals ("four"))
{
Return "4";
}
else if (S.equals ("Five"))
{
Return "5";
}
else if (s.equals ("Six"))
{
return "6";
}
else if (S.equals ("Seven"))
{
return "7";
}
else if (S.equals ("eight"))
{
Return "8";
}
Else
{
return "9";
}
}

}

Code written by my colleague (using command-line arguments to save English numbers to an array)

Java implementation:

public static void Main (string[] args)
{
Boolean lable=true;
int signposition=-1;
int left=0,right=0;
while (lable)
{
if (args.length==0)
{
SYSTEM.ERR.PRINTLN ("Your input is illegal.") Please re-enter: ");
}
Else
{
for (int i=0;i<args.length;i++)
{
if (Args[i].compareto ("+") ==0)
{
Signposition=i;
Break
}
}

int temp=-1;
for (int i=0;i<args.length;i++)
{

if (Args[i].compareto ("zero") ==0)
Temp=0;
else if (Args[i].compareto ("one") ==0)
Temp=1;
else if (Args[i].compareto ("two") ==0)
temp=2;
else if (Args[i].compareto ("three") ==0)
temp=3;
else if (Args[i].compareto ("four") ==0)
temp=4;
else if (Args[i].compareto ("five") ==0)
temp=5;
else if (Args[i].compareto ("six") ==0)
temp=6;
else if (Args[i].compareto ("seven") ==0)
temp=7;
else if (Args[i].compareto ("eight") ==0)
temp=8;
else if (Args[i].compareto ("nine") ==0)
temp=9;

if (i<signposition)
{
Left+=temp*math.pow (signposition-i-1);
}
else if (i>signposition)
{
Right+=temp*math.pow (args.length-i-2);
}

}

System.out.print (left+ "+" +right+ "=" + (left+right));

   }
   lable=false;
   break;
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.