码迷,mamicode.com
首页 > 其他好文 > 详细

进制转换及字符分割

时间:2018-10-15 21:46:12      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:iostream   i++   .com   ring   image   +=   ||   name   while   

技术分享图片技术分享图片

#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\n", ptr);
        ptr = strtok(NULL, "-+");
    }

    cout << ans << endl;

    return 0;
}

 

进制转换及字符分割

标签:iostream   i++   .com   ring   image   +=   ||   name   while   

原文地址:https://www.cnblogs.com/vactor/p/9794852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!