Advertisement
Caminhoneiro

Cmaismais

Jan 4th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.72 KB | None | 0 0
  1. C++ ESTUDOS!!!!
  2.  
  3. #include <iostream> ;       //Inclui librarys
  4. #include<vector>
  5. #include<string>
  6. #include <fstream> //for I/O
  7.  
  8. using namespace std; //Dont need throw std:: in front of any shit
  9.  
  10. ///FUNCTIONS!!!!!!!!!
  11. int addNumbers(int firstNum, int secondNum = 0){
  12.  
  13.     int combinedValye = firstNum + secondNum;
  14.    
  15.     return combinedValye;
  16.  
  17. }
  18.    
  19. //OVERLOAD FUNCTIONS
  20. int addNumbers(int firstNum, int secondNum, thirdNum){
  21.  
  22.     return firstNum + secondNum + thirdNum;
  23.  
  24. }
  25.  
  26. //RECURSIVE FUNCTIONS
  27. int getFactorial(int number){
  28.    
  29.     int sum;
  30.     if (number == 1) sum = 1;
  31.     else
  32.         sum = getFactorial(number - 1) * number;
  33.     return sum;
  34.     //getFactorial(2) [Returns 2] * 3 = 6
  35.     //getFactorial(1) [Returns 1] * 2
  36. }
  37.  
  38. //POINTER FUNCION***
  39.  
  40. void makeMeYoung(int* age)
  41. {
  42.    
  43.     cout << "I used to be: " << *age << endl;
  44.     *age = 21;
  45.  
  46. }
  47.  
  48. //Todo o codigo vai ser mantido em um int main (wtf)
  49. int main()
  50. {
  51.  
  52.     //PRINTANDO:
  53.     std::cout
  54.     //or
  55.     cout << "Hello WOrld" << endl;
  56.    
  57.     //Variables:
  58.     const double PI = 3.1415926535; //const says this value cannot be changed // Double says this value can be a int or a float
  59.    
  60.     char myGrade = 'A'; //1 byte in memory
  61.    
  62.     bool isHappy = false;
  63.    
  64.     int myAge = 26;
  65.    
  66.     float favNumber =3.14;
  67.    
  68.     double otherFavNum = 3.14893275982375;
  69.    
  70.     cout <<"Favorite Number" << favNum << endl;
  71.    
  72.     //Other types include
  73.     //short int : At least 16 bits
  74.     //long  int : at least 32 bits
  75.     //long long int : at least 64 bits
  76.     //unsigned int : Same size as signed version
  77.     //long double : Not less then double
  78.    
  79.     cout << "Size of int " << sizeof(myAge)
  80.         <<endl;
  81.        
  82.         //SIZES
  83.         //Size of int 4
  84.         //char 1
  85.         //bool 1
  86.         //float 4
  87.         //double 8
  88.        
  89.        
  90.         //OPERATORS
  91.         // + , - , *, /, %
  92.    
  93.     int five = 5;
  94.    
  95.     cout << "5++ = " << five++ << endl; //5++ = 5 //Do the action after gets five
  96.     cout << "++5 = " << ++five << endl; //++5 = 7 //
  97.     cout << "5-- = " << five-- << endl; //5-- = 7
  98.     cout << "--5 = " << --five << endl; //--5 = 5
  99.    
  100.     five += 5; five = five + 5;
  101.    
  102.     //Order of operator states * and / is performed before + and - //
  103.    
  104.     cout << "4 / 5 = " << 4 / 5 << endl;            // 4 / 5 = 0
  105.     cout << "4 / 5 = " << (float)4 / 5 << endl;     // 4 / 5 = 0.8
  106.    
  107.     //others operators ==, != , > , < , >=, <=, &&, ||, !
  108.    
  109.     //I already know very well how to play with this logics//
  110.    
  111.     int greetingOption = 2;
  112.    
  113.     switch(greetingOption)
  114.     {
  115.         case 1:  
  116.             cout << "bonjour" << endl;
  117.             break;
  118.            
  119.         case 2:
  120.             cout << "Hedja" << endl;
  121.             break;
  122.            
  123.         case 3:
  124.             cout  << "Hello" << endl;
  125.             break;
  126.            
  127.         default:
  128.             cout << "Eae fdp" << endl;
  129.             break;
  130.     }
  131.    
  132.     //variable = (condition) ? true : false
  133.    
  134.     int largNum = (5>2) ? 5 : 2;
  135.    
  136.     //Array
  137.    
  138.     int myFavNumbs[5];
  139.    
  140.     int badNums[5] = {4, 13, 14, 24, 34};
  141.    
  142.     cout << "Bad number 1: " << badNums[0] << endl;
  143.    
  144.     char myName[4][4] = {{'v','i','n','i'},
  145.                         {'m','o','n','t'}};
  146.                        
  147.     cout << "2nd letter in 2nd array" << myName[1][1] <<endl;
  148.    
  149.     myName[0][2] = 'x' //Mudei a terceira letra da primeira lista, agora o N é X.
  150.    
  151.     for(int i  = 1; i <= 10; i++){
  152.         cout << i << endl;
  153.     }
  154.    
  155.     // LOOK AT WHAT THIS DO!!!!
  156.     int randNum = (rand() % 100) + 1;
  157.    
  158.     while(randNum != 100){
  159.         cout << randNum << ", ";
  160.        
  161.         randNu7m (rand() % 100) + 1;
  162.    
  163.     }
  164.     cout << endl;
  165.    
  166.     //LOOP MORE
  167.     int index = 1;
  168.    
  169.     while(index <= 10){
  170.         cout << index << endl;
  171.        
  172.         index++;
  173.        
  174.         //Printa ate 10
  175.     }
  176.    
  177.     string numberGuessed;
  178.     int intNumberGuessed = 0;
  179.    
  180.     do{
  181.    
  182.         cout << " Guess number between 1 and 10: ";
  183.        
  184.         getLine(cin, numberGuessed);
  185.        
  186.         intNumberGuessed = stod(numberGuessed); //sotd() : converts string to double
  187.        
  188.         cout <<intNumberGuessed << endl/
  189.    
  190.     }   while(intNumberGuessed != 4);
  191.    
  192.     cout <<" ~~You win~" << endl;
  193.    
  194.    
  195.     //DEEPER ON STRINGS!!!!
  196.    
  197.    
  198.     string
  199.    
  200.     char happyArray = 6] = {'h','a','p','p','y', '\0'}; //On C
  201.    
  202.     string birthdayString = "Birthday"; // On C++
  203.    
  204.     cout << happyArray + birthdayString << endl;
  205.    
  206.     //REMEMBER COUT = OUT CIN = IN
  207.    
  208.     string yourName;
  209.     cout << "What is your name";
  210.     getLine(cin, yourName);
  211.    
  212.     cout << "Hello " << yourName << endl;
  213.    
  214.     double eulersConstant = .57721;
  215.     string eulerGuess;
  216.     double eulerGuessDouble;
  217.    
  218.     cout << "What is the euler constant? " ;
  219.     getLine (cin, eulerGuess);
  220.    
  221.     eulerGuessDouble = stod(eulerGuess);
  222.    
  223.     if(eulerGuessDouble == eulersConstant){
  224.     cout<< "YOU ARE RIGHT!" << endl;
  225.     }else{
  226.     cout<< "YOU ARE WRONG" << endl;
  227.     }
  228.    
  229.     //Get the size/ uymber of characters on a string string.size() //EX: eulerGuess.size()
  230.    
  231.     //MORE ON STRINGS
  232.    
  233.     string dogString = "dog";
  234.     string catString = "cat";
  235.      
  236.     cout << dogString.compare(catString) << endl;   //prints 1
  237.     cout << dogString.compare(dogString) << endl;   //prints 0
  238.     cout << catString.compare(dogString) << endl;   //prints -1 //CAN USE WITH SWITCH :D //sEEMS COOL FOR SHADERS CONDITION AS WELL
  239.    
  240.      
  241.     //VECTORS!!!!!!!!!!//
  242.    
  243.     vector<int> lotteryNumVect(10);
  244.    
  245.     int lotteryNumArrray = {4, 13, 14, 24, 34};
  246.  
  247.     lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArrray,
  248.         lotteryNumArrray+3);
  249.    
  250.    
  251.     //TEST ADD SOMETHING
  252.     lotteryNumVect.insert(lotteryNumVect.begin()+5, 44); //Add value at the end of the vector
  253.    
  254.     lotteryNumVect.push_back(64); //Add value at the begining of the vector
  255.    
  256.     cout << lotteryNumVect.at(2) << endl;
  257.    
  258.     cout << "Final value " << lotteryNumVect.back() //Final value = 64
  259.    
  260.     lotteryNumVect.pop_back(); //Gonna remove the final value
  261.    
  262.     cout << "First value " << lotteryNumVect.front() //First value = 64 // lotteryNumVect.empty() //lotteryNumVect.size()
  263.    
  264.     //FILE I/O
  265.     //ESCREVE  NO DOCUMENTO POR VARIAVEL
  266.    
  267.    
  268.     string steveQuote = "A day without sunshine is like a night";
  269.    
  270.     ofstream writer("quote.txt"); //Save in this file
  271.    
  272.     if(!writer){
  273.         cout << "Error opening file" << endl;
  274.         return  -1;
  275.     }
  276.     else{
  277.         writer << steveQuote<< endl; //Escreve a frase na variavel steveQuote
  278.        
  279.         writer.close();
  280.        
  281.     }
  282.    
  283.     //Open a stream to append to wgats there with ios::app
  284.     //ios::binary : Treat  the file as binary
  285.     // ios::in : Open a file to read input
  286.     //ios::trunc : Default
  287.     //ios::out : Open a file to write output
  288.    
  289.     ofstream writer2("steveQuote.txt", ios::app);
  290.     if(!writer2){
  291.         cout << "Error opening file" << endl;
  292.         return  -1;
  293.     }
  294.     else{
  295.         writer2 << "\n -Steve Steve Steve Spider!!!" << endl; //Escreve a frase no documento
  296.        
  297.         writer2.close();
  298.        
  299.     }
  300.    
  301.     char letter;
  302.    
  303.     ifstream reader("stevequote.txt");
  304.    
  305.     if(!reader){
  306.    
  307.         cout << "Error opening file" << endl;
  308.         return -1;
  309.        
  310.     }else{
  311.         //use for loop in this situation to read every character till the end of tghe line
  312.         for(int i = 0; ! reader.eof(); i++){
  313.            
  314.             reader.get(letter);
  315.             cout << letter;
  316.            
  317.         }
  318.        
  319.         cout << endl;
  320.         reader.close();
  321.        
  322.     }
  323.    
  324.    
  325.     //EXCPETION HANDLING
  326.    
  327.     int numb = 0;
  328.    
  329.     try{
  330.         if(number != 0) {
  331.             cout << 2/number << endl;
  332.            
  333.         } else throw(number); //This "throw" will look in the catch!!!
  334.    
  335.     }
  336.    
  337.     catch(int number){
  338.    
  339.         cout << number << "is not valid" << endl;
  340.     }
  341.    
  342.     //POINTERS!!!!!!!!!!****
  343.    
  344.     //Remember the sizeof(variable) see how many bytes she have**
  345.    
  346.     int myAge = 39;
  347.     char myGrade = 'A';
  348.    
  349.     cout << "Size of int" << sizeof(myAge) << endl; //Size of int 4 bytes
  350.     cout << "Size of char" << sizeof(myGrade) << endl;  //Size of char 1 byte
  351.     cout << "myAge is locatae at " << &myAge << endl;   //myAge is locate at 0x7fff61de8948
  352.    
  353.    
  354.     int sum = 5;
  355.    
  356.     addThese(sum);
  357.    
  358.     int myAge = 39;
  359.    
  360.     int* agePtr = &myAge;
  361.    
  362.     cout << "Address of pointer " << agePtr << endl; //Address of pointer 0x7fff61522034
  363.    
  364.     cout << "Data at memory address " << *agePtr << endl; //Data at memory address 39
  365.    
  366.     //ARRAY IS AN ADRESS TO THE VALUES!!
  367.    
  368.    
  369.    
  370.    
  371.    
  372.    
  373.    
  374.    
  375.    
  376.    
  377.    
  378.    
  379.    
  380.    
  381.    
  382.    
  383.    
  384.    
  385.     return 0; //always end with this //void és tu?
  386.    
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement