jeudi 22 novembre 2018

How to fix these errors of this program(bar chart design with FLTK)?

I'm designing a class Bar, which be requested to meet the following demands: two data member "vector" (which be designed to store the value of x and y ). And this class need to store the units of them. And can be draw a bar chart and attach it to window.

1.Bar.h

#pragma once
#include "/程序/c++/std_lib_facilities.h"
#include "/程序/c++/GUI/Simple_window.h"
#include  "/程序/c++/GUI/GUI.h"
#include  "/程序/c++/GUI/Graph.h"

const int xmax = 500;

const int ymax = 600;

const int xlength = 3 / 4 * xmax;

const int ylength = 3 / 4 * ymax;



 class Bar :public Shape
{
public:
Bar(const vector<double>& ,const vector<double>& ,string s1="",string s2="");
 void set_x_unit(string& s);
 void set_y_unit(string& s);
 string get_x_unit() const;
 string get_y_unit() const;
 void draw_lines() const;
 private:
string x_unit;
string y_unit;
vector<double> x;
vector<double> y;
Graph_lib::Lines L;
Vector_ref<Graph_lib::Rectangle> r;
Vector_ref<Graph_lib::Text> tx;
};

//helper functions
double get_max(const vector<double>& v);
double get_min(const vector<double>& v);

2.my bar.cpp:

#include "Bar.h"

 //Class Bar

   Bar::Bar(const vector<double>& d1, const vector<double>& d2, string s1 , string s2 ):
x(d1),y(d2),x_unit(s1),y_unit(s2)
{
//Axis
L.add(Point(xmax / 8, 7 * ymax / 8), Point(xmax / 8, ymax / 8));         //Axis y
L.add(Point(xmax / 8, 7 * ymax / 8), Point(7 * xmax / 8, 7 * ymax / 8)); // Axis x
//Rectangle and text
int dist = xlength / (3 * y.size());
double y_max = get_max(y);
double y_min = get_min(y);
int scale = ylength / y_max;
for (int i = 0; i < y.size(); ++i) {
    tx.push_back(new Graph_lib::Text(Point(xmax / 8 + dist * (i + 1), 7 * ymax / 8 + 10), to_string(x[i]))); //mark the x value
    r.push_back(new Graph_lib::Rectangle(Point(xmax / 8 + dist * (i + 1), 7 * ymax / 8 - y[i] * scale), 2 * dist, y[i] * scale));//rectangle 
    if (y[i] == y_max) {
        r[i].set_fill_color(Graph_lib::Color::red);
    }
    else if (y[i] == y_min) {
        r[i].set_fill_color(Graph_lib::Color::blue);
    }
    else {
        r[i].set_fill_color(Graph_lib::Color::yellow);
    }
};
//mark units
tx.push_back(new Graph_lib::Text(Point(xmax / 16, ymax / 8 + 10), y_unit));
tx.push_back(new Graph_lib::Text(Point(7*xmax / 8, 7*ymax / 8 + 10), x_unit));
 };

void Bar::set_x_unit(string& s) {
x_unit = s;
};

 void Bar::set_y_unit(string& s) {
y_unit = s;
   };

string Bar::get_x_unit() const{
return x_unit; 
   };

string Bar::get_y_unit() const{
return y_unit;
 };

void Bar::draw_lines() const {
L.draw_lines();
for (int i = 0; i < r.size(); ++i) {
    r[i].draw_lines();
};
for (int j = 0; j < tx.size(); j++) {
    tx[j].draw_lines();

}
}

 //helper function
 double get_max( const vector<double>& v) {
double max = 0;
for (int i = 0; i < v.size(); ++i) {
    if (v[i] > max)max = v[i];
};
return max;
    }

   double get_min(const vector<double>& v) {
double min = 0;
for (int i = 0; i < v.size(); ++i) {
    if (v[i] <min)min = v[i];
};
return min;
}

3.my main.cpp:

#include "Bar.h"

int main() {
    try {
    Simple_window win(Point(100, 100), xmax, ymax, "BAR CHART");
            string name;
    cout << "please input the file name:";
    cin >> name;
    ifstream ist(name.c_str());
    if (!ist)error("this file cann't open");
    double d = 0;
    vector<double> y_sample;
    while (!ist.eof()) {
        ist >> d;
        y_sample.push_back(d);
    }

    vector<double> x_sample;
    for (int i = 0; i < y_sample.size(); ++i) {
        x_sample.push_back((double)(i + 1.00));
    };

    Bar bar(x_sample, y_sample);
    win.attach(bar);
    win.wait_for_button();
    return 1;
}
catch (runtime_error &e) {
    cout << e.what() << endl;
    cout << "runtime errorr" << endl;
}
catch (...) {
    cerr << "something unexpected!";
}
}

But this can't operate it. how to fix it?

Aucun commentaire:

Enregistrer un commentaire