Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication4.cpp : Defines the entry point for the console application.
- //
- //#include "stdafx.h"
- #include <iostream>
- #include <stdio.h>
- #include <string>
- #include <math.h>
- #include <fstream>
- using namespace std;
- double ** read(int &numberOfPoints);
- void save(int numberOfPoints, double ** table);
- void insertsort(double **table, int numberOfPoints);
- void bubblesort(int numberOfPoints, double **table);
- void compute(int numberOfPoints, double **table);
- void print(int precision, int numberOfPoints, double **table);
- void printHeader(int precision);
- void initialize(double & variabilityOfX, int & numberOfPoints, int & precision);
- int instruction();
- void initialize(double & variabilityOfX, int & numberOfPoints, int & precision) {
- std::cout << "Podaj liczbe punktow: " << std::endl;
- std::cin >> numberOfPoints;
- std::cout << "Podaj zmiennosc x: " << std::endl;
- std::cin >> variabilityOfX;
- std::cout << "Podaj dokladnosc: " << std::endl;
- std::cin >> precision;
- }
- void printHeader(int precision) {
- int prec = 7;
- if (precision > prec) {
- prec = precision;
- }
- printf("|%-3s", "i");
- printf("|%*s", (-1)*(precision + 2), "x");
- printf("|%*s", (-1)*(precision + 2), "sin");
- printf("|%*s", (-1)*(precision + 2), "cos");
- printf("|%*s", (-1)*(precision + 2), "tg");
- printf("|%*s", (-1)*(precision + 2), "ctg");
- printf("|%*s", (-1)*(precision + 2), "asin");
- printf("|%*s", (-1)*(precision + 2), "acos");
- printf("|%*s", (-1)*(precision + 2), "atg");
- printf("|%*s|", (-1)*(precision + 2), "actg");
- printf("%\n");
- }
- void print(int precision, int numberOfPoints, double **table) {
- for (int i = 0; i < numberOfPoints; i++) {
- printf("|%*d", -3, (int)table[i][0]);
- for (int j = 1; j < 10; j++) {
- int num = (int)(table[i][j]);
- int c = 0;
- while ((num /= 10) != 0)
- c++;
- if (std::isnan(table[i][j])) {
- printf("|%*s", precision + 2, "-");
- }
- else if (std::isinf(table[i][j])) {
- printf("|%*s", precision + 2, "inf");
- }
- else if (table[i][j] < 0) {
- printf("|%.*f", precision - 1 - c, table[i][j]);
- }
- else
- printf("|%.*f", precision - c, table[i][j]);
- }
- printf("|%\n");
- }
- }
- void compute(int numberOfPoints, double **table) {
- for (int i = 0; i < numberOfPoints; i++) {
- table[i][2] = sin(table[i][1]);
- table[i][3] = cos(table[i][1]);
- table[i][4] = tan(table[i][1]);
- table[i][5] = 1.0 / (tan(table[i][1]));
- table[i][6] = asin(table[i][1]);
- table[i][7] = acos(table[i][1]);
- table[i][8] = atan(table[i][1]);
- table[i][9] = 1.0 / (atan(table[i][1]));
- }
- }
- void bubblesort(int numberOfPoints, double **table) {
- cout << "Wybierz kryterium sortowania" << endl;
- cout << "1 - sin" << endl;
- cout << "2 - cos" << endl;
- cout << "3 - tan" << endl;
- cout << "4 - ctg" << endl;
- cout << "5 - arcsin" << endl;
- cout << "6 - arccos" << endl;
- cout << "7 - arctan" << endl;
- cout << "8 - arctg" << endl;
- int ask1;
- cin >> ask1;
- int i, j;
- int index = ask1 + 1;
- double *temp;
- double temp1, temp2;
- for (i = (numberOfPoints - 2); i >= 0 ; i--)
- {
- for (j = 0; j <= i; j++) {
- temp1 = table[j][index], temp2 = table[j + 1][index];
- if (temp1 > temp2) {
- double *temp = table[j];
- table[j] = table[j+1];
- table[j + 1] = temp;
- }
- }
- }
- for (int i = 0; i < numberOfPoints; i++) table[i][0] = i;
- }
- void insertsort(double **table, int numberOfPoints) {
- cout << "Wybierz kryterium sortowania" << endl;
- cout << "1 - sin" << endl;
- cout << "2 - cos" << endl;
- cout << "3 - tan" << endl;
- cout << "4 - ctg" << endl;
- cout << "5 - arcsin" << endl;
- cout << "6 - arccos" << endl;
- cout << "7 - arctan" << endl;
- cout << "8 - arctg" << endl;
- int ask1;
- cin >> ask1;
- int j;
- int index = ask1 + 1;
- double *currentLine;
- for (int i = 1; i < numberOfPoints; i++) {
- currentLine = table[i];
- for (j = i - 1; j >= 0 && table[j][index] > currentLine[index]; j--) {
- table[j + 1] = table[j];
- }
- table[j + 1] = currentLine;
- }
- for (int i = 0; i < numberOfPoints; i++) table[i][0] = i;
- }
- void save(int numberOfPoints, double ** table) {
- fstream file;
- file.open("wynik.txt", ios::out);
- if (file.good() == true)
- {
- file << numberOfPoints << endl;
- for (int i = 0; i < numberOfPoints; i++) {
- for (int j = 1; j < 10; j++) {
- file << table[i][j]<<endl;
- }
- }
- file.close();
- }
- }
- double ** read(int &numberOfPoints)
- {
- fstream file;
- string temp;
- double ** table = NULL;
- file.open("wynik.txt", ios::in | ios::out);
- if (file.good() == true)
- {
- file >> numberOfPoints;
- table = new double*[numberOfPoints];
- for (int i = 0; i < numberOfPoints; i++) {
- table[i] = new double[10];
- table[i][0] = i;
- }
- for (int i = 0; i < numberOfPoints && !file.eof(); i++) {
- for (int j = 1; j < 10 && !file.eof(); j++) {
- file >> temp;
- if (temp != "inf") table[i][j] = stod(temp.c_str());
- else table[i][j] = INFINITY;
- }
- }
- file.close();
- }
- return table;
- }
- int instruction() {
- int answer = 0;
- while (answer < 1 || answer > 7) {
- cout << "***MENU***" << endl;
- cout << "[1] Utworz nowa tablice" << endl;
- cout << "[2] Wczytaj tablice z pliku" << endl;
- cout << "[3] Wyswietl tablice" << endl;
- cout << "[4] Sortuj (bubble sort)" << endl;
- cout << "[5] Sorutuj (insert sort)" << endl;
- cout << "[6] Zapisz do pliku" << endl;
- cout << "[7] Wyjdz" << endl;
- cin >> answer;
- }
- return answer;
- }
- int main() {
- double variabilityOfX;
- int numberOfPoints;
- int precision = 3;
- double **table = NULL;
- int answer = 0;
- while (answer != 7) {
- system("cls");
- answer = instruction();
- if (answer == 1) {
- if (table != NULL) {
- for (int i = 0; i < numberOfPoints; i++) {
- delete table[i];
- }
- delete table;
- }
- initialize(variabilityOfX, numberOfPoints, precision);
- table = new double*[numberOfPoints];
- for (int i = 0; i < numberOfPoints; i++) {
- table[i] = new double[10];
- table[i][0] = i;
- table[i][1] = i*variabilityOfX;
- }
- compute(numberOfPoints, table);
- }
- else if (answer == 2) {
- if (table != NULL) {
- for (int i = 0; i < numberOfPoints; i++) {
- delete table[i];
- }
- delete table;
- }
- table = read(numberOfPoints);
- }
- else if (answer == 3) {
- if (table != NULL) {
- printHeader(precision);
- print(precision, numberOfPoints, table);
- }
- else cout << "Brak tablicy!" << endl;
- }
- else if (answer == 4) {
- if(table != NULL) bubblesort(numberOfPoints, table);
- else cout << "Brak tablicy!" << endl;
- }
- else if (answer == 5) {
- if(table != NULL) insertsort(table, numberOfPoints);
- else cout << "Brak tablicy!" << endl;
- }
- else if (answer == 6) {
- if(table != NULL) save(numberOfPoints, table);
- else cout << "Brak tablicy!" << endl;
- }
- getchar();
- getchar();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement