Zju2311 inglish-number translator-analog English digit Conversion

Source: Internet
Author: User

Question:

Returns an English word of a number and a corresponding number. For example, "Two hundred twenty two" indicates 222.

Analysis:

First, convert the read string to a numeric array. For example, the preceding strings are saved as 2,100, 20, and 2.

After thinking for a long time, I don't know how to deal with it in a concise way. Later I usedRecursion.

The Int work (int l, int R) function is used to solve the numbers in the range from a [] l to R.

Search for the numbers 100, and in sequence in this interval.

If one digit num is found at the K position, the number represented by this interval is work (L, k-1) × num + work(K + 1, R)

If these three numbers are not found, the number represented by this interval is the sum of all numbers in the interval.

--------------------------------------

/*
Zju2311 inglish-number Translator
*/

# Include <stdio. h>
# Include <string. h>
# Define CLR (a) memset (A, 0, sizeof ())

# Define n 205
# Define M 32

Char num [] [20] = {"negative ",
"Zero", "one", "two", "three", "four ",
"Five", "Six", "Seven", "eight", "Nine ",
"Ten", "Eleven", "Twelve", "Thirteen ",
"Fourteen", "Fifteen", "Sixteen ",
"Seventeen", "Eighteen", "Nineteen ",
"Twenty", "Thirty", "Forty", "ty", "Sixty ",
"Seventy", "Eighty", "Ninety ",
"Hundred", "Thousand", "million "};
Int e [] = {-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 30, 40, 50, 60, 70, 80, 90,
1000000 };

Int ord [] = {100,100 0, 1000000 };

Int getnum (char s []) {
Int I = 0;
While (strcmp (S, num [I]) I ++;
Return e [I];
}

Int local (int A [], int L, int R, int X ){
Int I;
For (I = L; I <= r; I ++) if (a [I] = x) return I;
Return-1;
}

Int work (int A [], int L, int R ){
Int I, J, K, M, T;
For (k = 2; k> = 0; k --){
If (t = Local (A, L, R, ord [k])> = 0 ){
Return work (a, L, t-1) * ord [k] + work (a, t + 1, R );
}
}
M = 0;
For (I = L; I <= r; I ++) m + = A [I];
Return m;
}

Int main ()
{
Int I, J, K, M, N;
Int sum, X, neg;
Int A [n];
Char STR [N], s [N];

CLR (STR );
While (gets (STR), strcmp (STR ,"")){
// Input
N = 0; k = 0;
While (sscanf (STR + k, "% s", S )! = EOF ){
K + = strlen (s) + 1;
A [n ++] = getnum (s );
If (k> = strlen (STR) break;
}

// Work
If (A [0] =-1) sum =-work (A, 1, n-1 );
Else sum = work (a, 0, n-1 );

Printf ("% d/N", sum );
CLR (STR );
}

Return 0;
}

 

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.