55
rest(int a[], int n)
{
int i,low,high,t;
for(i=0,low=0,high=n-1;i<=high;)
{
if(a[i]>0)
{
/*a[i]与a[high]交换,随之high减1*/
t=a[i];
a[i]=a[high];
a[high]=t;
high--;
}
else if(a[i]==0)
i++; /* 掠过该元素 */
else
{
/*a[i]与a[low]交换,随之low增1, i增1*/
t=a[i];
a[i]=a[low];
a[low]=t;
low++;
i++;
}
}
}
int s[]={8,4,0,-1,6,0,-5};
main()
{
int i;
clrscr();
printf("\n The arry before rest is:\n");
for(i=0;i<sizeof(s)/sizeof(s[0]);i++)
printf("%4d",s[i]);
rest(s,sizeof(s)/sizeof(s[0]));
printf("\n The arry after rest is:\n");
for(i=0;i<sizeof(s)/sizeof(s[0]);i++)
printf("%4d",s[i]);
printf("\n Press any key to quit...\n");
getch();
}
56
rest(int a[], int n)
{
int i,low,high,t;
for(i=0,low=0,high=n-1;i<=high;)
{
if(a[i]>0)
{
/*a[i]与a[high]交换,随之high减1*/
t=a[i];
a[i]=a[high];
a[high]=t;
high--;
}
else if(a[i]==0)
i++; /* 掠过该元素 */
else
{
/*a[i]与a[low]交换,随之low增1, i增1*/
t=a[i];
a[i]=a[low];
a[low]=t;
low++;
i++;
}
}
}
int s[]={8,4,0,-1,6,0,-5};
main()
{
int i;
clrscr();
printf("\n The arry before rest is:\n");
for(i=0;i<sizeof(s)/sizeof(s[0]);i++)
printf("%4d",s[i]);
rest(s,sizeof(s)/sizeof(s[0]));
printf("\n The arry after rest is:\n");
for(i=0;i<sizeof(s)/sizeof(s[0]);i++)
printf("%4d",s[i]);
printf("\n Press any key to quit...\n");
getch();
}
57
#define M sizeof(unsigned int)*8
int trans(unsigned n, int d, char s[])
{
static char digits[] ="0123456789ABCDEF"; /* 十六进制数字的字符 */
char buf[M+1];
int j, i = M;
if(d<2||d>16)
{
s[0]='\0'; /* 不合理的进制,置s为空字符串 */
return 0; /* 不合理的进制,函数返回0 */
}
buf[i]='\0';
do
{
buf[--i]=digits[n%d]; /*译出最低位,对应字符存入对应工作数组中*/
n/=d;
}while(n);
/* 将译出在工作数组中的字符串复制到s */
for(j=0;(s[j]=buf[i])!='\0';j++,i++);
/* 其中控制条件可简写成s[j]=buf[i] */
return j;
}
/* 主函数用于测试函数 trans() */
main()
{
unsigned int num = 253;
int scale[]={2,3,10,16,1};
char str[33];
int i;
clrscr();
for(i=0;i<sizeof(scale)/sizeof(scale[0]);i++)
{
if(trans(num,scale[i],str))
printf("%5d = %s(%d)\n",num,str,scale[i]);
else
printf("%5d => (%d) Error! \n",num,scale[i]);
}
printf("\n Press any key to quit...\n");
getch();
}