2013-12-08 4 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

관련 문제