如何在C ++中将字符串中的数字相乘?

时间:2016-07-23 04:51:11

标签: c++ string

我误解了我的家庭作业,所以我试图解决它。这是作业

- 对于两圈比赛,计算比赛时间。要做到这一点,你需要将用户输入的单圈时间乘以2并乘以比赛时间。

可以用字符串吗?

以下是我目前的样本。请忽略race_time变量说明。我认为比赛时间和单圈时间是相同的,但他们不是。我想做点什么...

race_time1 * 2

获取比赛时间结果作为输出。

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;


int main(){

    string car_number1;
    string car_number2;
    string car_number3;

    string car_color1;
    string car_color2;
    string car_color3;

    string race_time1;
    string race_time2;
    string race_time3;

    cout<<"We are doing a 2 lap race." << ' ' ;

    //Data for car 1

    cout<<"Enter a number for the first race car: ";
    cin>>car_number1;
    cin.ignore();
    cout<<"Enter a color for car number " << car_number1 << endl;
    getline(cin,car_color1);
    cout<<"Enter a lap time in MM:SS: for the "  << car_color1  <<' '<<          car_number1  << ' '<< "car" << endl;
    getline(cin,race_time1);

    cout<<"You have entered a"<<' '<< car_number1<<' '<<car_color1<<' '<< "car with a lap time of" << ' ' << race_time1 <<endl;

2 个答案:

答案 0 :(得分:0)

经典类运算符重载和类定义,我相信你可能会有很多疑问。为什么如此复杂,但有很多细节和使用的简单性。

// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

// Lap_time class represents minutes and seconds
struct lap_time {
    int minutes;
    int seconds;

    // Default constructors
    lap_time() : minutes(0),seconds(0) { }
    lap_time(const string& str) { 
        stringstream ss(str);
        string value;
        if (std::getline(ss, value, ':'))
            minutes = stoi(value);
        if (std::getline(ss, value, ':'))
            seconds = stoi(value);
    }

    lap_time(int m, int s) : minutes(m), seconds(s) { }


    // input operator overload
    friend istream& operator >> (istream& is, lap_time&);

    // output operator overload
    friend ostream& operator << (ostream& os, lap_time&);

    // Time multiply operation
    lap_time operator*(const int& x) {
        int time = (this->minutes) * 60 + this->seconds;
        time = time * 2;
        return lap_time(time / 60, time % 60);
    }

};


// input operator overload declaration
istream& operator>>(istream& is, lap_time& lt)
{
        string laptime;
        is >> laptime;
        stringstream ss(laptime);
        string value;
        if (std::getline(ss, value, ':'))
            lt.minutes = stoi(value);
        if (std::getline(ss, value, ':'))
            lt.seconds = stoi(value);
        return is;
}

// ouput operator overload declaration
ostream& operator<<(ostream& os, lap_time& lt)
{
    stringstream ss;
    ss << lt.minutes << ":" << lt.seconds;
    os << ss.str();
    return os;
}

// getline function overload to read lap_time 
void getline(std::istream& is, lap_time& lt)
{
    string laptime;
    is >> laptime;
    stringstream ss(laptime);
    string value;
    if (std::getline(ss, value, ':'))
        lt.minutes = stoi(value);
    if (std::getline(ss, value, ':'))
        lt.seconds = stoi(value);
}

int main()
{
    string car_number1;
    string car_number2;
    string car_number3;

    string car_color1;
    string car_color2;
    string car_color3;

    lap_time race_time1;
    lap_time race_time2;
    lap_time race_time3;

    cout << "We are doing a 2 lap race." << ' ' << endl;

    //Data for car 1

    cout << "Enter a number for the first race car: " << endl;
    cin >> car_number1;
    cin.ignore();
    cout << "Enter a color for car number " << car_number1 << endl;
    getline(cin, car_color1);
    cout << "Enter a lap time in MM:SS: for the " << car_color1 << ' ' << car_number1 << ' ' << "car" << endl;
    getline(cin, race_time1);
    cout << "Single Lap race time is :" << race_time1 << endl;
    // I do not think you should multiply by two unless assuming both laps equal timing.
    cout << "Two lap race time is : " << (race_time1 * 2) << endl;
}

答案 1 :(得分:0)

你可以使用这样的功能:

template<typename IntFunction>
std::string transform_numbers_in_string(std::string text, IntFunction func)
{
    auto cond = [](auto& x) {return std::isdigit(x);};
    for (auto it = std::find_if(text.begin(), text.end(), cond);
              it != text.end();
              it = std::find_if(it, text.end(), cond))
    {
        std::string before, after;
        auto pos = it - text.begin();
        std::size_t i;
        for(i=0; cond(*(it+i)); ++i)
            before += *(it+i);
        after = std::to_string(func(std::atoll(before.c_str())));
        text.replace(pos, i, after);
        it = text.begin() + pos + after.size();
    }
    return text;
}

示例程序:

int main()
{
    auto lambda1 = [](const auto& x) {return x*2;};
    auto lambda2 = [](const auto& x) {return x*x;};
    auto lambda3 = [](const auto&  ) {return 666;};

    std::string text {"I have 3 dogs and 4 cats and 5 hamsters."};
    std::cout << text << std::endl << std::endl;

    std::cout << transform_numbers_in_string(text, lambda1) << std::endl;
    std::cout << transform_numbers_in_string(text, lambda2) << std::endl;
    std::cout << transform_numbers_in_string(text, lambda3) << std::endl;
}

输出:

I have 3 dogs and 4 cats and 5 hamsters.

I have 6 dogs and 8 cats and 10 hamsters.
I have 9 dogs and 16 cats and 25 hamsters.
I have 666 dogs and 666 cats and 666 hamsters.