定义了两个结构体,分别通话记录和用户记录,要注意字符串的比较方式,用户id的长度(24),记录的最高数量(1000) 字符串与字符数组的转化, 数据的保留精度问题(2位小数)。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<iostream>
int fee[25];
//存放输入的通话信息
struct customer
{
char id[30];
int MM, dd, HH, mm;
int status= -1;
}record[1010] ;
//存放用户的完整电话记录
struct customer_bee
{
int MM;
char id[30];
char one_bee_record[50][50];
double sum = 0.0;
}customer_bee[1010];
//对输入的通话信息进行排序
bool cmp(customer a, customer b)
{
int s = strcmp(a.id, b.id);
if (s != 0) return s < 0;
else if (a.MM != b.MM) return a.MM < b.MM;
else if (a.dd != b.dd) return a.dd < b.dd;
else if (a.HH != b.HH) return a.HH < b.HH;
else if (a.mm != b.mm) return a.mm < b.mm;
}
//比较两份记录,获得单次完整记录的资费(单位元)和时长(分钟)
std::pair<int, double> get_ans(customer a, customer b)
{
double money = 0.0;
int total_time = 0;
while (a.MM < b.MM || a.dd < b.dd || a.HH < b.HH || a.mm < b.mm)
{
total_time++;
money += fee[a.HH];
a.mm++;
if (a.mm == 60) { a.mm = 0; a.HH++; }
if (a.HH == 24) { a.HH = 0; a.dd++; }
if (a.dd == 31) { a.dd = 0; a.MM++; }
}
money /= 100;
std::pair<int ,double> result = std::make_pair(total_time, money);
return result;
}
int main()
{ //存资费
for (int i = 0; i < 24; i++)
{
std::cin >> fee[i];
}
int record_number;
std::cin >> record_number;
char line[10];
//存通话记录
for (int i = 0; i < record_number; i++)
{
scanf("%s %d:%d:%d:%d %s", record[i].id, &record[i].MM, &record[i].dd, &record[i].HH, &record[i].mm, line);
if (strcmp(line,"on-line") == 0) { record[i].status = 1; }
else {
record[i].status = 0;
}
}
//对通话记录进行排序
std::sort(record, record + record_number, cmp);
//对符合要求的通话记录,写入用户的完整电话记录中
for (int i = 0; i < record_number; i++)
{
if (strcmp(record[i].id,record[i + 1].id) == 0 && record[i].status == 1 && record[i + 1].status == 0)
{
int j=0, k = 0;
//如果用户记录的id不为空 && 通话记录的id和用户记录id不匹配,用户记录加一
while (customer_bee[j].id[0] != '\0' && strcmp(record[i].id, customer_bee[j].id) != 0) { j++; }
//符合要求的记录复制id
strcpy(customer_bee[j].id, record[i].id);
//获得单次的通话记录的费用和时长
std::pair<int ,double>result = get_ans(record[i], record[i + 1]);
double money = result.second;
int total_time = result.first;
//如果当前用户记录的通话记录不为空,用户通话记录加一
while (customer_bee[j].one_bee_record[k][0] != '\0' ) { k++; }
//将用户通话记录转存为字符数组
char str_money[15];
sprintf(str_money,"%.2lf", money);
char str_dd[5], str_HH[5], str_mm[5],str_ddd[5],str_HHH[5],str_mmm[5];
sprintf(str_dd, "%02d", record[i].dd);
sprintf(str_HH, "%02d", record[i].HH);
sprintf(str_mm, "%02d", record[i].mm);
sprintf(str_ddd, "%02d", record[i+1].dd);
sprintf(str_HHH, "%02d", record[i+1].HH);
sprintf(str_mmm, "%02d", record[i+1].mm);
std::string dd(&str_dd[0], &str_dd[strlen(str_dd)]);
std::string HH(&str_HH[0], &str_HH[strlen(str_HH)]);
std::string mm(&str_mm[0], &str_mm[strlen(str_mm)]);
std::string ddd(&str_ddd[0], &str_ddd[strlen(str_ddd)]);
std::string HHH(&str_HHH[0], &str_HHH[strlen(str_HHH)]);
std::string mmm(&str_mmm[0], &str_mmm[strlen(str_mmm)]);
std::string temp = dd + ":" + HH + ":" + mm + " " + ddd + ":" + HHH + ":" + mmm + " " + std::to_string(total_time) + " " + "$" + str_money;
//转存为字符数组
strcpy( customer_bee[j].one_bee_record[k],temp.c_str());
//用户的总费用,记录月份
customer_bee[j].sum += money;
customer_bee[j].MM = record[i].MM;
}
}
int mm = 0,mmm=0;
//遍历结构体输出
while (customer_bee[mm].id[0] != '\0')
{
printf("%s %02d\n", customer_bee[mm].id,customer_bee[mm].MM);
while (customer_bee[mm].one_bee_record[mmm][0] != '\0')
{
printf("%s\n", customer_bee[mm].one_bee_record[mmm]);
mmm++;
}
printf("Total amount: $%.2lf\n", customer_bee[mm].sum);
mm++;
mmm = 0;
}
return 0;
}