简体   繁体   English

从字符串中提取整数

[英]Extract integer from string

I am new to C++ strings and vectors and would like to know what the issue with this code is, I was provided a vector input of strings in the form "a/b+c/d" and had to print a vector in "e/f" for where a, b, c, d are integers and e/f is in the reduced fraction form. 我是C ++字符串和向量的新手,想知道此代码的问题是什么,我以“ a / b + c / d”的形式提供了向量的字符串输入,并且必须在“ e”中打印向量/ f”,其中a,b,c,d为整数,e / f为缩减分数形式。 Please help as I want to learn concept of strings properly and shift to C++ from C 请帮忙,因为我想正确学习字符串的概念并从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;
}

Use std::stoi to convert Single integer from string, std::to_string to convert integer to string and __gcd to calculate GCD which is defined in algorithm header file to find reduced form of fraction. 使用std::stoi从字符串转换单个整数,使用std::to_string将整数转换为字符串,并使用__gcd计算algorithm头文件中定义的GCD ,以找到分数的简化形式。

#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 Ideone链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM