to_string /atoi/c_str()

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);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值