#include <iostream>
#include <cmath>
using namespace std;

double calculateSaturationVaporPressure(double temperature) {
    // Using Tetens formula
    double saturationVaporPressure = 6.11 * pow(10, (7.5 * temperature) / (237.3 + temperature));
    return saturationVaporPressure;
}

double calculateActualVaporPressure(double relativeHumidity, double saturationVaporPressure) {
    // Calculating actual vapor pressure
    double actualVaporPressure = (relativeHumidity / 100) * saturationVaporPressure;
    return actualVaporPressure;
}

double calculateMixingRatio(double actualVaporPressure, double atmosphericPressure) {
    // Calculating mixing ratio in kg/kg
    double mixingRatio = 0.622 * actualVaporPressure / (atmosphericPressure - actualVaporPressure);
    return mixingRatio;
}

int main() {
    double outsideTemperature;       // Temperature in degrees Celsius
    cout << "Enter Outside Temperature: ";
    cin >> outsideTemperature;

    double insideTemperature;
    cout << "Enter Inside Temperature: ";
    cin >> insideTemperature;

    double outsideRelativeHumidity;  // Relative Humidity in %
    cout << "Enter Outside Relative Humidity: ";
    cin >> outsideRelativeHumidity;

    double atmosphericPressure = 1008;  // Atmospheric pressure in hPa

    // Step 1: Calculate Saturation Vapor Pressure
    double outsideSaturationVaporPressure = calculateSaturationVaporPressure(outsideTemperature);

    // Step 2: Calculate Actual Vapor Pressure
    double outsideActualVaporPressure = calculateActualVaporPressure(outsideRelativeHumidity, outsideSaturationVaporPressure);

    // Step 3: Calculate Mixing Ratio (Humidity Ratio)
    double outsideMixingRatio = calculateMixingRatio(outsideActualVaporPressure, atmosphericPressure);

    // Step 4: Convert to Grams per Kilogram
    double outsideSpecificHumidity = outsideMixingRatio * 1000;  // Convert kg/kg to g/kg

    // Output the result
    cout << "The specific humidity at " << outsideTemperature << "°C and " << outsideRelativeHumidity 
         << "% relative humidity is approximately " << outsideSpecificHumidity << " grams of water vapor per kilogram of dry air (g/kg d.a)." << endl;

    float m = 1000;
    float Cp = 1.005;
    float dT = outsideTemperature - insideTemperature;
    float Q = (m * Cp * dT) / (4.186 * 540);
    float finalSpecificHumidity = Q + outsideSpecificHumidity;

    // Calculate inside relative humidity
    double insideSaturationVaporPressure = calculateSaturationVaporPressure(insideTemperature);
    double insideActualVaporPressure = calculateActualVaporPressure(100, insideSaturationVaporPressure);
    double insideMixingRatio = calculateMixingRatio(insideActualVaporPressure, atmosphericPressure);
    double insideSpecificHumidity = insideMixingRatio * 1000;  // Convert kg/kg to g/kg
    float insideRelativeHumidity = (finalSpecificHumidity / insideSpecificHumidity) * 100;
    cout << "Inside Relative Humidity: " << insideRelativeHumidity << endl;

    return 0;
}