当前位置:网站首页>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
 Insert picture description here
because 8 and 14 Less than 100, So go straight to the stack . Insert picture description here
And the next one 1000 Greater than 100, To carry out while loop
 Insert picture description here

take 8 and 14 From the stack pop come out , And then sum Value stack
 Insert picture description here
Next 20 and 2 All less than 100;
Stack in turn
 Insert picture description here
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

原网站

版权声明
本文为[bolite]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020543428306.html