i--------> làm chán wa minh khong biết đếm cả số 0 vào các so duong nên chết "Nhap ds so nguyen tinh trung binh các số dương so and số am"
Mã:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
typedef struct sn
{ int so;
sn *next;
} ;
sn *L;
void taods(int x,sn **T)
{
sn*p;
p=new sn;
p->so=x;
p->next=(*T);
(*T)=p;
}
void xuatds(sn*T)
{
sn*p;
p=T;
while(p!=NULL)
{
cout<<" "<<p->so;
p=p->next;
}
}
void tb(sn*T)
{
sn*p;
p=T;
int da=0,ta=0;
int dd=0,td=0;
while(p!=NULL)
{
if(p->so<0)
{
da=da+1;
ta=ta+p->so;
p=p->next;
}
else {
if(p->so>0)
{
dd=dd+1;
td=td+p->so;
p=p->next;
}
else
p=p->next;
}
}
cout<<endl<<"Tong (-) la :"<<float(ta)/da<<endl;
cout<<"Tong (+) la :"<<float(td)/dd<<endl;
}
void main()
{
int x;char ch;
clrscr();
L=NULL;
do
{
cout<<"Nhap phan tu :";
cin>>x;
taods(x,&L);
cout<<"Nhap tiep hay khong ('y' or 'k')";
cin>>ch;
}while(ch!='k');
cout<<endl<<"Danh sach vua tao:"<<endl;
xuatds(L);
tb(L);
getch();
}[-X