Advertisement
Guest User

Untitled

a guest
May 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include "my_sort_razrad.hpp"
  4. #include <ctime>
  5. #include <fstream>
  6. using namespace std;
  7. int main(){
  8. srand(time_t(NULL));
  9. int n = 0,i,boolean;
  10. cout<<"Введите 1 (для вода с клавиатуры и записи в файл)\n\t\t 2 (для генерации случайных чисел и вывода в фийл)\n\t\t 3 (для ввода из текстового файла)\n";
  11. cin>>boolean;
  12. if((boolean>3)||(boolean<1)){
  13. cout<<"Читайте внимательно условие"<<endl;
  14. return 0;
  15. }
  16. if (boolean!=3){
  17. cout<<"Введите размерность массива n= ";
  18. cin>>n;
  19. cout<<endl;
  20. }
  21. //------------------------------------1--------------------------------------------------------------------
  22. if(boolean==1){
  23. FILE *out;
  24. int *a= new int [n];
  25. for (i=0;i<n;i++){
  26. cin>>a[i];
  27. }
  28. cout<<endl;
  29. *a = my_sort_razrad (a,n);
  30. string mas;
  31. for(i=0;i<n;i++){
  32. mas+=to_string(a[i]);
  33. mas+=" ";
  34. }
  35. if ((out=fopen("/Users/berkovichpasha/Desktop/УНИВЕР/Мой_1_курс/Programs/УП/UP_sort/out.txt","w")) == NULL){
  36. cout<<"Выходной файл не открыт"<<endl;
  37. return 0;
  38. }
  39. char *cstr = new char[mas.length() + 1];
  40. strcpy(cstr, mas.c_str());
  41. fputs(cstr,out);
  42. delete [] cstr;
  43. delete [] a;
  44. }
  45. //------------------------------------2--------------------------------------------------------------------
  46. if(boolean==2){
  47. FILE *out;
  48. int *a= new int [n];
  49. int c,d;
  50. do{
  51. cout<<"Ввелите границы [c;d] ";
  52. cin>>c>>d;}
  53. while(c>d);
  54. cout<<endl;
  55. for (i=0;i<n;i++){
  56. a[i]=rand()%(d-c+1)+c;
  57. cout<<a[i]<<" ";
  58. }
  59. cout<<endl;
  60. *a = my_sort_razrad (a,n);
  61. string mas;
  62. for(i=0;i<n;i++){
  63. mas+=to_string(a[i]);
  64. mas+=" ";
  65. }
  66. if ((out=fopen("/Users/berkovichpasha/Desktop/УНИВЕР/Мой_1_курс/Programs/УП/UP_sort/out.txt","w")) == NULL){
  67. cout<<"Выходной файл не открыт"<<endl;
  68. return 0;
  69. }
  70. char *cstr = new char[mas.length() + 1];
  71. strcpy(cstr, mas.c_str());
  72. fputs(cstr,out);
  73. delete [] cstr;
  74. delete [] a;
  75. }
  76. //------------------------------------3--------------------------------------------------------------------
  77. if(boolean==3){
  78. ifstream f;
  79. f.open ("/Users/berkovichpasha/Desktop/УНИВЕР/Мой_1_курс/Programs/УП/UP_sort/in.txt");
  80. if (!f){
  81. cout<< "Ошибка, файл не открыт" << endl;
  82. return 0;
  83. }
  84. int v;
  85. f>>v;
  86. if((f.eof())){
  87. cout<<"Файл пустой"<<endl;
  88. return 0;
  89. }
  90. int kol(1);
  91. while(true){
  92. //int v;
  93. f>>v;
  94. if(!f.eof())
  95. kol++;
  96. else break;
  97. }
  98. f.close();
  99. int *a= new int [kol];
  100. ifstream fl("/Users/berkovichpasha/Desktop/УНИВЕР/Мой_1_курс/Programs/УП/UP_sort/in.txt");
  101. for(i=0;i<kol;i++){
  102. fl>>a[i];
  103. }
  104. fl.close();
  105. *a = my_sort_razrad (a,kol);
  106. ofstream fout("/Users/berkovichpasha/Desktop/УНИВЕР/Мой_1_курс/Programs/УП/UP_sort/out.txt");
  107. if (!fout.is_open()){
  108. cout<<"Не открылся"<<endl;
  109. return 0;
  110. }
  111. for(i=0;i<kol;i++){
  112. fout<<a[i]<<" ";
  113. }
  114. fout.close();
  115. }
  116. cout<<endl;
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement