#!/usr/bin/env python
import math

# --- 1. Helper to understand "3,35*10^-5" ---
def parse_scientific(val):
    """
    Parses strings like '3,35*10^-5' or '0,005' into floats.
    """
    if not val: return 0.0
    val = val.strip().replace(" ", "")  # Remove spaces
    val = val.replace(",", ".")         # Fix decimal comma
    val = val.replace("*10^", "e")      # Fix scientific notation
    val = val.replace("*10", "e")
    return float(val)

# --- 2. Helper to Calculate Volume based on Shape ---
def get_volume_input():
    print("\n--- Select Object Shape ---")
    print("1. Sphere (Requires Radius)")
    print("2. Cylinder (Requires Radius + Height)")
    print("3. Cube (Requires Side)")
    print("4. Rectangular Box (Requires Length, Width, Height)")
    print("5. I already have the Volume (Manual Input)")
    
    choice = input("Enter choice (1-5): ").strip()
    
    if choice == '1': # Sphere
        r_str = input("Enter Radius (r) [m]: ")
        r = parse_scientific(r_str)
        # Formula: 4/3 * pi * r^3
        return (4/3) * math.pi * (r**3)
        
    elif choice == '2': # Cylinder
        r_str = input("Enter Radius (r) [m]: ")
        h_str = input("Enter Height (h) [m]: ")
        r = parse_scientific(r_str)
        h = parse_scientific(h_str)
        # Formula: pi * r^2 * h
        return math.pi * (r**2) * h
        
    elif choice == '3': # Cube
        s_str = input("Enter Side length [m]: ")
        s = parse_scientific(s_str)
        # Formula: s^3
        return s**3
        
    elif choice == '4': # Box
        l = parse_scientific(input("Enter Length [m]: "))
        w = parse_scientific(input("Enter Width [m]: "))
        h = parse_scientific(input("Enter Height [m]: "))
        return l * w * h
        
    else: # Manual
        v_str = input("Enter Volume directly [m^3]: ")
        return parse_scientific(v_str)

# --- 3. Main Calculation ---
def main():
    print("--- Physics II Mass Calculator ---")
    print("Supports formats like: 3,35*10^-5 or 1.5e-3\n")
    
    try:
        # 1. Get Gravity (Standard is 9.81, but usually good to ask or set)
        g = 9.81
        
        # 2. Get Liquid Density
        rho_str = input("Enter Specific Mass of Liquid (Density) [kg/m^3]: ")
        rho_liq = parse_scientific(rho_str)
        
        # 3. Get Volume (via shape menu)
        vol_obj = get_volume_input()
        print(f"-> Calculated Volume: {vol_obj:.4e} m^3")
        
        # 4. Get Normal Force
        norm_str = input("\nEnter Normal Force [N]: ")
        normal_force = parse_scientific(norm_str)

        # 5. Apply Formula
        # Physics Note: rho*V*g = Buoyancy (Force).
        # Buoyancy + Normal = Total Upward Force (Weight)
        buoyancy = rho_liq * vol_obj * g
        weight_force = buoyancy + normal_force
        
        # Mass = Weight / g
        mass_obj = weight_force / g

        print("\n" + "="*30)
        print(f"Buoyancy (Push): {buoyancy:.4f} N")
        print(f"Normal Force:    {normal_force:.4f} N")
        print(f"Total Weight:    {weight_force:.4f} N")
        print("-" * 30)
        print(f"FINAL MASS:      {mass_obj:.4f} kg")
        print("="*30)

    except ValueError:
        print("\n[!] Error: Please check your number formatting.")
    except Exception as e:
        print(f"\n[!] Error: {e}")

if __name__ == "__main__":
    main()

