Vcoderz Community

Vcoderz Community (http://forum.vcoderz.com/index.php)
-   Computers & Information Technologies (http://forum.vcoderz.com/forumdisplay.php?f=6)
-   -   Rail ticket machine using c++ (http://forum.vcoderz.com/showthread.php?t=17415)

J()e 01-01-2010 05:41 PM

Rail ticket machine using c++
 
First,Merry Christmas and Happy New Year.
Second,as you all know,we enter uni on the 4th of January and I have a c++ assignment in which our dr told us that is a little difficult for beginners in c++, I look at the assignment and agreed with the dr:D,so i'm seeking your help here (as always:p). Fa farjouna bara3etkon:D.
================================================== ========

“Rail ticket machine”

Design and implement in C++ a program that manages a rail ticket machine.
The rail ticket machine functions as follows: a user selects a departure town, a destination town, a class (first or second), and a possible deduction (30%, 50%, or 60%). The rail ticket machine shows then the price of the ticket (as an integer expressed in Euros). The user inserts a sum of money as a set of coins. Four cases are then possible:

1. the sum of money is equal to the price of the ticket, the rail ticket machine delivers the ticket to the user;

2. the sum of money is greater than the price of the ticket and the rail ticket machine has enough change, the rail ticket machine returns the change and delivers the ticket to the user;

3. the sum of money is less than the price of the ticket, the rail ticket machine displays a message saying that the inserted sum is insufficient and returns the inserted money,

4. the sum of money is greater than the price of the ticket but the rail ticket machine does not have enough change, it indicates that the change is insufficient and returns the inserted money.

5. We assume that the accepted coins are in the categories 0.2, 0.5, 1 and 2 Euro.

6. We provide a table showing for two towns linked up directly by the rail the distance separating them. We also provide the price of a ticket in the first and the second class.

Write an algorithm that implements the rail ticket machine. When no direct link exists between the two given towns, your algorithm should find an intermediate town so that the total distance be minimized. Your solution should manage the stock of money available in the machine in order to minimize the number of coins returned to the user. The algorithm should print a ticket containing the following information: departure town, destination tow, via town, class, deduction, price.

DATA

NOTE:I don't know if this is important,but ba3ed ma darasna el pointers,we only know arrays and functions and loops ,pointers ba3ed ma wsolnelon.


so "GOOGLE","TAWA" and vcoderz c++ freaks i really need your help:D

Tawa 01-02-2010 01:17 AM

No, it's not difficult, you should've improved from the time we helped you last until now.

You're not learning anything if everytime you want someone to do it for you.

In addition, you didn't even make an effort and showed us where you reached. // Looks like you havn't even started.

Anyways, I'll look into it.

Tawa 01-02-2010 03:16 AM

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.

m0 01-18-2010 05:38 AM

Tawa: I liked your first answer better :( I am really against this when people go online and post their homework/assignment in full and ask for someone to do it. You are causing more harm then doing good.

It would have been better if he had specific questions. That proves to me that he is trying to understand the problem.

Google 01-18-2010 04:11 PM

Free to ask for help. Free to respond.

Tawa 01-18-2010 04:12 PM

Quote:

Originally Posted by m0 (Post 207663)
Tawa: I liked your first answer better :( I am really against this when people go online and post their homework/assignment in full and ask for someone to do it. You are causing more harm then doing good.

It would have been better if he had specific questions. That proves to me that he is trying to understand the problem.

Well, the thing is that J()e is not studying programing as his major :p it is just something he has to take.

In addition, some people won't learn even if they made their own homework, and others will just lookup some exercises to learn and maybe they can learn from this one :p


All times are GMT +1. The time now is 01:21 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger