1.to_stirng()是一个把数字转换为字符的函数,在头文件<string>中
2.atoi转换函数,只能转换以数字开头的字符串
3.atoi需要一个const char*类型的参数,c_str()会返回一个指向字符串const char*的指针
#include<iostream>
#include<string>
using namespace std;
char a[110];
int b[110];
int main(){
int n;
cin>>n;
cin.ignore();
int k=0;
for(int i=0;i<n;i++){
string s;
getline(cin,s);
int x,y;
int n;string m;
if(s[0]>='a'&&s[0]<='c'){
x=atoi(s.substr(2,s.find(' ',2)-2).c_str());
y=atoi(s.substr(s.find(' ',2)+1).c_str());
if(s[0]=='a') a[k]='+',k++,n=x+y,m=to_string(x)+"+"+to_string(y)+"="+to_string(n),cout<<m<<endl,cout<<m.length()<<endl;
if(s[0]=='b') a[k]='-',k++,n=x-y,m=to_string(x)+"-"+to_string(y)+"="+to_string(n),cout<<m<<endl,cout<<m.length()<<endl;
if(s[0]=='c') a[k]='*',k++,n=x*y,m=to_string(x)+"*"+to_string(y)+"="+to_string(n),cout<<m<<endl,cout<<m.length()<<endl;
}else{
x=atoi(s.substr(0,s.find(' ',0)).c_str());
y=atoi(s.substr(s.find(' ',0)+1).c_str());
if(a[k-1]=='+') n=x+y,m=to_string(x)+"+"+to_string(y)+"="+to_string(n),cout<<m<<endl,cout<<m.length()<<endl;
else if(a[k-1]=='-') n=x-y,m=to_string(x)+"-"+to_string(y)+"="+to_string(n),cout<<m<<endl,cout<<m.length()<<endl;
else if(a[k-1]=='*') n=x*y,m=to_string(x)+"*"+to_string(y)+"="+to_string(n),cout<<m<<endl,cout<<m.length()<<endl;
}
}
return 0;
}
4.transform(a.begin(),a.end(),a.begin(),::tolower)
a.begin():返回字符串a的第一个字符迭代器
a.end():返回字符串a的最后一个字符的下一个位置的迭代器
a.begin():表示转换后的存储位置,在这里是从字符串a的开始位置进行存储
第四个参数:应用于每个字符的函数
transform(a.begin(), a.end(), a.begin(), ::tolower);