我应该制作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,然后构建grapfic。我制作了一些代码,但它无法正常工作。需要帮助。
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;
}
答案 0 :(得分:0)
您的第一个循环没有将节点链接在一起,因此它们的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
类,让它为您处理内存管理:
#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;
}