从字符串中提取整数

时间:2017-09-13 16:14:15

标签: c++ string

我是C ++字符串和向量的新手,想知道这段代码的问题是什么,我提供了一个字符串的矢量输入,形式为“a / b + c / d”,并且必须打印一个矢量在“e / f”中,其中a,b,c,d是整数,e / f是缩减分数形式。请帮忙,因为我想正确地学习字符串的概念并从C

转移到C ++
    vector< string > reducedFractionSums(vector < string > expressions) { int a[4]={0,0,0,0}; vector < string > results;


    vector < string > results;

    for(int i=0;i<expressions.size();i++){
        int tmp=0;
        int count=0;
        for(int j=0;j<expressions[i].length();j++){
            if(isdigit(expressions[i][j])){
            tmp=expressions[i][j];
            tmp=tmp-48;
            a[count]=a[count]*10 + tmp;
            }
                else
                count++;    
        }
    int min=a[0];
    for(int i=1;i<4;i++){
        if(a[i]<min){
            min=a[i];
        }
    }
    int e,f,g=1;    //g for GreatestCommonDivisor
    f=a[1]*a[3];
    e=(a[0]*a[3])+(a[2]*a[1]);
    for(int i=1;i<=min;i++){
        if(e%i==0 && f%i==0){
            g=i;
        }
    }
    e=e/g;
    f=f/g;
        results.push_back(e + "/" + f);
    }  
    return results;
}
int main(){
    vector < string > input;
    vector < string > output;
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        input.push_back(s);
    }
    output=reducedFractionSums(input);
    for(int i = 0;i<output.size();i++){
        cout<<output.at(i);
    }
    return 1;
}

1 个答案:

答案 0 :(得分:0)

使用std::stoi转换单个整数来自字符串,std::to_string将整数转换为字符串,使用__gcd来计算GCD,其中定义了algorithm头文件,用于查找缩减形式的分数。

#include <bits/stdc++.h>
using namespace std;

vector<int>reduced_fraction(vector<int>v)   // Function to find reduced fraction form
{
    int num = v[0]*v[3]+v[1]*v[2];         // numerator = a*d+b*c
    int den = v[1]*v[3];                   // denominator = b*d
    int gcd = __gcd(num,den);              // gcd = gcd(greatest common divisor) of numerator and denominator
    num/=gcd;                              // num/den = e/f(reduced fraction)
    den/=gcd;                              
    vector<int>frac;
    frac.push_back(num);
    frac.push_back(den);
    return frac; 
}

int main() {
    string s,st;
    vector<string>fr;                       // vector to store strings of form a/b+c/d 
    int n=2;                                //Number of Strings
    for(int i=0;i<n;i++)
    {
        cin>>s;
        fr.push_back(s);
    }
    for(int i=0;i<n;i++)
    {
        s=fr[i];
        vector <int>v,red_frac;
        while(s.compare("")!=0)
        {
            if(s.at(0)=='/' || s.at(0)=='+')
                s.erase(0,1);
            else
           {
                st=to_string(stoi(s));              // fetch integer from string
                v.push_back(stoi(s));
                s=s.erase(0,st.length());           // remove integer from string
            }
        }
        red_frac=reduced_fraction(v);               // red_frac->reduced fraction
        cout<<red_frac[0]<<"/"<<red_frac[1]<<endl;  
    }
    return 0;
}

Input:
14/2+9/6
8/4+5/4

Output:
17/2
13/4

<强> Ideone Link