OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

office message

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]


Subject: [OASIS Issue Tracker] Commented: (OFFICE-2322) NEEDS-DISCUSSION:Input Constraints for Arabic Function



    [ http://tools.oasis-open.org/issues/browse/OFFICE-2322?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150#action_18150 ] 

David Wheeler  commented on OFFICE-2322:
----------------------------------------

All: For your amusement, here is C code that implements the semantics of the ARABIC function,
which translates Roman numerals (e.g. "IV") into Arabic numbers (e.g., 4).
It not only implements the minimum required by the current specification (e.g., it handles "IV"), but it also
handles extensions like "VVVM".

The *primary* purpose of this code is to show that implementing the extended semantics of ARABIC
can be done in one pass and is actually really easy.  So there's certainly no reason to forbid it.

I'm releasing this code under MIT/X license, so anyone can use it for just about any reason.

You can download the file from:
 http://www.dwheeler.com/openformula/arabic.c

I'm *also* including the entire contents of "arabic.c" below,
so that archivers will have this text as it is today.


// Arabic.c - convert Roman numeral into Arabic (traditional) number.
// by David A. Wheeler
// This is a one-pass algorithm that can convert arbitrary sequences of
// Roman numeral digits into a traditional (Arabic) number, including
// bizarre ones like "VVVL".
//
// Released under the MIT/X license:
// Copyright (c) 2010 David A. Wheeler
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// This algorithm accepts these Roman numeral digits, in upper or lower case:
// I 1
// V 5
// X 10
// L 50
// C 100
// D 500
// M 1000
// Whitespace is ignored.
// A "larger" digit value makes all preceding digit values negative.
// Thus, "IX" is (-1)+(10)=9, "IM"=1999, and "VVVX"=-5.
// The empty string is zero; "VVX"=0 as well.

#include <stdio.h>


const roman_symbols = 7;  // Number of distinct roman numeral symbols I...M.

// Returns value of string x; sets *error to 1 if there's an error.
double arabic(char *x, int *error) {
	int i;
	int symbol;                   // Current symbol
	int positives[roman_symbols]; // Count of roman symbols with + values
	int negatives[roman_symbols]; // Count of roman symbols with - values
	*error = 0;
	// Initialize count of symbols seen.
	for (i=0; i<roman_symbols; i++) {
		positives[i] = negatives[i] = 0;
	}

	for (; *x; x++) {  // Examine each character in input.
		switch (*x) {
			case ' ':  // Ignore whitespace.
			case '\t':
			case '\n':
				continue;
			case 'I':
			case 'i':
				symbol = 0;
				break;
			case 'V':
			case 'v':
				symbol = 1;
				break;
			case 'X':
			case 'x':
				symbol = 2;
				break;
			case 'L':
			case 'l':
				symbol = 3;
				break;
			case 'C':
			case 'c':
				symbol = 4;
				break;
			case 'D':
			case 'd':
				symbol = 5;
				break;
			case 'M':
			case 'm':
				symbol = 6;
				break;
			default:  // Error - some other character.
				*error = 1;
				return 0;
				
		}
		// All smaller symbols are now negative:
		for (i=0; i < symbol ; i++) {
			negatives[i] += positives[i];
			positives[i] = 0;
		}
		// Count this symbol:
		positives[symbol]++;
	}
	// We've gone through every character, return the result:
	return       (positives[0]-negatives[0]) +         // "I"
	       5 *   (positives[1]-negatives[1]) +         // "V"
	       10 *  (positives[2]-negatives[2]) +         // "X"
	       50 *  (positives[3]-negatives[3]) +         // "L"
	       100 * (positives[4]-negatives[4]) +         // "C"
	       500 * (positives[5]-negatives[5]) +         // "D"
	       1000 *(positives[6]-negatives[6]);          // "M"
}

// Demo/test
main() {
	double result;
	int  error;
	char input[1024];

	fprintf(stderr, "Your roman numeral?\n");
	fgets(input, sizeof(input), stdin);

	error = 0;
	result = arabic(input, &error);
	if (error)
		printf("ERROR\n");
	else
		printf("%f\n", result);
}



> NEEDS-DISCUSSION: Input Constraints for Arabic Function
> -------------------------------------------------------
>
>                 Key: OFFICE-2322
>                 URL: http://tools.oasis-open.org/issues/browse/OFFICE-2322
>             Project: OASIS Open Document Format for Office Applications (OpenDocument) TC
>          Issue Type: Bug
>          Components: OpenFormula
>            Reporter: Eric Patterson
>            Assignee: Eike Rathke
>             Fix For: ODF 1.2
>
>
> It's unclear in the spec (5.19.2) whether "non-traditional" roman numerals are supported as input strings.  For example, are things like IIIV or IIVI expected to work?  The spec needs more detail here.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://tools.oasis-open.org/issues/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]