Programalama > C++


Ort. 0
Puan ver:
#include <stdlib.h>     //cout ,cin
#include <iostream.h>   //EXIT_SUCCESS
#include <string.h>     //strlen
#define MAXSTRING 25
int main(void) {

    int  value,tmp,len,TwoMultiple;
    int i=0;
    char Roman[MAXSTRING],Result[MAXSTRING]="";; 
	int Arabicnum = 0;
	cout<<"Roman: ";
    cin>>Roman;
	len=strlen(Roman);                                     
    tmp =0;
    for (i =len-1 ; i >= 0; i--) {
		switch ( Roman[i]) {
				case 'M' :                      
				value = 1000;
					if (Roman[i-1]=='I' || Roman[i-1]=='V' || Roman[i-1]=='X' || Roman[i-1]=='L' || Roman[i-1]=='D' || Roman[i-3]=='M' || (Roman[i-1]=='C' && Roman[i+1]=='C')) {
						cout<<"You entered an invalid romen number.Please try again"<<endl;
						return EXIT_FAILURE;
					}
					break;
				case 'D' :
                value = 500;
					if (Roman[i-1]=='I' || Roman[i-1]=='V' || Roman[i-1]=='X' || Roman[i-1]=='L' ||  Roman[i-2]=='C' || Roman[i-1]=='D' || (Roman[i-1]=='C' && Roman[i+1]=='C')) {
					cout<<"You entered an invalid romen number.Please try again."<<endl;
					return EXIT_FAILURE;
					}
					break;
                case 'C':
                value = 100;
					if (Roman[i-1]=='I' || Roman[i-1]=='V' || Roman[i-1]=='L' || Roman[i-2]=='X' || (Roman[i-3]=='C' && Roman[i-2]=='C') || (Roman[i-1]=='X' && Roman[i+1]=='X')) {
						cout<<"You entered an invalid romen number.Please try again."<<endl;
						return EXIT_FAILURE;
					}
					break;
                case 'L':
                value = 50;
					if (Roman[i-1]=='I' || Roman[i-1]=='V' || Roman[i-1]=='L' || Roman[i-2]=='X') {
						cout<<"You entered an invalid romen number.Please try again."<<endl;
						return EXIT_FAILURE;
					}
					break;
				case 'X':
                value = 10;
					if (Roman[i-1]=='V' || Roman[i-2]=='I' || (Roman[i-1]=='X' && Roman[i-3]=='X') || (Roman[i-1]=='I' && Roman[i+1]=='I')) {
						cout<<"You entered an invalid romen number.Please try again."<<endl;
						return EXIT_FAILURE;
					} 
				    break;
				case 'V' :
                value = 5;
					if (Roman[i-1]=='V' ||Roman[i-2]=='V'|| Roman[i-2]=='I' || Roman[i-3]=='I' || (Roman[i-1]=='I' && Roman[i+1]=='I')) {
						cout<<"You entered an invalid romen number.Please try again."<<endl;
						return EXIT_FAILURE;
					}
					break;
                case 'I':
                value = 1;
					if (Roman[i-1]=='I' && Roman[i-3]=='I') {
						cout<<"You entered an invalid romen number.Please try again."<<endl;
						return EXIT_FAILURE;
					}
					break;
				default :
					cout<<"You entered an unacceptable value"<<endl;
					return EXIT_FAILURE;
					break;
			}
          if (value >= tmp) {
          Arabicnum += value;
          } else {
          Arabicnum -= value;
          }
          tmp = value;
       }
           cout<<"Arabic: "<< Arabicnum <<endl;
		   cout<<"Result: "<< 2*Arabicnum <<" ";
                 TwoMultiple=Arabicnum*2;   
	                 i=0;           
				 while (TwoMultiple > 0) {
                            if (TwoMultiple>= 1000) {                              
								Result[i]='M';
								TwoMultiple-= 1000;
                               i++;
                              } 
							else if (TwoMultiple >= 900) {                              
								Result[i]='C';
								Result[i+1]='M';
								TwoMultiple-= 900;
								i+=2;							   
                              } 
							else if (TwoMultiple >= 500) {                              
								Result[i]='D';
								TwoMultiple-= 500;
								i++;
                              } 
							else if (TwoMultiple >= 400) {                              
								Result[i]='C';
								Result[i+1]='D';
								TwoMultiple-= 400;
								i+=2;
                              } 
							else if (TwoMultiple >= 100) {
								TwoMultiple-= 100;
								Result[i]='C';
								i++;
                              } 
							else if (TwoMultiple >= 90) {                              
								Result[i]='X';
								Result[i+1]='C';
								TwoMultiple-= 90;
								i+=2;
                              } 
							else if (TwoMultiple >= 50) {                             
								Result[i]='L';
								TwoMultiple-= 50;
								i++;
                              } 
							else if (TwoMultiple >= 40) {                              
								Result[i]='X';
								Result[i+1]='L';
								TwoMultiple -= 40;
								i+=2;
                              } 
							else if (TwoMultiple >= 10) {                              
								Result[i]='X';
								TwoMultiple-= 10;
								i++;
                              } 
							else if (TwoMultiple>= 9)  {                             
								Result[i]='I';
								Result[i+1]='X';
								TwoMultiple -= 9;
								i+=2;
                              } 
							else if (TwoMultiple >= 5) {                              
								Result[i]='V';
								TwoMultiple -= 5;
								i++;
                              } 
							else if (TwoMultiple >= 4) {
								Result[i]='I';
								Result[i+1]='V';
								TwoMultiple-= 4;
								i+=2;
                              } 
							else {                             
								Result[i]='I';
								TwoMultiple -= 1;
								i++;
                              }
                      }
				cout<< Result <<endl;
			    return EXIT_SUCCESS;
}


Yorumlar                 Yorum Yaz
Bu hazır kod'a ilk yorumu siz yapın!
KATEGORİLER
ASP - 240
ASP.NET - 24
C# - 75
C++ - 174
CGI - 8
DELPHI - 247
FLASH - 49
HTML - 536
PASCAL - 246
PERL - 11
PHP - 160
WML - 9
XML - 2
Copyright © 2002 - 2024 Hazır Kod - Tüm Hakları Saklıdır.
Siteden yararlanırken gizlilik ilkelerini okumanızı tavsiye ederiz.
hazirkod.com bir İSOBİL projesidir.