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

int main() {
    double result = 0;
    map<string,double> memory;
    cout << "Simple Calculator (+ - * / %) with memory slots M1, M2...\n";
    cout << "Commands: number op number (e.g. 3 + 5), store M1, recall M1, exit\n";

    while(true) {
        cout << "> ";
        string cmd;
        getline(cin, cmd);
        if(cmd=="exit") break;

        if(cmd.substr(0,5)=="store") {
            string slot = cmd.substr(6);
            memory[slot] = result;
            cout << "Stored " << result << " in " << slot << "\n";
            continue;
        }
        if(cmd.substr(0,6)=="recall") {
            string slot = cmd.substr(7);
            if(memory.count(slot)) {
                cout << slot << " = " << memory[slot] << "\n";
            } else cout << "Slot empty\n";
            continue;
        }

        double a, b;
        char op;
        if(sscanf(cmd.c_str(), "%lf %c %lf", &a, &op, &b)==3) {
            switch(op) {
                case '+': result = a+b; break;
                case '-': result = a-b; break;
                case '*': result = a*b; break;
                case '/': result = (b!=0)? a/b : 0; break;
                case '%': result = (int)a % (int)b; break;
                default: cout << "Unknown operator\n"; continue;
            }
            cout << "Result: " << result << "\n";
        } else {
            cout << "Invalid input\n";
        }
    }
    return 0;
}
