2016-07-23 71 views
0

我誤解了我的家庭作業,所以我試圖解決它。這裏是任務如何在C++中將字符串中的數字相乘?

- 對於雙圈比賽,計算比賽時間。要做到這一點,您需要將單圈時間的用戶輸入乘以2,並將其乘以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; 
+3

爲什麼您使用的字符串?只需'int a; cin >> a'本來可以做到這一點 –

+0

爲自己省事,從一開始就用'int'。 –

+0

即使你首先閱讀字符串,也有'std :: stoi' – Dutow

回答

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

大家好!所以我知道我應該用我的單圈時間爲了正確地乘以2來獲得我的單圈時間。我不知道該怎麼做,但我知道我錯過了什麼。我應該使用namespace std進行編碼;這將允許我添加int並保留我的字符串。謝謝! –

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. 
+1

我不確定這樣的模板函數對於某個非常有用的模板功能非常有用,他非常清楚地執行*非常*基本的編程練習,並且幾乎不能掌握類C語法...... – hyde

相關問題