Ich mache eine frühere Aufgabe in C++ und versuche herauszufinden, wie man das richtige ASCII X-Y Diagramm erhält.Wie organisiert man das Ergebnis von X- und Y-Koordinaten als ASCII-X-Y-Plot?
Also was wirklich passiert, ist, dass Benutzer ein Menü erhalten, das ihnen erlaubt, eine bestimmte trig Funktion zu wählen. Alle Funktionen haben einen Bereich zwischen [-4..6] für X und [-12 ... 5] für Y.Next Benutzer wird erlaubt, die Menge der Abstufungen (oder Werte zwischen den eingeschränkten x und y-Bereich) zu wählen, und wenn sie resultierende Werte oder Bitmap sehen wollen.Die endgültige Ausgabe wird in Werten/Bitmap sein. Ich habe Wolfram alpha eingefügt Link für die Funktionen in den Kommentaren
Was ich getan habe, ist jede Spalte in 2D-Ausgabe um Produkt von 1/(Graduierung-1) erhöht (dh 1/3, wenn der Wert 4 ist) und Spalte # .Einmal dieses Produkt erreicht 1, ich bekomme keine korrekte output für die Werte
#include <iostream>
#include <cmath>
using namespace std;
double minX=-4;
double maxX=6;
double minY=-12;
double maxY=5;
void displayValues(double result[],int result_size,int graduationVal,double percentIncrease,int menuSelection){
int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;
int k=0;
int i=0;
int division=0;
//Add Values into array until result-1 values
while(i < result_size){
//cout<<"Before j";
//Calculate all the horizontal axis values
for(int j=0; j< graduationVal;j++){
if(displaySelection==1){
//cout<<"In if";
if(menuSelection==1){
result[i]=sin(minX+(percentIncrease*j))*cos(minY+(percentIncrease*k));
cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";
//cout<<sin(minX+(percentIncrease*j))<<"\n";
//cout<<cos(minY+(percentIncrease*k))<<"\n";
cout<<result[i]<<" ";
}else if(menuSelection==2){
result[i]=sin(minX+(percentIncrease*j))+pow(cos(minY+(percentIncrease*j)),2)-(minX+(percentIncrease*j))/(minY+(percentIncrease*k));
cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";
cout<<result[i]<<" ";
}else if(menuSelection==3){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(0.5 *cos(minY+(percentIncrease*k)));
cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";
cout<<result[i]<<" ";
}else if(menuSelection==4){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(minX+(percentIncrease*j)) * cos(3 * (minY+(percentIncrease*k)));
cout<<result[i]<<" ";
}
//cout<<"J is"<<j<<"\n";
//cout<<"K is"<<k<<"\n";
//cout<<"I is"<<i<<"\n";
}else{
if(menuSelection==1){
result[i]=sin(minX+(percentIncrease*j))*cos(minY+(percentIncrease*k));
cout<<((result[i] > 0)?"O":"X");
}else if(menuSelection==2){
result[i]=sin(minX+(percentIncrease*j))+pow(cos(minY+(percentIncrease*j)),2)-(minX+(percentIncrease*j))/(minY+(percentIncrease*k));
cout<<((result[i] > 0)?"O":"X");
}else if(menuSelection==3){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(0.5 *cos(minY+(percentIncrease*k)));
cout<<((result[i] > 0)?"O":"X");
}else if(menuSelection==4){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(minX+(percentIncrease*j)) * cos(3 * (minY+(percentIncrease*k)));
cout<<((result[i] > 0)?"O":"X");
}
}//End display choice if
//Increment Array Index
i++;
//cout<<"Bottom of j";
}//End of j loop
cout<<"\n";
//Increment y-values
if(k<graduationVal){
k++;
}
}//End of While
}
int main(){
int menuChoice=-1;
int displaychoice=0;
double distanceFromMinMaxX=4+6;
double distanceFromMinMaxY=12+5;
int graduations=0;
// double precisionX;
// double precisionY;
double pctIncrease;
while(menuChoice!=0){
cerr<<"Select your function\n";
cerr<<"1. sin(x)cos(y)\n";
cerr<<"2. sin(x)+cos^2(x)-x/y\n";
cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
cerr<<"0. Quit\n";
cin>>menuChoice;
if(menuChoice == 0){
return 0;
}
cerr<<"Number of graduations per axis: ";
cin>>graduations;
pctIncrease=1/(double)(graduations - 1);
int values_size=graduations * graduations;
double values[values_size];
/*int yValues[graduationVal];*/
// precisionX=distanceFromMinMaxX/graduations;
// precisionY=distanceFromMinMaxY/graduations;
displayValues(values,values_size,graduations,pctIncrease,menuChoice);
}
}
Edit:
#include <iostream>
#include <cmath>
using namespace std;
double minX=-4;
double maxX=6;
double minY=-12;
double maxY=5;
double* calculateValues(double val[],int val_size,int graduationVal,double xPrecision,double yPrecision,int menuSelection){
/*int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;*/
int k=0;
int i=0;
//Add Values into array until result-1 values
while(i < val_size){
for(int j=0; j<graduationVal;j++){
if(menuSelection==1){
val[i]=sin(minX+(xPrecision*j))*cos(minY+(yPrecision*k));
}else if(menuSelection==2){
val[i]=sin(minX+(xPrecision*j))+pow(cos(minY+(xPrecision*j)),2)-(minX+(xPrecision*j))/(minY+(yPrecision*k));
}else if(menuSelection==3){
val[i]=(0.5 * sin(minX+(xPrecision*j)))+(0.5 *cos(minY+(yPrecision*k)));
}else if(menuSelection==4){
val[i]=(0.5 * sin(minX+(xPrecision*j)))+(minX+(xPrecision*j)) * cos(3 * (minY+(yPrecision*k)));
}
//Increment Array Index
i++;
}//End of j loop
//Increment y-values
if(k<graduationVal){
k++;
}
}//End of While
return val;
}
void displayValues(double result[],int result_size,int numOfGraduations){
int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;
int k=0;
int i=0;
while(i< result_size){
for(int j=0;j<numOfGraduations;j++){
if(displaySelection==1){
cout<<result[i]<<" ";
}else{
cout<<((result[i] > 0)?"O":"X");
}
i++;
}
cout<<"\n";
}
}
int main(){
int menuChoice=-1;
int displaychoice=0;
double distanceFromMinMaxX=4+6;
double distanceFromMinMaxY=12+5;
int graduations=0;
double precisionX;
double precisionY;
double pctIncrease;
while(menuChoice!=0){
cerr<<"Select your function\n";
cerr<<"1. sin(x)cos(y)\n";
cerr<<"2. sin(x)+cos^2(x)-x/y\n";
cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
cerr<<"0. Quit\n";
cin>>menuChoice;
if(menuChoice == 0){
return 0;
}
cerr<<"Number of graduations per axis: ";
cin>>graduations;
pctIncrease=1/(double)(graduations - 1);
int values_size=graduations * graduations;
double values[values_size];
/*int yValues[graduationVal];*/
precisionX=distanceFromMinMaxX/graduations;
precisionY=distanceFromMinMaxY/graduations;
/*cout << "# of graduations: " << graduations << endl;
cout << "Precision: "<< endl;
cout << "x: " << precisionX << endl;
cout << "y: " << precisionY << endl;*/
calculateValues(values,values_size,graduations,precisionX,precisionY,menuChoice);
displayValues(values,values_size,graduations);
}
}
Edit: Ich benutze gcc
"Ich bekomme immer noch keine gewünschte Ausgabe." Es wird hilfreich sein zu beschreiben, was die Ausgabe ist, die Sie wünschen. –
Hallo. Ich habe ein Bild für die Ausgabe hinzugefügt, die ich versuche, aber es funktioniert nicht. –
Das Bild, das Sie verlinkt haben, sagt mir nichts. Verwenden Sie Wörter. –