我试图让这个程序正常工作,因为它需要计算链表中输入的连分数。该程序没有显示任何错误,但它总是在中间崩溃。有人能帮我吗?
任务只是将程序存储在链表上,然后通过从链接中获取参数来计算它。它是一个连分数。
#include <iostream>
#include <cmath>
using namespace std;
struct node
{
int data;
node *next;
} *h=nullptr, *t=nullptr;
float calculation (int co)
{
float a,c;
node *b,*f;
b->next = f;
while(co != 0)
{
f = t;
a = (float)f->data;
a = 1/a;
c = (float)b->data;
a = c + a;
t = b;
co--;
}
return a;
}
void storedata (int& c)
{
node *n = new node;
n->data = c;
n->next = nullptr;
cout<<n->data<<endl;
if(h==nullptr)
{
h=n;
t=n;
n=nullptr;
}
else
{
t->next=n;
t=n;
}
}
void formula (int a, int b, int co)
{
int c;
int z;
while (co!=0)
{
c = a/b;
storedata(c);
z = b*c;
z = a-z;
a = b;
b = z;
co--;
}
}
int main ()
{
int a,b,c,z,co,d;
float e;
a = 123;
b = 100;
co = 5;
formula (a,b,co);
e = calculation(co);
cout<<"cf1 = 123/100 ="<<e;
}