天天看點

單片機中用到的一些C語言基礎進制轉換C語言中常用類型  C語言中常見的一些運算

進制轉換

十進制 二進制 十六進制 十進制 二進制 十六進制
0000 8 1000 8
1 0001 1 9 1001 9
2 0010 2 10 1010 A
3 0011 3 11 1011 B
4 0100 4 12 1100 C
5 0101 5 13 1101 D
6 0110 6 14 1110 E
7 0111 7 15 1111 F

C語言中常用類型 

單片機中用到的一些C語言基礎進制轉換C語言中常用類型  C語言中常見的一些運算

 C語言中常見的一些運算

單片機中用到的一些C語言基礎進制轉換C語言中常用類型  C語言中常見的一些運算

位運算示例

單片機中用到的一些C語言基礎進制轉換C語言中常用類型  C語言中常見的一些運算

輸入一個正整數輸出二進制

#include<stdio.h>
void tag(int n)
{
	int i, j, k,c,t;
	int a[100];//用數組存儲每次得到的餘數 
	t = n;
	k = 0;
	while (n)
	{
		i = t % 2;//i是餘數
		a[k] = i;
		k++;
		j = t / 2;//j是商
		if (j != 0)
			t = t / 2;
		else
			break;
	}
	for (c = k-1; c >= 0; c--)//反向輸出即是二進制 
		printf("%d",a[c]);
}
int main()
{
	int n;
	printf("輸入一個正整數:");
	scanf("%d", &n);
	tag(n);
	return 0;
}
           
單片機中用到的一些C語言基礎進制轉換C語言中常用類型  C語言中常見的一些運算
單片機中用到的一些C語言基礎進制轉換C語言中常用類型  C語言中常見的一些運算
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node* next;
}Stacknode,*LinkStack;
int Empty_LinkStack(LinkStack top)
{
	if (top == NULL)
		return 1;
	else
		return 0;
}
void conversion(int N, int r)
{
	LinkStack top = NULL;
	Stacknode* s, * p;
	while (N!=0)
	{
		s = (Stacknode*)malloc(sizeof(LinkStack));
		s->data = N % r;
		s->next = top;
		top = s;
		N = N / r;
	}
	while (Empty_LinkStack(top)!=1)
	{
		if (top == NULL)
			printf("空棧\n");
		else
		{
			printf("%d", top->data);
			p = top;
			top = top->next;
			free(p);
		}
	}
}
int main()
{
	int N, r, n;
	printf("0--------退出!1--------進制轉換\n");
	while(1)
	{
		printf("輸入執行的功能:\n");
		scanf("%d",&n);
		switch(n)
		{
			case 0:
					break;
			case 1:
					printf("輸入一個十進制數:");
					scanf("%d", &N);
					printf("轉化為r進制:");
					scanf("%d", &r);
					conversion(N, r);
					printf("\n");
					break;
		}
		if(n==0)
			break;
	}
	
	return 0;
}
           
單片機中用到的一些C語言基礎進制轉換C語言中常用類型  C語言中常見的一些運算

以上如有錯誤請指正,謝謝!

繼續閱讀