//二元四则运算
int Arithmoter(int argu1, int argu2, int doit, unsigned int *P1,unsigned int *P2)
{
unsigned int res=0;
unsigned int yu=0;
switch(doit)
{
case 0:
res=argu1+argu2;
break;
case 1:
res=argu1-argu2;
break;
case 2:
res=argu1*argu2;
break;
case 3:
res=argu1/argu2;
yu=res%argu2;
break;
}
*P1=res;
*P2=yu;
return 0;
/*
//test
printf("res=%d ",res);
printf("yu=%d ",yu);
printf("*P_res1=%d ",*P_res1);
printf("*P_res2=%d ",*P_res2);
printf("%d...%d",*P1,*P2);
*/
}
void main()
{
unsigned int argu1,argu2,doit;
unsigned int res1=0;
unsigned int res2=0;
unsigned int *P_res1=&res1;
unsigned int *P_res2=&res2;
// char temp;
char Sdo[4];
Sdo[0]='+';Sdo[1]='-';Sdo[2]='*';Sdo[3]='/';
srand(time(NULL));
argu1=rand()%100;
argu2=rand()%100;
srand(time(NULL)+rand());
doit=rand()%4;
/*
Here is a test, by input arguments from keyboard.
printf("argu1:\n");
scanf("%d",&argu1);
printf("argu2:\n");
scanf("%d",&argu2);
printf("doit:\n");
scanf("%d",&doit);
*/
Arithmoter(argu1, argu2, doit, &res1, &res2);
/*
I want to reach the same output by this line with the next 3,
but it finally give wrong results.
//.....................................................
//printf("%d %c %d = %d\n",argu1,argu2,Sdo[doit],res);
//.....................................................
*/
printf("%d ",argu1);
printf("%c ",Sdo[doit]);
if (0!=*P_res2)
{
printf("%d = %d...%d\n",argu2,res1,res2);
}
else
{
printf("%d = %d\n",argu2,*P_res1);
}
system("Pause");
//scanf("%d",&temp);
}
两个数的四则运算。
最新推荐文章于 2022-12-10 14:00:34 发布