<commit_before>//
//  ViewController.swift
//  Calculator
//
//  Created by laurenyew on 7/23/16.
//  Copyright © 2016 CS193p. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    fileprivate struct Constants{
        static let NumDecimalDigits = 6
    }

    @IBOutlet fileprivate weak var display: UILabel!
    @IBOutlet fileprivate weak var history: UILabel!
    
    fileprivate var userIsInTheMiddleOfTyping: Bool = false
    
    //CalculatorBrain
    fileprivate var brain = CalculatorBrain()
    
    //Dictionary to store variables
    fileprivate var variableDict = [String:Double]()
    
    
    //UI Label display value
    fileprivate var displayValue: Double? {
        get{
            if let text = display.text, let value = Double(text){
                return value
            }else{
                return nil
            }
        }
        set {
            if let doubleValue = newValue {
                let formatter = NumberFormatter()
                formatter.numberStyle = .decimal
                formatter.maximumFractionDigits = Constants.NumDecimalDigits
                display.text = formatter.string(from: NSNumber(value: doubleValue))
//                history.text = brain.description +
//                                (brain.isPartialResult ? " ... " : " = ")
            }else{
                display.text = "0"
                history.text = " "
            }
        }
    }
    
    
    
    //User has touched a digit button
    @IBAction fileprivate func touchDigit(_ sender: UIButton) {
        let digit = sender.currentTitle!
        
        let textCurrentlyInDisplay = display.text!
        
        if userIsInTheMiddleOfTyping {
            if(digit != "." || textCurrentlyInDisplay.range(of: digit) == nil){
                display.text = textCurrentlyInDisplay + digit
            }
        } else {
            display.text = digit
        }
        
        userIsInTheMiddleOfTyping = true
    }
    
    //(-> M) Set the variable in the dict with display value, and 
    //evaluate current set of operations with M with it.
    @IBAction func setVariableInDictionary(_ sender: UIButton) {
    }
    
    //(M) Add the variable to the set of brain operations
    @IBAction func useVariableInOperations(_ sender: UIButton) {
    }
    
    //User has touched an operation button
    @IBAction fileprivate func performOperation(_ sender: UIButton) {
        //Have typed numbers -- put in operand
        if(userIsInTheMiddleOfTyping) {
            brain.setOperand(displayValue!)<commit_msg>Assignment 2: Hook View Controller to use brain<commit_after>//
//  ViewController.swift
//  Calculator
//
//  Created by laurenyew on 7/23/16.
//  Copyright © 2016 CS193p. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    fileprivate struct Constants{
        static let NumDecimalDigits = 6
    }

    @IBOutlet fileprivate weak var display: UILabel!
    @IBOutlet fileprivate weak var history: UILabel!
    
    fileprivate var userIsInTheMiddleOfTyping: Bool = false
    
    //CalculatorBrain
    fileprivate var brain = CalculatorBrain()
    
    //Dictionary to store variables
    fileprivate var variableDict = [String:Double]()
    
    //UI Label display value
    fileprivate var displayValue: Double? {
        get{
            if let text = display.text, let value = Double(text){
                return value
            }else{
                return nil
            }
        }
        set {
            if let doubleValue = newValue {
                let formatter = NumberFormatter()
                formatter.numberStyle = .decimal
                formatter.maximumFractionDigits = Constants.NumDecimalDigits
                display.text = formatter.string(from: NSNumber(value: doubleValue))
            }else{
                display.text = "0"
                history.text = " "
            }
        }
    }
    
    
    
    //User has touched a digit button
    @IBAction fileprivate func touchDigit(_ sender: UIButton) {
        let digit = sender.currentTitle!
        
        let textCurrentlyInDisplay = display.text!
        
        if userIsInTheMiddleOfTyping {
            if(digit != "." || textCurrentlyInDisplay.range(of: digit) == nil){
                display.text = textCurrentlyInDisplay + digit
            }
        } else {
            display.text = digit
        }
        
        userIsInTheMiddleOfTyping = true
    }
    
    //(-> M) Set the variable in the dict with display value, and 
    //evaluate current set of operations with M with it.
    @IBAction func setVariableInDictionary(_ sender: UIButton) {
        variableDict.updateValue(displayValue!, forKey: "M")
    }
    
    //(M) Add the variable to the set of brain operations
    @IBAction func useVariableInOperations(_ sender: UIButton) {
        let brainResult = brain.evaluate(using: variableDict)
        displayValue = brainResult.result
        history.text = brainResult.description +
            (brainResult.isPending ? " ... " : " = ")
    }
    
    //User has touched an operation button
    @IBAction fileprivate func performOperation(_ sender: UIButton) {
        //Have typed numbers -- put in operand
        if(userIsInTheMiddleOfTyping) {
            brain.setOperand(displayValue!)
{'fraction_non_alphanumeric': 0.07965594614809275, 'fraction_numerical': 0.005422587883320867, 'mean_word_length': 1.8528, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5396031', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1395, 'n_words': 401}