UVa139 - Telephone Tangles(水题)

A large company wishes to monitor the cost of phone calls made by its personnel. To achieve this the PABX logs, for each call, the number called (a string of up to 15 digits) and the duration in minutes. Write a program to process this data and produce a report specifying each call and its cost, based on standard Telecom charges.

International (IDD) numbers start with two zeroes (00) followed by a country code (1-3 digits) followed by a subscriber's number (4-10 digits). National (STD) calls start with one zero (0) followed by an area code (1-5 digits) followed by the subscriber's number (4-7 digits). The price of a call is determined by its destination and its duration. Local calls start with any digit other than 0 and are free.

Input

Input will be in two parts. The first part will be a table of IDD and STD codes, localities and prices as follows:

Code   tex2html_wrap_inline45  Locality name$price in cents per minute

where tex2html_wrap_inline45 represents a space. Locality names are 25 characters or less. This section is terminated by a line containing 6 zeroes (000000).

The second part contains the log and will consist of a series of lines, one for each call, containing the number dialled and the duration. The file will be terminated a line containing a single #. The numbers will not necessarily be tabulated, although there will be at least one space between them. Telephone numbers will not be ambiguous.

Output

Output will consist of the called number, the country or area called, the subscriber's number, the duration, the cost per minute and the total cost of the call, as shown below. Local calls are costed at zero. If the number has an invalid code, list the area as ``Unknown'' and the cost as -1.00.

Sample input

088925 Broadwood$81
03 Arrowtown$38
0061 Australia$140
000000
031526        22
0061853279  3
0889256287213   122
779760 1
002832769 5
#

Sample output

tabular28

注意:在计算number时,决定number的还有把code去掉后的字符长度要满足相应的要求

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct Node
{
	string code;
	string name;
	int price;
};

vector<Node> vecCode;

void input()
{
	string s;
	
	while (getline(cin, s), s != "000000") {
		istringstream line(s);
		string code;
		line >> code;
		Node node;
		node.code = code;
		string name = s.substr(code.length() + 1);
		int pos = name.find('$');
		node.name = name.substr(0, pos);
		node.price = atoi(name.substr(pos + 1).data());
		vecCode.push_back(node);
	}
}

void solve()
{
	string s;
	
	cout.precision(2);
	while (getline(cin, s), s != "#") {
		string number;
		int duration;
		istringstream line(s);
		line >> number >> duration;
		
		bool found = false;
		for (size_t i = 0, size = vecCode.size(); i < size; i++) {
			string code = vecCode[i].code;
			int len = code.length();
			bool flag = false;
			if (number.substr(0, len) == code) {
				string suffix;
				if (number.substr(0, 2) == "00") {
					suffix = number.substr(len);
					if (suffix.length() >= 4 && suffix.length() <= 10) flag = true;
				} else if (number[0] == '0') {
					suffix = number.substr(len);
					if (suffix.length() >= 4 && suffix.length() <= 7) flag = true;
				}
				
				if (flag) {
					found = true;
					cout << left << setw(17) << number;
					cout << left << vecCode[i].name;
					cout << right << setw(34 - vecCode[i].name.length()) << suffix;
					cout << right << setw(5) << duration;
					double price = vecCode[i].price;
					cout << right << setw(6) << fixed << price / 100;
					cout << right << setw(7) << fixed << price / 100 * duration;
					cout << endl;
					break;
				}
			}
		}
		
		if (!found) {
			if (number[0] != '0') {
				cout << left << setw(17) << number;
				cout << left << "Local";
				cout << right << setw(29) << number;
				cout << right << setw(5) << duration;
				cout << right << setw(6) << fixed << 0.0;
				cout << right << setw(7) << fixed << 0.0;
				cout << endl;
			} else {
				cout << left << setw(17) << number;
					cout << left << "Unknown";
					cout << right << setw(27) << "";
					cout << right << setw(5) << duration;
					cout << right << setw(6) << "";
					cout << right << setw(7) << fixed << -1.0;
					cout << endl;
			}
		}
	}
}

int main(int argc, char **argv)
{
	#ifndef ONLINE_JUDGE
		ifstream fin("e:\\uva_in.txt");
		streambuf *old = cin.rdbuf(fin.rdbuf());
	#endif
	
	input();
	solve();
	
	#ifndef ONLINE_JUDGE
		cin.rdbuf(old);
	#endif
	
	return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值