当前位置:网站首页>English digital converter
English digital converter
2022-06-11 05:03:00 【bolite】
English digital converter
STL in map,stack and string The use of
describe :
In this case , Will give you one or more integers in English . Your task is to convert these numbers into integer representations . The numbers range from -999,999,999 To 999,999,999. Here is a detailed list of English words that your program must consider :
negative, zero, one, two, three, four,five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen,fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty,sixty, seventy, eighty, ninety, hundred, thousand, million
Input
The input includes several samples , Be careful :
1. Negative numbers are preceded by the word negative
2. When available thousand When , Will not be used hundred. for example 1500 Write as "one thousand five hundred", instead of "fifteen hundred".
Input will end with a blank line
Output
The output will be each individual line , Each one is followed by a newline character
The sample input
six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two
Sample output
6
-729
1000101
814022
#include<iostream>//c++ The header file
#include<map>//STL in map The header file
#include<string>//string The header file
#include<stack>//STL in stack The header file
#define N 10000
using namespace std;
map<string, int>m;// Create a map
stack<int>zb;// Build a stack
void creatmap();// build map The mapping of
int ctoi(int* data, int n);// take data The number in the array becomes a target number
int main()// The main function
{
creatmap();
string s;
int data[N] = {
0 };
while (getline(cin, s)) {
// Read one line of English
string str;
int way = 0;// Record data The current length of the array
int now = 0;// Record the trailing subscript of the truncated letter
int front = 0;// Record the head subscript of the truncated letter
int i = 0;
int d = 0;// There is no space before the first letter , It is necessary to distinguish which letter is truncated
for (i = 0; i <= s.size(); i++) {
if (s[i] == ' ') {
now = i;
if (!d) str = s.substr(front, now);// Cut off the English paragraphs representing relevant figures
else str = s.substr(front+1, now-front-1);
data[way++] = m[str];// Use mapping to convert English into numbers and save them in data Array
d++;
}
front = now;
}
if (!d)str = s.substr(front, i);// The last English word is not processed in the loop , It must be recycled to supplement the treatment
else str = s.substr(front + 1, i-front-1);
data[way++] = m[str];
int aim = ctoi(data, way);// Enter the function of data composition
cout << aim << endl;// Output the synthesized number
}
return 0;
}
void creatmap()// Build the required mapping
{
m["zero"] = 0;
m["one"] = 1;
m["two"] = 2;
m["three"] = 3;
m["four"] = 4;
m["five"] = 5;
m["six"] = 6;
m["seven"] = 7;
m["eight"] = 8;
m["nine"] = 9;
m["ten"] = 10;
m["eleven"] = 11;
m["twelve"] = 12;
m["thirteen"] = 13;
m["fourteen"] = 14;
m["fifteen"] = 15;
m["sixteen"] = 16;
m["seventeen"] = 17;
m["eighteen"] = 18;
m["nineteen"] = 19;
m["twenty"] = 20;
m["thirty"] = 30;
m["forty"] = 40;
m["fifty"] = 50;
m["sixty"] = 60;
m["seventy"] = 70;
m["eighty"] = 80;
m["ninety"] = 90;
m["hundred"] = 100;
m["thousand"] = 1000;
m["million"] = 1000000;
m["negative"] = -1;
}
int ctoi(int* data, int n)
{
int i = 0;
int sum = 0;
int flag = 1;// Judge whether the number is negative
if (data[0] == -1) {
flag = -1;
zb.push(data[1]);// Put the first data on the stack
i = 2;
}
else {
zb.push(data[0]);// Put the first data on the stack
i = 1;
}
for (i; i < n; i++) {
// Loop stack , Out of the stack
sum = 0;
if (data[i] >= 100) {
// If the data is greater than or equal to 100, Need to distinguish
while (zb.size()&&zb.top() < data[i]) {
sum += zb.top();
zb.pop();
}
zb.push(sum * data[i]);
}
else {
// Less than 100 Direct stack , wait for data The next of is greater than 100 The number of
zb.push(data[i]);
}
}
sum = 0;
while (zb.size()) {
// Add up all the data in the stack to get the final result
sum += zb.top();
zb.pop();
}
return sum * flag;
}
about int ctoi(int* data, int n) Function of , Let's describe it in the form of a diagram
take (eight hundred fourteen thousand twenty two 814022) For example
1.data The contents of the array 
because 8 and 14 Less than 100, So go straight to the stack .
And the next one 1000 Greater than 100, To carry out while loop 
take 8 and 14 From the stack pop come out , And then sum Value stack 
Next 20 and 2 All less than 100;
Stack in turn 
Last, last , Add all the data in the stack to get the result
Because the title is out of date , I wonder if I can pass the test point completely , If you find something wrong , Please tell me in the comments area , thank you
边栏推荐
- C language test question 3 (advanced program multiple choice questions _ including detailed explanation of knowledge points)
- Following the wave of lack of core, Lianrui launched a number of gigabit network card solutions
- Lr-link Lianrui fully understands the server network card
- Simple knowledge distillation
- tensorflow1. X and tensorflow2 Conversion of X
- How to purchase 25g optical network card
- New product release: Lianrui launched a dual port 10 Gigabit bypass network card
- Leetcode question brushing series - mode 2 (datastructure linked list) - 83:remove duplicates from sorted list
- MySQL regularly deletes expired data.
- Let me tell you how to choose a 10 Gigabit network card
猜你喜欢

MySQL regularly deletes expired data.

Network security construction in 5g Era

Codesys get System Time

An adaptive chat site - anonymous online chat room PHP source code

PHP phone charge recharge channel website complete operation source code / full decryption without authorization / docking with the contract free payment interface

6 questions to ask when selecting a digital asset custodian

Sealem finance builds Web3 decentralized financial platform infrastructure

Lianrui electronics made an appointment with you with SIFA to see two network cards in the industry's leading industrial automation field first

RGB image histogram equalization and visualization matlab code

KD-Tree and LSH
随机推荐
Win10+manjaro dual system installation
Differences between the four MQ
Yolact paper reading and analysis
Google drive download failed, network error
Codesys get System Time
ROS compilation error: could not find a package configuration file provided by "XXX“
[Transformer]AutoFormerV2:Searching the Search Space of Vision Transformer
【入门级基础】Node基础知识总结
MySQL nested sorting: first sort and filter the latest data, and then customize the sorting of this list
力扣(LeetCode)161. 相隔为 1 的编辑距离(2022.06.10)
Share | guide language image pre training to achieve unified visual language understanding and generation
董明珠称“格力手机做得不比苹果差”哪里来的底气?
Simple knowledge distillation
Course design summary
Lr-link Lianrui fully understands the server network card
免费数据 | 新库上线 | CnOpenData全国文物商店及拍卖企业数据
Iris dataset - Introduction to machine learning
Detailed explanation of network security bypass network card
Huawei device configuration bgp/mpls IP virtual private network command
Minor problems encountered in installing the deep learning environment -- the jupyter service is busy