Advertisement
Strzyk

KOD

Feb 2nd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<time.h>
  4. #include<fstream>
  5. #include<string>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int* generujN(int N)
  11. {
  12. int* tmp;
  13. int i;
  14. srand(time(NULL));
  15. if (N<1)
  16. return 0;
  17. tmp=new int[N];
  18. for(i=0;i<N;i++)
  19. tmp[i]=rand()%11;
  20. return tmp;
  21. }
  22. int liczR(char name[]){
  23. ifstream p;
  24. string tmp;
  25. int i=0;
  26. p.open(name);
  27. while(p.good())
  28. {
  29. getline(p,tmp);
  30. i++;
  31. }
  32. p.close();
  33. return i-1;
  34. }
  35.  
  36. int liczC(char name[]){
  37. ifstream p;
  38. char c;
  39. int i=0,tmp;
  40. p.open(name);
  41. while(p.good())
  42. {
  43. p >> tmp;
  44. p.get(c);
  45. i++;
  46. if(c=='\n')break;
  47. }
  48. p.close();
  49. return i;
  50. }
  51.  
  52. int** generuj(int R, int C){
  53. int **tmp;
  54.  
  55. tmp = new int *[R];
  56.  
  57. for (int i = 0; i < R; i++)
  58. tmp[i] = new int [C];
  59.  
  60. return tmp;
  61. }
  62.  
  63. int** czytaj(char name[], int *R, int *C){
  64. int **tmp;
  65. ifstream p;
  66. *R=liczR(name);
  67. *C=liczC(name);
  68. tmp=generuj(*R,*C);
  69. p.open(name);
  70. for(int i=0; i<*R;i++){
  71. for(int j=0; j<*C; j++){
  72.  
  73. p>>tmp[i][j];
  74. p.get();
  75. }
  76. }
  77. p.close();
  78. return tmp;
  79. }
  80.  
  81. int main()
  82. {
  83. int *tab;
  84. int i,n,j;
  85. int **tab1, R,C;
  86. char fileO[]={"linia.csv"};
  87.  
  88. tab1 = czytaj(fileO,&R,&C);
  89.  
  90. ifstream inputFile;
  91. string nazwapliku;
  92. cout<<"Podaj nazwe pliku: ";
  93. getline(cin, nazwapliku);
  94. inputFile.open(nazwapliku.c_str());
  95. nazwapliku.erase(nazwapliku.find_last_of('.'),string::npos);
  96. nazwapliku=nazwapliku+"_nowu.csv";
  97. ofstream file(nazwapliku.c_str());
  98. if(inputFile.fail()){
  99. cerr<<"Blad otwarcia pliku"<<endl;
  100. return(1);
  101. }
  102.  
  103. tab = generujN(R+2);
  104. for (i=0; i<R+2; ++i)
  105. cout<<tab[i]<< " ";
  106.  
  107. cout<<endl;
  108. delete [] tab;
  109.  
  110. for( i=0; i<R; i++){
  111. for( j=0; j<C; j++){
  112. tab1[i][j]=tab1[i][j]+tab[i+2];
  113. cout << tab1[i][j]<<";";
  114. }
  115. cout<<endl;
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement