zoukankan      html  css  js  c++  java
  • 进制转换及字符分割

    #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;
    }
  • 相关阅读:
    xpath获取a标签下文本
    Python学习笔记Day26
    DNS原理
    命令实战解析
    linux系统ext文件系统知识
    磁盘分区重点知识
    机械磁盘读写磁盘数据的原理
    linux用户管理
    linux命令讲解
    linux系统定时任务
  • 原文地址:https://www.cnblogs.com/vactor/p/9794852.html
Copyright © 2011-2022 走看看