Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23400
  • 博文数量: 3
  • 博客积分: 161
  • 博客等级: 入伍新兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 10:50
文章分类

全部博文(3)

文章存档

2013年(1)

2011年(2)

分类: C/C++

2011-03-31 17:37:58

  1. //============================================================================

  2. // Name : multiply.cpp

  3. // Author : Rick

  4. // Version :

  5. // Copyright : Your copyright notice

  6. // Description : Hello World in C++, Ansi-style

  7. //============================================================================


  8. #include <iostream>
  9. #include <stdlib.h>
  10. #define MAXLEN 1023

  11. using namespace std;

  12. int strlen(const char *str);
  13. void add(char *num1, char *num2, char *add);
  14. void strcpy(char *str1, char *str2);
  15. void reverse(char *str);
  16. int multipy(char *num1, char *num2, char *mul);
  17. void multipy(char *num, int n, int *mul);
  18. void strshift(char *str, int shift);

  19. int main() {


  20.     char *s=new char[MAXLEN*2+1];
  21.     char *n=new char[MAXLEN*2+1];

  22.     cout<<"input you factor s: ";
  23.     cin>>s;
  24.     cout<<"input you factor n: ";
  25.     cin>>n;

  26.     char *mul =new char[MAXLEN*2+1] ;
  27.     mul[0]='\0';
  28.     int bit=multipy(s, n, mul);
  29.     cout << "Multiply " << s << " * " << n << " = " << mul << endl;
  30.     cout<<"bits: "<<bit<<endl;
  31.     delete []mul;
  32.     delete []s;
  33.     delete []n;

  34.     system("pause");
  35.     return 0;
  36. }

  37. int strlen(const char *str) {
  38.     int len = 0;
  39.     const char *p = str;
  40.     while (*p++ != '\0') {
  41.         len++;
  42.     }
  43.     return len;
  44. }

  45. void add(char *num1, char *num2, char *add) {
  46.     int carry = 0;
  47.     int len = 0;
  48.     int len1 = strlen(num1);
  49.     int len2 = strlen(num2);
  50.     bool iscarry;
  51.     int sum;
  52.     char *temp = new char[MAXLEN * 2 + 1];

  53.     while (len1 > 0 && len2 > 0) {
  54.         len1--;
  55.         len2--;

  56.         sum = carry + num1[len1] + num2[len2] - '0' - '0';
  57.         iscarry = (sum >= 10);

  58.         temp[len++] = iscarry ? sum % 10 + '0' : sum + '0';
  59.         carry = iscarry ? sum / 10 : 0;

  60.     }
  61.     while (len1 > 0) {
  62.         len1--;
  63.         sum = carry + num1[len1] - '0';
  64.         iscarry = (sum >= 10);

  65.         temp[len++] = iscarry ? sum % 10 + '0' : sum + '0';
  66.         carry = iscarry ? sum / 10 : 0;

  67.     }
  68.     while (len2 > 0) {
  69.         len2--;
  70.         sum = carry + num2[len2] - '0';
  71.         iscarry = (sum >= 10);

  72.         temp[len++] = iscarry ? sum % 10 + '0' : sum + '0';
  73.         carry = iscarry ? sum / 10 : 0;

  74.     }
  75.     if (carry)
  76.         temp[len++] = carry + '0';
  77.     temp[len] = '\0';
  78.     reverse(temp);
  79.     strcpy(add, temp);

  80.     delete[] temp;
  81. }
  82. void multipy(char *num, int n, char *mul) {
  83.     int lenNum = strlen(num);
  84.     int len = 0;
  85.     int carry = 0;
  86.     char *temp = new char[MAXLEN * 2 + 1];
  87.     while (--lenNum >= 0) {
  88.         temp[len++] = (num[lenNum] - '0') * n + carry >= 10 ? ((num[lenNum]
  89.                 - '0') * n + carry) % 10 + '0' : (num[lenNum] - '0') * n
  90.                 + carry + '0';
  91.         carry = (num[lenNum] - '0') * n + carry >= 10 ? ((num[lenNum] - '0')
  92.                 * n + carry) / 10 : 0;

  93.     }
  94.     if (carry)
  95.         temp[len++] = carry + '0';
  96.     temp[len] = '\0';
  97.     reverse(temp);
  98.     strcpy(mul, temp);

  99.     delete[] temp;
  100. }
  101. int multipy(char *num1, char *num2, char *mul) {
  102.     char *n1;
  103.     char *n2;
  104.     char *temp;
  105.     char *sum;
  106.     int len1;
  107.     int len2;
  108.     temp = new char[MAXLEN * 2 + 1];
  109.     sum = new char[MAXLEN * 2 + 1];
  110.     temp[0] = '0';
  111.     temp[1] = '\0';
  112.     sum[0] = '0';
  113.     sum[1] = '\0';
  114.     n1 = new char[MAXLEN * 2 + 1];
  115.     n2 = new char[MAXLEN * 2 + 1];
  116.     len1 = strlen(num1);
  117.     len2 = strlen(num2);
  118.     strcpy(n1, num1);
  119.     strcpy(n2, num2);
  120.     int i = 0;
  121.     int n = 0;
  122.     if (len1 > len2)
  123.         while (--len2 >= 0) {
  124.             n = n2[len2] - '0';
  125.             multipy(n1, n, temp);
  126.             strshift(temp, i++);
  127.             add(sum, temp, sum);
  128.         }
  129.     else
  130.         while (--len1 >= 0) {
  131.             n = n1[len1] - '0';
  132.             multipy(n2, n, temp);
  133.             strshift(temp, i++);
  134.             add(sum, temp, sum);
  135.         }
  136.     strcpy(mul, sum);
  137.     delete[] temp;
  138.     delete[] sum;
  139.     delete[] n1;
  140.     delete[] n2;
  141.     return strlen(mul);

  142. }
  143. void strshift(char *str, int shift) {
  144.     int len = strlen(str);

  145.     for (int i = len; i < len + shift; i++) {
  146.         str[i] = '0';
  147.     }
  148.     str[len + shift] = '\0';

  149. }
  150. void strcpy(char *str1, char *str2) {
  151.     int len2 = strlen(str2);
  152.     int len1 = 0;
  153.     while (len1 <= len2) {
  154.         str1[len1] = str2[len1];
  155.         len1++;
  156.     }
  157.     str1[len1] = '\0';

  158. }
  159. void reverse(char *str) {
  160.     int len = strlen(str);

  161.     char tmp;
  162.     int low = 0, hi = len - 1;
  163.     while (low < hi) {
  164.         tmp = str[hi];
  165.         str[hi] = str[low];
  166.         str[low] = tmp;
  167.         low++;
  168.         hi--;
  169.     }
  170. }
阅读(1872) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:插入排序

给主人留下些什么吧!~~