Hi! I have this kind of problem: My program should work like this:
1.Input the number of points that you want to create.
2.For each point write these:
name,x-position, y-position
3.Program should count the length from point to origin(0,0), it is counting in function Leng()
4.All parameters are saving in object abc
5.I create the table of objects TabOb
6. I want to sort my points from min length from point to origin(0,0), to maximum length between point and origin, I want to do this using qsort function from <stdlib.h>
I have an idea to create compare function which helps me to do that but I don’t know how to do this.. can someone help me?
I’ll give you an example of input and output that I want to get at the end:
INPUT
2
B 6 8
A 9 12
D 3 4
OUTPUT:
D 3 4
B 6 8
A 9 12
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
|
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Klasa {
string name;
int x,y;
double rr,leng;
Klasa* TabOb;//table of objects
int t;//number of points
public:
Klasa(){}
Klasa(string name, int x,int y) {
this->name = name;
this->x = x;
this->y = y;
this->leng = Leng();
}
double Leng(){
double rr, sq;
rr = x*x + y*y;
sq = sqrt(rr);
return sq;
}
int getX() { return x; }
int getY(){ return y; }
string getName() { return name; }
double getLength(){ return leng; }
int compare(const void * a, const void * b)
{
int _a = *(int*)a;
int _b = *(int*)b;
if (_a < _b) return -1;
else if (_a == _b) return 0;
else return 1;
}
void InputData(){
cin >> t;
TabOb = new Klasa[t];
for (int i = 0; i < t; i++){
string name;
int x, y;
cin >> name;
cin >> x;
cin >> y;
Klasa abc(name, x, y);
TabOb[i] = abc;
}
qsort(TabOb, t, sizeof(int), compare);
for (int i = 0; i < t; i++){
cout << TabOb[i].getName() << " " << TabOb[i].getX() << " " << TabOb[i].getY() << " " << TabOb[i].getLength() << endl;
}
}
}wy;
int main() {
wy.InputData();
return 0;
}
|