Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Программирование и базы данных (http://forum.oszone.net/forumdisplay.php?f=21)
-   -   [решено] Римский калькулятор на C++ (http://forum.oszone.net/showthread.php?t=140478)

slavutych 18-05-2009 22:10 1121843

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

slavutych 18-05-2009 23:18 1121908

Всё, в помощи больше не нуждаюсь, если нужен код - пишите. :)

Drongo 19-05-2009 11:20 1122147

Цитата:

Цитата slavutych
если нужен код - пишите. »

Нужен, давайте. :yes: :)

Diseased Head 19-05-2009 12:01 1122202

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

slavutych 20-05-2009 18:52 1123348

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

Код:

#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 20-05-2009 19:22 1123372

slavutych, Молодец! :up: Работает и считает правильно.

dima1981 23-05-2009 21:47 1125921

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


Время: 16:06.

Время: 16:06.
© OSzone.net 2001-