Commit 503841f0 authored by kjc015's avatar kjc015
Browse files

Lab 9 Complete

parent de68eae7
CFLAGS = -Wall -g
CC = gcc
all: ex1 ex2 ex3 ex4 ec
ex1: bigint.o ex1.o
ex2: bigint.o ex2.o
ex3: bigint.o ex3.o
ex4: bigint.o ex4.o
ec: bigint.o ec.o
clean:
/bin/rm -f core *.o $(EXE) *~
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "bigint.h"
struct bigint* bigint_init(char* value, int N){
int i;
int k = strlen(value);
struct bigint* r;
// ensure N is divisible by 4
if (N%4 > 0){
N += (4-(N%4));
}
if (k > N){
printf("ERROR: too many digits\n");
exit(1);
}
r = malloc(sizeof(struct bigint) + N);
r->N = N;
bigint_zero(r);
// copy digits from the C string value into the digits
for(i = 0; i < k; i++){
int index = k-i-1;
r->digit[i] = (int)value[index];
}
return r;
}
void bigint_zero(struct bigint* bn){
memset(bn->digit, '0', bn->N);
}
void bigint_str(char* dst, struct bigint* bn){
int i;
for(i = bn->N; i > 0; i--){
*dst++ = bn->digit[i-1];
if ( (i-1)%4 == 0){
*dst++ = ' ';
}
}
*dst = '\0';
}
void bigint_hexstr(char* dst, struct bigint* bn){
*dst= '\0';
}
// this might be useful
void full_add(char carry_in, char a, char b, char* sum, char* carry_out){
int i =0;
if (a == '1') i++;
if (b == '1') i++;
if (carry_in == '1') i++;
switch(i){
case 0: *sum = '0'; *carry_out = '0'; break;
case 1: *sum = '1'; *carry_out = '0'; break;
case 2: *sum = '0'; *carry_out = '1'; break;
case 3: *sum = '1'; *carry_out = '1'; break;
default: printf("Impossible error!\n"); exit(255);
}
}
void bigint_add(struct bigint* c, struct bigint* a, struct bigint* b){
int carry_out = 0;
int carry_in = 0;
int sum = 0;
int i;
for(i = 0; i < a->N; i++){
char digitA = a->digit[i];
char digitB = b->digit[i];
full_add(carry_in, digitA, digitB, &sum, &carry_out);
c->digit[i] = sum;
carry_in = carry_out;
if (i == 7 && carry_in == 1){
printf("Error: Overflow");
exit(255);
}
}
}
void bigint_inv(struct bigint* out, struct bigint* in){
}
void bigint_sub(struct bigint* c, struct bigint* a, struct bigint* b){
}
void bigint_rshift(struct bigint *bn, int bits){
int i;
int n = bn->N;
int temp = bn->digit[n-1];
for(i = n-1; i>0; i--){
bn->digit[i] = bn->digit[i-bits];
}
bn->digit[0] = temp;
}
void bigint_lshift(struct bigint *bn, int bits){
int i;
int n = bn->N;
int temp = bn->digit[0];
for (i = 0; i < n-1; i++){
bn->digit[i] = bn->digit[i+bits];
}
bn->digit[n-1] = temp;
}
void bigint_mult(
struct bigint* prod,
struct bigint* mcand,
struct bigint* mplier){
int i;
for (i = 0; i < prod->N; i++){
if (mplier->digit[0] == '1'){
bigint_add(prod, prod, mcand);
}
bigint_rshift(mplier, 1);
bigint_lshift(mcand, 1);
}
}
void bigint_div(
struct bigint* quotient,
struct bigint* remainder,
struct bigint* dividend,
struct bigint* divisor){
}
int bigint_iszero(struct bigint* in){
return 1;
}
struct bigint* bigint_copy(struct bigint* in){
}
void bigint_fact(
struct bigint* result,
struct bigint* in){
}
#ifndef bigint_h_
#define bigint_h_
struct bigint {
int N;
char digit[];
};
// INIT, create a bigint with value (given in binary)
struct bigint* bigint_init(char* value, int N);
// set the value to zero
void bigint_zero(struct bigint* bn);
// OUTPUT a bigint to a pre-allocated C string
void bigint_str(char* dst, struct bigint* bn);
// needed for extra credit
void bigint_hexstr(char* dst, struct bigint* bn);
struct bigint* bigint_copy(struct bigint* in);
// BITWISE OPERATIONS
void bigint_inv(struct bigint* out, struct bigint* in);
void bigint_rshift(struct bigint *bn, int bits);
void bigint_lshift(struct bigint *bn, int bits);
// LOGICAL operations
int bigint_iszero(struct bigint* in);
// ARITHMETIC
void bigint_add(struct bigint* c, struct bigint* a, struct bigint* b);
void bigint_sub(struct bigint* c, struct bigint* a, struct bigint* b);
void bigint_mult(
struct bigint* prod,
struct bigint* mcand,
struct bigint* mplier);
void bigint_div(
struct bigint* quotient,
struct bigint* remainder,
struct bigint* dividend,
struct bigint* divisor);
void bigint_fact(
struct bigint* result,
struct bigint* in);
#endif
/* ec.c FACTORIAL test */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bigint.h"
int main(int argc, char* argv[]){
struct bigint *a = bigint_init("110010", 512); // 50
struct bigint *b = bigint_init("", 512);
char a_str[512];
char b_str[512];
bigint_hexstr(a_str, a);
bigint_fact(b, a); // a!, factorial
bigint_hexstr(b_str, b);
printf(" %s! = %s\n", a_str, b_str);
free(a);
free(b);
return 0;
}
/* ex1.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bigint.h"
int main(int argc, char* argv[]){
struct bigint *a = bigint_init("1011", 4);
char a_str[8];
bigint_str(a_str, a);
printf("%d, %s\n", a->N, a_str);
free(a);
return 0;
}
/* ex2.c ADD test */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bigint.h"
int main(int argc, char* argv[]){
struct bigint *a = bigint_init("1001", 8); // 9
struct bigint *b = bigint_init("1100", 8); // 12
struct bigint *c = bigint_init("", 8); // 0
char a_str[16];
char b_str[16];
char c_str[16];
bigint_str(a_str, a);
bigint_str(b_str, b);
bigint_add(c, a, b); // 21 = 10101
bigint_str(c_str, c);
printf("ADD test\n");
printf(" %s\n", a_str);
printf("+ %s\n", b_str);
printf("------------\n");
printf(" %s\n", c_str);
free(a);
free(b);
free(c);
return 0;
}
/* ex3.c MULT test */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bigint.h"
int main(int argc, char* argv[]){
struct bigint *a = bigint_init("1001", 8); // 9
struct bigint *b = bigint_init("1100", 8); // 12
struct bigint *c = bigint_init("", 8); // 0
char a_str[16];
char b_str[16];
char c_str[16];
bigint_str(a_str, a);
bigint_str(b_str, b);
bigint_mult(c, a, b); // 9 * 12 = 108 = 0b1101100
bigint_str(c_str, c);
printf("MULT test\n");
printf(" %s\n", a_str);
printf("x %s\n", b_str);
printf("------------\n");
printf(" %s\n", c_str);
free(a);
free(b);
free(c);
return 0;
}
/* ex4.c DIV test */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bigint.h"
int main(int argc, char* argv[]){
struct bigint *a = bigint_init("1001010", 16); // 74 = dividend
struct bigint *b = bigint_init("10101", 16); // 21 = divisor
struct bigint *quo = bigint_init("", 16); // 0
struct bigint *rem = bigint_init("", 16); // 0
char a_str[32];
char b_str[32];
char q_str[32];
char r_str[32];
bigint_str(a_str, a);
bigint_str(b_str, b);
bigint_div(quo, rem, a, b); //74/21 = 3 R 11
bigint_str(q_str, quo);
bigint_str(r_str, rem);
printf(" %s\n", a_str);
printf(" --------- --------- = %s R %s \n", q_str, r_str);
printf(" %s\n", b_str);
free(a);
free(b);
free(quo);
free(rem);
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment