Share 13. roman to integer c++,

doanzoom

New member
#C ++, #Roman to Integer, #Integer, #Programming ** 13.Roman to Integer trong C ++ **

Số La Mã là một hệ thống các số viết sử dụng các chữ cái từ bảng chữ cái Latin.Chúng vẫn được sử dụng ngày nay trong một số bối cảnh, chẳng hạn như trên đồng hồ và trong các tiêu đề của một số bộ phim và chương trình truyền hình.Tuy nhiên, chúng có thể khó chuyển đổi thành số thập phân, đó là lý do tại sao chúng ta cần một bộ chuyển đổi Roman sang Integer.

Trong hướng dẫn này, chúng tôi sẽ học cách chuyển đổi chữ số La Mã thành số nguyên trong C ++.Chúng tôi sẽ sử dụng các bước sau:

1. Xác định một hàm để chuyển đổi một chữ số La Mã thành một số nguyên.
2. Viết một vòng lặp để lặp lại các ký tự trong chuỗi chữ số La Mã.
3. Sử dụng câu lệnh chuyển đổi để kiểm tra ký tự hiện tại và chuyển đổi nó thành một số nguyên.
4. Thêm số nguyên được chuyển đổi vào tổng giá trị.
5. Trả lại tổng giá trị.

Dưới đây là mã cho bộ chuyển đổi Roman sang Integer trong C ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

// có chức năng chuyển đổi một chữ số La Mã thành một số nguyên
int romantointeger (char romannumeral) {
// Tuyên bố chuyển đổi để kiểm tra ký tự hiện tại và chuyển đổi nó thành một số nguyên
Switch (Romannumeral) {
trường hợp 'i':
trả lại 1;
trường hợp 'v':
trả lại 5;
trường hợp 'x':
trả lại 10;
trường hợp 'l':
trả lại 50;
trường hợp 'c':
trả lại 100;
trường hợp 'd':
trả lại 500;
trường hợp 'm':
trả lại 1000;
}

// Trả về -1 nếu nhân vật không phải là chữ số La Mã hợp lệ
trả lại -1;
}

// Chức năng chuyển đổi chuỗi chữ số La Mã thành một số nguyên
int romantoint (chuỗi romannumeral) {
// Khởi tạo tổng giá trị
int TotalValue = 0;

// lặp lại các ký tự trong chuỗi số La Mã
for (int i = 0; i <romannumeral.length (); i ++) {
// Nhận nhân vật hiện tại
char romannumerchar = romannumeral ;

// Chuyển đổi ký tự hiện tại thành một số nguyên
int romannumeralvalue = Romantointeger (Romannumeralar);

// Kiểm tra xem ký tự tiếp theo có phải là chữ số La Mã nhỏ hơn không
if (i <romannumeral.length () - 1 && romannumeralValue <romantointeger (romannumeral [i + 1])) {
// trừ giá trị số La Mã hiện tại khỏi tổng giá trị
TotalValue -= RomannumeralValue;
} khác {
// Thêm giá trị số La Mã hiện tại vào tổng giá trị
TotalValue += RomannumeralValue;
}
}

// Trả về tổng giá trị
trả lại TotalValue;
}

int main () {
// Nhận chuỗi chữ số La Mã từ người dùng
chuỗi Romannumeral;
cout << "Nhập một chữ số La Mã:";
CIN >> Romannumeral;

// Chuyển đổi chuỗi số La Mã thành một số nguyên
int integer = romantoint (romannumeral);

// In số nguyên được chuyển đổi
cout << "Giá trị số nguyên là:" << Số nguyên << endl;

trả lại 0;
}
`` `

Dưới đây là 5 hashtag mà bạn có thể sử dụng cho bài viết này:

* #C ++
* #Roman to Integer
* #Integer
* #Programming
* #tutorial
=======================================
#C++, #Roman to Integer, #Integer, #Programming **13. Roman to Integer in C++**

Roman numerals are a system of writing numbers using letters from the Latin alphabet. They are still used today in some contexts, such as on clocks and in the titles of some movies and TV shows. However, they can be difficult to convert to decimal numbers, which is why we need a Roman to Integer converter.

In this tutorial, we will learn how to convert Roman numerals to integers in C++. We will use the following steps:

1. Define a function to convert a single Roman numeral to an integer.
2. Write a loop to iterate over the characters in the Roman numeral string.
3. Use a switch statement to check the current character and convert it to an integer.
4. Add the converted integer to the total value.
5. Return the total value.

Here is the code for the Roman to Integer converter in C++:

```c++
#include <iostream>

using namespace std;

// Function to convert a single Roman numeral to an integer
int romanToInteger(char romanNumeral) {
// Switch statement to check the current character and convert it to an integer
switch (romanNumeral) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}

// Return -1 if the character is not a valid Roman numeral
return -1;
}

// Function to convert a Roman numeral string to an integer
int romanToInt(string romanNumeral) {
// Initialize the total value
int totalValue = 0;

// Iterate over the characters in the Roman numeral string
for (int i = 0; i < romanNumeral.length(); i++) {
// Get the current character
char romanNumeralChar = romanNumeral;

// Convert the current character to an integer
int romanNumeralValue = romanToInteger(romanNumeralChar);

// Check if the next character is a smaller Roman numeral
if (i < romanNumeral.length() - 1 && romanNumeralValue < romanToInteger(romanNumeral[i + 1])) {
// Subtract the current Roman numeral value from the total value
totalValue -= romanNumeralValue;
} else {
// Add the current Roman numeral value to the total value
totalValue += romanNumeralValue;
}
}

// Return the total value
return totalValue;
}

int main() {
// Get the Roman numeral string from the user
string romanNumeral;
cout << "Enter a Roman numeral: ";
cin >> romanNumeral;

// Convert the Roman numeral string to an integer
int integer = romanToInt(romanNumeral);

// Print the converted integer
cout << "The integer value is: " << integer << endl;

return 0;
}
```

Here are 5 hashtags that you can use for this article:

* #C++
* #Roman to Integer
* #Integer
* #Programming
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top