我将给你四分代码,分别是 poly.h, stack.h, varpool.h, interpreter.h
时间: 2023-12-21 08:04:43 浏览: 134
poly.h:
```c++
#ifndef POLY_H
#define POLY_H
#include <iostream>
#include <vector>
class Poly {
public:
// constructors
Poly() : coef{0} {}
Poly(const std::vector<double>& c) : coef{c} {}
// overloaded operators
friend std::istream& operator>>(std::istream& is, Poly& p);
friend std::ostream& operator<<(std::ostream& os, const Poly& p);
friend Poly operator+(const Poly& p1, const Poly& p2);
friend Poly operator-(const Poly& p1, const Poly& p2);
friend Poly operator*(const Poly& p1, const Poly& p2);
friend Poly operator/(const Poly& p1, const Poly& p2);
friend Poly operator%(const Poly& p1, const Poly& p2);
// member functions
int degree() const { return coef.size() - 1; }
double operator[](int i) const { return coef[i]; }
double& operator[](int i) { return coef[i]; }
double eval(double x) const;
Poly deriv() const;
private:
std::vector<double> coef;
};
#endif
```
stack.h:
```c++
#ifndef STACK_H
#define STACK_H
#include <vector>
template <typename T>
class Stack {
public:
// constructors
Stack() : elem{} {}
Stack(const std::vector<T>& v) : elem{v} {}
// member functions
void push(const T& val) { elem.push_back(val); }
void pop() { elem.pop_back(); }
const T& top() const { return elem.back(); }
bool empty() const { return elem.empty(); }
private:
std::vector<T> elem;
};
#endif
```
varpool.h:
```c++
#ifndef VARPOOL_H
#define VARPOOL_H
#include <map>
#include <string>
class VarPool {
public:
// member functions
double get(const std::string& var_name) const;
void set(const std::string& var_name, double val);
private:
std::map<std::string, double> vars;
};
#endif
```
interpreter.h:
```c++
#ifndef INTERPRETER_H
#define INTERPRETER_H
#include <string>
#include "poly.h"
#include "varpool.h"
class Interpreter {
public:
// constructors
Interpreter() : pool{} {}
// member functions
void run_command(const std::string& cmd);
Poly parse_poly(const std::string& cmd) const;
private:
VarPool pool;
};
#endif
```
阅读全文