kestutis

3lygis | 1pamoka | Balsės a ir e | ištaisyta Vytauto Rimkaus

Nov 26th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const char IVEST[]  = "Duomenys.txt";
  8. const char ISVEST[] = "Rezultatai.txt";
  9.  
  10. void balses (const char IV[], unsigned &a, unsigned &e) {
  11.     ifstream in(IV);
  12.     char c;
  13.     while((c = in.get()) && in.good())
  14.         if (c=='a')
  15.             a++;
  16.         else if (c=='e')
  17.             e++;
  18. }
  19. void isvedimas(const char ISVEST[], unsigned &a, unsigned &e) {
  20.     ofstream out(ISVEST);
  21.     out << "Balsė a tekste pasikartoja " << a << " kartų" << endl;
  22.     out << "Balsė e tekste pasikartoja " << e << " kartų" << endl;
  23.     int proc;
  24.     if (a>e) {
  25.         proc=(a-e)*100.0/a+0.5;
  26.         out << "Balsė e tekste pasikartoja " << proc << " % rečiau" << endl;
  27.     } else if (e>a) {
  28.         proc=(e-a)*100.0/e+0.5;
  29.         out << "Balsė a tekste pasikartoja " << proc << " % rečiau" << endl;
  30.     }
  31.     out.close();
  32. }
  33. int main() {
  34.     setlocale(LC_ALL, "Lithuanian");
  35.     string sakinys;
  36.     unsigned a=0, e=0;
  37.     balses(IVEST, a, e);
  38.     isvedimas(ISVEST, a, e);
  39.     return 0;
  40. }
  41. /*
  42. Užduoties sumąstytojo sprendimas:
  43. // C++
  44. #include <iostream>
  45. #include <fstream>
  46. #include <iomanip>
  47. using namespace std;
  48. const char CDfv[] = "Duomenys.txt";
  49. const char CRfv[] = "Rezultatai.txt";
  50. //----------------------------------------
  51. void Skaitymas(int & na, int & ne);
  52. void Irasymas (int na, int ne, char chr, double proc);
  53. void Skaiciavimas(int na, int ne, char & chr, double & proc);
  54. //----------------------------------------
  55.  
  56. int main() {
  57.     int na = 0;
  58.     int ne = 0;
  59.     char chr = '-';
  60.     double proc = 0;
  61.     Skaitymas(na, ne);
  62.     Skaiciavimas(na, ne, chr, proc);
  63.     Irasymas(na, ne, chr, proc);
  64.     system("pause");
  65.     return 0;
  66. }
  67.  
  68. void Skaitymas(int & na, int & ne)
  69. {
  70.      char chr;
  71.      ifstream fd(CDfv);
  72.      while (fd >> chr)
  73.      {
  74.               if(chr == 'a')
  75.               na = na + 1;
  76.              
  77.               if(chr == 'e')
  78.               ne = ne + 1;
  79.      }
  80.      fd.close();
  81. }
  82.  
  83. void Skaiciavimas(int na, int ne, char & chr, double & proc)
  84. {
  85.     if (na == 0){chr = 'e'; proc = ne*100; return;}
  86.     if (ne == 0){chr = 'a'; proc = na*100; return;}
  87.      
  88.     if(na < ne)
  89.     {
  90.              proc = ((double)(ne-na)*100/(double)ne);
  91.              chr='a';
  92.              }
  93.     else
  94.     {
  95.          proc = ((double)(na-ne)*100/(double)na);
  96.          chr='e';
  97.          }
  98. }
  99.  
  100. void Irasymas (int na, int ne, char chr, double proc)
  101. {
  102.      ofstream fr(CRfv);
  103.      fr << "Raidë a pasikartoja tekste " << na << " kartø."<< endl;
  104.      fr << "Raidë e pasikartoja tekste " << ne << " kartø."<< endl;
  105.      if(na != 0 && ne != 0)
  106.               fr << "Raidë " << chr << " pasikartoja "<< proc << "% reèiau."<<endl;
  107.      fr.close();
  108. }
  109. */
Advertisement
Add Comment
Please, Sign In to add comment