1. Составить программу, которая выводит на экран римское представление введенного числа в диапазоне от 1 до 4000. (изпользуя case)
Ответы
Ответ:
#include <iostream>
#include <string>
using namespace std;
void main()
{
int numb;
int copy_numb;
int rozrad = 1;
int k;
string result;
do
{
cout << "Input number: ";
cin >> numb;
} while (numb<0 || numb > 4000);
copy_numb = numb;
while (rozrad!=0)
{
rozrad *= 10;
k = (copy_numb%rozrad) / (rozrad/10);
switch (rozrad)
{
case 10:
if (k < 4){
for (int i = 0; i < k; i++){
result += 'I';
}
}
else if (k == 4){
result += "IV";
}else if (k == 5){
result += 'V';
}
else if (k == 6){
result += "VI";
}
else if (k == 7){
result += "IIV";
}
else if (k == 8){
result += "IIIV";
}
else if (k == 9){
result += 'XI';
}break;
case 100:
if (k < 4) {
for (int i = 0; i < k; i++) {
result += 'X';
}
}
else if (k == 4) {
result += "LX";
}
else if (k == 5) {
result += 'L';
}
else if (k == 6) {
result += "XL";
}
else if (k == 7) {
result += "XXL";
}
else if (k == 8) {
result += "XXXL";
}
else if (k == 9) {
result += 'CX';
}break;
case 1000:
if (k < 4) {
for (int i = 0; i < k; i++) {
result += 'C';
}
}
else if (k == 4) {
result += "DC";
}
else if (k == 5) {
result += 'D';
}
else if (k == 6) {
result += "CD";
}
else if (k == 7) {
result += "CCD";
}
else if (k == 8) {
result += "CCCD";
}
else if (k == 9) {
result += 'MC';
}break;
case 10000:
for (int i = 0; i < k; i++) {
result += 'M';
}
default:
rozrad = 0;
break;
}
}
cout << "Result: ";
for (int i = result.size() - 1; i >= 0; i--)
{
cout << result[i];
}
}
Case читает розряд числа