Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- char romanian_numbers[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
- int arabic_numbers[7] = {1, 5, 10, 50, 100, 500, 1000};
- char romanian_groups[14] = {'M', 'C', 'D', 'C', 'C', 'X', 'L', 'X', 'X', 'I', 'V', 'I', 'I'};
- int arabic_groups[14] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
- int romanian_number_validator(char number[]){
- int i, j, is_valid, k, l;
- i = 0;
- is_valid = 0; // counter of symbols being romanian symbols
- int counters[7] = {0,0,0,0,0,0,0}; // counter of the occurences of romanian symbols
- while (number[i] != (char)0 ){
- for (j=0; j < 7; j++){
- if(number[i] == romanian_numbers[j]){
- counters[j] += 1;
- is_valid += 1;
- if (j % 2 == 0 && counters[j] > 4){
- return 2; // it means that there is more than 4 of I, X, C or M
- }
- if (j % 2 == 1 && counters[j] > 2){
- return 3; // it means that there is more than 2 of V, L or D
- }
- for (k=j-1; k > 0; k--){
- if (counters[k] > 1){
- return 4; // it means that there is more than one number smaller than
- // number[i] before this number (e.g. CCD)
- }
- }
- }
- }
- i++;
- }
- if (is_valid < i){ // checks if romanian number contains only M,D,C,L,X,V,I
- return 1;
- }
- for (l=0; l < 15; l++){
- if(number[l-3] != (char)0 && number[l-2] != (char)0 && number[l-1] != (char)0){
- if (number[l-3] == number[l] && number[l-2] ==number[l] && number[l-1] == number[l]){
- return 5;
- }
- }
- }
- return 0;
- }
- void validation_messenger(char number[]){
- if (romanian_number_validator(number) == 1){
- printf("Your number contains some not romanian symbols.");
- }
- if (romanian_number_validator(number) == 2){
- printf("Your number contains more than 4 symbols from {I, X, C, M}.");
- }
- if (romanian_number_validator(number) == 3){
- printf("Your number contains more than 2 symbols from {V, L, D}.");
- }
- if (romanian_number_validator(number) == 4){
- printf("Before some number your input contains more than 1 smaller number. e.g. XXC");
- }
- if (romanian_number_validator(number) == 5){
- printf("Your number has 4 the same numbers in a row.");
- }
- else{
- if (romanian_to_arabic(number) == -1){
- printf("You have wrong order of the symbols. e.g. CMD or XCL");
- }
- if (romanian_to_arabic(number) == -2){
- printf("Before and after some number you have smaller one. e.g. XLX");
- }
- if (romanian_to_arabic(number) == -3){
- printf("You have wrong smaller number before greater one.");
- }
- if (romanian_to_arabic(number) == -4){
- printf("You have L, D or V before greater number.");
- }
- }
- }
- int romanian_to_arabic(char number[]){
- int i, j, k;
- int arabic;
- arabic = 0;
- k=0;
- int result[16];
- for (i=0; i<16; i++){ // returns table of int, each int is the value of proper romanian symbol
- for (j=0; j<7; j++){
- if (romanian_numbers[j] == number[i]){
- result[i] = arabic_numbers[j];
- }
- }
- result[i+1] = 0;
- }
- while(result[k] > 0){
- if (result[k] < result[k+1] && result[k+2] != 0){
- if (0.5*(-result[k] + result[k+1]) <= result[k+2]){
- return -1;
- }
- if (result[k] == result[k+2]){
- return -2;
- }
- }
- if (result[k] >= result[k+1]){
- arabic += result[k];
- }
- if (result[k] < result[k+1] && result[k+1] != 0 && result[k] != 0){
- if (result[k] != 5 && result[k] != 50 && result[k] != 500){
- if (result[k] == result[k+1] / 5 || result[k] == result[k+1]/10 ){
- arabic -= result[k];
- }
- else {
- return -3;
- }
- }
- else {
- return -4;
- }
- }
- k++;
- }
- return arabic;
- }
- void arabic_to_romanian(int arabic){
- int i, j;
- char result[20];
- j = 0;
- i = 0;
- for (i=0; i<13; i++){
- while(arabic >= arabic_groups[i]){
- if( i % 2 != 0 ){
- result[j] = romanian_groups[i];
- result[j+1] = romanian_groups[i-1];
- j += 1;
- }
- else{
- result[j] = romanian_groups[i];
- }
- arabic -= arabic_groups[i];
- j++;
- }
- }
- result[j] = (char)0;
- int k = 0;
- while (result[k]!= (char)0){
- printf("%c ", result[k]);
- k++;
- }
- return;
- }
- int main(int argc, char *argv[]){
- const int length = 15; // length of max romanian number (MMMDCCCLXXXVIII) + 1
- char number[length];
- char letter;
- int arabic;
- letter = 'X';
- printf ("This is converter from romanian numbers to arabic or otherwise.\n");
- if (argc == 1){
- printf("Please insert A if you want to convert from arabic to romanian or R otherwise\n");
- scanf("%c", &letter);
- }
- if((argc > 1 && strcmp(argv[1], "R")==0) || letter == 'R'){
- printf ("Please insert romanian number.");
- scanf("%s", number);
- if (romanian_number_validator(number) == 0 && romanian_to_arabic(number) > 0){
- printf("%d", romanian_to_arabic(number));
- }
- else{
- validation_messenger(number);
- printf ("\nTry again.");
- }
- }
- if((argc > 1 && strcmp(argv[1], "A")==0) || letter== 'A'){
- printf ("Please insert arabic number");
- scanf("%d", &arabic);
- if (arabic>3999){
- printf("Please insert number lower than 4000");
- }
- else{
- arabic_to_romanian(arabic);
- }
- }
- if ((argc > 1 && (strcmp(argv[1], "R")!=0) && (strcmp(argv[1], "A")!=0)) && letter == 'X') {
- printf("Please use command line to run program with correct letter: R or A");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement