Trying to create a generic graphing tool

Hello everyone,

So I'm trying to write a couple of functions & classes that should allow me to create ascii graphs of functions that the user inputs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#define NEWLINE '\n'
#define TAB '\t'
#define VTAB '\v'
#define BEEP '\a'
#define BACK '\b'
using namespace std;

enum plotpoint { zero, one, two, three, four, five, six, seven, eight, nine, dash, vertbar, ppoint, space};
enum reltype {constant, linear, quadratic, cubic, quartic};

void Output_Plot_Point(plotpoint pp);

plotpoint Assign_PlotPoint_by_int(int c);

double Input_double(string prompt);
string Input_string(string prompt);

int Round_double_to_int (double in);

bool Compare_string(string i, string comp);

void ExitCall();

class DataPlot
{	public:
	vector<plotpoint [80]> plot;
	DataPlot(void);
	DataPlot(int data [80] [100], int h);
	~DataPlot(void);						
	void DisplayPlot();
											};

class CFunction
{	public:
	int datadesc [80] [100];
	reltype function_type;
	double a, b, c, d, e;						
	CFunction(void);
	CFunction(double ai, double bi, double ei);								// f(x) = a(x-b) + e			Overloaded Linear Constructor
	CFunction(double ai, double bi, double ci, double ei);					// f(x) = a(x-b)(x-c) + e		Overloaded Quadratic Constructor
	CFunction(double ai, double bi, double ci, double di, double ei);		// f(x) = a(x-b)(x-c)(x-d) + e	Overloaded Cubic Constructor
	void PlotFunction();
	int get_plot_cell(int hofs, int vofs);
	~CFunction(void);
	
					};

void User_cfunction();
void User_init_cfunction(CFunction * pFunction);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "FRepo.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <windows.h>
#include <mmsystem.h>
#define NEWLINE '\n'
#define TAB '\t'
#define VTAB '\v'
#define BEEP '\a'
#define BACK '\b'
using namespace std;

double Input_double(string prompt)
{	while(true)
	{	cout << prompt;
		double data;
		cin >> data;
		if (!cin.fail())
			{	return data;
				cout << NEWLINE << NEWLINE;		}
			cin.clear();
			cin.ignore();
			cout << NEWLINE << NEWLINE;		}	}

string Input_string(string prompt)
{	while(true)
	{	cout << prompt;
		string data;
		getline (std::cin,data);
		if (!cin.fail())
			{	return data;
				cout << NEWLINE << NEWLINE;		}
			cin.clear();
			cin.ignore();
			cout << NEWLINE << NEWLINE;		}	}

int Round_double_to_int (double in)
{	int d;
	d = in;
	in = in - d;
	if (in == 0.5)
	{	return d;	}
	else if (in > 0.5)
	{	d++;
		return d;	}
	else if (in < 0.5)
	{	return d;	}
							}


bool Compare_string(string i, string comp)
{	transform (i.begin (), i.end (), i.begin (), toupper);	
	if (i == comp)
	{	return true;	}
	else if (i != comp)
	{	return false;	}		}


void ExitCall()
{	exit(0);	} 

plotpoint Assign_PlotPoint_by_int(int c)
{	if (c == 0)
	{	return zero;	}
	if (c == 1)
	{	return one;	}
	if (c == 2)
	{	return two;	}
	if (c == 3)
	{	return three;	}
	if (c == 4)
	{	return four;	}
	if (c == 5)
	{	return zero;	}
	if (c == 6)
	{	return six;	}
	if (c == 7)
	{	return seven;	}
	if (c == 8)
	{	return eight;	}
	if (c == 9)
	{	return nine;	}

	if (c == 10)
	{	return dash;	}
	if (c == 11)
	{	return vertbar;	}
	if (c == 12)
	{	return ppoint;	}
	if (c == 13)
	{	return space;	}
								}

void Output_Plot_Point(plotpoint pp)
{	if (pp == zero)
        {       cout << "0";	}
	if (pp == one)
	{	cout << "1";	}
	if (pp == two)
	{	cout << "2";	}
	if (pp == three)
	{	cout << "3";	}
	if (pp == four)
	{	cout << "4";	}
	if (pp == five)
	{	cout << "5";	}
	if (pp == six)
	{	cout << "6";	}
	if (pp == seven)
	{	cout << "7";	}
	if (pp == eight)
	{	cout << "8";	}
	if (pp == nine)
	{	cout << "9";	}

	if (pp == dash)
	{	cout << "-";	}
	if (pp == vertbar)
	{	cout << "|";	}
	if (pp == ppoint)
	{	cout << "*";	}
	if (pp == space)
	{	cout << " ";	}		}

DataPlot::DataPlot(void)
{					}

DataPlot::DataPlot(int data [80] [100], int h)
{	int height = 0;
	plotpoint copyline [80];
		while (true)
		{	int p = 0;
			int c = data [p] [height];
			copyline [p] = Assign_PlotPoint_by_int(c);
			p++;
			if (p == 79)
			{	plot.emplace(plot.begin(), copyline);
				p = 0;
				height++;
				if (height == h)
				{	break;	}	}	}	}

void DataPlot::DisplayPlot()
{	vector<plotpoint [80]>::iterator it;
	for (it = (plot.end()-1); it != (plot.begin()-1); --it)
	{	while (true)
		{	int p_out = 0;
			Output_Plot_Point((*it) [p_out]);
			p_out++;
			if (p_out == 79)
			{	cout << NEWLINE;		}	}	}	}


DataPlot::~DataPlot(void)
{					}

void User_cfunction()
{	CFunction * pFx;
	User_init_cfunction(pFx);
	string i;
	i = Input_string("Plot Function? ");
	transform (i.begin (), i.end (), i.begin (), toupper);
	if ((i == "YES")||(i == "Y"))
	{	cout << NEWLINE << NEWLINE;
		pFx->PlotFunction();
		DataPlot Output (pFx->datadesc, 80);
		Output.DisplayPlot();		
		delete pFx;				}
	else
	{	cout << "deleting plot." << endl;
		delete pFx;		}	}

void User_init_cfunction(CFunction * pFunction)
{	string i;
	reltype ftype;
	while(true)
	{	i = Input_string("Please input function type: ");
		transform (i.begin (), i.end (), i.begin (), toupper);
		if ((i == "LINEAR")||(i == "L"))
		{	ftype = linear;	
			double ai, bi, ei;
			ai = Input_double("a value: ");
			bi = Input_double("b value: ");
			ei = Input_double("e value: ");
			pFunction = new CFunction(ai, bi, ei);
			break;		}
		else if ((i == "QUADRATIC")||(i == "Q"))
		{	ftype = quadratic;	
			double ai, bi, ci, ei;
			ai = Input_double("a value: ");
			bi = Input_double("b value: ");
			ci = Input_double("c value: ");
			ei = Input_double("e value: ");			
			pFunction = new CFunction(ai, bi, ci, ei);
			break;		}
		else if ((i == "CUBIC")||(i == "C"))
		{	ftype = quadratic;
			double ai, bi, ci, di, ei;
			ai = Input_double("a value: ");
			bi = Input_double("b value: ");
			ci = Input_double("c value: ");
			di = Input_double("d value: ");
			ei = Input_double("e value: ");
			pFunction = new CFunction(ai, bi, ci, di, ei);
			break;		}	}	}


CFunction::CFunction(double ai, double bi, double ei)
{		function_type = linear;
		a = ai;
		b = bi;
		c = 0;
		d = 0;
		e = ei;    }


CFunction::CFunction(double ai, double bi, double ci, double ei)
{		function_type = quadratic;
		a = ai;
		b = bi;
		c = ci;
		d = 0;
		e = ei;     }



CFunction::CFunction(double ai, double bi, double ci, double di, double ei)
{		function_type = cubic;
		a = ai;
		b = bi;
		c = ci;
		d = di;
		e = ei;      }
int CFunction::get_plot_cell(int hofs, int vofs)				// a-horizontal ofs (right) b-vertical ofs (down)
{	double xvar, fxvar;	
	int check;
	if (function_type = linear)									// f(x) = a(x-b) + e			Overloaded Linear Constructor
	{	xvar = hofs;
		fxvar = ((a*(xvar - b))+e);
		check = Round_double_to_int (fxvar);
		if (check == vofs)
		{	return 12;		}
		else
		{	return 13;		}    }
	else if (function_type = quadratic)							// f(x) = a(x-b)(x-c) + e		Overloaded Quadratic Constructor
	{	xvar = hofs;
		fxvar = ((a*(xvar - b)*(xvar - c))+e);
		check = Round_double_to_int (fxvar);
		if (check == vofs)
		{	return 12;		}
		else
		{	return 13;		}
								}
	else if (function_type = cubic)								// f(x) = a(x-b)(x-c)(x-d) + e	Overloaded Cubic Constructor
	{	xvar = hofs;
		fxvar = ((a*(xvar - b)*(xvar - c)*(xvar - d))+e);
		check = Round_double_to_int (fxvar);
		if (check == vofs)
		{	return 12;		}
		else
		{	return 13;		}
								}	}
void CFunction::PlotFunction()
{	int hofs, vofs;
	vofs = 0;
	while (true)
	{	hofs = 0;
		datadesc [hofs] [vofs] = get_plot_cell(hofs, vofs);
		hofs++;
			if (hofs == 79)
			{	hofs = 0;
				vofs++;
				if (vofs == 99)
				{	break;	}	}	}	}


Everything appears okay until I try to compile, when I get this:
1
2
3
4
5
6
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<plotpoint(&)[80]>(plotpoint (*)[80],_Other)' being compiled
1>          with
1>          [
1>              _Ty=plotpoint [80],
1>              _Other=plotpoint (&)[80]
1>          ]


I'm not sure what that means, but it sounds a lot like some issue with what I'm passing to the vector class as its content. Can anyone clarify what I'm doing wrong here?
closed account (o3hC5Di1)
Hi there,

In your .h: vector<plotpoint [80]> plot;

You can't store arrays in STL containers. You could use std::array if your compiler supports it:

vector< std::array<plotpoint, 80>> plot;

Don't forget to alter your .cpp file as well:
Line 147: vector<std::array<plotpoint, 80>>::iterator it;

Hope that helps.

All the best,
NwN
Last edited on
Tried it...

And no luck. I'm using MSVC++ 2010, so I guess my compiler doesnt support it?

Any ideas on how else I could handle the graph lines? Maybe I'll just leave the size as 80x100 standard...
MSVC 2010 does support std::array<>

1
2
3
4
5
#include <array>

// etc

std::array<plotpoint, 80> test;


Andy
closed account (o3hC5Di1)
Hi,

Just to clarify Andywestken's post, did you #include <array> ?

All the best,
NwN
Ahh, yes I did, but no luck anyways. I decided to make the plot size fixed at 80x100, and made quite a few other changes under the hood, but I cant get it to work yet. Im going to do a bit of a tear down & rebuild on this to fix the spaghetti code monster that I've created. If all goes well, I'll post the results here later.
Topic archived. No new replies allowed.