//二进制转换为十进制
#include <stdio.h>
#include <malloc.h>
#include <math.h>
int n = 0;
struct bit
{
int num;
struct bit *next;
};
struct bit *creat()
{
struct bit *head, *p1, *p2;
p1 = p2 = (struct bit *)malloc(sizeof(struct bit));
head= NULL;
printf("请输入第1个元素\n");
scanf("%d",&p1->num);
while (p1->num == 1 || p1->num== 0)
{
n++;
_flushall();
if (n == 1)
{
head = p1;
}
else p2->next = p1;
p2 = p1;
p1 = (struct bit *)malloc(sizeof(struct bit));
scanf("%d",&p1->num);
}
p2->next = NULL;
return (head);
}
int main()
{
int i = 0;
int sum = 0;
struct bit *p;
p=creat();
while (p!=NULL)
{
sum += (p->num)*pow(2,i);//pow()函数的使用
p = p->next;
i++;
}
printf("十进制是:%d",sum);
return 0;
}
#include <stdio.h>
#include <malloc.h>
#include <math.h>
int n = 0;
struct bit
{
int num;
struct bit *next;
};
struct bit *creat()
{
struct bit *head, *p1, *p2;
p1 = p2 = (struct bit *)malloc(sizeof(struct bit));
head= NULL;
printf("请输入第1个元素\n");
scanf("%d",&p1->num);
while (p1->num == 1 || p1->num== 0)
{
n++;
_flushall();
if (n == 1)
{
head = p1;
}
else p2->next = p1;
p2 = p1;
p1 = (struct bit *)malloc(sizeof(struct bit));
scanf("%d",&p1->num);
}
p2->next = NULL;
return (head);
}
int main()
{
int i = 0;
int sum = 0;
struct bit *p;
p=creat();
while (p!=NULL)
{
sum += (p->num)*pow(2,i);//pow()函数的使用
p = p->next;
i++;
}
printf("十进制是:%d",sum);
return 0;
}