进制转换及字符分割

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
    char str[100000];
    char *s;
    cin >> str;
    char *ptr;
    char *p;
    char fuhao[1000];
    int k=0;
    fuhao[0]='+';
    k++;
        for(int i=0; i<strlen(str); i++)
    {
        if(str[i]=='-'||str[i]=='+')
            fuhao[k++]=str[i];
    }
    ptr=strtok(str, "-+");
    long long ans=0;


    //cout << k << endl;
    int j=0;
    while (ptr != NULL)
    {
        long int num=0;
        s=ptr;
        //cout << s << endl;
        if(s[0]=='0'&&s[1]=='x')
        {
            char s1[1000];
            for(int i=2; i<strlen(s); i++)
                s1[i-2]=s[i];
            num=strtol(s,NULL,16);
           // cout << num << "   16" << endl;
        }
        else if(s[0]=='0')
        {
            char s1[1000];
            for(int i=1; i<strlen(s); i++)
                s1[i-1]=s[i];
            num=strtol(s,NULL,8);
           // cout << num << "   8" << endl;
        }
        else
        {
            num=strtol(s,NULL,10);
            //cout << num << "   10" << endl;
        }
        if(fuhao[j]=='+')
        {
            ans+=num;
            //cout << num << " " << fuhao[j] << endl;

        }
        else if(fuhao[j]=='-')
        {
            ans-=num;
            //cout << num << " " << fuhao[j] << endl;
        }
        j++;

        //cout << ans << endl;

        //printf("ptr=%s
", ptr);
        ptr = strtok(NULL, "-+");
    }

    cout << ans << endl;

    return 0;
}
原文地址:https://www.cnblogs.com/vactor/p/9794852.html