2013-12-08 54 views
1

所以我寫了一個小老虎機程序,但我不能爲我的生活弄清楚如何排序這些數字。C++在文本文件中升序排列數字

我把它寫出來生成一個文件的隨機數,但我需要他們按升序排序。我還需要進行二分搜索以搜索文件中的值,但我現在只是擔心排序問題。這裏是我有什麼幫助,非常感謝!

它寫出在虛空SlotMachine文件:: spinReel()

//////////// main.cpp /////////// 
// Created by ___Josh Modica___ on 11/10/13. 
/*                 */ 


#include <iostream> 
#include <time.h> 
#include <stdlib.h> 
#include <iomanip> 
#include <string.h> 
#include <Windows.h> 
#include <cstdio> 
#include <fstream> 
#include <algorithm> 
using namespace std; 

const int WIN_COL=100; 
const int LIMIT = 1000; 

const int PAY_A=1000; 
const int PAY_C=100; 
const int PAY_D=50; 
const int PAY_e=30; 
const int PAY_F=3; 
const int PAY_S=250; 

void macro(); 

class SlotMachine 
{ 
private: 
    int reels[3][3]; 
    //int elements = sizeof(reels)/sizeof(reels[0][0]); 
    int totCred; 
    int totBet; 
    int numLines; 
public: 
    SlotMachine(); 
    void getBet(); 
    void spinReel(); 
    void display(); 
    void checkWin(); 
    void payOut(); 
    void showPaytable(); 
    void cleanup(); 
}; 
SlotMachine::SlotMachine() 
{ 
    int i, j; 

    for (i = 0; i < 3; i++) 
    { 
     for (j = 0; j < 3; j++) 
     { 
      reels[i][j] = 0; 
     } 
    } 
    totBet = 0; 
    numLines = 0; 
    totCred = 2500; 
} 



void macro() 
{ 

    cout <<"   "<<right<<setw(80) << setfill('^') << "\n"; 
    cout << setw(88) <<setfill(' ')<<left<<"   *_      ____  ___   __"<< "_*"<< endl; 
    cout << setw(88) <<"   *_      /\\ _`\\ /\\_ \\   /\\ \\__"<< "_*"<< endl; 
    cout << setw(88) <<"   *_      \\ \\,\\_\\_\\\\//\\ \\  ___\\ \\ ,_\\"<< "_*"<< endl; 
    cout << setw(88) <<"   *_      \\/_\\__ \\ \\ \\ \\ /__`\\ \\ \\"<< "_*"<<endl; 
    cout << setw(88) <<"   *_       /\\ \\_\\ \\ \\_\\ \\_/\\ \\_\\ \\ \\ \\_"<< "_*"<< endl; 
    cout << setw(88) <<"   *_       \\ `\\____\\/\\____\\ \\____/\\ \\__\\"<< "_*"<< endl; 
    cout << setw(88) <<"   *_       \\/_____/\\/____/\\/___/ \\/__/"<< "_*"<< endl; 
    cout << setw(88) <<"   *_          __"<< "_*"<< endl; 
    cout << setw(88) <<"   *_    /'\\_/`\\     /\\ \\  __"<< "_*"<< endl; 
    cout << setw(88) <<"   *_    /\\  \\  __  ___\\ \\ \\___ /\\_\\ ___  __"<< "_*"<< endl; 
    cout << setw(88) <<"   *_    \\ \\ \\__\\ \\ /'__`\\ /'___\\ \\ _ `\\/\\ \\ /' _ `\\ /'__`\\"<< "_*"<< endl; 
    cout << setw(88) <<"   *_    \\ \\ \\_/\\ \\/\\ \\_\\.\\_/\\ \\__/\\ \\ \\ \\ \\ \\ \\/\\ \\/\\ \\/\\ __/"<< "_*"<< endl; 
    cout << setw(88) <<"   *_    \\ \\_\\\\ \\_\\ \\__/.\\_\\ \\____\\\\ \\_\\ \\_\\ \\_\\ \\_\\ \\_\\ \\____\\"<< "_*"<< endl; 
    cout << setw(88) <<"   *_     \\/_/ \\/_/\\/__/\\/_/\\/____/ \\/_/\\/_/\\/_/\\/_/\\/_/\\/____/"<< "_*"<< endl; 
    cout << setw(88) <<"   *                  "<< "*"; 
    cout <<right<< "    "<<setw(90) << setfill('`') << "\n"<<setfill(' '); 
    cout<<"  # ____ _ _  ____ _____ ___ _ _ __ __ _____ ____ ____ ___ __  #"<<endl; 
    cout<<"  # ( _ \\(\\/) (_ _)( _ )/ __)()_() ( \\/ )( _ )( _ \\(_ _)/ __) /__\\ #"<<endl; 
    cout<<"  # ) _ < \\/ .-_)( )(_)(\\__ \\) _ ( ) ()(_)()(_))_)(_((__ /(__)\\ #"<<endl; 
    cout<<"  # (____/ (__) \\____) (_____)(___/(_) (_) (_/\\/\\_)(_____)(____/(____)\\___)(__)(__) #"; 
    cout<< right<<"   "<< setw(90) << setfill('_') << "\n"<<setfill(' ')<<right<<endl<<endl<<endl; 
} 
void centerTxt(char*txt) 
{ 
    int center; 
    center=strlen(txt); 
    cout<<setw((WIN_COL+center)/2)<<txt<<endl; 
} 

void SlotMachine::getBet() 
{ 
    int bet=0; 

    centerTxt("How Many Lines To Play"); 
    centerTxt("----------------------"); 
    cout<<endl<<endl<<endl; 
    if (totCred < 1) 
    { 
     cout << "Game Over =(" << endl << endl; 
     system("pause"); 
     exit(0); 
    } 
    if (totCred >= 1) 
     cout << setw(WIN_COL/3/2)<<"1 line"; 
    if (totCred >= 2) 
     cout << setw(WIN_COL/3) << "2 lines"; 
    if (totCred >= 3) 
     cout << setw(WIN_COL/3) << "3 lines"; 
    cout << endl << endl; 
    while(numLines<1 || numLines>3){ 
     cout<<">>"; 
     cin >> setw(1) >> numLines; 
    } 
    system("cls"); 
    display(); 
    centerTxt("How Many Credits Per Line"); 
    centerTxt("-------------------------"); 
    cout<< endl<< endl; 
    cout <<" "<< setw(WIN_COL/5)<<left<<"1 credit"; 
    if (totCred >= numLines * 2) 
     cout << setw(WIN_COL/5) << "2 credits"; 
    if (totCred >= numLines * 3) 
     cout << setw(WIN_COL/5) << "3 credits"; 
    if (totCred >= numLines * 4) 
     cout << setw(WIN_COL/5) << "4 credits"; 
    if (totCred >= numLines * 5) 
     cout << setw(WIN_COL/5) << "5 credits"; 
    cout << endl << endl; 
    while(bet<1 || bet>5){ 
     cout<<">>"; 
     cin >> setw(1) >> bet; 
    } 
    totBet = numLines * bet; 
    totCred -= totBet; 
    system("cls"); 
    display(); 
} 

void SlotMachine::spinReel() 
{ 

    int i, j; 

    srand(time(NULL)); 

    for (i = 0; i < 3; i++) 
    { 
     for (j = 0; j < 3; j++) 
     { 
      reels[i][j] = rand() % LIMIT + 1; 
      ////// write out to file ///// 
      ofstream outputFile("project.txt", ios_base::app); 
       outputFile << reels[i][j]<<"\n"; 
       outputFile.close(); 

    //else cout << "Unable to open file"; 
     } 
    } 
    for (i = 0; i < 3; i++) 
    { 
     cout<<setw(30); 
     for (j = 0; j < 3; j++) 
     { 
      if((reels[i][j]==1)||(reels[i][j]==20)) 
      { 
       reels[i][j]=1; 
       cout<<"JACK POT"; 

      } 
      else if((reels[i][j]>20)&&(reels[i][j]<=160)) 
      { 
       reels[i][j]=2; 
       cout<<"$$$$$$$$"; 
      } 
      else if((reels[i][j]>160)&&(reels[i][j]<=360)) 
      { 
       reels[i][j]=3; 
       cout<<"GOLD BAR"; 
      } 
      else if((reels[i][j]>360)&&(reels[i][j]<=660)) 
      { 
       reels[i][j]=4; 
       cout<<"-SILVER-"; 
      } 
      else if((reels[i][j]>660)&&(reels[i][j]<=790)) 
      { 
       reels[i][j]=5; 
       cout<<"-BRONZE-"; 
      } 
      else if((reels[i][j]>790)&&(reels[i][j]<= 900)) 
      { 
       reels[i][j]=6; 
       cout<<"++++++++"; 
      } 
      else if((reels[i][j]>900)&&(reels[i][j]<=950)) 
      { 
       reels[i][j]=7; 
          cout<<"--------"; 
      } 
      else 
      { 
       reels[i][j]=8; 
       cout<<"LOSELOSE"; 
      } 
      cout << " \t\t"; 
     } 
     cout << endl<<endl<<endl<<endl; 
    } 
} 
void SlotMachine::checkWin() 
{ 
    int line1=0, line2=0, line3=0; 
    int scatter=0; 

    if(reels[1][0]==reels[1][1]==reels[1][2]) 
    { 
     switch(reels[1][0]) 
     { 
      case 1: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_A * (totBet/numLines); 
       line1=PAY_A*(totBet/numLines); 
       system("pause"); 
       break; 
      case 2: 
      case 3: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_C * (totBet/numLines); 
       line1=PAY_C*(totBet/numLines); 
           system("pause"); 

       break; 
      case 4: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_D * (totBet/numLines); 
       line1=PAY_D*(totBet/numLines); 
       system("pause"); 
       break; 
      case 5: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_e * (totBet/numLines); 
       line1=PAY_e*(totBet/numLines); 
       system("pause"); 
       break; 
      case 6: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_F * (totBet/numLines); 
       line1=PAY_F*(totBet/numLines); 
           system("pause"); 

       break; 
     } 
    } 
    if((numLines>=2)&&(reels[0][0]==reels[1][1])&&(reels[0][0]==reels[2][2])) 
    { 
     switch(reels[0][0]) 
     { 
      case 1: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_A * (totBet/numLines); 
       line2=PAY_A*(totBet/numLines); 
       system("pause"); 
       break; 
      case 2: 
      case 3: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_C * (totBet/numLines); 
       line2=PAY_C*(totBet/numLines); 
       system("pause"); 
       break; 
      case 4: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_D * (totBet/numLines); 
       line2=PAY_D*(totBet/numLines); 
       system("pause"); 
       break; 
      case 5: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_e * (totBet/numLines); 
       line2=PAY_e*(totBet/numLines); 
       system("pause"); 
       break; 
      case 6: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_F * (totBet/numLines); 
       line2+=PAY_F*(totBet/numLines); 
       system("pause"); 
       break; 
     } 
    } 
    if((numLines==3)&&(reels[2][0]==reels[1][1]==reels[0][2])) 
    { 
     switch(reels[0][2]) 
     { 
      case 1: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_A * (totBet/numLines); 
       line3=PAY_A*(totBet/numLines); 
       system("pause"); 
       break; 
      case 2: 
      case 3: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_C * (totBet/numLines); 
       line3=PAY_C*(totBet/numLines); 
       system("pause"); 
       break; 
      case 4: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_D * (totBet/numLines); 
       line3=PAY_D*(totBet/numLines); 
       system("pause"); 
       break; 
      case 5: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_e * (totBet/numLines); 
       line3=PAY_e*(totBet/numLines); 
       system("pause"); 
       break; 
      case 6: 
       centerTxt("You Win!!!!"); 
       cout<<setw(40)<<"New Balance: $"<<PAY_F * (totBet/numLines); 
       line3=PAY_F*(totBet/numLines); 
       system("pause"); 
       break; 
     } 
     system("pause"); 
    } 
    for(int i=0;i<3;i++) 
    { 
     for(int j=0;j<3;j++) 
     { 
      if(reels[i][j]==7) 
       scatter++; 
     } 
    } 
    if(scatter>=3) 
    { 
     centerTxt("You Win!!!"); 
     cout<<setw(40)<<PAY_S * totBet*(scatter-2); 
     totCred+=PAY_S*totBet*(scatter-2); 
     system("pause"); 
    } 
    if(line1+line2+line3==0) 
    { 
     centerTxt("You Lose =("); 
     system("pause"); 
    } 
    totCred+=line1+line2+line3; 
} 

void SlotMachine::display() 
{ 
    macro(); 
    cout<<endl; 
    cout<<"Credits: $"<<totCred<<endl; 
    cout<<"--------"<<endl<<endl<<endl; 
} 
void SlotMachine::cleanup() 
{ 
    int i,j; 

    system("cls"); 
    numLines=0; 
    totBet=0; 
    /*for (j = 0; j < 3; j++) 
    { 
     for (i = 0; i < 3; i++) 
     { 
      reels[i][j] = 0; 
      cout<<i<<", "<<j<<'\t'; 
     } 
     cout<<endl; 
    }*/ 
} 
int main() 
{ 
    system("mode 100,50"); 
////// picking the color ////////// 
    HANDLE hConsole; 
    int color =10; 
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hConsole, color); 
    //system("color 2"); 

    SlotMachine slot; 
    while(true) 
    { 
     slot.display(); 
     slot.getBet(); 
     slot.spinReel(); 
     slot.checkWin(); 
     slot.cleanup(); 
    } 
    return 0; 
} 
+1

請發佈一個自包含的示例代碼片段。沒有人想要閱讀100多行代碼。 – MoRe

回答

0

如果你希望所有的nnumbers進行排序,你可以使用矢量

#include <vector> //at the top 

....

vector<int> vAlls; 

    srand(time(NULL)); 

    for (i = 0; i < 3; i++) 
    { 
     for (j = 0; j < 3; j++) 
     { 
      reels[i][j] = rand() % LIMIT + 1; 
      ////// write out to file ///// 
      ofstream outputFile("project.txt", ios_base::app); 
       outputFile << reels[i][j]<<"\n"; 
       outputFile.close(); 

      vAlls.push_back(reels[i][j]); 
    //else cout << "Unable to open file"; 
     } 
    } 

    sort(vAlls.begin(),vAlls.end()); 

此矢量排序方式。 這是否回答你的問題?

+0

這不是它寫在project.txt文件中排序... 有沒有辦法,我可以把它放在別的地方,它打開它並對它進行排序然後關閉它? – iEpic

+0

我向你展示瞭如何檢索所有內容並將其排列在向量中。您可以自己完成其他任務(將矢量的內容放入文件中) – Gabriel