|
Компьютерный форум OSzone.net » Программирование, базы данных и автоматизация действий » Программирование и базы данных » C/C++ - [решено] Римский калькулятор на C++ |
|
C/C++ - [решено] Римский калькулятор на C++
|
Новый участник Сообщения: 3 |
Профиль | Отправить PM | Цитировать Приветствую уважаемые программеры. Помогите пожалуйста, нужен римский калькуль на С++. Времени уже ооочень мало осталось. Сам изучить не успею. Код обычного калькулятора есть. Help.
|
|
Отправлено: 22:10, 18-05-2009 |
Новый участник Сообщения: 3
|
Профиль | Отправить PM | Цитировать Всё, в помощи больше не нуждаюсь, если нужен код - пишите.
|
Отправлено: 23:18, 18-05-2009 | #2 |
Для отключения данного рекламного блока вам необходимо зарегистрироваться или войти с учетной записью социальной сети. Если же вы забыли свой пароль на форуме, то воспользуйтесь данной ссылкой для восстановления пароля. |
Будем жить, Маэстро... Сообщения: 6694
|
Профиль | Сайт | Отправить PM | Цитировать Цитата slavutych:
|
|
------- Отправлено: 11:20, 19-05-2009 | #3 |
Ветеран Сообщения: 881
|
Профиль | Отправить PM | Цитировать Ага нужен, давай. Очень интересная задача.
|
------- Отправлено: 12:01, 19-05-2009 | #4 |
Новый участник Сообщения: 3
|
Профиль | Отправить PM | Цитировать По вышеизложенным просьбам выкладываю сам калькулятор.
#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; } |
|
Отправлено: 18:52, 20-05-2009 | #5 |
Будем жить, Маэстро... Сообщения: 6694
|
Профиль | Сайт | Отправить PM | Цитировать slavutych, Молодец! Работает и считает правильно.
|
------- Отправлено: 19:22, 20-05-2009 | #6 |
Ветеран Сообщения: 512
|
Профиль | Отправить PM | Цитировать Бляха муха , что засмотрелся на этот код, понял отрывочно и только первые пару тройку строчек, но просмотрел полностью, строка зав строкой и понял, какой он все таки красивый и организованный по сути себя остается этим только восторгаться, спасибо slavutych, благодаря ему все таки рещил хоть маленькую рограмму да написать, хоть и переписать но пару строчек самому вручную написать в той программе, которая получится, спасибо.
|
------- Отправлено: 21:47, 23-05-2009 | #7 |
Участник сейчас на форуме | Участник вне форума | Автор темы | Сообщение прикреплено |
| |||||
Название темы | Автор | Информация о форуме | Ответов | Последнее сообщение | |
Калькулятор трупа | Greyman | Юмор | 78 | 04-06-2012 18:52 | |
C/C++ - Калькулятор на С++ | PRaetorian | Программирование и базы данных | 23 | 31-05-2011 12:26 | |
C/C++ - Калькулятор | hhh8486 | Программирование и базы данных | 5 | 11-04-2011 01:18 | |
Калькулятор WinStyle | prokazzza | Проект WinStyle | 47 | 19-02-2011 22:45 |
|