Noam_15

טיפול בקבצים יום רביעי, 1 בפברואר 2017

Feb 8th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. /*
  2. #include <stdio.h>
  3. void main() {
  4. FILE *fp;
  5. char str[200];
  6. char ch1,ch2='A';
  7. int num_of_char = 0;
  8. int num_of_word = 0;
  9.  
  10. printf("Enter path:\n");
  11. gets(str); // tttrrr.txt
  12. fp = fopen(str,"r");
  13. if (fp == NULL){
  14. printf("Can't open file\n");
  15. exit(0);
  16. }
  17.  
  18. while(!feof(fp)){
  19. num_of_char ++;
  20. ch1 = fgetc(fp);
  21. if((ch1== ' ' || ch1== '\n' || ch1== '\t')&& (ch2!= ' ' && ch2!= '\n' && ch2!= '\t')&& num_of_char>1)
  22. num_of_word ++;
  23. if(!feof(fp))
  24. ch2 = ch1;
  25. }
  26. fclose(fp);
  27.  
  28. if (ch2!= ' ' && ch2!= '\n' && ch2!= '\t'&& num_of_char>1){ // עבור המלה האחרונה
  29. num_of_word++;
  30. }
  31.  
  32. printf("num of characters: %d\n", num_of_char-1 );
  33. printf("num of words: %d\n", num_of_word );
  34. }
  35. */
  36.  
  37. /* // 2
  38. #include <stdio.h>
  39.  
  40. void main(){
  41. char str[200],ch;
  42. FILE *fp;
  43. int n0=0,n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0;
  44. printf("Enter path:\n");
  45. gets(str); // tttrrr.txt
  46. fp = fopen(str,"r");
  47. if (fp == NULL){
  48. printf("Can't open file\n");
  49. exit(0);
  50. }
  51.  
  52. while(!feof(fp)){
  53. ch = fgetc(fp);
  54.  
  55. switch (ch){
  56. case '0':{ n0 ++; break;}
  57. case '1':{ n1 ++; break;}
  58. case '2':{ n2 ++; break;}
  59. case '3':{ n3 ++; break;}
  60. case '4':{ n4 ++; break;}
  61. case '5':{ n5 ++; break;}
  62. case '6':{ n6 ++; break;}
  63. case '7':{ n7 ++; break;}
  64. case '8':{ n8 ++; break;}
  65. case '9':{ n9 ++; break;}
  66. }
  67. }
  68. fclose(fp);
  69. printf("0: %d\n1: %d\n2: %d\n3: %d\n4: %d\n5: %d\n6: %d\n7: %d\n8: %d\n9: %d\n",n0,n1,n2,n3,n4,n5,n6,n7,n8,n9);
  70. }
  71. */
  72.  
  73.  
  74.  
  75. #include <stdio.h>
  76. #include<string.h>
  77.  
  78. struct Product{
  79. char Name[20];
  80. double Price;
  81. int Amount;
  82. };
  83.  
  84.  
  85. void main(){
  86. FILE *fp;
  87. char str[200],ch;
  88. char annoying;
  89. struct Product P;
  90. int count=0,amount1=0,sum=0;
  91. double price1=0,best_price = 0,price_of_all=0;
  92. char name[20],best_name[20];
  93.  
  94.  
  95. printf("Enter path:\n");
  96. gets(str); // tttrrr.txt
  97. /*
  98. fp = fopen(str,"w");
  99.  
  100. do{
  101. printf("Enter Product Name: ");
  102. scanf("%s",P.Name);
  103. printf("Enter Price: ");
  104. scanf("%lf",&P.Price);
  105. printf("Enter Amount ");
  106. scanf("%d",&P.Amount);
  107.  
  108.  
  109. fprintf(fp,"%s\n%.2lf\n%d\n",P.Name,P.Price,P.Amount);
  110.  
  111.  
  112.  
  113.  
  114. printf("Enter 'y' to create a new product or 'n' to finish: ");
  115. flushall();
  116. annoying = getchar();
  117.  
  118. while(annoying != 'n'&& annoying != 'N' && annoying != 'y' && annoying != 'Y'){
  119. printf("Please enter a legal input: ");
  120. flushall();
  121. annoying = getchar();
  122. }
  123.  
  124. } while(annoying == 'y' || annoying == 'Y');
  125.  
  126. fclose (fp);
  127. */
  128. /////// עד כאן קלטנו
  129.  
  130.  
  131.  
  132. fp = fopen(str,"r"); // tttrrr.txt
  133. while (!feof(fp)) {
  134. fgets(name,19,fp);
  135. fscanf(fp,"%lf",& price1);
  136. if (feof(fp)) break; // למהההה
  137. amount1=0; // למהההההה
  138. fscanf(fp,"%d",& amount1);
  139.  
  140.  
  141. sum += amount1;
  142. price_of_all += (price1 * amount1);
  143.  
  144. if (price1 >= best_price){
  145. best_price = price1;
  146. strcpy(best_name,name);
  147. }
  148.  
  149. }
  150.  
  151. printf("How many products: %d\n",sum);
  152. printf("Price of all: %.2lf\n",price_of_all);
  153. printf("The most expensive: %s\n",best_name);
  154.  
  155. fclose (fp);
  156. }
Advertisement
Add Comment
Please, Sign In to add comment