plot.cpp
来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 142 行
CPP
142 行
int Plot::get_print_col(double x_value)
{
double interp_x = ((double) max_col) * (x_value - x_low) / (x_high - x_low) + 0.5;
int answer = (int) interp_x;
if (answer < 0 || answer > max_col) answer = -1;
return answer;
}
int Plot::get_print_row(double y_value)
/*
Post:
Returns the row of the screen at which a point with
y-coordinate y_value should be plotted, or
returns -1 if the point would fall off the edge of
the screen.
*/
{
double interp_y =
((double) max_row) * (y_high - y_value) / (y_high - y_low) + 0.5;
int answer = (int) interp_y;
if (answer < 0 || answer > max_row) answer = -1;
return answer;
}
Error_code Plot::set_limits()
{
double x, y;
double new_increment = x_increment,
new_x_low = x_low,
new_x_high = x_high,
new_y_low = y_low,
new_y_high = y_high;
cout << "Give a new value for x increment. Previous value = " << x_increment;
cout << "\n Enter a value or a new line to keep the old value: " << flush;
if (read_num(x) == success) new_increment = x;
cout << "Give a new value for lower x limit. Previous value = " << x_low;
cout << "\n Enter a value or a new line to keep the old value: " << flush;
if (read_num(x) == success) new_x_low = x;
cout << "Give a new value for upper x limit. Previous value = " << x_high;
cout << "\n Enter a value or a new line to keep the old value: " << flush;
if (read_num(x) == success) new_x_high = x;
cout << "Give a new value for lower y limit. Previous value = " << y_low;
cout << "\n Enter a value or a new line to keep the old value: " << flush;
if (read_num(y) == success) new_y_low = y;
cout << "Give a new value for upper y limit. Previous value = " << y_high;
cout << "\n Enter a value or a new line to keep the old value: " << flush;
if (read_num(y) == success) new_y_high = y;
if (new_increment <= 0 || new_x_high <= new_x_low || new_y_high <= new_y_low)
return fail;
x_increment = new_increment;
x_low = new_x_low;
x_high = new_x_high;
y_low = new_y_low;
y_high = new_y_high;
return success;
}
void Plot::clear()
{
points.clear();
}
void Plot::find_points(Expression &postfix)
/*
Post:
The Expression postfix is evaluated for values of
x that range from x_low to x_high in steps of
x_increment. For each evaluation we add a Point
to the Sortable_list points.
Uses:
Classes Token, Expression, Point, and List.
*/
{
double x_val = x_low;
double y_val;
while (x_val <= x_high) {
Token::set_x(x_val);
postfix.evaluate_postfix(y_val);
postfix.rewind();
Point p(get_print_row(y_val), get_print_col(x_val));
points.insert(0, p);
x_val += x_increment;
}
}
void Plot::draw()
/*
Post:
All screen locations represented in points have
been marked with the character '#' to produce a
picture of the stored graph.
Uses:
Classes Point and Sortable_list and its method merge_sort.
*/
{
points.merge_sort();
int at_row = 0, at_col = 0; // cursor coordinates on screen
for (int i = 0; i < points.size(); i++) {
Point q;
points.retrieve(i, q);
if (q.row < 0 || q.col < 0) continue; // off the scale of the graph
if (q.row < at_row || (q.row == at_row && q.col < at_col)) continue;
if (q.row > at_row) { // Move cursor down the screen.
at_col = 0;
while (q.row > at_row) {
cout << endl;
at_row++;
}
}
if (q.col > at_col) // Advance cursor horizontally.
while (q.col > at_col) {
cout << " ";
at_col++;
}
cout << "#";
at_col++;
}
while (at_row++ <= max_row) cout << endl;
}
Plot::Plot()
{
x_low = -10;
x_high = 10;
y_low = -1000;
y_high = 1000;
x_increment = 0.01;
max_row = 19;
max_col = 79;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?