Treceți la conținutul principal

transformare litere romane in cifre arabe cpp

#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;


class RomanNumeral
{
public:
    const static int M = 1000;
    const static int D = 500;
    const static int C = 100;
    const static int L = 50;
    const static int X = 10;
    const static int V = 5;
    const static int I = 1;

    RomanNumeral( const int arabic ) :
    m_roman( "" ),
    m_arabic( ((arabic > 0) ? arabic : 0) ) 
    {
 if( arabic > 0 )
 {
     int i = arabic;
     while( i > 0 )
     {
  if( ( i - M ) >= 0 )
  {
      m_roman += "M";
      i -= M;
      continue;
  }
  if( ( i - D ) >= 0 )
  {
      m_roman += "D";
      i -= D;
      continue;
  }
  if( ( i - C ) >= 0 )
  {
      m_roman += "C";
      i -= C;
      continue;
  }
  if( ( i - L ) >= 0 )
  {
      m_roman += "L";
      i -= L;
      continue;
  }
  if( ( i - X ) >= 0 )
  {
      m_roman += "X";
      i -= X;
      continue;
  }
  if( ( i - V ) >= 0 )
  {
      m_roman += "V";
      i -= V;
      continue;
  }
  if( ( i - I ) >= 0 )
  {
      m_roman += "I";
      i -= I;
      continue;
  }
     }
 }
 else
 {
     m_roman = "0";
 }
    }

    RomanNumeral( const std::string& string ) :
    m_roman( ((string.size() > 0 ) ? string : "0" ) ),
    m_arabic( 0 )
    {
 int i = 0;
 while( i < (int)string.size() )
 {
     char c = string[i++];
     switch( c )
     {
  case 'M':
  case 'm':
      m_arabic += M;
      break;
  case 'D':
  case 'd':
      m_arabic += D;
      break;
  case 'C':
  case 'c':
      m_arabic += C;
      break;
  case 'L':
  case 'l':
      m_arabic += L;
      break;
  case 'X':
  case 'x':
      m_arabic += X;
      break;
  case 'V':
  case 'v':
      m_arabic += V;
      break;
  case 'I':
  case 'i':
      m_arabic += I;
      break;
  default:
      throw new std::out_of_range( "Not a valid Roman numeral!" );
      break;
     }
 }
    } 
 
    int getArabic()
    {
 return m_arabic;
    }
    void setArabic( const int arabic );
    const std::string& getRoman()
    {
 return m_roman;
    }
protected:
    std::string m_roman;
    int m_arabic;
};

int main()
{
    cout << "Welcome to the Roman numeral to Arabic numeral converter!\nPlease enter a number in Roman numerals to be converted: ";
    
    std::string roman;
    cin >> roman;
    try
    {
 RomanNumeral rn( roman );
 cout << "Roman Numeral: " << roman << " Arabic: " << rn.getArabic() << endl;
    }
    catch( exception* ex )
    {
 cout << roman << " " << ex->what() << endl;
    }

    cout << "Now enter a Arabic number to be converted to Roman numerals: ";
    int arabic;
    cin >> arabic;
    try
    {
 RomanNumeral rn( arabic );
 cout << "Arabic number: " << arabic << " Roman: " << rn.getRoman() << endl;
    }
    catch( ... )
    {
 cout << "error during processing...too bad, I'm outta here!" << endl;
    }

 system("pause");

    return 0;
}

Comentarii

Postări populare de pe acest blog

program principal cpp

#include "clasa.h" #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define DELAY 9000000 void delay() { for(long i=0;i<DELAY;i++); } //constructor cu initializare de la tastatura BigInt::BigInt() {char x; signed char t[400]; int i; printf("\nNumarul cu semn "); do s=getche(); while((s!='+')&&(s!='-')); n=0; do {x=getche(); t[n]=x-'0'; n++; } while((x>='0')&&(x<='9')); n--; for(i=0;i<n;i++) nr[i]=t[n-i-1]; } //constructor cu initializare prin parametri BigInt::BigInt(char semn,signed char numar[],int dim) {int i; s=semn; n=dim; for(i=0;i<n;i++) nr[i]=numar[n-i-1]; } //transform un int negativ in pozitiv int BigInt::Pozitiv(int x) {int a,vb; a=0; vb=0; while(vb==0) if((x+a)==0) vb=1; else a=a+1; x=a; return x; } //constructor dintr-un nr int obisnuit BigInt::BigInt(int x) {int i; if(x>=0) s='+'…

NUMERE PRIME ALGORITM C++

// NUMERE PRIME ALGORITM C++//  reediting from scratch //on this page is just the study for a next algoritm for generating the parime nr series like Fibonnaci or ....if possibile

74111121313417374414124343447 if u know the red part you can generate the orange part
1 0 1 111112222 1 1 23

o aplicatie php localitati romania

//APLICATIA SE REFERA LA BAZA DE DATE SIRUTA

//dragtable.js


/* dragtable v1.0 June 26, 2008 Dan Vanderkam, http://danvk.org/dragtable/ http://code.google.com/p/dragtable/ \Bsortabledraggable\B Instructions: - Download this file - Add <script src="dragtable.js"></script> to your HTML. - Add class="draggable" to any table you might like to reorder. - Drag the headers around to reorder them. This is code was based on: - Stuart Langridge's SortTable (kryogenix.org/code/browser/sorttable) - Mike Hall's draggable class (http://www.brainjar.com/dhtml/drag/) - A discussion of permuting table columns on comp.lang.javascript Licensed under the MIT license. */ // Here's the notice from Mike Hall's draggable script: //***************************************************************************** // Do not remove this notice. // // Copyright 2001 by Mike Hall. // See http…