按:这是信息安全原理课程的第二次作业,作业要求是Write a +-*/ algorithm for large integers. Implement the DH algorithm.
2014-4-27更新:过了DDL了,重新公开原先隐藏的代码(只是现在不能在代码片中打开,有人能告诉我要怎么做吗?)。补上了所有的图片。
Large integer algorithm
How to use
Open “\SEC2014-HW2-3130000064-林涛\large_integer_algorithm\large_integer_algorithm.exe”,input the value of A and B (it can be as long as 800 numbers, negative is alsoavailable). You can see the results of addition, subtraction, multiplication,division and modulus.
Figure1 test for general largeintegers
Figure2 divide zero
Figure3 negative cases
Figure4 A is zero
Figure5 interesting case
Figure6 A has 300 numbers and Bhas 200 numbers
Algorithm
Use character array to store large number,and use the same way as primate school studens.
We can optimize the multiplicationalgorithm by calculating A*1, A*2 …, A*9 first, which don’t have to becalculated again and again.
Source code
large_integer_algorithm.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 800
char answer[2 * SIZE];
char remainder[SIZE];
//a,b都为自然数,且a>=b
void sum1(char *a, char *b)
{
int carry = 0;
int pa, pb;
int temp;
int i;
int na = strlen(a);
int nb = strlen(b);
answer[na] = '\0';
pa = na - 1; pb = nb - 1;
while (pa >= 0) {
temp = (a[pa] - '0') + carry;
if (pb >= 0)temp += (b[pb] - '0');
if (temp >= 10) {
answer[pa] = temp - 10 + '0';
carry = 1;
}
else {
answer[pa] = temp + '0';
carry = 0;
}
pa--; pb--;
}
if (carry) {
for (i = na; i >= 0; i--)
answer[i + 1] = answer[i];
answer[0] = '1';
}
}
void sub1(char *a, char *b)
{
int borrow = 0;
int pa, pb, pc;
int temp;
int i;
int na = strlen(a);
int nb = strlen(b);
answer[na] = '\0';
pa = na - 1; pb = nb - 1;
while (pa >= 0) {
temp = (a[pa] - '0') - borrow;
if (pb >= 0)temp -= (b[pb] - '0');
if (temp < 0) {
answer[pa] = temp + 10 + '0';
borrow = 1;
}
else {
answer[pa] = temp + '0';
borrow = 0;
}
pa--; pb--;
}
pc = 0;
while (answer[pc] == '0')
pc++;
for (i = 0; i <= na - pc; i++)
answer[i] = answer[i + pc];
}
//a,b都为自然数
void sum(char *a, char *b)
{
int na = strlen(a);
int nb = strlen(b);
if (na >= nb)
sum1(a, b);
else
sum1(b, a);
}
void sub(char *a, char *b)
{
int na = strlen(a);
int nb = strlen(b);
if (strcmp(a, b) == 0)
strcpy(answer, "0");
else if (na > nb || na == nb && strcmp(a, b) > 0)
sub1(a, b);
else {
printf("-");
sub1(b, a);
}
}
//a,b都是正整数
void mul(char *a, char *b)
{
char muls[10][SIZE + 1];
char temp[2 * SIZE];
int i, j;
int na = strlen(a);
int nb = strlen(b);
strcpy(muls[0], "0");
strcpy(temp, "0");
for (i = 1; i <= 9; i++) {
sum(muls[i - 1], a);
strcpy(muls[i], answer);
}
strcpy(answer, "0");
for (i = nb - 1; i >= 0; i--) {
if (b[i] != '0') {
strcpy(temp, muls[b[i]-'0']);
//后面加(nb-i-1)个零
for (j = 0; j < nb - i - 1; j++)
strcat(temp, "0");
sum(answer, temp);
}
}
printf("%s\n", answer);
}
//a,b为自然数,若a>b返回1,不然0
int larger(char *a, char *b)
{
int na, nb;
na = strlen(a);
nb = strlen(b);
if (na > nb)
return 1;
else if (na == nb && strcmp(a, b) > 0)
return 1;
else
return 0;
}
void divide(char *a, char*b)
{
int na, nb; na = strlen(a); nb = strlen(b);
char quotient[SIZE];
char temp[2]; temp[1] = '\0';
int tryq;
int i;
char muls[10][SIZE + 1];
strcpy(muls[0], "0");
strcpy(temp, "0");
for (i = 1; i <= 9; i++) {
sum(muls[i - 1], b);
strcpy(muls[i], answer);
}
strcpy(quotient, "");
strcpy(remainder, "");
tryq = 1;
i = 0;
while (i != na) {
temp[0] = a[i];
strcat(remainder, temp);
tryq = 1;
//remainder>=muls[tryq]
while (tryq!=10 && !larger(muls[tryq], remainder))
tryq++;
temp[0] = tryq -1 + '0';
if (!(temp[0] == '0' && quotient[0] == '\0'))
strcat(quotient, temp);
sub(remainder, muls[tryq - 1]);
strcpy(remainder, answer);
if (remainder[0] == '0')
remainder[0] = '\0';
i++;
}
if (quotient[0] == '\0')
strcpy(quotient, "0