מבוא למדעי המחשב תרגיל 8

benny++C, מבוא למדעי המחשב3 Comments


/*************************
targil bait 8 shela 1

lecture: Dr meir komer
discription: create a dynamic data base ,1) add information or 2) delete information from the 
data base. 3) search in the data base. 4) print some clculations from the data base.
**************************/
#include <iostream>
using namespace std;

//prototipe

void addLine(int **buses, int numBuses, int line, int stops, int durationOfRide);
void print(int**buses, int numBuses);
void deleteLine(int** buses, int &numBuses, int line);
void sortBuble(int** buses, int numBuses);
int search(int **buses, int numBuses, int line);
float averageTravel(int**buses, int numBuses);
int shortest(int** buses, int &numBuses);
float averageStops(int** buses, int numBuses); 


int  main()
{
	int ** buses = NULL;
	int numBuses = 0;
	int line;
	int stops;
	int durationOfRide;
	int choice;
	int searchAnswer;
	float averageRide;
	float average_stops;
	int shortRide;

	do {	 	  	 	   	   		   	 	    	 	
		cout << "enter 0-7" << endl;
		cin >> choice;
		switch (choice)
		{
		case 0: // add line
			cout << "enter the line to add" << endl;
			cin >> line;
			cout << "enter the number of stops" << endl;
			cin >> stops;
			cout << "enter the duration of the ride" << endl;
			cin >> durationOfRide;

			numBuses++;      // keep the number of buses in data base updated;
			if (numBuses == 1)
				buses = new int *[1];// creating a dynamic memory for the firsr bus.
			addLine(buses, numBuses, line, stops, durationOfRide);

			sortBuble(buses, numBuses);

			print(buses, numBuses);
			break;

		case 1:	// delete line
			cout << "enter the line to delete" << endl;
			cin >> line;
			deleteLine(buses, numBuses, line);
			print(buses, numBuses);
			break;
		case 2: // print all lines
			print(buses, numBuses);
			break;
		case 3: // search for a particular line
			cout << "enter the line to search for" << endl;
			cin >> line;
			searchAnswer = search(buses, numBuses, line);
			cout << searchAnswer << endl;
			break;
		case 4: // calculate average travel time of all buses
			averageRide = averageTravel(buses, numBuses);
			cout << averageRide << endl;
			break;
		case 5:  // calcuate average stops of all buses
			average_stops = averageStops(buses, numBuses);
			cout << average_stops << endl;
			break;
		case 6: // calculate the bus with the shortest travel time
			shortRide = shortest(buses, numBuses);
			cout << shortRide << endl;
			break;
		case 7: break;   // exit the program

		default: cout << "ERROR" << endl;
		}// switch
	} while (choice != 7);

	/*for (int i = 0; i < numBuses; i++)
	delete[] buses[i];
	if (numBuses > 0)
	delete[] buses;*/

	//	system("pause");
	return 0;
}	 	  	 	   	   		   	 	    	 	

void addLine(int **buses, int numBuses, int line, int stops, int durationOfRide)
{
	buses[numBuses - 1] = new int[3];
	buses[numBuses - 1][0] = line;
	buses[numBuses - 1][1] = stops;
	buses[numBuses - 1][2] = durationOfRide;

}
void print(int**buses, int numBuses)
{
	for (int i = 0; i < numBuses; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << buses[i][j] << " ";

		}
		cout << endl;
	}

}

void deleteLine(int** buses, int &numBuses, int line)
{

	int**temp = NULL; int chack = 0;
	temp = new int *[numBuses];
	for (int i = 0; i < numBuses; i++)
	{
		temp[i] = new int[3];
	}

	for (int i = 0; i < numBuses; i++)
	{
		for (int j = 0; j < 3; j++)
		{	 	  	 	   	   		   	 	    	 	
			if (buses[i][0] == line)    // if the first element (the line number is always in the index 0) is equal to line 
			{
				chack = 1;           // let me know and change the all the information about this line (all element in thisrow) to (-1) value . 
				buses[i][j] = -1;
				buses[i][j + 1] = -1;
				buses[i][j + 2] = -1;
			}

		}
	}

	if (chack == 1)   // if the line that we wont to delete is Exist
	{
		int *tempArray1;              // create a 1D dynamic temporary array
		tempArray1 = new int[3 * numBuses];  // the size will be 'number of buses *3'

		for (int i = 0, w = 0; i < numBuses; i++)        //  copy all lines data to the temporary array (includind (-1) values).
		{
			for (int j = 0; j < 3; j++)
			{
				tempArray1[w] = buses[i][j];
				w++;
			}
		}

		int* tempArray2 = new int[3 * numBuses];            //  create a another 1D dinamyc temporary_2 array

		for (int i = 0, j = 0; i < 3 * numBuses; i++)
		{
			if (tempArray1[i] != -1)            // copy all lines data to the temporary_2 array without (-1) values.
			{
				tempArray2[j++] = tempArray1[i];
			}
		}

		numBuses--;
		for (int i = 0, w = 0; i < numBuses; i++)
		{	 	  	 	   	   		   	 	    	 	
			for (int j = 0; j < 3; j++)                //  copy all lines data back to the busus data base.
			{
				buses[i][j] = tempArray2[w];
				w++;
			}
		}
		//  release the dynamic memory.
		delete[]tempArray2;
		delete[]tempArray1;
		for (int i = 0; i < numBuses; i++)
			delete[] temp[i];
		delete[]temp;
	}

}
void sortBuble(int** buses, int numBuses)
{
	int*array = new int[3 * numBuses];          //  create a 1D dynamic temporary array.
	int temp[3];                        // create another 1D  temporary array 
	for (int i = 0, w = 0; i < numBuses; i++)
	{
		for (int j = 0; j < 3; j++, w++)
		{
			array[w] = buses[i][j];   // copy all lines data to the dynamic temporary array.
		}
	}
	//sortBuble
	int i;
	for (int r = 0; r < numBuses - 1; r++)
	{
		for (i = 0; i < 3 * (numBuses - 1); i += 3)
		{
			if (array[i]>array[i + 3])
			{
				for (int j = i, k = 0; k < 3; k++, j++)
				{	 	  	 	   	   		   	 	    	 	
					temp[k] = array[j + 3];

				}

				for (int k = 0, j = i; k < 3; k++, j++)
				{
					array[j + 3] = array[j];
				}


				for (int k = 0, j = i; k < 3; k++, j++)
				{
					array[j] = temp[k];

				}

			}

		}

	}


	for (int i = 0, w = 0; i < numBuses; i++)
	{
		for (int j = 0; j < 3; j++, w++)
		{
			buses[i][j] = array[w];      // copy all the sorted data back to the busus data base.
		}
	}
	delete[]array;
}
int search(int **buses, int numBuses, int line)
{

	int i = 0, flag = 0;
	for (i = 0; i < numBuses; i++)
	{	 	  	 	   	   		   	 	    	 	
		if (buses[i][0] == line)
		{
			flag = i;
		}
	}

	if (flag == i)
	{
		return flag;
	}
	if (flag == 0)
		return -1;


}
float averageTravel(int**buses, int numBuses)
{
	float averageTravel; int sum = 0;
	for (int i = 0; i < numBuses; i++)
	{
		sum += buses[i][2];
	}
	averageTravel = (float)sum / numBuses;
	return averageTravel;

}
float averageStops(int** buses, int numBuses)
{
	float averageStops; int sum = 0;
	for (int i = 0; i < numBuses; i++)
	{
		sum += buses[i][1];
	}
	averageStops = (float)sum / numBuses;
	return averageStops;

}	 	  	 	   	   		   	 	    	 	
int shortest(int** buses, int &numBuses)
{
	int small = 0, flag = 0;
	for (int i = 0; i < numBuses; i++)
	{
		for (int j = 0; j < numBuses; j++)
		{
			if (buses[i][2] < buses[j][2])
			{
				small++;
				if (small == numBuses - 1)
				{
					flag = buses[i][0];
				}
			}
		}
	}
	if (flag>0)
	{
		return flag;

	}
	else
	{
		return -1;
	}
}

/***********OUTPUT*************
enter 0-7
0
enter the line to add
1
enter the number of stops
1
enter the duration of the ride
1
1 1 1
enter 0-7
0
enter the line to add
3
enter the number of stops
2
enter the duration of the ride
4
1 1 1
3 2 4
enter 0-7
0
enter the line to add
5
enter the number of stops
6
enter the duration of the ride
4
1 1 1
3 2 4
5 6 4
enter 0-7
0
enter the line to add
7
enter the number of stops
6
enter the duration of the ride
5
1 1 1
3 2 4
5 6 4
7 6 5
enter 0-7
1
enter the line to delete
1
3 2 4
5 6 4
7 6 5
enter 0-7
6
5
enter 0-7
4
4.33333

****************************/

	 	  	 	   	   		   	 	    	 	

benny

3 Comments on “מבוא למדעי המחשב תרגיל 8”

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *