煤矿途径地点初始温度代码接收java数据算法.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from flask import Flask, request, jsonify
  2. import math
  3. app = Flask(__name__)
  4. # initial_temperature:初始温度。
  5. # k:热传递系数。
  6. # times:煤矿在每个地点停留的时间列表。
  7. # locations:煤矿经过的每个地点的环境温度列表。
  8. def calculate_temperature(initial_temperature, k, times, locations):
  9. current_temperature = initial_temperature
  10. for i in range(len(locations)):
  11. t = times[i]
  12. T_env = locations[i]
  13. current_temperature = current_temperature + (T_env - current_temperature) * (1 - math.exp(-k * t))
  14. return current_temperature
  15. @app.route('/calculate', methods=['POST'])
  16. def calculate():
  17. # 获取 JSON 数据
  18. data = request.get_json()
  19. initial_temperature = data.get("initial_temperature")
  20. k = data.get("k")
  21. times = data.get("times")
  22. locations = data.get("locations")
  23. # 计算结果
  24. result = calculate_temperature(initial_temperature, k, times, locations)
  25. return jsonify({"final_temperature": result})
  26. if __name__ == "__main__":
  27. # 启动 Flask 服务
  28. app.run(port=9997, debug=True)