大数加,减,乘,取余一起

#include<iostream>
#include<string>
#define LEN 100
using namespace std;

void add(char a[],char b[]){
/*********************装换*********************/
int num1[LEN];
int num2[LEN];
for (int i = 0; i < LEN; i++){
num1[i] = 0;
num2[i] = 0;
}
int lena = strlen(a);
int lenb = strlen(b);

for (int i = lena - 1,j = 0; i >= 0; i--){
num1[j++] = a[i] - '0';
}

for (int i = lenb - 1,j = 0; i >= 0; i--){
num2[j++] = b[i] - '0';
}
for (int i = 0; i < LEN; i++){
num1[i] += num2[i];
}
/*****************求和*******************/
for (int i = 0; i < LEN; i++)
{
if (num1[i] >= 10){
num1[i + 1] += num1[i] / 10;
num1[i] %= 10;
}
}
/************打印************************/
int i;
for (i = LEN - 1; i > 0; i--){
if (num1[i] != 0)
break;
}
for (; i >= 0; i--)
cout << num1[i];
cout << endl;
}

void mul(char a[],char b[])
{
/****************装换****************/
int num1[LEN];
int num2[LEN];
for (int i = 0; i < LEN; i++){
num1[i] = 0;
num2[i] = 0;
}
int lena = strlen(a);
int lenb = strlen(b);

for (int i = lena - 1,j = 0; i >= 0; i--){
num2[j++] = b[i] - '0';
}
/************************乘积*************/
int sum[LEN];
for (int i = 0; i < LEN; i++){
sum[i] = 0;
}

for (int i = 0; i < lena; i++)
for (int j = 0; j < lenb; j++){
sum[i + j] += num1[i] * num2[j];
if (sum[i + j] >= 10){
sum[i + j + 1] += sum[i + j] / 10;
sum[i + j] %= 10;
}
}
/*******************打印*******************/
int i;
for (i = LEN - 1; i > 0; i--){
if (sum[i] != 0)
break;
}
for (; i >= 0; i--)
cout << sum[i];
cout << endl;
}

void mod(char a[],int mod){
///****************装换****************/
//int num1[LEN];
//int num2[LEN];
//for (int i = 0; i < LEN; i++){
// num1[i] = 0;
// num2[i] = 0;
/

相关文章

发表回复

您的电子邮箱地址不会被公开。