6进制转化成10进制的c语言程序
%代表0 )代表1 ~代表2 @ 代表3 ?代表4 \ 代表5 $ 代表 -1他们的系统是以6为基础的,每位上的数值达到6就向该位的左边进位,如同随后的例子:
)@%表示1*6^2+3*6+0=36+18+0=54
?$~~ 表示4*6^3+(-1)*6^2+2*6+2=864-36+12+2=842
$~~表示(-1)*6^2+2*6+2=-36+12+2=-22
请帮我画个程序流程图,写下程序。谢谢了!
答案:#include <stdio.h>
#include <string.h>
#define LEN_SHANG sizeof(struct node_shang)
#define LEN_YUSHU sizeof(struct node_yushu)struct node_shang {/*商结点*/
char data;
struct node_shang *next;
} *shang_head=0, *shang_tail=0;struct node_yushu {/*余数结点*/
long data;
struct node_yushu *next;
} *yushu_head=0, *yushu_tail=0;
int shang_empty(void) {/*判断商串是否空*/
return shang_head==0;
}
int yushu_empty(void) {/*判断余数串是否空*/
return yushu_head==0;
}
struct node_shang *new_shang_node(char ch) {/*新建商的结点*/
struct node_shang *ptr= (struct node_shang *) malloc(LEN_SHANG);
ptr->data=ch;
ptr->next=0;
return ptr;
}
struct node_yushu *new_yushu_node(long a) {/*新建余数结点*/
struct node_yushu *ptr= (struct node_yushu *) malloc(LEN_YUSHU);
ptr->data= a;
ptr->next=0;
return ptr;
}
void insert_shang(char ch) {/*插入商字符串的结点*/
struct node_shang *newptr= new_shang_node(ch);if(shang_empty())
shang_head=shang_tail=newptr;
else {
shang_tail->next= newptr;
shang_tail= newptr;
}
}
void insert_yushu(long a) {/*插入余数结点*/
struct node_yushu *newptr= new_yushu_node(a);if(yushu_empty())
yushu_head=yushu_tail=newptr;
else {
yushu_tail->next= newptr;
yushu_tail= newptr;
}
}
char *longinttostr(long a, char *str) {/*将长整型转化为字符串*/
char temp;
int i, j;i=0;
if(a==0) {/*a=0时特别处理*/
str[0]='0';
i++;
} else {
while(a!=0) {
str[i]=(a%10)+'0';
a=a/10;
i++;
}
}
str[i]='\0';
for(j=0; j<=(i-1)/2; j++) {/*倒置*/
temp= str[j];
str[j]= str[i-1-j];
str[i-1-j]= temp;
}return str;/*返回长度*/
}long found_xunhuan(void) {/*通过余数是否相等判断是否出现循环节,若出现返回出现位置的指针ind(相对小数点的偏移量),若无反回-1*/
struct node_yushu *i;
long ind;for(i=yushu_head, ind=0; i->next!=0; i=i->next, ind++)
if(yushu_tail->data==i->data)
return ind;
if(i->next==0)
return -1;
}void div(long d, long n) {/*d是被除数,n是除数*/
long yushu, shang_zhenshu, temp, i, len_temp;
char str[7];
struct node_shang *j, *new_node;/*计算整数部分*/
shang_zhenshu= d/n;
d=d%n;
insert_yushu(d);/*余数保存到余数链表*/
longinttostr(shang_zhenshu, str);
i=0;
while(str[i]!='\0') {/*商保存到商链表*/
insert_shang(str[i]);
i++;
}
insert_shang('.');
if(d==0) {/*恰好整除的情况*/
insert_shang('0');
return;
}
while((d!=0)&&((temp=found_xunhuan())==-1)) {/*当除尽或发现循环节时停止*/
d=d*10;/*进位*/
insert_shang((d/n)+'0');
insert_yushu(d%n);
d=d%n;
}
/*除法已完成*/
if(temp!=-1) {/*发现循环节*/
j=shang_head;
while(j->data!='.')/*找到小数点的位置*/
j=j->next;
for(i=0;i<temp; i++)
j=j->next;/*找到循环节开始的前一位*/
new_node= new_shang_node('(');
new_node->next=j->next;
j->next= new_node;
new_node= new_shang_node(')');
shang_tail->next= new_node;
shang_tail= new_node;
}
}void output(void) {
struct node_shang *i;
long temp;i=shang_head;
temp=0;
while(i->next!=0) {
putchar(i->data);
i=i->next;
temp++;
if((temp%76)==0)/*每行输出76个字符*/
putchar('\n');
}
putchar(shang_tail->data);
putchar('\n');
}int main(void) {
long d, n;scanf("%ld %ld", &d, &n);
while((d<1)||(d>100000)||(n<1)||(n>100000)) {
printf("Input is wrong! Please inpute again!(1<=d, n<=100000)\n");
scanf("%ld %ld", &d, &n);
}
div(d, n);
output();return 0;
}