Skip to content
Snippets Groups Projects
Commit 7672348b authored by Strozzi's avatar Strozzi
Browse files

Add main C file

Add conversion functions and the fibonacci vector
parent 5ca9543a
No related branches found
No related tags found
No related merge requests found
Pipeline #
cMIPS/vhdl/*.o
cMIPS/vhdl/*.cf
cMIPS/vhdl/.last_import
cMIPS/tb_cmips
cMIPS/include/*.o
cMIPS/include/*~
cMIPS/include/cMIPSio.s
#include<stdio.h>
#include "vetorFib.h"
#define MAXPOW 7
int Strlen(char *c)
{
int i= 0;
while(c[i++]);
return i-1;
}
int pow16(int ex)
{
int i, ret=1;
for (i= 0; i< ex/2; i++)
ret*=16;
ret *= ret;
return (ex%2?ret*16:ret);
}
int hex2int(char *hex)
{
int s= Strlen(hex), i, ans= 0;
for (i= 0; i< s; i++) {
switch(hex[i]) {
case 'F':
case 'f':
ans+= 15*pow16(s-(i+1));
break;
case 'E':
case 'e':
ans+= 14*pow16(s-(i+1));
break;
case 'D':
case 'd':
ans+= 13*pow16(s-(i+1));
break;
case 'C':
case 'c':
ans+= 12*pow16(s-(i+1));
break;
case 'B':
case 'b':
ans+= 11*pow16(s-(i+1));
break;
case 'A':
case 'a':
ans+= 10*pow16(s-(i+1));;
break;
case '9':
ans+= 9*pow16(s-(i+1));;
break;
case '8':
ans+= 8*pow16(s-(i+1));;
break;
case '7':
ans+= 7*pow16(s-(i+1));;
break;
case '6':
ans+= 6*pow16(s-(i+1));;
break;
case '5':
ans+= 5*pow16(s-(i+1));;
break;
case '4':
ans+= 4*pow16(s-(i+1));;
break;
case '3':
ans+= 3*pow16(s-(i+1));;
break;
case '2':
ans+= 2*pow16(s-(i+1));;
break;
case '1':
ans+= 1*pow16(s-(i+1));;
break;
default:
if (hex[i] != '0')
printf("ERROR!\n");
break;
}
}
return ans;
}
int int2hex(char *c, int i)
{
int j= 0, val;
int p;
p = pow16(MAXPOW);
while (p > 0) {
val= 0;
if (i/p) {
val= i/p;
i%= p;
}
if ((val < 10) && (j || val)) { // impede que '0' sejam escritos à esquerda
c[j++] = val+'0';
}
else if (val > 9)
c[j++] = 'a'+ (val-10);
p/=16;
}
c[j] = 0;
return j-1;
}
int main()
{
char n[16], f[16];
int fib;
while (scanf("%s", n) > 0) {
fib= hex2int(n);
if (fib < BUF_SZ) {
fib = buf[fib];
int2hex(f, fib);
printf("%d(%s)\n", fib, f);
} else
printf("Valor fora dos limites!\n");
}
}
#define BUF_SZ 45
int buf[BUF_SZ] = {
1,1,2,3,5,8,13,21,
34,55,89,144,233,377,610,987,
1597,2584,4181,6765,10946,17711,28657,46368,
75025,121393,196418,317811,514229,832040,1346269,2178309,
3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,
165580141,267914296,433494437,701408733
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment