简体   繁体   English

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

[英]How can you multiply numbers in string in C++?

I misunderstood my homework assignment so I am trying to fix it. 我误解了我的作业,所以我试图解决它。 Here is the assignment 这是作业

-- For a two lap race, calculate the race time. -对于两圈比赛,计算比赛时间。 To do that, you would need to multiply the user input of lap time and multiply it by 2 to get race time. 为此,您需要将用户输入的单圈时间乘以2,以获得比赛时间。

Is it possible to do it in string? 可以用字符串做吗?

Here is a sample of what I have so far. 这是我到目前为止的样本。 Please disregard race_time variable description. 请忽略race_time变量描述。 I thought race time and lap time were the same, but they are not. 我以为比赛时间和单圈时间是相同的,但事实并非如此。 I want to do something like... 我想做类似...

race_time1 * 2

to obtain the race time result as the output. 获得比赛时间结果作为输出。

#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;

Classic class operator overloading and class definition, I am sure you might have lot of doubts. 经典的类运算符重载和类定义,我相信您可能会有很多疑问。 Why so complex but there are lot of details and simplicity of use involved in it. 为什么这么复杂,却涉及到许多细节和使用的简便性。

// 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;
}

You could use function like this: 您可以使用如下功能:

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;
}

Example program: 示例程序:

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;
}

Output: 输出:

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.

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

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