Pointer hjælp til grafregner
Jeg er igang med at lave en lille grafregner, selve koden ser således ud:#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
// Registere Brugte Funktioner
// Der bliver brugt void i disse fordi funktionerne ikke returnere nogle værdier
void in_func(), valg_func(), tegn_func(), show_func(), delete_func();
// Class
class graf {
public:
// Indskriver værdierne
void in_a(int); // a værdien
void in_b(int); // b værdien
void in_c(int); // c værdien
void in_x_min(float); // x_min værdien
void in_x_max(float); // x_max værdien
void in_y_min(float); // y_min værdien
void in_y_max(float); // y_max værdien
void in_tegn(char); // graf tegnet
void in_type(int); // graf typen
void in_xy(char); // x og y koordinater
void in_xm(float); // min x min værdi positiv
// Returnere værdierne
int out_a() { return a; }
int out_b() { return b; }
int out_c() { return c; }
float out_x_min() { return x_min; }
float out_x_max() { return x_max; }
float out_y_min() { return y_min; }
float out_y_max() { return y_max; }
char out_tegn() { return tegn; }
int out_type() { return type; }
char out_xy() { return xy; }
float out_xm() { return xm; }
// Mine variabler - private
private:
int a;
int b;
int c;
float x_min;
float x_max;
float y_min;
float y_max;
char tegn;
int type;
char xy;
float xm;
};
void graf::in_a(int vaerdi_a) {
a = vaerdi_a;
cout << a << endl;
}
void graf::in_b(int vaerdi_b) {
b = vaerdi_b;
cout << b << endl;
}
void graf::in_c(int vaerdi_c) {
c = vaerdi_c;
cout << c << endl;
}
void graf::in_x_min(float vaerdi_x_min) {
x_min = vaerdi_x_min;
cout << x_min << endl;
}
void graf::in_x_max(float vaerdi_x_max) {
x_max = vaerdi_x_max;
cout << x_max << endl;
}
void graf::in_y_min(float vaerdi_y_min) {
y_min = vaerdi_y_min;
cout << y_min << endl;
}
void graf::in_y_max(float vaerdi_y_max) {
y_max = vaerdi_y_max;
cout << y_max << endl;
}
void graf::in_tegn(char vaerdi_tegn) {
tegn = vaerdi_tegn;
cout << tegn << endl;
}
void graf::in_type(int vaerdi_type) {
type = vaerdi_type;
cout << type << endl;
}
void graf::in_xy(char vaerdi_xy) {
xy = vaerdi_xy;
cout << xy << endl;
}
void graf::in_xm(float vaerdu_xm) {
xm = vaerdu_xm;
cout << xm << endl;
}
// Opretter mit prinær objekt til classen på heaperen
graf * ix = new graf();
graf * ox = new graf();
graf * px = new graf();
graf * fx = NULL;
// Globale Variabler
short int graf_valg = 0; // Hvilken graf der er valgt
// Main
int main() { // Opretter main funktionen, som bliver kaldet som den første funktion
clrscr(); // Clear skærmen for tekst
short int korrekt_hm = 0; // Opretter variablen for menu'en
do { // Opretter en do...while løkke som vælger om man indtaste det korrekte til menu'en
clrscr(); // Clear skærmen for tekst
// Opretter menu'en man kan vælge ud fra
cout << endl;
cout << " ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»" << endl;
cout << " º Hoved Menu: º" << endl;
cout << " ÌÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
cout << " º01:º V‘lg Funktion º" << endl;
cout << " º02:º Indtast v‘rdier til funktion º" << endl;
cout << " º03:º Gem Funktionsv‘rdier º" << endl;
cout << " º04:º ben Funktionsv‘rdier º" << endl;
cout << " º05:º Vis Funktionsv‘rdier º" << endl;
cout << " º06:º Vis Udregningsv‘rdier º" << endl;
cout << " º07:º Tegn V‘rdi-graf º" << endl;
cout << " º08:º Tegn Standart-graf º" << endl;
cout << " º09:º Tegn Vilk†rlig-graf º" << endl;
cout << " º10:º Bestem Integralet º" << endl;
cout << " º11:º Bestem Rod vha Bisektion º" << endl;
cout << " ÌÍÍÍÎÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
cout << " º12:º Afslut Programmet º" << endl;
cout << " ÈÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ" << endl;
cout << endl;
short int valg_hm; // Opretter variablen for menu valg
cin >> valg_hm; // Skriver fra tastaturen til valg_hm, hvilket punkt man vælger.
// Opretter switch'en for valg_hm, om hvilket menupunkt der er valgt
switch (valg_hm) {
case 1: // Hvis menupunktet er 1
valg_func(); // Åben funktionen valg_func()
break; // Afslutter switch'en.
case 2: // Hvis menupunktet er 2
in_func(); // Åbner funktionen in_func()
break; // Afslutter switch'en.
case 5:
show_func();
break;
case 7:
tegn_func();
break;
case 12: // Hvis menupunktet er 12
delete_func();
korrekt_hm = 1;
break;
}
} while (korrekt_hm != 1);
// Returnere 0 til Operativsystemet
return 0;
}
void valg_func() {
int valg;
// Giver valgmuligheden for at vælge mellem 3 funktioner.
cout << "V‘lg hvilken grad du vil regne med:" << endl;
cout << endl;
cout << "\t 1: f(x)" << endl;
cout << "\t 2: g(x)" << endl;
cout << "\t 3: h(x)" << endl;
cin >> valg;
if (valg == 1) {
// graf * ix = new graf();
fx = ix;
}
else if (valg == 2) {
// graf * ox = new graf();
fx = ox;
}
else if (valg == 3) {
// graf * px = new graf();
fx = px;
}
else {
cout << "Forkert funktionsvalg!" << endl;
}
cout << "valg_func";
getch();
}
void in_func() {
// Opretter midlertidige variabler
int valg;
int a;
int b;
int c;
float x_min;
float x_max;
char tegn;
int type;
char xy;
clrscr();
cout << "Indtast din a:" << endl;
cout << "a: ";
cin >> a;
fx->in_a(a);
cout << "Indtast din b:" << endl;
cout << "b: ";
cin >> b;
fx->in_b(b);
cout << "Indtast din c:" << endl;
cout << "c: ";
cin >> c;
fx->in_c(c);
cout << "Indtast din X min:" << endl;
cout << "X min: ";
cin >> x_min;
fx->in_x_min(x_min);
cout << "Indtast din X max:" << endl;
cout << "X max: ";
cin >> x_max;
fx->in_x_max(x_max);
cout << "Indtast tegn:" << endl;
cout << "Tegn: ";
cin >> tegn;
fx->in_tegn(tegn);
cout << " Type 1:---------" << endl;
cout << " "<< fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << endl;
cout << " "<< fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << endl;
cout << " "<< fx->out_tegn() << fx->out_tegn() << endl;
cout << " "<< fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << endl;
cout << " "<< fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << fx->out_tegn() << endl;
cout << " ----------------" << endl;
cout << " Type 2:---------" << endl;
cout << " " << fx->out_tegn() << endl;
cout << " " << fx->out_tegn() << endl;
cout << " " << fx->out_tegn() << endl;
cout << " " << fx->out_tegn() << endl;
cout << " " << fx->out_tegn() << endl;
cout << " ----------------" << endl;
cout << " Type 3:---------" << endl;
cout << " " << fx->out_tegn() << "-(x,y)" << endl;
cout << " " << fx->out_tegn() << "-(x,y)" << endl;
cout << " " << fx->out_tegn() << "-(x,y)" << endl;
cout << " " << fx->out_tegn() << "-(x,y)" << endl;
cout << " " << fx->out_tegn() << "-(x,y)" << endl;
cout << "-----------------" << endl;
cout << endl;
cout << "V‘lg din graf type:" << endl;
cout << "Type: ";
cin >> type;
fx->in_type(type);
if (fx->out_type() == 3) {
fx->in_xy(1);
}
else {
do {
cout << "Skal der vises x og y koordinater?" << endl;
cout << "V‘lg J/N: ";
cin >> xy;
fx->in_xy(xy);
} while (xy != 'j' && xy != 'J' && xy != 'n' && xy != 'N');
}
cout << "in_func";
getch();
}
void tegn_func() {
clrscr(); // Jeg clear skærmbilledet
// Finder y_min og y_max inden for intervallet - x_min og x_max
for (float xi = fx->out_x_min(); xi <= fx->out_x_max(); xi++) {
float yi = fx->out_a() * xi * xi + fx->out_b() * xi + fx->out_c();
if (yi < fx->out_y_min()) {
fx->in_y_min(yi);
}
if (yi > fx->out_y_max()) {
fx->in_y_max(yi);
}
if (fx->out_y_min() < 0) {
fx->in_xm(fx->out_y_min() * -1);
}
}
float sk_x;
float sk;
int sk_index = 65; // 79
for (float xs = fx->out_x_min(); xs <= fx->out_x_max(); xs++) {
sk_x = fx->out_a() * xs * xs + fx->out_b() * xs + fx->out_c();
if (sk_x > fx->out_y_max()) {
fx->in_y_max(sk_x);
}
if (sk_x < fx->out_y_min()) {
fx->in_y_min(sk_x);
}
sk = ((fx->out_y_max() - fx->out_y_min()) / sk_index);
}
cout << "Skalar: "<< sk << endl;
for (int x = fx->out_x_min(); x <= fx->out_x_max(); x++) {
float y = fx->out_a() * x * x + fx->out_b() * x + fx->out_c();
float ym = y + fx->out_xm();
if ((fx->out_xy() == 'J') || (fx->out_xy() == 'j')) {
if ((x >= 0) && (x < 10)) {
cout << " ";
}
cout << x << " ";
}
for (float y_tael = ym; y_tael >= 0; y_tael--) {
if (fx->out_type() == 1) {
cout << fx->out_tegn();
}
else if ((fx->out_type() == 2) || (fx->out_type() == 3)) {
cout << " ";
}
else {
cout << endl << "Der er noget galt" << endl;
}
y_tael = y_tael - sk; // Hvorfor ?
}
if (fx->out_type() == 2) {
cout << fx->out_tegn();
}
if (fx->out_type() == 3) {
cout << fx->out_tegn() << "-(" << x << "," << y << ")";
}
if ((fx->out_xy() == 'j') || (fx->out_xy() == 'J')) {
cout << y;
}
cout << endl;
}
cout << "tegn_func";
getch();
}
void show_func() {
cout << endl;
cout << " ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»" << endl;
cout << " º Show Menu: º" << endl;
cout << " ÌÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹" << endl;
cout << " º01:º " << fx->out_a() << " º" << endl;
cout << " º02:º " << fx->out_b() << " º" << endl;
cout << " º03:º " << fx->out_c() << " º" << endl;
cout << " º04:º " << fx->out_y_min() << " º" << endl;
cout << " º05:º " << fx->out_y_max() << " º" << endl;
cout << " º06:º " << fx->out_x_min() << " º" << endl;
cout << " º07:º " << fx->out_x_max() << " º" << endl;
cout << " ÈÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ" << endl;
cout << endl;
cout << &fx << endl << &ix << endl << & ox << endl << px << endl;
getch();
}
void delete_func() {
delete fx;
delete ix;
delete ox;
delete px;
}
Jeg har det problem at den ikke altid skifter pointerne og at den bare generelt arbejder underligt.. Nogen der ved hvad der kan være galt?