等价的C程序如下,deque类的front(),push_back()等方法由等价的C自定义函数实现。
//---------------------------------------------------------------------------
#include
#include
typedef struct Node
{
double val;
int deep;
int order;
struct Node *next;
} Node;
typedef struct deque{
int count;
Node *head;
} deque;
void print(Node *p)
{
printf("%d %d %f\n", p->deep, p->order-1, p->val);
}
void push_back(deque *dq,Node no)
{
Node *t,*t2,*a=malloc(sizeof(Node));
a->val =no.val ;
a->deep =no.deep ;
a->order =no.order ;
dq->count++;
if (dq->head ==NULL) {
dq->head =a;
}
else{
t2=t=dq->head ;
while (t !=NULL){
t2=t;
t=t->next ;
}
a->next =t ;
t2->next =a;
}
}
Node *front(deque *a)
{
return a->head ;
}
void pop_front(deque *a)
{
Node *t=a->head ;
a->head =a->head ->next ;
a->count --;
free(t);
}
int empty(deque *a)
{
return a->count ;
}
int main()
{
int s0,i,t=0,*s,dep;
deque deq;
Node node,*temp,p,q;
double u;
deq.count =0;
deq.head =NULL;
scanf("%d%lf%d",&s0,&u,&t);
++t;
s =malloc(sizeof(int)*t);
for( i= 0; i < t; ++i )
s[i] = 0;
node.val = s0;
node.deep = 0;
node.order = 1;
push_back(&deq,node);
s[0] = 1;
while( !empty(&deq) )
{
temp=front(&deq);
pop_front(&deq);
dep = temp->deep+1;
if( dep == t + 1 )
break;
print(temp);
if( s[dep] == 0 )
{
++s[dep];
p.val = temp->val * u;
p.deep = temp->deep + 1;
p.order = s[dep];
push_back(&deq,p);
}
++s[dep];
q.val = temp->val / u;
q.deep = temp->deep + 1;
q.order = s[dep];
push_back(&deq,q);
}
scanf("%d",&s0);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include
/*
#include
#include
using namespace std;
*/
struct Node
{
double val;
int deep;
int order;
};
void print(Node p)
{
printf("%d,%d,%f\n", p.deep, p.order - 1, p.val);
//cout << p.deep << "," << p.order-1 << "," << p.val << endl;
}
int main()
{
int s0,t;
deque
double u;
scanf("%d%lf%d",&s0,&u,&t);
++t;
int *s = (int*)malloc(t);
//int *s = new int[t];
for( int i= 0; i < t; ++i )
s[i] = 0;
Node node;
node.val = s0;
node.deep = 0;
node.order = 1;
deq.push_back(node);//类,同上
s[0] = 1;
while( !deq.empty() )//类,同上
{
Node temp;//这里好象错了。。要用struct Node temp;吧?
temp = deq.front();//类,同上
deq.pop_front();//类,同上
int dep = temp.deep+1;
if( dep == t + 1 )
break;
print(temp);//这个是自己定义的函数。。。
if( s[dep] == 0 )
{
Node p;
++s[dep];
p.val = temp.val * u;
p.deep = temp.deep + 1;
p.order = s[dep];
deq.push_back(p);//类,同上
}
Node q;
++s[dep];
q.val = temp.val / u;
q.deep = temp.deep + 1;
q.order = s[dep];
deq.push_back(q);//类,同上
}
scanf("%d", s0);
//cin >> s0;
}
问题补充:什么意思?你不是要把c++改成c吗? 我就在你的程序上改了。c++有类,c没有,c和c++区别也不是很大的啊,c++是扩展了c。
你把我注释的和类都去掉就是c语言的程序了。