Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 19th, 2010 | Syntax: C++ | Size: 3.79 KB | Hits: 85 | Expires: Never
Copy text to clipboard
  1. // Gizmo.cpp : Defines the entry point for the console application.
  2. //
  3. #include "StdAfx.h"
  4. #include<iostream>
  5. #include<string>
  6. #include<fstream>
  7. #include<iomanip>
  8.  
  9. using namespace std;
  10.  
  11. struct statistics{
  12.         int words;
  13.         double averagewordlength;
  14. int wordlengthcount[13];
  15. };
  16. void outputstats (statistics textstat){
  17.        
  18. cout << "Total words are " << textstat.words << "\n";
  19. int i = 0;
  20. cout << "Number of words with 'X' letters \n";
  21.  
  22. while (i <13)
  23.         {
  24.                 double percent = double(textstat.wordlengthcount[i])/double(textstat.words) *100 ;
  25.                 if (i == 12)
  26.                 {
  27.                         cout << "=>";
  28.                 }
  29.                
  30.                 cout << i+1 << " has " << textstat.wordlengthcount[i] << " words and makes up " <<  percent << "%\n" ;
  31.                
  32.                
  33.                 i++;
  34.                
  35.         }
  36. cout << "Average word length is: " << textstat.averagewordlength/textstat.words << "\n";
  37. }
  38. string createpxtfile(string fname)
  39. {
  40.         //default
  41.         string rtnstr="Successful creation of PXT file\n";
  42.         string fName2 = fname.substr(0,fname.length() -4) + "converted.pxt" ;
  43. ofstream outFile(fName2.c_str());
  44. if (!outFile)
  45. rtnstr = "Failure\n";
  46.  
  47.         return rtnstr;
  48. }
  49. string readLine(istream& fin)
  50. {
  51. char buff[256];
  52. bool ignored = true;
  53.  
  54. // Clear any remaining end of line characters
  55. while (ignored == true)
  56.         {
  57.                 if(fin.peek()=='\n')
  58.                         {
  59.                                 ignored = true;
  60.                                 fin.ignore();
  61.                         }
  62.                 else
  63.                         ignored = false;
  64.         }
  65.  
  66.  
  67.  
  68. // Read the line into a buffer
  69. fin.getline(buff, 256, '\n');
  70.  
  71. // Convert the buffer to a string and return it
  72. return string(buff);
  73. }
  74. string getfile(){
  75.        
  76.         cout << "Please type in the filename for the program to read from, e.g C:\\example.txt\n";
  77.        
  78.         string fileloc;
  79.         cin >> fileloc;
  80.  
  81.         return string (fileloc);
  82. }
  83. string editpxtfile(string fname, statistics* text)
  84. {
  85.         string word;
  86. ifstream ptxFile(fname.c_str());
  87.  
  88. //warning, some wacked out error occurs in here to screw up wordcount we must make adjustments.
  89.         int lastwordlength = 0;
  90.                 while(!ptxFile.eof())
  91.                         {
  92.                                
  93.                                 text->words++;
  94.                                 ptxFile>>word;
  95.                                 lastwordlength = word.length();
  96.                                 text->averagewordlength = text->averagewordlength + word.length();
  97.                                 if(word.length()>12)
  98.                                         {
  99.                                                 text->wordlengthcount[12]++;
  100.                                         }
  101.                                 else
  102.                                         {
  103.                                                 text->wordlengthcount[word.length()-1]++;
  104.                                         }
  105.                         }
  106.                 if(lastwordlength>12)
  107.                                         {
  108.                                                 text->wordlengthcount[12]--;
  109.                                         }
  110.                                 else
  111.                                         {
  112.                                                 text->wordlengthcount[lastwordlength-1]--;
  113.                                         }
  114.  text->averagewordlength = text->averagewordlength - lastwordlength;
  115.  text->words = text->words -1;
  116.  
  117.         //ran through without error. thus successful
  118.         return "successful editing of structure through pointer\n";
  119. }
  120. //struct text
  121. statistics text;
  122. void main()
  123. {
  124.  
  125. text.words= 0;
  126. text.averagewordlength=0;
  127. int i = 0;
  128. while (i < 13)
  129. {
  130.         text.wordlengthcount[i] = 0;
  131.                 i++;
  132. }
  133. string word;
  134. bool eofReached = false;
  135. string fName = getfile();
  136. ifstream inFile(fName.c_str());
  137. string fName2 = fName.substr(0,fName.length() -4) + "converted.pxt" ;
  138. //now we create the file and check for errors
  139. cout << createpxtfile(fName);
  140. //now we write to the file
  141.  
  142. ofstream outFile(fName2.c_str());
  143.  
  144. if(!inFile)
  145.         {
  146.                 // If file is not read: Show error message
  147.                 cout << "Error, can't open file" << endl;
  148.         }
  149. else
  150.         {
  151.  
  152.                 // If file is read: Continue
  153.        
  154.                 while(!inFile.eof())
  155.                         {
  156.                                 inFile >> word;
  157.                                 for(int i = 0; i < (int)word.length(); i++)
  158.                                         {
  159.                                                 word[i] = tolower(word[i]);
  160.                                                 if(ispunct (word[i]))
  161.                                                         {
  162.                                                                 word[i] = ' ';
  163.                                                         }                      
  164.                                         }
  165.  
  166.                                 outFile << word << endl;
  167.                                 eofReached = inFile.eof();
  168.                         }      
  169.  
  170.                 inFile.close();
  171.                 outFile.close();
  172.         }
  173.  //we now edit the struct text through a function we pass the pointer of an array to it and the pxt filename.
  174. statistics* textpointer = &text;
  175. cout << editpxtfile(fName2, textpointer);
  176. //now function to output.
  177. outputstats(text);
  178. system("pause");
  179. }