⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 romannum.cpp

📁 帮别人写的一个罗马数字运算的程序. 比较简单.
💻 CPP
字号:
#include <iostream.h> 
#include <string.h> 
#define MAX 32

class romanType 
{ 
protected: 
	char romanNum[MAX]; //存放罗马数字

public: 
	romanType()
	{
		for(int i=0;i<MAX;i++)
			romanNum[i]='\0';
	}
	romanType(char ch[MAX])
	{
		for(int i=0;i<MAX;i++)
			romanNum[i]='\0';
		strcpy(romanNum,ch);
	}
	int romanToDecimal(); //罗马数字转化为阿拉伯数字 
	void decimalToRoman(int n); //阿拉伯数字转化为罗马数字 
 
	friend istream& operator >> (istream &input,romanType &d);// 重载输入流
	friend ostream& operator << (ostream &output,romanType &d);//重载输出流
}; 


istream& operator >> (istream& input,romanType &d) //输入
{
	//cout<<"please input the roman number:"<<endl;
	for(int i=0;i<MAX;i++)
		d.romanNum[i]='\0';
	input>>d.romanNum;
	return input;
}

ostream& operator << (ostream &output,romanType &d) //输出
{
	output<<d.romanNum;
	return output;
}

int romanType::romanToDecimal() //将罗马数字转换为十进制的阿拉伯数字
{
	int length,i;
	int num=0;
	char ch=' '; //用于记录上次是什么字母, 用于处理IV IX的问题
	length=strlen(romanNum); 
	for(i=0;i<length;i++) 
	{ 
		if(romanNum[i]=='I') 
		{ 
			num+=1;
		} 
		else if(romanNum[i]=='V') 
		{ 
			if(ch=='I')
			{	
				num-=1;
				num+=4;
			}
			else
				num+=5;
		} 
		else if(romanNum[i]=='X') 
		{ 
			if(ch=='I')
			{	
				num-=1;
				num+=9;
			}
			else
				num+=10; 
		} 
		else if(romanNum[i]=='L')
			num+=50;
		else if(romanNum[i]=='C')
			num+=100;
		else if(romanNum[i]=='D')
			num+=500;
		else if(romanNum[i]=='M')
			num+=1000;

		ch=romanNum[i];
	} 

	return num;
} 

void romanType::decimalToRoman(int n)//将十进制的阿拉伯数字转换为罗马数字
{
	int a=0,b=0; //a记结果 b记余数
	int index=0; //记录写入的位数

	int i;

	char temp[MAX];
	for(i=0;i<MAX;i++)//清空掉数据
	{
		romanNum[i]='\0';
		temp[i]='\0';
	}

	//先除个1000看看
	a=n/1000;
	b=n%1000;

	for(i=0;i<a;i++)
	{
		temp[index]='M';
		index++;
	}

	//除个500看看
	a=b/500;
	b=b%500;

	for(i=0;i<a;i++)
	{
		temp[index]='D';
		index++;
	}

	//除个100看看
	a=b/100;
	b=b%100;

	for(i=0;i<a;i++)
	{
		temp[index]='C';
		index++;
	}

	//除个50看看
	a=b/50;
	b=b%50;

	for(i=0;i<a;i++)
	{
		temp[index]='L';
		index++;
	}

	//除个10看看
	a=b/10;
	b=b%10;

	for(i=0;i<a;i++)
	{
		temp[index]='X';
		index++;
	}
	
	//最后的数字是10以下了, 懒得想那么多, 直接对应出来
	switch(b)
	{
	case 1:
		temp[index]='I';
		index++;
		break;
	case 2:
		temp[index]='I';
		index++;
		temp[index]='I';
		index++;
		break;
	case 3:
		temp[index]='I';
		index++;
		temp[index]='I';
		index++;
		temp[index]='I';
		index++;
		break;
	case 4:
		temp[index]='I';
		index++;
		temp[index]='V';
		index++;
		break;
	case 5:
		temp[index]='V';
		index++;
		break;
	case 6:
		temp[index]='V';
		index++;
		temp[index]='I';
		index++;
		break;
	case 7:
		temp[index]='V';
		index++;
		temp[index]='I';
		index++;		
		temp[index]='I';
		index++;
		break;
	case 8:
		temp[index]='V';
		index++;
		temp[index]='I';
		index++;		
		temp[index]='I';
		index++;
		temp[index]='I';
		index++;
		break;
	case 9:
		temp[index]='I';
		index++;
		temp[index]='X';
		index++;
		break;
	}

	strcpy(romanNum,temp);
}



class extRomanType : public romanType//应题目要求, 从romanType继承出新类, 加入运算操作
{
public:
	extRomanType()
	{
		for(int i=0;i<MAX;i++)
			romanNum[i]='\0';
	}
	extRomanType(char ch[MAX])
	{
		for(int i=0;i<MAX;i++)
			romanNum[i]='\0';
		strcpy(romanNum,ch);
	}
	extRomanType operator + (extRomanType &d); //重载 + 
	extRomanType operator - (extRomanType &d); //重载 - 
	extRomanType operator * (extRomanType &d); //重载 * 
	extRomanType operator / (extRomanType &d); //重载 /
	extRomanType operator ++();//前置
	extRomanType operator ++(int);//后置
	extRomanType operator --();
	extRomanType operator --(int);
};

extRomanType extRomanType::operator + (extRomanType &d) //加法
{
	extRomanType tmp;
	int a=romanToDecimal()+d.romanToDecimal();//十进制相加
	tmp.decimalToRoman(a);//十进制转罗马字符
	return tmp;
}

extRomanType extRomanType::operator - (extRomanType &d) //减法
{
	extRomanType tmp;
	int a,b,c;
	b=romanToDecimal();
	c=d.romanToDecimal();
	if(b<c)
	{
		cout<<"Because the first number is smaller than the second, the numbers cannot be subtracted"<<endl;
		a=0;//出错给个0值,因为罗马数字是没有0的, 这里你也可以做其它处理
	}
	else
		a=b-c;//十进制相减

	tmp.decimalToRoman(a);//十进制转罗马字符
	return tmp;
}

extRomanType extRomanType::operator * (extRomanType &d) //乘法
{
	extRomanType tmp;
	int a,b,c;
	b=romanToDecimal();
	c=d.romanToDecimal();
	a=b*c;//十进制相乘
	tmp.decimalToRoman(a);//十进制转罗马字符
	return tmp;
}

extRomanType extRomanType::operator / (extRomanType &d) //除法
{
	extRomanType tmp;
	int a,b,c;
	b=romanToDecimal();
	c=d.romanToDecimal();
	if(b<c)
	{
		cout<<"Because the first number is smaller than the second, the numbers cannot be divided"<<endl;
		a=0;//出错给个0值,因为罗马数字是没有0的, 这里你也可以做其它处理
	}
	else
		a=b/c;//十进制相除

	tmp.decimalToRoman(a);//十进制转罗马字符
	return tmp;
}

extRomanType extRomanType::operator ++ () //增量,前置
{
	int a=romanToDecimal();
	a++;
	decimalToRoman(a);//十进制转罗马字符
	return *this;
}

extRomanType extRomanType::operator ++ (int) //增量,后置
{
	extRomanType tmp(extRomanType::romanNum);//这里会调用拷贝构造函数进行对象的复制工作 
	int a=romanToDecimal();
	a++;
	decimalToRoman(a);//十进制转罗马字符
	return tmp;
}

extRomanType extRomanType::operator -- () //减量,前置
{
	int a=romanToDecimal();
	a--;
	decimalToRoman(a);//十进制转罗马字符
	return *this;
}

extRomanType extRomanType::operator -- (int) //减量,后置
{
	extRomanType tmp(extRomanType::romanNum);//这里会调用拷贝构造函数进行对象的复制工作 
	int a=romanToDecimal();
	a--;
	decimalToRoman(a);//十进制转罗马字符
	return tmp;
}

int main()

{

        extRomanType num1("XXXIV");

        extRomanType num2("XV");

        extRomanType num3;

 

        cout << "num1 = " << num1 << endl;

        cout << "num2 = " << num2 << endl;

        cout << "num1 + num2 = " << num1 + num2 << endl;

        cout << "num1 * num2 = " << num1 * num2 << endl;

 

        cout << "Enter two numbers in Roman format: ";

        cin >> num1 >> num2;

        cout << endl;


        cout << "num1 = " << num1 << endl;

        cout << "num2 = " << num2 << endl;

        num3 = num1 + num2;

        cout << "num1 + num2: num3 = " << num3 << endl;

        num3 = num1 - num2;

        cout << "num1 - num2: num3 = " << num3 << endl;

        num3 = num1 * num2;

        cout << "num1 * num2: num3 = " << num3 << endl;

        num3 = num1 / num2;

        cout << "num1 / num2: num3 = " << num3 << endl;
 
        cout << "num1 = " << num1 << endl;
        cout << "--num1: " << --num1 << endl;

        cout << "num1 = " << num1 << endl;
        cout << "num1--: " << num1-- << endl;

        cout << "num1 = " << num1 << endl;
        cout << "++num1: " << ++num1 << endl;

        cout << "num1 = " << num1 << endl;
        cout << "num1++: " << num1++ << endl;
 

        return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -