int chucun0[2];
int yuefen(int fenzi, int fenmu)//较简便的一个约分函数,功能并不完全
{
double fenmujisuan;
int i;
int z;
int y;
int x;
int fenmuchucun[999];
int fenzichucun[999];
int jiancha[999];
fenmujisuan = fenmu / fenzi;
if (fenmujisuan == (int)fenmujisuan)
{
fenzi = fenzi / fenmujisuan;
fenmu = fenmu / fenmujisuan;
goto end;
}
for (i = 1, z = 0;i <= (fenmu / 2); i++)//区分分母是否为质数
{
if (fenmu % i == 0)
{
fenmuchucun[z] = i;
z++;
}
}
for (i = 1, z = 0;i <= (fenzi / 2); i++)//区分分子是否为质数
{
if (fenzi % i == 0)
{
fenzichucun[z] = i;
z++;
}
}
for (i = 0, x = 0;i <= z;i++)
{
for (y = 0;y <= z;y++)
{
if (fenmuchucun[i] == fenzichucun[y])
{
jiancha[x] = fenmuchucun;
x++;
}
}
}
if(x==0)
{
goto end;
}
fenzi = fenzi / jiancha[x - 1];
fenmu = fenmu / jiancha[x - 1];
end:
//printf("%d/%d", fenzi, fenmu);
chucun0[0] = fenzi;
chucun0[1] = fenmu;
return(*chucun0);
}
int tongfen_jia(int fenzi1, int fenmu1, int fenzi2, int fenmu2)
{
int fenzi3;
int fenzi4;
int fenzi;
int fenmu;
fenmu = fenmu1 * fenmu2;
fenzi3 = fenzi1 * fenmu2;
fenzi4 = fenzi2 * fenmu1;
fenzi = fenzi3 + fenzi4;
return(yuefen(fenzi, fenmu));
}
int tongfen_jian(int fenzi1, int fenmu1, int fenzi2, int fenmu2)
{
int fenzi3;
int fenzi4;
int fenzi;
int fenmu;
fenmu = fenmu1 * fenmu2;
fenzi3 = fenzi1 * fenmu2;
fenzi4 = fenzi2 * fenmu1;
if (fenzi3 > fenzi4)
{
fenzi = fenzi3 - fenzi4;
}
else
{
fenzi = fenzi4 - fenzi3;
}
return(yuefen(fenzi, fenmu));
}
double jisuan(int* xuanze)
{
int yangbenkongjian = 1;
int switch1;
int fenzi[5] = { 0 };
int fenmu[5] = { 1,1,1,1,1 };
double gailv[5] = { 0 };
int chucun[2];
int chucun1[2];
//************这里要做的是检测知道的数量然后对不同情况进行计算*//////////////////////////
//**********这里有想到一个更简单的办法,但是需要点技术力*///////
if ((xuanze[1] && xuanze[2] && xuanze[3]) == 1)
{
printf("输入你知道的P(A)的概率的分子和分母,格式为分子/分母\n");
scanf_s("%d/%d", &fenzi[0], &fenmu[0]);
gailv[0] = (double)fenzi[0] / fenmu[0];
printf("输入你知道的P(B)的概率的分子和分母,格式为分子/分母\n");
scanf_s("%d/%d", &fenzi[1], &fenmu[1]);
gailv[1] = (double)fenzi[1] / fenmu[1];
printf("输入你知道的P(A-B)的概率的分子和分母,格式为分子/分母\n");
scanf_s("%d/%d", &fenzi[2], &fenmu[2]);
gailv[2] = (double)fenzi[2] / fenmu[2];
gailv[4] = gailv[0] - gailv[2];//P(AB)
gailv[3] = gailv[0] + gailv[1] - gailv[4];//P(A+B)
printf("P(A)的概率为 %d/%d,小数化为 %0.3f %%\n", fenzi[0], fenmu[0], gailv[0]);
printf("P(B)的概率为 %d/%d,小数化为 %0.3f %%\n", fenzi[1], fenmu[1], gailv[1]);
printf("P(A-B)的概率为 %d/%d,小数化为 %0.3f %%\n", fenzi[2], fenmu[2], gailv[2]);
*chucun = tongfen_jian(fenzi[0], fenmu[0], fenzi[2], fenmu[2]);
printf("P(AB)的概率为 %d/%d ,小数化为 %0.3f %%\n", chucun[0], chucun[1], gailv[4]);//P(AB)
*chucun = tongfen_jia(fenzi[0], fenmu[0], fenzi[1], fenmu[1]);
*chucun1 = tongfen_jian(chucun[0], chucun[1], fenzi[4], fenmu[4]);
printf("P(A+B)的概率为 %d/%d ,小数化为 %0.3f %%\n", chucun1[0], chucun1[1], gailv[3]);//P(A+B)
}
}
void main()
{
int fenzi1 = 0;
int fenmu1 = 1;
int fenzi2 = 0;
int fenmu2 = 1;
int xuanze[5] = { -1 };
for (;;)
{
int switch1 = 0;
printf("输入1进行模拟实验,输入2进入概率计算。\n");
scanf_s("%d", &switch1);
if (switch1 == 1)
{
moni();
}
else
{
printf("选择已知的数据。\n");
printf("1.P(A) 2.P(B) 3.P(A-B) 4.P(A+B) 5.P(AB)\n");
printf("已知则输入1,未知则输入-1即可。例如:1,-1,1,-1,1\n");
scanf_s("%d,%d,%d,%d,%d", &xuanze[0], &xuanze[1], &xuanze[2], &xuanze[3], &xuanze[4]);
jisuan(xuanze);
}
}
}