Advertisement
Guest User

Untitled

a guest
Sep 29th, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. #include <cmath>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <cstdlib>
  8.  
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <ctime>
  12.  
  13.  
  14. ofstream outf;
  15. ifstream inf;
  16. ifstream inf2;
  17.  
  18. struct ele
  19. {
  20. string elem;
  21. float weight;
  22.  
  23. };
  24.  
  25. ele elements[30];
  26.  
  27.  
  28. int read(ele []);
  29. void sort(ele [], int);
  30. int locate(ele [], int, string);
  31. int find2(ele a[], int size, string word);
  32.  
  33. int main()
  34. {
  35. outf.open("elements.txt");
  36. inf.open("element.txt");
  37. inf2.open("formula.txt");
  38.  
  39. if (!inf)
  40. {
  41. cout << "Error opening file" << endl;
  42.  
  43. return 0;
  44. }
  45.  
  46. if(!inf2)
  47. {
  48. cout << "Error opening file" << endl;
  49.  
  50. return 0;
  51. }
  52.  
  53.  
  54. int max, pos, e;
  55. float sum = 0;
  56.  
  57. string sym;
  58.  
  59. char a, b;
  60.  
  61. string c, d;
  62.  
  63. max = read(elements);
  64.  
  65. sort(elements, max);
  66.  
  67.  
  68. while(inf2 >> a){
  69.  
  70. cout << a;
  71.  
  72. b = inf2.peek();
  73. c = a;
  74. d = b;
  75.  
  76. if(b == '\n'){
  77.  
  78. if(isupper(a)){
  79.  
  80. sym = a;
  81.  
  82. pos = find2(elements, max, sym);
  83.  
  84. sum = sum + elements[pos].weight;
  85. }
  86.  
  87. cout << " = " << sum;
  88. cout << endl;
  89.  
  90. sum = 0;
  91. }
  92.  
  93. if(islower(b)){
  94.  
  95. sym = c + d;
  96.  
  97. }
  98.  
  99. if(isdigit(b)){
  100.  
  101. inf2 >> e;
  102.  
  103. cout << e;
  104.  
  105. pos = find2(elements, max, sym);
  106.  
  107. sum = sum + (e * elements[pos].weight);
  108.  
  109.  
  110. }
  111.  
  112. if(ispunct(b)){
  113.  
  114. if(isupper(a)){
  115.  
  116. sym = a;
  117. }
  118. }
  119.  
  120. if(isupper(b)){
  121.  
  122. if(islower(a)){
  123.  
  124. pos = find2(elements, max, sym);
  125. sum = sum + elements[pos].weight;
  126. }
  127. }
  128.  
  129. if(isupper(a)){
  130.  
  131. if(isupper(b)){
  132.  
  133. sym = a;
  134. pos = find2(elements, max, sym);
  135. sum = sum + elements[pos].weight;
  136.  
  137.  
  138. }
  139. }
  140.  
  141. }
  142.  
  143. outf.close();
  144. inf.close();
  145. return 0;
  146.  
  147. }
  148.  
  149.  
  150. int read(ele a[]){
  151.  
  152. int x = 0;
  153.  
  154. while(inf >> a[x].elem >> a[x].weight){
  155.  
  156. x++;
  157.  
  158. }
  159.  
  160. return x;
  161.  
  162. }
  163.  
  164. void sort(ele a[], int max){
  165.  
  166. float temp;
  167. string temp2;
  168.  
  169.  
  170. for(int i = 0; i < max - 1; i++){
  171. for(int x = 0; x < max - 1; x++){
  172.  
  173. if(a[x].weight > a[x + 1].weight){
  174.  
  175. temp = a[x].weight;
  176. a[x].weight = a[x + 1].weight;
  177. a[x + 1].weight = temp;
  178.  
  179.  
  180. temp2 = a[x].elem;
  181. a[x].elem = a[x + 1].elem;
  182. a[x + 1].elem = temp2;
  183.  
  184. }
  185. }
  186. }
  187.  
  188.  
  189. }
  190.  
  191. int find2(ele a[], int size, string word){
  192.  
  193. int pos;
  194.  
  195. for(int x = 0; x < size; x++){
  196.  
  197. if(word == a[x].elem){
  198.  
  199. return x;
  200. }
  201. }
  202.  
  203. return -1;
  204.  
  205.  
  206.  
  207. }
  208.  
  209. int locate(ele a[], int size, string word){
  210.  
  211. int low = 0;
  212. int high = size;
  213. int mid;
  214. int pos;
  215.  
  216. while(low <= high)
  217. {
  218. mid = (low + high )/2;
  219.  
  220. if (word == a[mid].elem){
  221.  
  222. return mid;
  223. }
  224.  
  225. else if (a[mid].elem > word){
  226.  
  227. high = mid - 1;
  228.  
  229. }
  230. else{
  231.  
  232. low = mid + 1;
  233. }
  234. }
  235.  
  236. return -1;
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement