View Single Post
Old 01-02-2010   #3
Tawa
 
Tawa's Avatar
 
Last Online: 12-20-2021
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,121
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
Default

Code:

#include "stdafx.h"
#include <iostream>
using namespace std;

int distances[9][9] = { {-1, 560, 320, 360, 300, 555, 760, 963, -1}, 
                        {557, -1, 745, -1, -1, -1, 395, 600, 225},
                        {320, 750, -1, 60, -1, 760, -1, -1, -1},
                        {370, 780, 60, -1, 215, -1, -1, 990,-1},
                        {298, -1, -1, 215, -1, 800, -1, -1, -1},
                        {560, -1, 765, -1, -1, -1, 290, 530, 890},
                        {-1, 395, -1, 1034, -1, 282, -1, 237, -1},
                        {-1, 595, -1, -1, -1, 531, 240, -1, 580},
                        {785, 221, -1, -1, -1, -1, -1, 585, -1}};

char* town(int);
int distance(int, int);
int totalDistance(int[]);
void getRoute(int, int, int[]);
double price(int[], int, double);

void main(){
    int a, b;    // a is Source, b is Destination
    int c, d;    // c is Class, d is Deduction
    double ded = 0;
    int i;
    int tab[4] = {-1,-1,-1,-1};
    double money = 0;
    double coin = 0;
    double totalPrice;

    cout << "Towns: " << endl;
    for(i = 1; i < 10; i++)
        cout << '\t' << i << "- " << town(i) << endl;
    do{
        cout << "Enter Source: ";
        cin >> a;
    }while(a < 1 || a > 9);
    do{
        cout << "Enter Destination: ";
        cin >> b;
    }while(b < 1 || b > 9 || b == a);
    do{
        cout << "Enter Class (1 for 1st, 2 for 2nd) : ";
        cin >> c;
    }while(c < 1 || c > 2);
    do{
        cout << "Enter Deduction (1 for 30%, 2 for 50%, 3 for 60%) : ";
        cin >> d;
    }while(d < 1 || d > 3);
    if(d == 0)
        ded = 0;
    else if(d == 1)
        ded = 0.3;
    else if(d == 2)
        ded = 0.5;
    else if(d == 3)
        ded = 0.6;
    if(distance(a,b) >= 0){
        tab[0] = a;
        tab[1] = b;
        tab[2] = -1;
    }
    else
        getRoute(a,b,tab);

    cout << "Program is: " << endl
         << "Source: " << town(a) << endl;
    if(tab[2] != -1)
        cout << "Station: " << town(tab[1]) << endl;
    cout << "Destination: " << town(b) << endl
         << "Class: ";
    if(c == 1)
        cout << "1st.";
    else
        cout << "2nd.";
    cout << endl << "Deduction: ";
    if(d == 1)
        cout << "30%";
    else if(d == 2)
        cout << "50%";
    else
        cout << "60%";
    cout << endl << "Total Distance: " << totalDistance(tab) << endl;
    totalPrice = price(tab,c,ded);
    cout << "Total Price: " << totalPrice << endl;

    cout << "Please Insert Coins, accepted coins are (0.2, 0.5, 1 and 2 Euro, 0 to stop): ";
    money = 0;
    do{
        cin >> coin;
        if(coin != 0.2 && coin != 0.5 && coin != 1 && coin != 2 && coin != 0)
            continue;
        money += coin;
    }while(coin != 0);
    cout << "Money entered is : " << money << endl;



}
int totalDistance(int tab[4]){
    int total = 0;
    int i;
    for(i = 0; tab[i+1] > 0; i++)
        total += distance(tab[i], tab[i+1]);
    return total;
}
double price(int tab[4], int c, double d){
    double s;
    int totalDistance = 0;
    if(c == 1)
        s = 0.05;
    else
        s = 0.04;
    int i;
    for(i = 0; tab[i+1] > 0; i++)
        totalDistance += distance(tab[i], tab[i+1]);
    return s*totalDistance*(1-d);
}

void getRoute(int a, int b, int tab[4]){
    int i;
    if(distance(a, b) > 0){
        tab[0] = a;
        tab[1] = b;
        tab[2] = -1;
    }
    else{
        for(i = 0; i < 9; i++){
            if(i == a || i == b)
                continue;
            if(distance(a,i) > 0 && distance(i,b) > 0){
                tab[0] = a;
                tab[1] = i;
                tab[2] = b;
                tab[3] = -1;
            }
        }
    }
}

int distance(int a, int b){
    a--;
    b--;
    return distances[a][b];
}

char* town(int i){
    i--;
    if(i == 0)
        return "Paris";
    else if(i == 1)
        return "Bordea";
    else if(i == 2)
        return "Metz";
    else if(i == 3)
        return "Luxem.";
    else if(i == 4)
        return "Bruxell.";
    else if(i == 5)
        return "Valence";
    else if(i == 6)
        return "Narbon.";
    else if(i == 7)
        return "Barcelo.";
    else if(i == 8)
        return "St seb.";
    return "Invalid";
}
Try this one, I didn't comment, I'll work on it later, and you need to add the code concerning the Money available in the Machine.
__________________
What we do in life, echoes in eternity.
Tawa is offline   Reply With Quote