PDA

View Full Version : مشکل با اعداد اعشاری



root88
دوشنبه 05 مهر 1389, 15:53 عصر
دوستان این قسمتی از کد هست تو کلاس اول یک ایتم تولید شده که یک شناسه و یک کران پایین و بالای برای احتمال ایتم داره. تو کلاس دوم یه لیست تعریف شده که شامل تعدادی ایتم از کلاس قبله. این کلاس یه تابع داره به اسم SetEqualProb که میاد مقدار 1 تقسیم بر تعداد گره های لیست رو به عنوان احتمال هر گره به ایتم های لیست تخصیص میده. تو یه قسمت دیگه از برنامه یه تابع دارم که یه عدد تصادفی بین 0 تا 100 تولید میکنه و به 100 تقسیمش میکنه تا عددی بین 0 و1 به دست بیاد بعد باید تو این لیست بگرده و گره ای که این احتمال تو محدوده اون قرار داره برگردونه. من این تابع رو نوشتم اما درست کار نمیکنه.یعنی محدوده اعداد درست نیست.وقتی که یکی از عناصر لیست رو حذف میکنم باید دوباره احتمال رو تا یک داشته باشم برای همین احتمال هر گره رو به مجموعه احتمالات تقسیم میکنم تا احتمال جدید به دست بیاد که محدوده اعداد باز درست در نمی یاد. لطفا یکی کمکم کنه.


class A
{
int Id;
double Prob,low,high;
public:
A(int Id,double low,double high)
{
this->Id=Id;
this->low=low;
this->high=high;
}//----
void setId(int v){Id=v;}
void setLow(double v) {low=v;}
void setHigh(double v) {high=v;}
void setProb(double v){Prob=v;}
int getId() {return Id;}
double getLow() {return low;}
double getHigh() {return high;}
double getProb() {Prob=high-low; return Prob;}
};
//----
class AA
{
public:
list <A> actionList;
list <A> InactionList;
list <A>::iterator it;
list <A>::iterator it1;
list<double> k;
double totalProb;
void Add(A ac)
{
it=actionList.end();
actionList.insert(it,ac);
}//----
void Remove(int Id)
{
for(it=actionList.begin();it!=actionList.end();it+ +)
{
if(it->getId()==Id)
{InactionList.push_back(*it);actionList.erase(it); break;}
}
}//----
double fround( double num, double d )
{return floor( num * pow( 10, d ) + 0.5) / pow( 10, d );}

void setEqualProb()
{
int i=1;
double x,low=0;
x=(double)1/actionList.size();
for(it=actionList.begin();it!=actionList.end();it+ +)
{it->setLow(low);
it->setHigh(i*x-0.01000000);
low=i*x;
i++;
}
}//----
A& getAction(int id)
{
A act(0,0,0);
for(it=actionList.begin();it!=actionList.end();it+ +)
if(it->getId()==id)
return *it;
}//----
void setNewProb()
{
double low=0,high=0;
totalProb=0;
for(it=actionList.begin();it!=actionList.end();it+ +)
totalProb +=it->getProb();
totalProb=fround(totalProb,4);
k.push_back(totalProb);
for(it=actionList.begin();it!=actionList.end();it+ +)
{
high=fround((it->getProb()/totalProb),4);
it->setLow(low);
it->setHigh(fround(high+low,4));
low=fround((high+low)+0.01,4);
}
}//----
};
int main()
{

AA actions;
A action(2,3,4);
actions.Add(action);
A action2(3,3,4);
actions.Add(action2);
A action1(4,0,0);
actions.Add(action1);
A action6(5,3,4);
actions.Add(action6);
A action11(8,3,4);
actions.Add(action11);
A action12(9,3,4);
actions.Add(action12);
actions.setEqualProb();
}