itoa函數用法

#include<cstdlib>
#include<cstdio>
int main() { int num = 10; char str[100]; itoa(num, str, 2); printf("%s\n", str); return 0; }
itoa()函數有3個參數:第一個參數是要轉換的數字,第二個參數是目標字符串,第三個參數是轉移數字時所用 的基數。在上例中,轉換基數爲10。10:十進制;2:二進制……
於是想到了一個十進制轉二進制的方法:
#include<cstdlib>
#include<cstdio>
int main() { int num = 10; char str[100]; int n = atoi(itoa(num, str, 2)); printf("%d\n",n); return 0; }
先把num轉換爲二進制的字符串,再把該字符串轉換爲整數。

#include <iostream>
using namespace std;
string a,l=""; char s[50]; int top=-1;
int main()
{
	cin>>a;
	for (int i=0;i<a.size();++i){
		if (a[i]>='0' && a[i]<='9' || a[i]>='a' && a[i]<='z')
			l+=a[i];
		if (a[i]=='(')
			s[++top]=a[i];
		if (a[i]==')'){
			while (s[top]!='(')
				l+=s[top--];
			top--;
		}
		if (a[i]=='+' || a[i]=='-'){
			while (top>=0 && (s[top]=='+' || s[top]=='-' || s[top]=='*' || s[top]=='/'))
				l+=s[top--];
			s[++top]=a[i];
		}
		if (a[i]=='*' || a[i]=='/'){
			while (top>=0 && (s[top]=='*' || s[top]=='/'))
				l+=s[top--];
			s[++top]=a[i];
		}
	}
	while (top>=0)
		l+=s[top--];
	cout<<l<<endl;
	return 0;
}



發佈了105 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章