C/C++ - [решено] Римский калькулятор на C++

Ответить
Аватара пользователя
slavutych

C/C++ - [решено] Римский калькулятор на C++

Сообщение slavutych »

Приветствую уважаемые программеры. Помогите пожалуйста, нужен римский калькуль на С++. Времени уже ооочень мало осталось. Сам изучить не успею. Код обычного калькулятора есть. Help. Изображение
Аватара пользователя
slavutych

Re: C/C++ - [решено] Римский калькулятор на C++

Сообщение slavutych »

Всё, в помощи больше не нуждаюсь, если нужен код - пишите. Изображение
Аватара пользователя
Diseased Head

Re: C/C++ - [решено] Римский калькулятор на C++

Сообщение Diseased Head »

Ага нужен, давай. Очень интересная задача.
Аватара пользователя
slavutych

Re: C/C++ - [решено] Римский калькулятор на C++

Сообщение slavutych »

По вышеизложенным просьбам выкладываю сам калькулятор. Изображение



Код: Выделить всё

#include <iostream>
using namespace std;
#include <stdlib.h>
void instructUser();
int getRomanConvertToInt(int n);
void outputInt(int num);
char getOperator();
int doOp(int num1, int num2, char oper);
void outputRomanNumeralResult(int result, char oper);
bool doAnotherCalculation(void);
int main()
{
int num1, num2, result;
bool again;
char oper;
instructUser();
do
{
num1=getRomanConvertToInt(1);
outputInt(num1);
num2=getRomanConvertToInt(2);
outputInt(num2);
oper=getOperator();
result=doOp(num1, num2, oper);
outputRomanNumeralResult(result, oper);
outputInt(result);
again=doAnotherCalculation();
}while (again);
system("PAUSE");
return 0;
}
void instructUser()
{
cout << "This program does arithmetic calculations using additive Roman numeral\n";
cout << "notation. The program will repeatedly prompt you for 2 Roman numeral\n";
cout << "numbers and an operator and then calculate and print the result. In\n";
cout << "entering a Roman numeral number do not leave any space in front of the\n";
cout << "number, use only the letters:\n";
cout << "M (=1000), D (= 500), C (= 100), L (= 50), X (= 10), V (= 5), I (= 1)\n";
cout << "in upper case and in that order, and follow the number immediately by <ENTER>.\n";
cout << "In entering the operator do not leave any space before the operator. Enter\n";
cout << "either +, *(for multiplication) as the operator, and immediately follow\n";
cout << "the operator by <ENTER>. Similarly when responding to the question about\n";
cout << "whether you want to do another calculation enter Y or N in uppercase with\n";
cout << "no space in front and immediately followed by <ENTER>.\n" << "\n";
}
/*****************************************************************************************
The function getRomanConvertToInt prompts for and reads the characters that are entered
for the Roman numeral number and converts them to an intger value returned as the value
of the function. The parameter n should be either 1 or 2 depending on which number is
being inputted. It is used in the prompting message.
*****************************************************************************************/
int getRomanConvertToInt(int n) 
{
char ch;
int num=0;
cout << "Enter Roman Numeral Number" << n << ": "; 
cin.get(ch); //get function inputs next character into ch even if it is a new-line character
while(ch != '\n') //Input loop where the new-line character is the sentinel
{
if(ch == 'M') 
num += 1000; 
else if (ch == 'D') 
num += 500;
else if (ch == 'C') 
num += 100;
else if (ch == 'L') 
num += 50;
else if (ch == 'X') 
num += 10;
else if (ch == 'V') 
num += 5;
else if (ch == 'I') 
num += 1;
else 
cout << ch << " is a bad character for a Roman number. It is being ignored\n";
cin.get(ch);
}
return num;
}
void outputInt(int num)
{
cout << " = " << num << "\n";
}
char getOperator()
{
char oper;
cout << "Enter an operator (+, -, *): ";
cin >> oper;
return oper;
}
int doOp(int num1, int num2, char oper)
{
int numTotal;
if (oper=='+')
{
numTotal=num1+num2;
}
else if (oper=='*')
{
numTotal=num1*num2;
}
else if (oper=='-')
{
numTotal=num1-num2;
}
return numTotal;
}
void outputRomanNumeralResult(int result, char oper)
{
int n, x=0, numMs=0, numDs=0, numCs=0, numLs=0, numXs=0, numVs=0, numIs=0;
n = result;
while (n >= 1000)
{
numMs=n/1000; 
n%=1000;
}
while ((n < 1000) && (n >= 500))
{
numDs=n/500;
n%=500;
}
while ((n < 500) && (n >= 100))
{
numCs=n/100;
n%=100;
}
while ((n < 100) && (n >= 50))
{
numLs=n/50;
n%=50;
}
while ((n < 50) && (n >= 10))
{
numXs=n/10;
n%=10;
}
while ((n < 10) && (n >= 5))
{
numVs=n/5;
n%=5;
}
while ((n < 5) && (n >= 1))
{
numIs=n/1;
n%=1;
}
cout << "\n" << "Number1 " << oper << " Number2 = ";
while (x!=numMs)
{
cout << "M";
x++;
}
x=0;
while (x!=numDs)
{
cout << "D";
x++;
}
x=0;
while (x!=numCs)
{
cout << "C";
x++;
}
x=0;
while (x!=numLs)
{
cout << "L";
x++;
}
x=0;
while (x!=numXs)
{
cout << "X";
x++;
}
x=0;
while (x!=numVs)
{
cout << "V";
x++;
}
x=0;
while (x!=numIs)
{
cout << "I";
x++;
}
cout << "\n";
}
bool doAnotherCalculation()
{
char response;
cout << "\nDo you want to do another calculation?\n";
cout << "Enter Y (yes) or N (no): ";
cin >> response;
if (response == 'Y')
{
return true;
}
else if (response == 'N')
{
return false;
}
return false;
}
Аватара пользователя
Drongo

Re: C/C++ - [решено] Римский калькулятор на C++

Сообщение Drongo »

slavutych, Молодец! Изображение Работает и считает правильно.
Аватара пользователя
dima1981

Re: C/C++ - [решено] Римский калькулятор на C++

Сообщение dima1981 »

Бляха муха , что засмотрелся на этот код, понял отрывочно и только первые пару тройку строчек, но просмотрел полностью, строка зав строкой и понял, какой он все таки красивый и организованный по сути себя остается этим только восторгаться, спасибо slavutych, благодаря ему все таки рещил хоть маленькую рограмму да написать, хоть и переписать но пару строчек самому вручную написать в той программе, которая получится, спасибо.
Ответить

Вернуться в «Программирование и базы данных»