交互式圖表_如何構建羅馬數字轉換器和交互式羅馬數字圖表

交互式圖表

The Roman numerals are no longer an essential part of our daily lives. But we do use them when designing monuments, clocks, and even for sporting events.

羅馬數字不再是我們日常生活中必不可少的部分。 但是我們在設計紀念碑,鐘表甚至體育賽事時都會使用它們。

什么是羅馬數字? (What are the Roman Numerals?)

Roman numerals originated in ancient Rome and remained the common way of writing numbers throughout Europe for many centuries. Their use long outlived the Roman Empire itself. They were gradually replaced by the Hindu-Arabic system of numeration that we use today – the numbers zero through nine.

羅馬數字起源于古羅馬,并且在許多世紀以來一直是整個歐洲書寫數字的常用方式。 他們的使用早已超出了羅馬帝國本身。 它們逐漸被我們今天使用的印度-阿拉伯計數系統所取代-數字從零到九。

Roman numerals are represented by combinations of letters of the Latin alphabet, that serve as digits in this system. But unlike decimal base, with symbols 0 through 9, the the Roman system employs seven capitalized Latin letters I, V, X, L, C, D, M.

羅馬數字由拉丁字母的組合表示,該拉丁字母在此系統中用作數字。 但是與十進制以0到9表示的十進制不同,羅馬系統使用七個大寫拉丁字母I,V,X,L,C,D,M

Originally, there was no single letter designation for zero. Instead, they used the Latin word Nulla, which means "none".

最初,沒有單個字母指定為零。 相反,他們使用拉丁詞Nulla ,意思是“無”。

羅馬數字如何工作? (How do Roman Numerals work?)

The Hindu-Arabic representation of these letters is as follows: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000.

這些字母的印度阿拉伯語表示如下: I = 1,V = 5,X = 10,L = 50,C = 100,D = 500和M = 1000

Other numbers are formed by combining these letters per certain rules: A symbol placed after another of equal or greater value, adds its value.

其他數由這些字母每一定的規則形成的組合:一個符號放置另一個相等或更大的值時,增加了它的價值。

For example, ? VI = V + I = 5 + 1 = 6 or LX = L + X = 50 + 10 = 60. The notations VI and LX are read as "one more than five" and "ten more than fifty".

例如, VI = V + I = 5 +1 = 6LX = L + X = 50 + 10 = 60 VI和LX表示為“大于五個”和“大于十個十”。

A symbol placed before one of greater values subtracts its value. For example, IX = X - I = 10 - 1 = 9, and XC = C - X = 100 - 10 = 90.

位于較大值之一前面的符號減去其值。 例如, IX = X-I = 10-1 = 9,XC = C-X = 100-10 = 90

The notations IX and XC are read as "one less than ten" and "ten less than a hundred."

標記IX和XC分別表示為“少于十個”和“少于十個十”。

Numbers greater than 1,000 are formed by placing a dash over the symbol. Thus V? = 5,000, X? = 10,000, L? = 50,000, C? = 100,000, D? = 500,000 and M? = 1,000,000.

大于1,000的數字是通過在符號上放置一個破折號而形成的。 因此, V + = 5,000X + = 10,000L + = 50,000C + = 100,000D + = 500,000M + = 1,000,000

The so called "standard" form disallows using the same symbol more than three times in a row. But occasionally, exceptions can be seen. For example, IIII for number 4, VIIII for number 9, and LXXXX for 90.

所謂的“標準”格式不允許同一符號連續使用三遍以上。 但是偶爾也可以看到例外。 例如,IIII代表4,VIIII代表9,LXXXX代表90。

羅馬數字及其組合的互動圖表 (An Interactive Chart of Roman Numerals and Their Combinations)

Hover over each symbol to reveal its Hindu-Arabic equivalent:

將鼠標懸停在每個符號上,以顯示其等效的印度-阿拉伯語:

I wrote the code for this interactive Roman numeral chart to embed here on freeCodeCamp News.

我為這個交互式羅馬數字圖表編寫了代碼,并將其嵌入在freeCodeCamp News上。

Given the fact that the HTML embed feature is not a full scale code editor, the given code is not structured and presented as separate HTML, CSS, and JavaScript files. Rather it is written as a single HTML file with <style> and <script> elements added for styling and functionality.

考慮到HTML嵌入功能不是完整的代碼編輯器,因此給定的代碼沒有結構化并以單獨HTML,CSS和JavaScript文件的形式呈現。 相反,它是作為單個HTML文件編寫的,其中添加了<style><script>元素以用于樣式和功能。

Here is the full code repository for my interactive Roman Numeral Chart.

這是我的交互式羅馬數字圖表的完整代碼存儲庫 。

羅馬數字轉換器 (Roman Numeral Converter)

Enter a non negative integer between 0 and 5,000. Then click Convert to reveal a Roman numeral equivalent.

輸入介于0到5,000之間的非負整數。 然后單擊轉換以顯示等效的羅馬數字。



There is no programmatic limitation to the number 5,000 or beyond. The algorithm that governs the conversion would work all the same.

5,000或以上的人數沒有編程限制。 控制轉換的算法將完全相同。

The space required to display Roman numeral equivalents of large numbers grows larger and larger without much added benefit of revealing something new.

顯示大數的羅馬數字等效項所需的空間越來越大,而沒有揭示新事物的更多好處。

The code itself consists of an HTML part describing the content with inline styles for ease of interaction and added JavaScript for functionality.

該代碼本身包括一個HTML部分,該部分以內聯樣式描述內容以簡化交互,并添加了JavaScript以實現功能。

The is an input element of the type "number" to limit input data to numeric values and two buttons. The "Convert" button is wired to the function that performs the conversion, and the "Display" button outputs the Roman number equivalent.

是輸入類型“數字”,用于將輸入數據限制為數值和兩個按鈕。 “轉換”按鈕連接到執行轉換的功能,“顯示”按鈕輸出等效的羅馬數字。

Why output through a button element? Styling worked well when applied to both buttons together. And considering the limited functionality of the embed, it seemed like a time saver.

為什么通過按鈕元素輸出? 一起應用于兩個按鈕時,樣式效果很好。 考慮到嵌入的功能有限,這似乎節省了時間。

For clarity, these elements are assigned to variables:

為了清楚起見,將這些元素分配給變量:

const inputField = document.querySelector('input'); // input element
const convertButton = document.getElementById('convert'); // convert button
const outputField = document.getElementById('display'); // output element

Function convertToRoman() contains the logic and renders the result:

函數convertToRoman()包含邏輯并呈現結果:

function convertToRoman() {let arabic = document.getElementById('arabicNumeral').value; // input valuelet roman = '';  // variable that will hold the result
}

The numerical value from input element is saved in a variable called "arabic" for further testing. The variable named "roman" will hold the string representing Roman equivalent of Arabic input.

輸入元素的數值保存在名為“ arabic ”的變量中,以進行進一步測試。 名為“ roman ”的變量將保存表示與阿拉伯文輸入法等效的字符串。

Next, there are two arrays of equal lengths, one holding Arabic numerals and one holding their Roman counterparts. Both are in descending order to simplify subtraction:

接下來,有兩個等長的數組,一個數組包含阿拉伯數字,一個數組包含羅馬數字。 兩者均為降序以簡化減法:

// descending order simplifies subtraction while looping
const arabicArray = [5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] 
const romanArray = ['V&#773;', 'MV&#773;','M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']

Unicode tables help with forming symbols greater than 1,000.

Unicode表有助于形成大于1,000的符號。

Finally, here is the logic that test the inputted number and converts it.

最后,這是測試輸入的數字并將其轉換的邏輯。

if (/^(0|[1-9]\d*)$/.test(arabic)) {// Regular expression testsif (arabic == 0) {// for decimal points and negativeoutputField.innerHTML = "Nulla"; // signs} else if (arabic != 0) {for (let i = 0; i < arabicArray.length; i++) {while (arabicArray[i] <= arabic) {roman += romanArray[i];arabic -= arabicArray[i];}}outputField.innerHTML = roman;}
} else {outputField.innerHTML ="Please enter non negative integers only. No decimal points.";
}

The first test checks for decimal points and negative signs. If found, the message asks to "enter non-negative integers only."

第一個測試檢查小數點和負號。 如果找到,該消息將要求“僅輸入非負整數”。

The next test checks whether the number entered equals zero. In such a case, the string "Nulla" is displayed.

下一個測試檢查輸入的數字是否等于零。 在這種情況下,將顯示字符串“ Nulla”。

Otherwise, the loops keep concatenating Roman strings while subtracting Arabic numbers until the latter satisfies the condition for the while loop. Then it displays the Roman equivalent of the user input.

否則,循環將繼續連接羅馬字符串,同時減去阿拉伯數字,直到阿拉伯數字滿足while循環的條件為止。 然后顯示與用戶輸入等效的羅馬字。

Just like with the interactive chart, the code for the Roman Numeral Converter is all set for you to copy it and embed it into any article. Here's the full code repository.

就像交互式圖表一樣,Roman Numeral Converter的代碼已全部設置好,可以將其復制并嵌入到任何文章中。 這是完整的代碼存儲庫 。

翻譯自: https://www.freecodecamp.org/news/roman-numeral-converter-interactive-roman-numerals-chart/

交互式圖表

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390257.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390257.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390257.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python 08 面向對象

Python 面向對象 1、編程范式 2、面向對象特性 3、屬性、方法 4、三大特性 5、高級方法 6、類的特殊成員方法 7、反射 8、異常處理 9、單例模式 一、編程范式 編程&#xff1a;程序員用特定的語法數據結構算法組成的代碼來告訴計算機如何執行任務的過程 &#xff0c; 實現一個…

eclipse手動添加SVN插件

https://www.cnblogs.com/hcl1991/p/5888461.html 1.手動下載svn插件&#xff08;百度SVNsite-1.8.18&#xff09; 2.將下載好的SVNsite-1.8.18.zip 解壓 3.在eclipse安裝目錄的plugins新建SVN文件夾 4.將SVNsite-1.8.18解壓包下的features和plugins拷貝到新建的SVN文件夾下 5.…

440. 字典序的第K小數字

440. 字典序的第K小數字 給定整數 n 和 k&#xff0c;找到 1 到 n 中字典序第 k 小的數字。 注意&#xff1a;1 ≤ k ≤ n ≤ 109。 示例 : 輸入: n: 13 k: 2 輸出: 10 解釋: 字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]&#xff0c;所以第二小的數字是…

微信小程序 設置背景占滿整個頁面

微信小程序,默認的根節點是<page></page>&#xff0c;所有內容都包裹在這個標簽里&#xff0c;如何讓頁面的背景占滿整個屏幕&#xff1f;&#xff1f; <page><view class"bg">....</view> </page> .bg {background-image:ur…

udemy下載課程無法播放_最好的Udemy Web開發課程+熱門免費課程

udemy下載課程無法播放Heres a list of some of the most popular web development courses on Udemy:以下是Udemy上一些最受歡迎的Web開發課程的列表&#xff1a; Best General Web Development Courses on Udemy:關于Udemy的最佳常規Web開發課程&#xff1a; The Complete …

滲透測試初學者_滲透測試許可證:面向初學者的道德黑客課程

滲透測試初學者A penetration test is an authorized cyberattack on a computer system, performed to evaluate the security of the system. 滲透測試是對計算機系統的授權網絡攻擊&#xff0c;旨在評估系統的安全性。 Weve released a full pentesting course on the free…

Codeforces 915 E Physical Education Lessons

題目描述 This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is …

JDK 11 還有一個處于計劃階段的 JEP:讓其支持 TLS 1.3

開發四年只會寫業務代碼&#xff0c;分布式高并發都不會還做程序員&#xff1f; >>> JDK 11 最近有什么消息&#xff1f;我們不妨來看一下它的進展情況&#xff0c;包括最新的 JEP 提案。Java 的新版本發布計劃意味著總會有一款新的 JDK 即將推出。根據他們的計劃&a…

498. 對角線遍歷

498. 對角線遍歷 給定一個含有 M x N 個元素的矩陣&#xff08;M 行&#xff0c;N 列&#xff09;&#xff0c;請以對角線遍歷的順序返回這個矩陣中的所有元素&#xff0c;對角線遍歷如下圖所示。 示例: 輸入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 輸出: [1,2,4,7,5…

軟件測試測試用例編寫_不要先編寫所有軟件測試-只需編寫一個

軟件測試測試用例編寫Test Driven Development (TDD) is sometimes described as “writing tests first”. The TDD mantra states that we should not write code before we have written automated tests that exercise that code. Writing code first is considered subopt…

練習五

1.團隊模式和團隊的開發模式有什么關系&#xff1f; 答&#xff1a; 首先我來解釋一下這兩個名詞&#xff1a; 我查資料了解了一下&#xff0c;團隊模式&#xff0c;更偏向于多人合作的那種&#xff0c;而且我理解的“團隊”會是一種多人合作的情況下&#xff0c;長期磨合后的一…

Squid 訪問控制配置

Squid 訪問控制配置 主配置文件內加入限制參數 vim /etc/squid/squid.conf # 訪問控制 acl http proto HTTP # 限制訪問 good_domain添加兩個域名“.”表示半解析(相當于*) acl good_domain dstdomain .kevin.net .baidu.com # 允許good_domain內的域名訪問 http_access allow …

劍指 Offer 62. 圓圈中最后剩下的數字

0,1,,n-1這n個數字排成一個圓圈&#xff0c;從數字0開始&#xff0c;每次從這個圓圈里刪除第m個數字&#xff08;刪除后從下一個數字開始計數&#xff09;。求出這個圓圈里剩下的最后一個數字。 例如&#xff0c;0、1、2、3、4這5個數字組成一個圓圈&#xff0c;從數字0開始每…

rust 編程入門_面向初學者的Rust –最受歡迎的編程語言入門

rust 編程入門Rust has been voted Stack Overflow’s most loved programming language for five years in a row. This article will tell you why Rust is awesome.Rust已連續五年被評為Stack Overflow最受歡迎的編程語言。 本文將告訴您為什么Rust很棒。 Rust is a system…

【轉載】springboot:如何優雅的使用mybatis

這兩天啟動了一個新項目因為項目組成員一直都使用的是mybatis&#xff0c;雖然個人比較喜歡jpa這種極簡的模式&#xff0c;但是為了項目保持統一性技術選型還是定了 mybatis。到網上找了一下關于spring boot和mybatis組合的相關資料&#xff0c;各種各樣的形式都有&#xff0c;…

創建react應用程序_通過構建電影搜索應用程序在1小時內了解React

創建react應用程序If youve been meaning to learn React but are unsure of where to start, Scrimbas brand new Build a Movie Search App course is perfect for you! 如果您一直想學習React&#xff0c;但是不確定從哪里開始&#xff0c;那么Scrimba全新的Build a Movie S…

GeoServer自動發布地圖服務

1 NetCDF氣象文件自動發布案例 GeoServer是一個地理服務器&#xff0c;提供了管理頁面進行服務發布&#xff0c;樣式&#xff0c;切片&#xff0c;圖層預覽等一系列操作&#xff0c;但是手動進行頁面配置有時并不滿足業務需求&#xff0c;所以GeoServer同時提供了豐富的rest接口…

selenium+ python自動化--斷言assertpy

前言&#xff1a; 在對登錄驗證時&#xff0c;不知道為何原因用unittest的斷言不成功&#xff0c;就在網上發現這個assertpy&#xff0c;因此做個筆記 準備&#xff1a; pip install assertypy 例子&#xff1a; 1 from assertpy import assert_that2 3 4 def check_login():5 …

11. 盛最多水的容器

11. 盛最多水的容器 給你 n 個非負整數 a1&#xff0c;a2&#xff0c;…&#xff0c;an&#xff0c;每個數代表坐標中的一個點 (i, ai) 。在坐標內畫 n 條垂直線&#xff0c;垂直線 i 的兩個端點分別為 (i, ai) 和 (i, 0) 。找出其中的兩條線&#xff0c;使得它們與 x 軸共同構…

深入理解ES6 pdf

下載地址&#xff1a;網盤下載目錄 第1章 塊級作用域綁定 1var聲明及變量提升&#xff08;Hoisting&#xff09;機制 1塊級聲明 3-- let聲明 3-- 禁止重聲明 4-- const聲明 4-- 臨時死區&#xff08;Temporal Dead Zone&#xff09; 6循環中的塊作用域綁定 7-- 循環中的函…