你好,帮我做个C语言题目
你好,我是Assistant。很高兴为您做题。
请提供题目的完整题面,并提供任何相关的输入、输出、约束条件等信息。
如果您有任何其他问题,请随时与我联系。我将竭诚为您服务。
897354667@qq.com
假设电话收费标准为: (1)国际长途1.00元/分钟,(2) 国内长途0.60元/分钟,(3)市话前3分钟0.20元,3分钟以后0.10元/分钟。现假设某话单文件中每条话单包含如下信息:通话日期(10位字符)、主叫码(11位字符) 、被叫码(12位字符)、起始时间(8位字符)、 通话时长(整型,单位:秒),要求从话单文件中读取话单信息,计算每条话单的话费,并按话费从高到低输出所有的话单信息。
话单格式:
2020-05-06 6786210101068790908 10:20:50 552020-05-07 13707103274 00156789035 21:30:20 125
您好,根据您给出的题目要求,我给出如下的C语言代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 每条通话记录的结构体
typedef struct {
char date[11]; // 通话日期
char caller[12]; // 主叫号码
char callee[13]; // 被叫号码
char start_time[9]; // 起始时间
int duration; // 通话时长(单位:秒)
double charge; // 话费(单位:元)
} Record;
// 比较两条通话记录的话费大小,用于排序
int compare(const void *a, const void *b) {
double diff = (*(Record *)b).charge - (*(Record *)a).charge;
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
return 0;
}
}
int main() {
Record records[100]; // 存储通话记录的数组
int n = 0; // 记录条数
char line[100]; // 读取每一行的数据
while (fgets(line, sizeof(line), stdin)) {
// 从读入的字符串中提取出每条通话记录的数据
sscanf(line, "%s%s%s%s%d", records[n].date, records[n].caller, records[n].callee, records[n].start_time, &records[n].duration);
// 计算话费
// 判断是否为市话
if (records[n].caller[0] == '0' && records[n].caller[1] == '1' && records[n].caller[2] == '3' && records[n].caller[3] == '7') {
// 市话,前3分钟每分钟0.2元
if (records[n].duration <= 3 * 60) {
records[n].charge = records[n].duration * 0.2 / 60.0;
} else {
// 3分钟以后每分钟0.1元
records[n].charge = 3 * 0.2 / 60.0 + (records[n].duration - 3 * 60) * 0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 每条通话记录的结构体
typedef struct {
char date[11]; // 通话日期
char caller[12]; // 主叫号码
char callee[13]; // 被叫号码
char start_time[9]; // 起始时间
int duration; // 通话时长(单位:秒)
double charge; // 话费(单位:元)
} Record;
// 比较两条通话记录的话费大小,用于排序
int compare(const void *a, const void *b) {
double diff = (*(Record *)b).charge - (*(Record *)a).charge;
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
return 0;
}
}
int main() {
Record records[100]; // 存储通话记录的数组
int n = 0; // 记录条数
char line[100]; // 读取每一行的数据
while (fgets(line, sizeof(line), stdin)) {
// 从读入的字符串中提取出每条通话记录的数据
sscanf(line, "%s%s%s%s%d", records[n].date, records[n].caller, records[n].callee, records[n].start_time, &records[n].duration);
// 计算话费
// 判断是国内长途还是国际长途
if (records[n].callee[0] == '0') {
records[n].charge = records[n].duration * 0.6 / 60.0; // 国内长途,每分钟0.6元
} else {
records[n].charge = records[n].duration * 1.0 / 60.0; // 国际长途,每分钟1.0元
}
// 判断是否为市话
if (records[n].caller[0] == '0' && records[n].caller[1] == '1' && records[n].caller[2] == '3' && records[n].caller[3] == '7') {
// 市话,前3分钟每分钟0.2元
if (records[n].duration <= 3 * 60) {
records[n].charge = records[n].duration * 0.2 / 60.0;
} else {
// 3分钟以后每分钟0.1元
records[n].charge = 3 * 0.2 / 60.0 + (records[n].duration - 3 * 60) * 0.1 / 60.0;
}
} else {
// 非市话,按照国内长途或国际长途的标准计算话费
if (records[n].callee[0] == '0') {
records[n].charge = records[n].duration * 0.6 / 60.0; // 国内长途,每分钟0.6元
} else {
records[n].charge = records[n].duration * 1.0 / 60.0; // 国际长途,每分钟1.0元
}
}