2016-07-23 2 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로 늘리기 위해 무릎을 사용해야한다고 생각했습니다. 나는 그것을하는 법을 알지 못했지만, 내가 빠진 것이 무엇인지 알았다. 네임 스페이스 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

관련 문제