Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9.  
  10.  
  11. struct Node
  12. {
  13. int condition;
  14. int nodeID;
  15. int xValue;
  16.  
  17. };
  18.  
  19. struct Element
  20. {
  21.  
  22. int* nodeID1;
  23. int* nodeID2;
  24. int lElement;
  25. float kElement=1;
  26. float sElement=1;
  27.  
  28. };
  29.  
  30. float localMatrix[1][1];
  31. static int elements;
  32. static int nodes;
  33. static float L;
  34. static float k;
  35. static float S;
  36. static float alfa;
  37. static float q;
  38. vector <Node> nodeNet;
  39. vector <Element> elementNet;
  40. vector <int> xVector;
  41.  
  42.  
  43. void ReadFromFile()
  44. {
  45. fstream file;
  46. file.open("input",ios::in);
  47. if (file.is_open())
  48. {
  49. cout<<"Plik został otwarty!"<<endl;
  50.  
  51.  
  52. string line;
  53.  
  54. getline(file,line);
  55. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  56. elements=atoi (line.c_str());
  57. cout<<endl<<"Liczba elemntow: "<<elements<<endl;
  58.  
  59. getline(file,line);
  60. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  61. nodes=atoi (line.c_str());
  62. cout<<endl<<"Liczba wezlow: "<<nodes<<endl;
  63.  
  64. getline(file,line);
  65. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  66. L=atof (line.c_str());
  67. cout<<endl<<"Dlugosc L: "<<L<<endl;
  68.  
  69. getline(file,line);
  70. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  71. k=atof (line.c_str());
  72. cout<<endl<<"Stala k: "<<k<<endl;
  73.  
  74. getline(file,line);
  75. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  76. S=atof (line.c_str());
  77. cout<<endl<<"Powierzchnia S: "<<S<<endl;
  78.  
  79. getline(file,line);
  80. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  81. alfa=atof (line.c_str());
  82. cout<<endl<<"Wspolczynnik alfa: "<<alfa<<endl;
  83.  
  84. getline(file,line);
  85. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  86. q=atof (line.c_str());
  87. cout<<endl<<"Strumien ciepla q: "<<q<<endl;
  88.  
  89.  
  90. getline(file,line);
  91. line=line.substr(line.find_first_of('"')+1,line.find_last_of('"')-(line.find_first_of('"')+1));
  92. stringstream stream(line);
  93. int n;
  94. while(stream >> n)
  95. {
  96. xVector.push_back(n);
  97. }
  98.  
  99. // for(int i=0;i<xVector.size();i++)
  100. // cout<<endl<<"Element numer "<<i<<"rowna sie "<<xVector[i]<<endl;
  101. //
  102. file.close();
  103. }
  104. else
  105. {
  106. cout<<"ERROR! Sprawdz dane wejsciowe i pamietaj by umieścic je między znakami \"\"";
  107. }
  108. }
  109.  
  110.  
  111. void CreateNets()
  112. {
  113. for (int i=1; i<=nodes; ++i)
  114. {
  115. Node node;
  116. node.nodeID=i;
  117. if(i==1 )
  118. node.condition=1;
  119. else if(i==nodes)
  120. node.condition=2;
  121. else
  122. node.condition=0;
  123.  
  124. node.xValue=xVector[i-1];
  125.  
  126. nodeNet.push_back(node);
  127. }
  128.  
  129.  
  130. //for(int i=0;i<nodeNet.size();i++)
  131. //cout<<endl<<"Element numer "<<i<<"rowna sie "<<nodeNet[i].xValue<<endl;
  132.  
  133.  
  134. for (int i=1; i<=elements; ++i)
  135. {
  136. Element element;
  137. element.lElement=(nodeNet[i].xValue - nodeNet[i-1].xValue);
  138.  
  139. elementNet.push_back(element);
  140. }
  141. }
  142.  
  143.  
  144. void CreateLocalMatrix(Element element)
  145. {
  146.  
  147. float c;
  148.  
  149. c=(element.sElement*element.kElement)/element.lElement;
  150.  
  151. localMatrix[0][0]= c;
  152. localMatrix[0][1]= c*(-1);
  153. localMatrix[1][0]= c*(-1);
  154. localMatrix[1][1]= c;
  155.  
  156.  
  157. cout<<"\t\t C jest rowne: \t"<<c<<endl;
  158. }
  159.  
  160. void CreateGlobalMatrix()
  161. {
  162.  
  163. float** globalMatrix = new float*[elements+1];
  164. for(int i = 0; i < elements+1; ++i)
  165. globalMatrix[i] = new float[elements+1];
  166.  
  167. for(int i=0; i<elements+1; ++i)
  168. for(int j=0; j<elements+1; ++j)
  169. globalMatrix[i][j]=0;
  170.  
  171. //CREATING LOCAL MATRIX
  172. int k=0;
  173. for(int i=0; i<elementNet.size(); ++i)
  174. {
  175. CreateLocalMatrix(elementNet[i]);
  176.  
  177. cout<<"Macierz lokalna wynosi"<<endl;
  178. for(int i=0; i<2; ++i)
  179. {
  180. {
  181. for(int j=0; j<2; ++j)
  182. cout<<localMatrix[i][j]<<" ";
  183. }
  184. cout<<endl;
  185. }
  186.  
  187. globalMatrix[i][k]+=localMatrix[0][0];
  188. globalMatrix[i][k+1]+=localMatrix[0][1];
  189. globalMatrix[i+1][k]+=localMatrix[1][0];
  190. globalMatrix[i+1][k+1]+=localMatrix[1][1];
  191. k++;
  192.  
  193. }
  194.  
  195. cout<<"Macierz globalna jest rowna: "<<endl;
  196. for(int i=0; i<elements+1; ++i)
  197. {
  198. {
  199. for(int j=0; j<elements+1; ++j)
  200. cout<<globalMatrix[i][j]<<" \t";
  201. }
  202. cout<<endl<<endl;
  203. }
  204. }
  205.  
  206. int main()
  207. {
  208. ReadFromFile();
  209. CreateNets();
  210. CreateGlobalMatrix();
  211.  
  212.  
  213.  
  214. return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement