我替一位仁兄改的n进制数相加的问题

下面是我替一位仁兄改的n进制数相加的问题的程序,保存下来也供以后自己参考。

如果哪位仁兄觉得有不恰之处,欢迎赐教!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int abc(char x[], char y[], int n)
{
 char z[80];
 int w = strlen(x);
 for(int i = w-1; i >= 0;  i--)
 {
  char temp_x[2],temp_y[2];
  temp_x[0]=x[i];
  temp_x[1]='/0';
  temp_y[0]=y[i];
  temp_y[1]='/0';
  char temp_total[2];
  if( (atoi(temp_x) + atoi(temp_y) ) >= n)
  {
   char temp[2];
   temp[0]=x[i-1];
   temp[1]='/0';
    
   _itoa( atoi(temp) + 1,  temp_total, 10);
   x[i-1] =temp_total[0];
   _itoa( (atoi(temp_x) + atoi(temp_y) )%n, temp_total,10);
   z[i] = temp_total[0];
  }
  else
  {
   _itoa( atoi( temp_x ) + atoi( temp_y ),temp_total,10);
   z[i] =temp_total[0];
  }
 }
 return atoi(z);//转为int返回。
}

int main ()
{
 char x[80];
 char y[80];
 int n;
 int total;

 printf("please input n: ");
 scanf("%d",&n);
 printf("please input string x: ");
 scanf("%s",x);
 printf("please input string y: ");
 scanf("%s",y);
 
 total=abc(x,y,n);
 printf("the result is: %d/n", total) ;

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值