user-0 提问: 进制转换

回复:
user: user-0
content: 引用 2 楼 qq423399099 的回复:什么问题呢 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。 恩恩 好的 我已经找到了 谢谢

user: user-1
content: #include <stdio.h> #include <stdlib.h>  typedef struct Node { 	int date; 	struct Node *next; } LinkNode, *LinkList;   void conversion(LinkList top , int n) { 	int x; 	LinkList NewNode, tmpNode; 	  	//find the link tail 	tmpNode = top; 	while(tmpNode->next != NULL) { 		tmpNode = tmpNode->next; 	} 	 	while (n != 0) { 		x = n % 2; 		n /= 2; 		NewNode = (LinkList)malloc(sizeof(LinkNode)); 		NewNode->date = x; 		NewNode->next = NULL; 		 		tmpNode->next = NewNode; 		tmpNode = NewNode; 	} 	//add "." 	NewNode = (LinkList)malloc(sizeof(LinkNode)); 	NewNode->date = -1; 	NewNode->next = NULL; 		 	tmpNode->next = NewNode; 	tmpNode = NewNode;	 }  void conversion1(LinkList top, float n) { 	float x, y; 	LinkList NewNode, tmpNode; 	 	//find the link tail 	tmpNode = top; 	while(tmpNode->next!=NULL) { 		tmpNode = tmpNode->next; 	} 	 	 	while(n) { 		n *= 2; 		y = n - 1; 		if(y > 0) { 			n -= 1;	 			x = 1; 		} else if (y < 0) { 			x = 0; 		} else { 			x = 1; 		} 		 		NewNode = (LinkList)malloc(sizeof(LinkNode)); 		NewNode->date = x; 		tmpNode->next = NewNode; 		tmpNode = NewNode; 		if(0 == y) 		{ 			break; 		} 	} }  	 void input(LinkList top) { 	LinkList tmpNode = NULL; 	 	tmpNode = top->next; 	while(tmpNode != NULL) { 		if (tmpNode->date >= 0){ 			printf("%d",tmpNode->date); 		}else { 			printf("."); 		} 		 		tmpNode = tmpNode->next; 	} 	printf("\n"); }   int main(void) { 	LinkList head, tmpNode; 	float nubmer,decimal; 	int integers; 	 	printf("Please input a float number: "); 	scanf("%f",&nubmer);  	integers = (int)nubmer;  	decimal = nubmer - integers;   	head = (LinkList)malloc(sizeof(LinkNode)); 	head->next = NULL; 	 	//conv float to binary 	conversion(head, integers); 	conversion1(head, decimal); 	 	//display 	input(head); 	 	//free the link table 	while (head) { 		tmpNode = head; 		head = head->next; 		free(tmpNode); 	} 	 	return 0; }

user: user-0
content: 要求用到链表

user: user-2
content: 单步调试，观察数据变化找问题

user: user-3
content: 什么问题呢 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-0
content: 求大腿指点下

user: user-4
content: 引用 楼主 baidu_33333408 的回复:#include "stdio.h" #include"stdlib.h" typedef struct Node { 	int date; 	struct Node *next; } *LinkList; void conversion( LinkList top , int n) { 	int x;int i=0; 	LinkList NewNode; 	while(n!=0) 	{ 		x=n%2; 		n/=2; 		NewNode=(LinkList)malloc(sizeof(LinkList)); 		NewNode->date=x; 		NewNode->next=NULL; 		top->next=NewNode; 		top=top->next; 	 	 	} } void conversion1(LinkList top,float n) { 	float x; 	LinkList NewNode; 	while(top->next!=NULL) 	{ 		top=top->next; 	} 	while(n) 	{ 		n*=2; 		if(n-1>0) 		{ 		n-=1;	x=1; 		} 		if(n-1<0) 		x=0; 		if(n-1==0) 		x=1; 		NewNode=(LinkList)malloc(sizeof(LinkList)); 		NewNode->date=x; 		top->next=NewNode; 		top=top->next; 		if(n-1==0) 		{ 			top->next=NULL; break; 		} 	}  }   void input(LinkList top) { 	top=top->next; 	while(top!=NULL) 	{	 		printf("%d\t",top->date); 		top=top->next; 	} } int main (void) { 	LinkList head; 	float nubmer,decimal; 	int integers; 	scanf("%f",&nubmer);  	integers=(int)nubmer;  	decimal=nubmer-integers;  	head=(LinkList)malloc(sizeof(LinkList)); 	head->next=NULL; 	conversion(head,integers); //	input(head); //	printf("\n"); 	conversion1(head,decimal); 	input(head); 	printf("\n");  } 我很奇怪，为什么数值进制转换要用到链表？

user: user-5
content: 仅供参考：#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() {     int i,v;     char bs[33];     char b[33];     char hs[9];     char h[9];     char s[4];     char *e;  // 十进制整数转二进制串；     i=1024;     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("i=%d,bs=%s\n",i,bs); // 十进制整数转十六进制串；     i=1024;     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("i=%d,hs=%s\n",i,hs); // 十六进制字符串转成十进制数     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     printf("hs=%s,i=%d\n",hs,i); // 二进制字符串转化为十六进制字符串；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("bs=%s,hs=%s\n",bs,hs); // 二进制字符串转化为十进制数；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     printf("bs=%s,i=%d\n",bs,i); // 十六进制字符串转成二进制串     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("hs=%s,bs=%s\n",hs,bs); // ASC\GBK字符串转十六进制串     strcpy(s,"a汉");     i=0;     while (1) {         if (0==s[i]) break;         sprintf(hs+i*2,"%02X",(unsigned char)s[i]);         i++;     }     setlocale(LC_ALL,"chs");     printf("s=%s,hs=%s\n",s,hs); // 十六进制字符串转成汉字（GBK）及字符(ASC)     strcpy(hs,"61BABA");     i=0;     while (1) {         if (1!=sscanf(hs+i*2,"%2x",&v)) break;         s[i]=(char)v;         i++;     }     s[i]=0;     printf("hs=%s,s=%s\n",hs,s);      return 0;  } //i=1024,bs=00000000000000000000010000000000 //i=1024,hs=00000400 //hs=00000400,i=1024 //bs=00000000000000000000010000000000,hs=00000400 //bs=00000000000000000000010000000000,i=1024 //hs=00000400,bs=00000000000000000000010000000000 //s=a汉,hs=61BABA //hs=61BABA,s=a汉