C ++在文本文件中按升序排序数字

时间:2013-12-08 18:33:01

标签: c++ sorting text

所以我写了一个小老虎机程序,但我不能为我的生活弄清楚如何对这些数字进行排序。

我让它将生成的随机数写入文件但我需要按升序排序。我还需要二进制搜索来搜索文件中的值,但我现在只是担心排序。这就是我所拥有的,非常感谢任何帮助!!

它写入void 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 个答案:

答案 0 :(得分:0)

如果您想要对所有数字进行排序,您可以使用向量

#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()); 

此时矢量已排序。 这会回答你的问题吗?