306905482,

问题描述:

306905482,

5-1
#include
#include
#include
typedef struct{
unsigned shortA;//百位
unsigned shortB;//十位
unsigned shortC;//个位
}Digital_t;
typedef struct{
Digital_tDigInfo;
unsigned shortData;//数据
unsigned short Key;
}Hundred_t;
#define Divide(pData)\
(pData)->DigInfo.A = (pData)->Data / 100;\
(pData)->DigInfo.B = ((pData)->Data - (pData)->DigInfo.A * 100) / 10;\
(pData)->DigInfo.C = (pData)->Data - ((pData)->DigInfo.A * 100) - ((pData)->DigInfo.B *10);
// Function Declaration
int DigitalSort(Hundred_t *pDataList, int num);
int sort(Hundred_t *pDataList,int num);
int output(Hundred_t *pDataList, int iDataNum );
int main(int argc, char* argv[])
{
intiDataNum, iDataSize;
inti;
Hundred_t*pDataList = NULL;
printf("请输入个数:\n");
scanf("%d", &iDataNum);
iDataSize = sizeof(Hundred_t) * iDataNum;
pDataList = (Hundred_t*) malloc(iDataSize);
if (pDataList ==NULL){
return -1;
}
memset((void *)pDataList, 0, iDataSize);
printf("请输入数字:\n");
// input
for (i = 0; i {
scanf("%d", &((pDataList + i)->Data));
}
// sort
DigitalSort(pDataList, iDataNum);
// output
printf("\n最后排序结果:");
output(pDataList,iDataNum);
free(pDataList);
getchar();
return 0;
}
// sort function
int DigitalSort(Hundred_t *pDataList, int num)
{
int i;
// divide digital
for ( i= 0; i {
Divide(pDataList+i);
}
//个位
for ( i= 0; i {
(pDataList+i)->Key = (pDataList+i)->DigInfo.C;
}
sort(pDataList, num);
printf("个位排序:");
output(pDataList,num);
//十位
for ( i= 0; i {
(pDataList+i)->Key = (pDataList+i)->DigInfo.B;
}
sort(pDataList, num);
printf("十位排序:");
output(pDataList,num);
//百位
for ( i= 0; i {
(pDataList+i)->Key = (pDataList+i)->DigInfo.A;
}
sort(pDataList, num);
printf("百位排序:");
output(pDataList,num);
return 0;
}
int sort(Hundred_t *pDataList,int num)
{
inti,j;
Hundred_tPD;
for (i=0; i{
for (j=0; j{
if ((pDataList+j)->Key > (pDataList+j+1)->Key)
{
PD = *(pDataList + j);
*(pDataList + j) = *(pDataList + j +1);
*(pDataList + j +1) = PD;
}
}
}
return 0;
}
int output(Hundred_t *pDataList, int iDataNum )
{
int i;
for (i = 0; i {
printf("%d ", (pDataList + i)->Data);
}
printf("\n");
return 0;
}
7-1
typedef struct
{
unsigned long low;
unsigned long high;
}Bit64_t;
int ADD(Bit64_t *Ope1, Bit64_t *Ope2, Bit64_t *Result)
{
//容错处理
if ((0xFFFF - Ope1->high) high)
{
// 发生溢出
printf("发生溢出");
return -1;
}
if ((0xFFFF - Ope1->low) >= Ope2->low)
{
Result->low= Ope1->low + Ope2->low;
Result->high= Ope1->high + Ope2->high;
}
else//有进位
{
Result->low= Ope1->low + Ope2->low - 0x10000;
Result->high= Ope1->high + Ope2->high + 1;
}
return 0;
}
int SUB(Bit64_t *Minuend, Bit64_t *Subtrahend, Bit64_t *Result)
{
if ( ( Minuend->high high) ||
(( Minuend->high == Subtrahend->high) && ( Minuend->low low))
)
{
// 发生溢出
printf("发生溢出");
return -1;
}
if ( Minuend->low >= Subtrahend->low)
{
Result->low= Minuend->low - Subtrahend->low;
Result->high= Minuend->high - Subtrahend->high;
}
else
{
//注意0xFFFF - Subtrahend->low + 1不能颠倒顺序
Result->low= Minuend->low + (0xFFFF - Subtrahend->low + 1);
Result->high= Minuend->high - Subtrahend->high - 1;
}
return 0;
}
int main(int argc, char* argv[])
{
Bit64_t A,B,E,F;
unsigned long a,b;
char f;
printf("输入第一个数:");
scanf("%x,%x",&A.high,&A.low);
printf("输入第二个数:");
scanf("%x,%x",&B.high,&B.low);
printf("输入符号:");
scanf("%s",&f);
if(f == '+')
{
if(-1 != ADD(&A,&B,&E))
{
if (E.high == 0)
{
printf("0x%X\n", E.low);
}
else
{
printf("0x%X%X\n", E.high, E.low);
}
}
}
else if(f == '-')
{
if(-1 != SUB(&A, &B, &F))
{
if (F.high == 0)
{
printf("0x%X\n", F.low);
}
else
{
printf("0x%X%X\n", F.high, F.low);
}
}
}
return 0;
}