ip地址转换 十进制输入转成二进制输出

欢迎大家评论指导!

#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
string dtd(int a)
{
	string s="00000000";
	stack<char> str;
	int y,flag=0;
	while(a)
	{
		flag++;
		y=a%2;
		a=a/2;
		if(y)
		{
			str.push('1');
		}
		else
		{
			str.push('0');
		}

	}
	while(flag<=7)
	{
		flag++;
		str.push('0');
	}
	int i=0;
	while(!str.empty())
	{
		s[i++]=str.top();
		str.pop();
	}
	return s;
}
int main()
{

	char *p;
	queue<char> stk;
	p=(char*)malloc(15);
	int i=0;  //length
	char ch;
	while((ch=getchar())!='\n')
	{
		p[i++]=ch;
	}
	int k=0;
	int flag=0;
	char* ip;
	while(k<=i)
	{
		flag=0;

		while(p[k++]!='.'&&p[k]!='\n')
		{
			flag++;
			stk.push(p[k-1]);

		}
		ip=(char*)malloc(flag);
		for(int r=0;r<flag;r++)
		{
			ip[r]=stk.front();
			stk.pop();
		}
		int a=atoi(ip);
		cout<<a<<endl;
		cout<<dtd(a)<<endl;




	}



	//int j=atoi(c);
	system("pause");
	return 0;
}

 

    原文作者:进制转换
    原文地址: https://blog.csdn.net/lhwsysu/article/details/8160397
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞