构建图形并使用链表
Build the graphic and use linked list
我应该制作 2 个构建图形的代码。我已经有1张了,只是建筑图:
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
Series1-> Clear();
for( float i = -5; i <= 5; i+=S ) {
float y = exp(i*log(a));
Series1->AddXY( i , y ,"",clBlue);
}
但是第二个任务对我来说更难。我应该制作新结构
struct K {
double x;
double y;
struct K *next;
};
然后制作链表。然后将点 (x,y) 放入 StringGrid,然后构建图形。我做了一些代码,但它不能正常工作。需要帮助。
K* head = 0;
K* curr = 0;
K* vyv;
double x = 1;
double y;
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
int i=0;
for (i = 0; i < 500; ++i) {
if (i==0) {
y = exp(x*log(a));
head = (K*) malloc(sizeof K);
head->x = x;
head->y = y;
head->next = NULL;
}
else {
y = exp(x*log(a));
curr = (K*) malloc(sizeof K);
curr->x = x;
curr->y = y;
curr->next = NULL;
}
x++;
}
vyv = head;
i = 0;
int l = 0, I = 0;
while(vyv){
x = vyv->x;
StringGrid1->Cells[i][0] = x;
y = vyv->y;
StringGrid1->Cells[i][1] = y;
Series1->AddXY( i , y ,"",clBlue);
vyv = vyv->next;
++i;
}
您的第一个循环没有将节点链接在一起,因此它们的 next
值为 NULL
,因此您的第二个循环仅运行 1 次迭代(对于 head
节点).
而且您还应该使用 new
而不是 malloc()
。
试试这个:
K* head = 0;
K* curr = 0;
K* last = 0;
double x = 1;
double y;
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
int i;
for (i = 0; i < 500; ++i) {
y = exp(x*log(a));
curr = new K;
curr->x = x;
curr->y = y;
curr->next = NULL;
if (!head) head = curr;
if (last) last->next = curr;
last = curr;
x++;
}
curr = head;
i = 0;
int l = 0, I = 0;
while(curr){
x = curr->x;
StringGrid1->Cells[i][0] = x;
y = curr->y;
StringGrid1->Cells[i][1] = y;
Series1->AddXY( i , y ,"",clBlue);
curr = curr->next;
++i;
}
并且不要忘记在使用完节点后释放它们:
curr = head;
while(curr){
K *next = curr->next;
//...
delete curr;
curr = next;
}
话虽如此,您应该改用 std::list
class,让它为您处理内存管理:
#include <list>
struct K {
double x;
double y;
};
std::list<K> myList;
double x = 1;
double y;
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
int i;
for (i = 0; i < 500; ++i) {
y = exp(x*log(a));
K curr;
curr.x = x;
curr.y = y;
myList.push_back(curr);
x++;
}
i = 0;
int l = 0, I = 0;
for(std::list<K>::iterator iter = myList.begin(); iter != myList.end(); ++iter)
{
x = iter->x;
StringGrid1->Cells[i][0] = x;
y = iter->y;
StringGrid1->Cells[i][1] = y;
Series1->AddXY( i , y ,"",clBlue);
++i;
}
我应该制作 2 个构建图形的代码。我已经有1张了,只是建筑图:
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
Series1-> Clear();
for( float i = -5; i <= 5; i+=S ) {
float y = exp(i*log(a));
Series1->AddXY( i , y ,"",clBlue);
}
但是第二个任务对我来说更难。我应该制作新结构
struct K {
double x;
double y;
struct K *next;
};
然后制作链表。然后将点 (x,y) 放入 StringGrid,然后构建图形。我做了一些代码,但它不能正常工作。需要帮助。
K* head = 0;
K* curr = 0;
K* vyv;
double x = 1;
double y;
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
int i=0;
for (i = 0; i < 500; ++i) {
if (i==0) {
y = exp(x*log(a));
head = (K*) malloc(sizeof K);
head->x = x;
head->y = y;
head->next = NULL;
}
else {
y = exp(x*log(a));
curr = (K*) malloc(sizeof K);
curr->x = x;
curr->y = y;
curr->next = NULL;
}
x++;
}
vyv = head;
i = 0;
int l = 0, I = 0;
while(vyv){
x = vyv->x;
StringGrid1->Cells[i][0] = x;
y = vyv->y;
StringGrid1->Cells[i][1] = y;
Series1->AddXY( i , y ,"",clBlue);
vyv = vyv->next;
++i;
}
您的第一个循环没有将节点链接在一起,因此它们的 next
值为 NULL
,因此您的第二个循环仅运行 1 次迭代(对于 head
节点).
而且您还应该使用 new
而不是 malloc()
。
试试这个:
K* head = 0;
K* curr = 0;
K* last = 0;
double x = 1;
double y;
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
int i;
for (i = 0; i < 500; ++i) {
y = exp(x*log(a));
curr = new K;
curr->x = x;
curr->y = y;
curr->next = NULL;
if (!head) head = curr;
if (last) last->next = curr;
last = curr;
x++;
}
curr = head;
i = 0;
int l = 0, I = 0;
while(curr){
x = curr->x;
StringGrid1->Cells[i][0] = x;
y = curr->y;
StringGrid1->Cells[i][1] = y;
Series1->AddXY( i , y ,"",clBlue);
curr = curr->next;
++i;
}
并且不要忘记在使用完节点后释放它们:
curr = head;
while(curr){
K *next = curr->next;
//...
delete curr;
curr = next;
}
话虽如此,您应该改用 std::list
class,让它为您处理内存管理:
#include <list>
struct K {
double x;
double y;
};
std::list<K> myList;
double x = 1;
double y;
double a = LabeledEdit1 -> Text.ToDouble();
double S = LabeledEdit2 -> Text.ToDouble();
int i;
for (i = 0; i < 500; ++i) {
y = exp(x*log(a));
K curr;
curr.x = x;
curr.y = y;
myList.push_back(curr);
x++;
}
i = 0;
int l = 0, I = 0;
for(std::list<K>::iterator iter = myList.begin(); iter != myList.end(); ++iter)
{
x = iter->x;
StringGrid1->Cells[i][0] = x;
y = iter->y;
StringGrid1->Cells[i][1] = y;
Series1->AddXY( i , y ,"",clBlue);
++i;
}