2016-11-05 7 views
-2

私はこのプロジェクトに取り組んでいます。ここには日付が含まれています:str1-> "01/17/17" str2 - > "12/29/16" atoiやstoiなどの変換機能を使用することはできません。 私は文字列の各文字を見て、どれが少ないかを見るためにそれらを比較しています。 str1の月が< = str2の場合、ブール配列をtrueに設定します。私は明らかに間違っています。私は、異なるデータ型に変換することを伴わない単純な解決策は考えられませんが、私はそうすることは許されません。私は大いに助けてくれる誰にでも感謝します。優先順位(年>月>日)の、後は見つけるので、は、フォーマットされたデータを含む文字列を比較します

sortData(items); 
bool date[5]; 
date[0] = false;  //month 
date[1] = true;   // '/' 
date[2] = false;  //day 
date[3] = true;   // '/' 
date[4] = false;  //year 
//looking for smallest string 
string str1; 
string str2; 
for (int i = 4; i < 7; i++) 
{ 
    str1 = items[i].date; 
    str2 = items[i + 1].date; 
    int size = str1.length(); 
    int count = 0; 
    while (count < size) 
    { 
     if (str1[count] <= str2[count] || str1[count + 1] <= str2[count + 1]) 
     { 
      date[0] = true; 

     } 
     //0,1 

     count=count+3;   //3,4 
     if (str1[count] <= str2[count] || str1[count + 1] <= str2[count + 1]) 
      date[2] = true;  //day 
     count = count + 3; 
       //6,7 
     if (str1[count] <= str2[count] || str1[count + 1] <= str2[count + 1]) 
      date[4] = true; 
     count=count+1; 

    } 

} 
int m = 0;  //for debugging 
+0

入出力の例はなく、エラーはありませんか?まあ、char '0'はchar '1'よりも小さいので、日、月、年があるだけなので、しばらくするのはナイスで、優先順位(年 - >月 - >日) – cpatricio

答えて

0

これは、2列の日付とちょうどソリューションの一例であり、それが比較されます年、その後、月、そして最後の日: はここに私のコードです最初は、停止して最小のものを印刷します。あなたが/ mmとyyの文字列を再編成した場合

#include <iostream> 

using namespace std; 

int main() 
{ 
    string str1 = "01/17/17"; 
    string str2 = "12/29/16"; 
    string smallest; 

    for(int i=7; i >=0 ; i-=3) 
    { 
     if(str1[i-1] < str2[i-1]) 
     { 
      smallest = str1; 
      break; 
     } 
     else if(str1[i-1] > str2[i-1]) 
     { 
      smallest = str2; 
      break; 
     } 
     if(str1[i] < str2[i]) 
     { 
      smallest = str1; 
      break; 
     } 
     else if(str1[i] > str2[i]) 
     { 
      smallest = str2; 
      break; 
     } 
    } 

    cout << smallest << endl; 

    return 0; 
} 
1

は/あなたが他よりも小さいか以上である1見つけるために、文字列比較を使用することができますddを。文字列が常に2桁のフォーマットであると仮定すると、次のようになります。

//This assumes mm/dd/yy 
string FixString(string input) 
{ 
    return input.substr(6) + "/" + input.substr(0, 5); 
} 
int main() 
{ 
    string test = FixString("01/17/17"); 
    string test2 = FixString("12/29/16"); 
    bool test3 = test < test2; 
    return 0; 
} 
関連する問題