请教,交流
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* fp;
fp=fopen("data.in", "r");
int i, j, sum;
char bin[100000];
if(fp==NULL){
printf("Error 1\n");
fclose(fp);
return 1;
}
fscanf(fp,"%u",&j);
if(j<=0||j>=100000){
printf("Error 2\n");
fclose(fp);
return 2;
}
sum=0;
for(i=0;i<sizeof(int)*8;++i)
if(j & 1<<i) sum++;
printf("%d:%d\n",j ,sum);
return 0;
这是求10进制转换成2进制的程序,并计算输出的2进制中有多少个1,还有一个问题,如果是转换成16进制或8进制呢
为什么这个语句就转换成了2进制了呢
[解决办法]
j & 1<<i 这种是 位运算,楼主可以去了解下。。
[解决办法]
我这里有完全代码,我也写过的 ----》
#include<stdio.h> //This is the operation of changing a number to another
#include<stdlib.h>
#define MAXSIZE 100
typedef struct
{
int data[MAXSIZE]; //This is the defination of Stack and Queue
int top;
}SeqStack,*PSeqStack;
typedef struct
{
int data[MAXSIZE];
int front,rear;
}SeqQueue,*PSeqQueue;
PSeqQueue Init_SeqQueue(void) //To Init a SeqQueue
{
PSeqQueue Q;
Q=(PSeqQueue) malloc(sizeof(SeqQueue));
if(Q)
{
Q->front=0;
Q->rear=0;
}
return Q;
}
int Empty_SeqQueue(PSeqQueue Q) //To estimate a SeqQueue
{
if( Q && Q->front == Q->rear )
return 1;
else
return 0;
}
int In_SeqQueue(PSeqQueue Q,int x) //To insert a element to a Queue
{
if( (Q->rear+1) % MAXSIZE == Q->front )
{
printf("The Queue is full");
return -1;
}
else
{
Q->rear = ( Q->rear+1 ) % MAXSIZE;
Q->data[Q->rear]=x;
return 1;
}
}
int Out_SeqQueue(PSeqQueue Q,int *x) //To output a element from a Queue
{
if(Empty_SeqQueue(Q))
{
printf("The Queue is empty");
return -1;
}
else
{
Q->front=(Q->front+1)%MAXSIZE;
*x=Q->data[Q->front];
return 1;
}
}
void Destroy_SeqQueue(PSeqQueue *Q) //To destroy a Queue you have created
{
if(* Q)
free(*Q);
*Q=NULL;
}
PSeqStack Init_SeqStack(void) /*To create a Stack*/
{
PSeqStack S;
S=(PSeqStack) malloc(sizeof(SeqStack));
if(S)
{
S->top=-1;
return S;
}
}
int Push_SeqStack(PSeqStack S,int x) /*To insert a element to Stack*/
{
if(S->top==MAXSIZE-1)
return 0;
else
{
S->top++;
S->data[S->top]=x;
return 1;
}
}
int Empty_SeqStack(PSeqStack S) //To estimate a Stack is empty or not
{
if(S->top==-1)
return 1;
else
return 0;
}
int Pop_SeqStack(PSeqStack S,int *y) /*To delete the top element and save him*/
{
if(Empty_SeqStack(S))
return 0;
else
{
*y=S->data[S->top];
S->top--;
return 1;
}
}
int Destroy_SeqStack(PSeqStack *S) /*To destroy a Stack*/
{
if( *S)
free(*S);
*S=NULL;
return 1;
}
int conversion(float n,int r) //Now this is The Arithmetic Of Changing A num To Another
{
PSeqStack S;
PSeqQueue Q;
int x,y;
float m;
if(!r)
{
printf("杞㈢烘涓涓0");
return 0;
}
S=Init_SeqStack();
Q=Init_SeqQueue();
if(!S || !Q)
{
printf("澶辫触浜濮澶辫触浜");
return 0;
}
int p=int (n);
m=n-p;
while(p)
{
Push_SeqStack(S,p%r);
p=p/r;
}
while(m)
{
float q;
int p;
q=m*r;
int n=(int) q;
In_SeqQueue(Q,n);
m=q-n;
}
printf("The New Number with %d system is:\n",r);
while( !Empty_SeqStack(S) )
{
Pop_SeqStack(S,&x);
printf("%d",x);
}
printf(".");
//printf("\n\n");
//printf("The other part is:\n");
while(!Empty_SeqQueue(Q))
{
Out_SeqQueue(Q,&y);
printf("%d",y);
}
printf("\n");
Destroy_SeqQueue(&Q);
Destroy_SeqStack(&S);
return 1;
}
int main()
{
int R;
float N;
printf("Please enter a float number: N=?\n");
scanf("%f",&N);
printf("What is the system do you want to change:R=?\n");
scanf("%d",&R);
conversion( N , R );
printf("\n\n");
return 1;
}
随便你转换为几进制都可以哦,如果不懂加我QQ:568202560
我们讨论哈