#include<cstdlib>itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是目标字符串,第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制……
#include<cstdio>
int main() { int num = 10; char str[100]; itoa(num, str, 2); printf("%s\n", str); return 0; }
于是想到了一个十进制转二进制的方法:
#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; }