0

我的程序是根据邻居拥有的资源来计算资源的价格。

它编译,但在运行时,它中断并说程序已停止响应。

任何帮助是极大的赞赏!

#include <iostream>
#include <fstream>

using namespace std;

class Resource{
    string resName;
    int amount;
    int price;
    public:
        void set_values(string resName, int amount, int price){resName=resName; amount=amount; price=price;};
        string get_name(){return resName;};
        int get_amount(){return amount;};
        int get_price(){return price;}; 
};

class State{
    string stName;
    Resource resources[5];
    public:
        void set_values(string stName){
            stName=stName;
            Resource r; 
            r.set_values(" ", 0, 0);
            for (int i=0; i<5; i++){
                resources[i] = r;
            }
        };
        string get_name(){return stName;};
        void addResource(string name, int amount, int price){
            Resource r;
            r.set_values(name, amount, price);
            for (int i=0; i<5; i++){
                if(resources[i].get_name() == " "){
                    resources[i] = r;
                }
            }
        };
        Resource get_resource(string resource){
            for(int i=0; i<5; i++){
                if (resources[i].get_name() == resource){
                    return resources[i];
                }
            }
        };
        bool checkForResource(string resource){
            for(int i=0; i<5; i++){
                if (resources[i].get_name() == resource){
                    return true;
                }
            }
        }

};

class Area{
    State allStates[10][10];
    public:
        void initialize(){
            for (int i=0; i<10; i++){
                for (int j=0; j<10; j++){
                    allStates[i][j].set_values(" ");    
                }
            }   
        };
        void addState(string stateName){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == " ")
                        allStates[row][col].set_values(stateName);
                }   
            }   
        };
        State get_state(string name){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == name)
                        return allStates[row][col];
                }   
            }
        };
        void deleteState(string name){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == name)
                        allStates[row][col].set_values(" ");
                }   
            }
        };
        void moveRowsSouth(){
            for(int z=0; z<10; z++){
                for(int y=10; y>=1; y--){
                    allStates[y][z] = allStates[y-1][z];
                }
                allStates[0][z].set_values(" ");
            }
        };
        void moveColsEast(){
            for(int z=0; z<10; z++){
                for(int y=10; y>=1; y--){
                    allStates[z][y] = allStates[z][y-1];
                }
                allStates[z][0].set_values(" ");
            }
        };
        void addNeighbor(string s1, string s2, string direction){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        if (direction == "North"){
                            if (row-1 < 0){
                                moveRowsSouth();
                            }
                            else {
                                if (allStates[row-1][col].get_name() == " "){
                                    allStates[row-1][col] = get_state(s2);
                                    deleteState(s2);
                                }
                                else
                                    cout << "Unable to add this neighbor. North is occupied.";
                            }
                        }
                        else if (direction == "South"){
                            if (allStates[row+1][col].get_name() == " "){
                                allStates[row+1][col] = get_state(s2);
                                deleteState(s2);
                            }
                            else
                                cout << "Unable to add this neighbor. South is occupied.";

                        }
                        else if (direction == "East"){
                            if (allStates[row][col+1].get_name() == " "){
                                allStates[row][col+1] = get_state(s2);
                                deleteState(s2);
                            }
                            else
                                cout << "Unable to add this neighbor. East is occupied.";
                        }
                        else if (direction == "West"){
                            if (col-1 < 0){
                                moveColsEast();
                            }
                            else {
                                if (allStates[row][col-1].get_name() == " "){
                                    allStates[row][col-1] = get_state(s2);
                                    deleteState(s2);
                                }
                                else
                                    cout << "Unable to add this neighbor. West is occupied.";
                            }
                        }
                }   
            }
        };
        State getNeighborN(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row-1][col];
                }   
            }
        };
        State getNeighborS(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row+1][col];
                }   
            }
        };
        State getNeighborE(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row][col+1];
                }   
            }
        };
        State getNeighborW(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row][col-1];
                }   
            }
        };  
};

int main()
{
    Area a;
    a.initialize();
    ifstream infile;
    string command;
    infile.open ("proj2.txt", ifstream::in);
    while(!infile.eof()){
        infile >> command;
        if (command == "addState"){
            string stateName;
            infile >> stateName;
            a.addState(stateName);
        }
        else if (command == "addNeighbor"){
            string state1;
            string state2;
            string direction;
            infile >> state1;
            infile >> state2;
            infile >> direction;
            a.addNeighbor(state1, state2, direction);
        }
        else if (command == "addResource"){
            string state;
            string name;
            int amount;
            int price;
            infile >> state;
            infile >> name;
            infile >> amount;
            infile >> price;
            a.get_state(state).addResource(name, amount, price);
        }
        else if (command == "getPrice"){
            string state;
            string resource;
            int amount;
            infile >> state;
            infile >> resource;
            infile >> amount;
            State N = a.getNeighborN(state);
            State S = a.getNeighborS(state);
            State E = a.getNeighborE(state);
            State W = a.getNeighborW(state);
            int countOfNeigh = 0;
            bool stateHas = false;
            if (N.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (S.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (E.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (W.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (a.get_state(state).checkForResource(resource) == true) {
                stateHas = true;
            }
            int price = 0;
            if ((stateHas = false && countOfNeigh == 1) ||
                 (stateHas = true && countOfNeigh == 0)){
                price = a.get_state(state).get_resource(resource).get_price();
                price = price+(price*.25);
            }
            else if (stateHas = true && (countOfNeigh == 2 || countOfNeigh == 3)){
                price = a.get_state(state).get_resource(resource).get_price();
            }
            else if (stateHas = true && countOfNeigh == 4){
                price = a.get_state(state).get_resource(resource).get_price();
                price = price-(price*.1);
            }
            else if (stateHas = false && countOfNeigh > 1){
                price = a.get_state(state).get_resource(resource).get_price();
                price = price+(price*.1);
            }
            cout << "The price for " << amount << " units of " << resource << " is " << price << ".";
        }
    }
}
4

1 回答 1

0

您没有检查文件打开是否成功 - http://www.cplusplus.com/reference/iostream/ifstream/open/

如果是这样,您的程序将进入无限循环。

也没有其他类型的错误检查。在处理读取文件时,尤其是您希望以某种方式格式化的数据时,这是一条通往灾难的捷径。

于 2012-04-26T20:19:33.623 回答