On 3¤ë19¤é, ¤U¤È11®É56¤À, mark_blue...@pobox.com wrote:
Previously on the homework channel, jyc...@gmail.com wrote:
please~help me to write a program to convert the baseN(2-9) to base10
eg. 101(2)----->5(10)
1211(3)------->X(10)
.....
P.S. i am really misunderstand the C programming, plz write the simple
statments as you can
If you have a value "abc" representing a value inbaseX, then theconversioncan be expressed as this:-
(((a * X) + b) * X) + c
So all you need to do (excluding handling invalid input, negative
values and the like), is to take a digit at a time from the left of
the input string and add its numeric value to a running total,
multiplying by thebaseeach time round.
Try producing some code to do this, and get back to us.
For simplicity, pass the value to be converted and thebaseas
arguments to the program, so you don't need any input I/O - you can
just use argv[1] and argv[2], for example. You'll want something like
atoi() or strtol() to convert thebaseto an integer or long, but the
value to convert is probably easier handled as a string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define LENGTH 20
main()
{
char num[LENGTH];
int i, temp, base10, baseN, j;
printf("Enter a number and baseN:");
scanf("%s%d",&num,baseN);
if( baseN >= 2){ // ensure the base is larger than 1
for ( i=0; i<strlen(num)-1; i++) // check the length of
string, in fact , i don't know why i wirte it
*line14 temp = num[i]; // change the position of num
num[i] = num[LENGTH]; // same
num[LENGTH]= temp; // same
for ( j=0; j<=strlen(num); i++) // actually i don't know what
i do
base10 = 0;
base10 = base10 + ((num[LENGTH] * baseN)+ num[i]+1);// find
the num of base10
printf("base10 = %d\n",base10);
system("PAUSE");
}
}
At line14, actually i want to eg..
1011(2)-----1*2^3+ 0*2^2+ 1*2^1+ 1*2^0---1*2^0+ 1*2^1+ 0*2^2+
1*2^3
and for this (((a * X) + b) * X) + c
i am not really understand how to write it