T003 平均值

C语言成绩求和与平均
#include<stdio.h>

int main()
{
    float m, e, c, sum, ave;
    scanf("%f%f%f", &m, &e, &c);
        sum=m+e+c;
        ave=sum/3;
        printf("%.6f\n%.6f\n", sum, ave);
    return 0;
}

代码如上,但是如何规避求平均时分子相加可能会溢出的风险。。。待我研究研究。。

转载于:https://www.cnblogs.com/striderdu/p/4952706.html

import sys import matplotlib.pyplot as plt import numpy as np import seaborn as sns # 添加这一行即可解决问题 from matplotlib import patheffects from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas from PySide6.QtWidgets import ( QApplication, QMainWindow, QPushButton, QDialog, QVBoxLayout, QLabel, QComboBox, QWidget ) # 数据:单位为小时 data = { "T001": [2.78, 1.88, 1.65, 0.9, 0.97, 2.33], "T002": [2.27, 1.85, 2.87, 2.08, 1.2, 1.13], "T003": [2.02, 2.23, 1.05, 1.37, 2.46, 0.98], "T004": [1.82, 1.27, 1.07, 1.0, 2.15, 2.02], } # 流程名称 process_names = ['接车', '送车1', '送车2', '确报1', '确报2', '编成', '发车'] class ChartDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("流程时间折线图") self.layout = QVBoxLayout(self) # 流程选择下拉框 self.process_combo = QComboBox() self.process_combo.addItems(process_names) # 设置默认选择第一个选项:“接车” self.process_combo.setCurrentIndex(0) self.layout.addWidget(QLabel("选择流程:")) self.layout.addWidget(self.process_combo) # 图表画布 self.figure = plt.figure(figsize=(8, 6), facecolor='#1e1e1e') self.canvas = FigureCanvas(self.figure) self.layout.addWidget(self.canvas) # 绑定下拉框选择变化事件 self.process_combo.currentIndexChanged.connect(self.plot_selected) # 初始绘制图表 self.plot_selected() def plot_selected(self): selected_index = self.process_combo.currentIndex() selected_process = process_names[selected_index] self.figure.clear() # 设置图表区域 ax = self.figure.add_subplot(111) self.figure.set_facecolor('#1e1e1e') ax.set_facecolor('#1e1e1e') # 获取数据 train_ids = sorted(data.keys()) x = list(range(len(train_ids))) y = [data[tid][selected_index] for tid in train_ids] avg_time = sum(y) / len(y) # 绘制主折线 line, = ax.plot(x, y, marker='o', color='#00ffe1', linewidth=2, markersize=6, linestyle='-', markeredgecolor='white', zorder=2, label='流程时间') # 平均线 ax.axhline(avg_time, linestyle='--', color='#ff6600', alpha=0.7, linewidth=2, zorder=1, label='平均时间') # ✅ 设置图标题为:列车{流程名}组织时间采集折线图 ax.set_title(f"列车{selected_process}组织时间采集折线图", fontsize=18, fontweight='bold', color='white', pad=20) # ✅ 去掉横纵坐标轴名称 ax.set_xlabel("") ax.set_ylabel("") # 设置坐标轴刻度 ax.set_xticks(x) ax.set_xticklabels(train_ids, color='white', fontsize=12) ax.set_yticklabels([f"{y:.1f}" for y in ax.get_yticks()], color='white', fontsize=12) # 关闭网格 ax.grid(False) # 边框样式 for spine in ax.spines.values(): spine.set_edgecolor('#444') spine.set_linewidth(1.2) # 添加背景渐变 gradient = np.linspace(0, 1, 256).reshape(1, -1) ax.imshow(gradient, aspect='auto', extent=[*ax.get_xlim(), *ax.get_ylim()], cmap='inferno', alpha=0.05, zorder=0) # 图例 ax.legend(loc='upper right', facecolor='#2e2e2e', edgecolor='none', fontsize=12, labelcolor='white') # 折线阴影效果 line.set_path_effects([ patheffects.Stroke(linewidth=4, foreground='black', alpha=0.3), patheffects.Normal() ]) # 保存数据用于事件处理 self.line = line self.ax = ax self.train_ids = train_ids self.x = x self.y = y # ✅ 绑定鼠标移动事件 self.figure.canvas.mpl_connect('motion_notify_event', self.on_mouse_move) self.canvas.draw() def on_mouse_move(self, event): if event.inaxes != self.ax: if hasattr(self, 'tooltip'): self.tooltip.remove() del self.tooltip self.canvas.draw_idle() return # 查找最近的点 xdata = self.line.get_xdata() ydata = self.line.get_ydata() min_dist = 0.05 # 控制悬停灵敏度 distances = [(x - event.xdata) ** 2 + (y - event.ydata) ** 2 for x, y in zip(xdata, ydata)] min_index = np.argmin(distances) min_distance = distances[min_index] if min_distance < 0.1: x = xdata[min_index] y = ydata[min_index] train_id = self.train_ids[min_index] # 移除旧的 tooltip if hasattr(self, 'tooltip'): self.tooltip.remove() # 创建全屏提示框 self.tooltip = self.ax.text( 0.98, 0.95, f"车次: {train_id}\n时间: {y:.2f} 小时", transform=self.ax.transAxes, fontsize=14, verticalalignment='top', horizontalalignment='right', color='white', bbox=dict(boxstyle="round", facecolor="#333", edgecolor="none", alpha=0.9) ) self.canvas.draw_idle() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("列车流程时间图表") self.setGeometry(100, 100, 400, 300) # 主按钮 self.button = QPushButton("显示图表") self.button.clicked.connect(self.show_chart_dialog) # 设置布局 layout = QVBoxLayout() layout.addWidget(self.button) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) def show_chart_dialog(self): dialog = ChartDialog(self) dialog.exec() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 请将这个折线图美化,做出高级感和科技感最好能是动态的,一定要有科技感
08-29
import sys import matplotlib.pyplot as plt import numpy as np import seaborn as sns from matplotlib import patheffects, animation from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas from PySide6.QtWidgets import ( QApplication, QMainWindow, QPushButton, QDialog, QVBoxLayout, QLabel, QComboBox, QWidget ) from matplotlib.font_manager import FontProperties # 设置中文字体(确保系统支持) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 数据:单位为小时 data = { "T001": [2.78, 1.88, 1.65, 0.9, 0.97, 2.33], "T002": [2.27, 1.85, 2.87, 2.08, 1.2, 1.13], "T003": [2.02, 2.23, 1.05, 1.37, 2.46, 0.98], "T004": [1.82, 1.27, 1.07, 1.0, 2.15, 2.02], } # 流程名称 process_names = ['接车', '送车1', '送车2', '确报1', '确报2', '编成', '发车'] class ChartDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("流程时间折线图") self.layout = QVBoxLayout(self) # 流程选择下拉框 self.process_combo = QComboBox() self.process_combo.addItems(process_names) self.process_combo.setCurrentIndex(0) self.layout.addWidget(QLabel("选择流程:")) self.layout.addWidget(self.process_combo) # 图表画布 self.figure = plt.figure(figsize=(8, 6), facecolor='#0f0f0f') self.canvas = FigureCanvas(self.figure) self.layout.addWidget(self.canvas) # 绑定下拉框选择变化事件 self.process_combo.currentIndexChanged.connect(self.plot_selected) # 初始绘制图表 self.plot_selected() # 动态动画 self.anim = animation.FuncAnimation(self.figure, self.update_animation, interval=2000, repeat=True) def update_animation(self, frame): if hasattr(self, 'line'): y = [data[tid][self.process_combo.currentIndex()][0] for tid in sorted(data.keys())] self.line.set_ydata(y) self.figure.canvas.draw_idle() def plot_selected(self): selected_index = self.process_combo.currentIndex() selected_process = process_names[selected_index] self.figure.clear() ax = self.figure.add_subplot(111) self.figure.set_facecolor('#0f0f0f') ax.set_facecolor('#0f0f0f') train_ids = sorted(data.keys()) x = list(range(len(train_ids))) y = [data[tid][selected_index] for tid in train_ids] avg_time = sum(y) / len(y) # 折线样式 line, = ax.plot(x, y, marker='o', color='#00ffe1', linewidth=2.5, markersize=8, linestyle='-', markeredgecolor='white', zorder=2, label='流程时间') # 平均线 ax.axhline(avg_time, linestyle='--', color='#ff6600', alpha=0.8, linewidth=2.2, zorder=1, label='平均时间') # 标题 ax.set_title(f"列车{selected_process}组织时间采集折线图", fontsize=18, fontweight='bold', color='white', pad=20) # 坐标轴标签 ax.set_xticks(x) ax.set_xticklabels(train_ids, color='white', fontsize=12) ax.set_yticklabels([f"{y:.1f}" for y in ax.get_yticks()], color='white', fontsize=12) ax.set_xlabel("") ax.set_ylabel("") # 边框颜色 for spine in ax.spines.values(): spine.set_edgecolor('#444') spine.set_linewidth(1.2) # 背景渐变 gradient = np.linspace(0, 1, 256).reshape(1, -1) ax.imshow(gradient, aspect='auto', extent=[*ax.get_xlim(), *ax.get_ylim()], cmap='inferno', alpha=0.05, zorder=0) # 图例 ax.legend(loc='upper right', facecolor='#2e2e2e', edgecolor='none', fontsize=12, labelcolor='white') # 光晕效果 line.set_path_effects([ patheffects.Stroke(linewidth=6, foreground='black', alpha=0.5), patheffects.Normal() ]) # 悬停提示 self.line = line self.ax = ax self.train_ids = train_ids self.x = x self.y = y self.figure.canvas.mpl_connect('motion_notify_event', self.on_mouse_move) self.canvas.draw() def on_mouse_move(self, event): if event.inaxes != self.ax: if hasattr(self, 'tooltip'): self.tooltip.remove() del self.tooltip self.canvas.draw_idle() return xdata = self.line.get_xdata() ydata = self.line.get_ydata() distances = [(x - event.xdata) ** 2 + (y - event.ydata) ** 2 for x, y in zip(xdata, ydata)] min_index = np.argmin(distances) min_distance = distances[min_index] if min_distance < 0.05: x = xdata[min_index] y = ydata[min_index] train_id = self.train_ids[min_index] if hasattr(self, 'tooltip'): self.tooltip.remove() self.tooltip = self.ax.text( 0.98, 0.95, f"车次: {train_id}\n时间: {y:.2f} 小时", transform=self.ax.transAxes, fontsize=14, verticalalignment='top', horizontalalignment='right', color='white', bbox=dict(boxstyle="round", facecolor="#333", edgecolor="none", alpha=0.9) ) self.canvas.draw_idle() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("列车流程时间图表") self.setGeometry(100, 100, 400, 300) self.button = QPushButton("显示图表") self.button.clicked.connect(self.show_chart_dialog) layout = QVBoxLayout() layout.addWidget(self.button) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) def show_chart_dialog(self): dialog = ChartDialog(self) dialog.exec() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 程序报错
08-30
import sys import matplotlib.pyplot as plt from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas from PySide6.QtWidgets import ( QApplication, QMainWindow, QPushButton, QDialog, QVBoxLayout, QLabel, QComboBox, QWidget ) from PySide6.QtCore import Qt # 数据:单位为小时 data = { "T001": [2.78, 1.88, 1.65, 0.9, 0.97, 2.33], "T002": [2.27, 1.85, 2.87, 2.08, 1.2, 1.13], "T003": [2.02, 2.23, 1.05, 1.37, 2.46, 0.98], "T004": [1.82, 1.27, 1.07, 1.0, 2.15, 2.02], } # 流程名称 process_names = ['接车', '送车1', '送车2', '确报1', '确报2', '编成', '发车'] class ChartDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("流程时间折线图") self.layout = QVBoxLayout(self) # 流程选择下拉框 self.process_combo = QComboBox() self.process_combo.addItems(process_names) # 设置默认选择第一个选项:“接车” self.process_combo.setCurrentIndex(0) self.layout.addWidget(QLabel("选择流程:")) self.layout.addWidget(self.process_combo) # 图表画布 self.figure = plt.figure(figsize=(8, 6), facecolor='#1e1e1e') self.canvas = FigureCanvas(self.figure) self.layout.addWidget(self.canvas) # 绑定下拉框选择变化事件 self.process_combo.currentIndexChanged.connect(self.plot_selected) # 初始绘制图表 self.plot_selected() def plot_selected(self): selected_index = self.process_combo.currentIndex() selected_process = process_names[selected_index] self.figure.clear() ax = self.figure.add_subplot(111, facecolor='#1e1e1e') # 获取车次列表 train_ids = sorted(data.keys()) x = list(range(len(train_ids))) y = [data[tid][selected_index] for tid in train_ids] avg_time = sum(y) / len(y) # 绘制折线 line, = ax.plot(x, y, marker='o', color='#00ffcc', linewidth=2, markersize=6) ax.axhline(avg_time, linestyle='--', color='gray', alpha=0.5) ax.text(len(x) - 1, avg_time, f" avg={avg_time:.2f}h", va='center', color='gray') # 设置图表样式 ax.set_title(f"{selected_process} 耗时折线图", fontsize=16, fontweight='bold', color='#cccccc', fontname='Arial') ax.set_xlabel("车次", fontsize=14, color='#cccccc', fontname='Arial') ax.set_ylabel("时间(小时)", fontsize=14, color='#cccccc', fontname='Arial') ax.set_xticks(x) ax.set_xticklabels(train_ids, color='#cccccc', fontsize=12, fontname='Arial') ax.set_yticklabels(ax.get_yticks(), color='#cccccc', fontsize=12, fontname='Arial') # 去掉背景网格线 ax.grid(False) # 隐藏右侧和上侧边框 ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['bottom'].set_color('#cccccc') ax.spines['left'].set_color('#cccccc') # 保存数据用于事件处理 self.line = line self.ax = ax self.train_ids = train_ids self.x = x self.y = y # 绑定鼠标移动事件 self.figure.canvas.mpl_connect('motion_notify_event', self.on_mouse_move) self.canvas.draw() def on_mouse_move(self, event): # 如果鼠标不在图表区域内,直接返回 if not event.inaxes: self.figure.canvas.setToolTip('') return # 获取当前折线的 x, y 数据 x_data = self.line.get_xdata() y_data = self.line.get_ydata() ax = self.ax # 获取鼠标坐标 x_mouse, y_mouse = event.xdata, event.ydata # 设置一个距离阈值,判断是否靠近某个点 min_dist = 0.1 found = False for xi, yi in zip(x_data, y_data): if abs(xi - x_mouse) < min_dist and abs(yi - y_mouse) < min_dist: # 找到最近的点,显示坐标 train_id = self.train_ids[int(xi)] tooltip_text = f"车次: {train_id}\n时间: {yi:.2f}h" self.figure.canvas.setToolTip(tooltip_text) found = True break if not found: self.figure.canvas.setToolTip('') class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("列车流程时间图表") self.setGeometry(100, 100, 400, 300) # 主按钮 self.button = QPushButton("显示图表") self.button.clicked.connect(self.show_chart_dialog) # 设置布局 layout = QVBoxLayout() layout.addWidget(self.button) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) def show_chart_dialog(self): dialog = ChartDialog(self) dialog.exec() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 请美化我的折线图,具备大气且具有科技感,更高级一点
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值