|
|
|
| Нужен код программы, переводящей число, записанное арабскими цифрами в число, записанное римскими цифрами. В соответствии со следующим: I это 1, V – 5, X – 10, L – 50, M – 1000. | |
|
|
|
|
 1.4 Кб |
|
|
для: Ks0RkSs
(01.11.2008 в 17:21)
| | Вот код | |
|
|
|
|
|
|
|
для: Lipton
(06.11.2008 в 20:10)
| | нашёл ещё другое решение
// written by Ste Cork, free for any and all use.
//
const char *Number_AsRomanString( int iNumber )
{
struct RomanDigit_t
{
char *m_psString;
int m_iValue;
};
static const RomanDigit_t RomanDigits[]=
{
{"M", 1000},
{"CM", 900},
{"D", 500},
{"CD", 400},
{"C", 100},
{"XC", 90},
{"L", 50},
{"XL", 40},
{"X", 10},
{"IX", 9},
{"V", 5},
{"IV", 4},
{"I", 1},
};
// Strictly speaking, Roman digits can't display something
// such as 4999 without using overlaid bars and so forth,
// but for now this is a quick-and-dirty piece of code that'll
// just keep using M's...
//
static char sRomanString[20];
sRomanString[0] = '\0';
for (int i=0; iNumber && i<sizeof(RomanDigits)/
sizeof(RomanDigits[0]); i++)
{
while ( RomanDigits[i].m_iValue <= iNumber )
{
strcat( sRomanString, RomanDigits[i].m_psString );
iNumber -= RomanDigits[i].m_iValue;
}
}
return sRomanString;
} |
Добавил простую функцию main()
и получились примерно такие ошибки
str.c: In function `Number_AsRomanString':
str.c:9: error: syntax error before "RomanDigits"
str.c: At top level:
str.c:32: error: conflicting types for 'sRomanString'
str.c:31: error: previous declaration of 'sRomanString' was here
str.c:32: error: invalid initializer
str.c:32: warning: data definition has no type or storage class
str.c:34: error: syntax error before "for"
Непойму не хватает-ли чего-то компилятору , или здесь ошибка ? | |
|
|
|
|
|
|
|
для: Ks0RkSs
(01.11.2008 в 17:21)
| | Забыли 100(C) и 500(D) =) | |
|
|
|
|
|
|
|
для: bronenos
(06.12.2008 в 15:45)
| | создал проект как C++
добавил
#include <conio.h>
#include <stdio.h>
#include <string.h>
и всё-равно ошибка
stri.cpp: In function `const char* Number_AsRomanString(int)':
stri.cpp:36: error: invalid conversion from `const char*' to `char'
жалуется на строку sRomanString[0] = "\0"; | |
|
|
|
|
|
|
|
для: exp
(06.12.2008 в 16:06)
| | Вроде разрешилось :)
заменил на строку sRomanString[0] = 0x00;
Интересно почему такой баг у DevCpp, или гдето надо искать где отключается | |
|
|
|
|
|
|
|
для: exp
(06.12.2008 в 16:14)
| | Двойные кавычки - это char* (строка), а одинарные - это char (символ), так что '\0' надо, либо 0x00 | |
|
|
|
|
|
|
|
для: Ks0RkSs
(01.11.2008 в 17:21)
| | Вот нашол компилирующийся код в досовском Борланде:
#include <stdio.h>
#include <conio.h>
#include <string.h>
char *a2roman (int value, char *c1, char *c2, char *c3);
int main (void)
{
int arabicalNumeral = 1;
int result;
char roman[15] = "";
do
{
clrscr();
textcolor(GREEN);
cprintf("..::Programma dlya preobrazovaniyz arabskih chisel v rimskie::..");
cprintf("\n\r================================================================");
textcolor(RED);
cprintf("\n\n\rVvedite chislo ot 1 do 3000: ");
scanf ("%d", &arabicalNumeral);
}
while ((arabicalNumeral < 1) || (arabicalNumeral > 3000));
if ((arabicalNumeral <= 3000) && (arabicalNumeral >= 1000))
{
result = arabicalNumeral / 1000;
strcat (roman, a2roman(result, "M", " ", " "));
arabicalNumeral -= (result * 1000);
}
if ((arabicalNumeral < 1000) && (arabicalNumeral >= 100))
{
result = arabicalNumeral / 100;
strcat (roman, a2roman(result, "C", "D", "M"));
arabicalNumeral -= (result * 100);
}
if ((arabicalNumeral < 100) && (arabicalNumeral >= 10))
{
result = arabicalNumeral / 10;
strcat (roman, a2roman(result, "X", "L", "C"));
arabicalNumeral -= (result * 10);
}
if ((arabicalNumeral < 10) && (arabicalNumeral >= 1))
{
strcat (roman, a2roman(arabicalNumeral, "I", "V", "X"));
}
textcolor(LIGHTBLUE);
cprintf("Eto chiclo v rimskom napisanii: %s\n\n", roman);
textcolor(YELLOW);
cprintf("\n\r ...Press any key to exit... ");
getch();
return 0;
}
|
| |
|
|
|
|
автор: Aertyom141z (05.03.2020 в 20:18) |
|
|
для: Ks0RkSs
(01.11.2008 в 17:21)
| | #include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
int chislo;
cout << "Введите число:" << endl;
cin >> chislo;
int I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000;
int i = 0, m = 0, d = 0, c = 0, l = 0, x = 0, v = 0, i1 = 0;
for (int i = chislo; i >= M; i = i - M) {
m++;
}
for (int i = chislo - m * M; i >= D; i = i - D) {
d++;
}
for (int i = chislo - m * M - d * D; i >= C; i = i - C) {
c++;
}
for (int i = chislo - m * M - d * D - c * C; i >= L; i = i - L) {
l++;
}
for (int i = chislo - m * M - d * D - c * C - l * L; i >= X; i = i - X) {
x++;
}
for (int i = chislo - m * M - d * D - c * C - l * L - x * X; i >= V; i = i - V) {
v++;
}
for (int i = chislo - m * M - d * D - c * C - l * L - x * X - v * V; i >= I; i--) {
i1++;
}
for (int i = 0; i <= m - 1; i++) {
cout << "M";
}
cout << "Римское число: ";
if (d == 1 && c == 4) {
cout << "CM";
}
else {
for (int i = 0; i <= d - 1; i++) {
cout << "D";
}
}
if (d == 1 && c == 4) {
c = 0;
}
else if (c == 4) {
cout << "CD";
}
else {
for (int i = 0; i <= c - 1; i++) {
cout << "C";
}
}
if (l == 1 && x == 4) {
cout << "XC";
}
else {
for (int i = 0; i <= l - 1; i++) {
cout << "L";
}
}
if (l == 1 && x == 4){
x = 0;
}
else if (x == 4) {
cout << "XL";
}
else {
for (int i = 0; i <= x - 1; i++) {
cout << "X";
}
}
if (v == 1 && i1 == 4){
cout << "IX";
}
else {
for (int i = 0; i <= v - 1; i++) {
cout << "V";
}
}
if (v == 1 && i1 == 4) {
i1 = 0;
}
else if (i1 == 4) {
cout << "IV";
}
else {
for (int i = 0; i <= i1 - 1; i++) {
cout << "I";
}
}
} | |
|
|
|