Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- #include <sstream>
- #include <fstream>
- int main()
- {
- std::map<char, int> alphabet;
- std::string alphString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int stringOfNumbers = 1;
- int iterations = 0;
- // put every letter of the alphabet into std::map
- for(int i = 0; i != alphString.length(); i ++){
- alphabet.insert(std::pair<char, int>(alphString[i], i+1));
- }
- for(std::map<char, int>::iterator it = alphabet.begin(); it != alphabet.end(); it ++){
- std::cout << it->first << " => " << it->second << std::endl;
- } std::cout << std::endl;
- // first file open to read number of lines (there is 7 lines)
- std::fstream file("test.txt");
- int countLines = 0;
- if(file.is_open()){
- std::string word;
- while(getline(file, word)){
- std::istringstream iss(word);
- while(iss){
- std::string s;
- iss >> s;
- countLines++;
- }
- }
- file.close();
- }
- // second file open to read character by character and calculate
- std::fstream file2("test.txt");
- if(file2.is_open()){
- char singleLetter;
- int sum = 0;
- while(file2.get(singleLetter)){
- std::cout << alphabet.find(singleLetter)->first << " => " << alphabet.find(singleLetter)->second << std::endl;
- // sum the values of the single chars for the whole word
- sum += alphabet.find(singleLetter)->second;
- } std::cout << "Sum: " << sum << std::endl;
- // I musted divide by 2, because lines were counted somehow bad
- std::cout << "Lines: " << countLines/2 << std::endl;
- file2.close();
- } std::cout << std::endl;
- // first ten elements of the string of numbers
- for(int i = 2; iterations != 10; i ++){
- stringOfNumbers += i;
- std::cout << stringOfNumbers << " ";
- iterations++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment