Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C++ ESTUDOS!!!!
- #include <iostream> ; //Inclui librarys
- #include<vector>
- #include<string>
- #include <fstream> //for I/O
- using namespace std; //Dont need throw std:: in front of any shit
- ///FUNCTIONS!!!!!!!!!
- int addNumbers(int firstNum, int secondNum = 0){
- int combinedValye = firstNum + secondNum;
- return combinedValye;
- }
- //OVERLOAD FUNCTIONS
- int addNumbers(int firstNum, int secondNum, thirdNum){
- return firstNum + secondNum + thirdNum;
- }
- //RECURSIVE FUNCTIONS
- int getFactorial(int number){
- int sum;
- if (number == 1) sum = 1;
- else
- sum = getFactorial(number - 1) * number;
- return sum;
- //getFactorial(2) [Returns 2] * 3 = 6
- //getFactorial(1) [Returns 1] * 2
- }
- //POINTER FUNCION***
- void makeMeYoung(int* age)
- {
- cout << "I used to be: " << *age << endl;
- *age = 21;
- }
- //Todo o codigo vai ser mantido em um int main (wtf)
- int main()
- {
- //PRINTANDO:
- std::cout
- //or
- cout << "Hello WOrld" << endl;
- //Variables:
- const double PI = 3.1415926535; //const says this value cannot be changed // Double says this value can be a int or a float
- char myGrade = 'A'; //1 byte in memory
- bool isHappy = false;
- int myAge = 26;
- float favNumber =3.14;
- double otherFavNum = 3.14893275982375;
- cout <<"Favorite Number" << favNum << endl;
- //Other types include
- //short int : At least 16 bits
- //long int : at least 32 bits
- //long long int : at least 64 bits
- //unsigned int : Same size as signed version
- //long double : Not less then double
- cout << "Size of int " << sizeof(myAge)
- <<endl;
- //SIZES
- //Size of int 4
- //char 1
- //bool 1
- //float 4
- //double 8
- //OPERATORS
- // + , - , *, /, %
- int five = 5;
- cout << "5++ = " << five++ << endl; //5++ = 5 //Do the action after gets five
- cout << "++5 = " << ++five << endl; //++5 = 7 //
- cout << "5-- = " << five-- << endl; //5-- = 7
- cout << "--5 = " << --five << endl; //--5 = 5
- five += 5; five = five + 5;
- //Order of operator states * and / is performed before + and - //
- cout << "4 / 5 = " << 4 / 5 << endl; // 4 / 5 = 0
- cout << "4 / 5 = " << (float)4 / 5 << endl; // 4 / 5 = 0.8
- //others operators ==, != , > , < , >=, <=, &&, ||, !
- //I already know very well how to play with this logics//
- int greetingOption = 2;
- switch(greetingOption)
- {
- case 1:
- cout << "bonjour" << endl;
- break;
- case 2:
- cout << "Hedja" << endl;
- break;
- case 3:
- cout << "Hello" << endl;
- break;
- default:
- cout << "Eae fdp" << endl;
- break;
- }
- //variable = (condition) ? true : false
- int largNum = (5>2) ? 5 : 2;
- //Array
- int myFavNumbs[5];
- int badNums[5] = {4, 13, 14, 24, 34};
- cout << "Bad number 1: " << badNums[0] << endl;
- char myName[4][4] = {{'v','i','n','i'},
- {'m','o','n','t'}};
- cout << "2nd letter in 2nd array" << myName[1][1] <<endl;
- myName[0][2] = 'x' //Mudei a terceira letra da primeira lista, agora o N é X.
- for(int i = 1; i <= 10; i++){
- cout << i << endl;
- }
- // LOOK AT WHAT THIS DO!!!!
- int randNum = (rand() % 100) + 1;
- while(randNum != 100){
- cout << randNum << ", ";
- randNu7m (rand() % 100) + 1;
- }
- cout << endl;
- //LOOP MORE
- int index = 1;
- while(index <= 10){
- cout << index << endl;
- index++;
- //Printa ate 10
- }
- string numberGuessed;
- int intNumberGuessed = 0;
- do{
- cout << " Guess number between 1 and 10: ";
- getLine(cin, numberGuessed);
- intNumberGuessed = stod(numberGuessed); //sotd() : converts string to double
- cout <<intNumberGuessed << endl/
- } while(intNumberGuessed != 4);
- cout <<" ~~You win~" << endl;
- //DEEPER ON STRINGS!!!!
- string
- char happyArray = 6] = {'h','a','p','p','y', '\0'}; //On C
- string birthdayString = "Birthday"; // On C++
- cout << happyArray + birthdayString << endl;
- //REMEMBER COUT = OUT CIN = IN
- string yourName;
- cout << "What is your name";
- getLine(cin, yourName);
- cout << "Hello " << yourName << endl;
- double eulersConstant = .57721;
- string eulerGuess;
- double eulerGuessDouble;
- cout << "What is the euler constant? " ;
- getLine (cin, eulerGuess);
- eulerGuessDouble = stod(eulerGuess);
- if(eulerGuessDouble == eulersConstant){
- cout<< "YOU ARE RIGHT!" << endl;
- }else{
- cout<< "YOU ARE WRONG" << endl;
- }
- //Get the size/ uymber of characters on a string string.size() //EX: eulerGuess.size()
- //MORE ON STRINGS
- string dogString = "dog";
- string catString = "cat";
- cout << dogString.compare(catString) << endl; //prints 1
- cout << dogString.compare(dogString) << endl; //prints 0
- cout << catString.compare(dogString) << endl; //prints -1 //CAN USE WITH SWITCH :D //sEEMS COOL FOR SHADERS CONDITION AS WELL
- //VECTORS!!!!!!!!!!//
- vector<int> lotteryNumVect(10);
- int lotteryNumArrray = {4, 13, 14, 24, 34};
- lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArrray,
- lotteryNumArrray+3);
- //TEST ADD SOMETHING
- lotteryNumVect.insert(lotteryNumVect.begin()+5, 44); //Add value at the end of the vector
- lotteryNumVect.push_back(64); //Add value at the begining of the vector
- cout << lotteryNumVect.at(2) << endl;
- cout << "Final value " << lotteryNumVect.back() //Final value = 64
- lotteryNumVect.pop_back(); //Gonna remove the final value
- cout << "First value " << lotteryNumVect.front() //First value = 64 // lotteryNumVect.empty() //lotteryNumVect.size()
- //FILE I/O
- //ESCREVE NO DOCUMENTO POR VARIAVEL
- string steveQuote = "A day without sunshine is like a night";
- ofstream writer("quote.txt"); //Save in this file
- if(!writer){
- cout << "Error opening file" << endl;
- return -1;
- }
- else{
- writer << steveQuote<< endl; //Escreve a frase na variavel steveQuote
- writer.close();
- }
- //Open a stream to append to wgats there with ios::app
- //ios::binary : Treat the file as binary
- // ios::in : Open a file to read input
- //ios::trunc : Default
- //ios::out : Open a file to write output
- ofstream writer2("steveQuote.txt", ios::app);
- if(!writer2){
- cout << "Error opening file" << endl;
- return -1;
- }
- else{
- writer2 << "\n -Steve Steve Steve Spider!!!" << endl; //Escreve a frase no documento
- writer2.close();
- }
- char letter;
- ifstream reader("stevequote.txt");
- if(!reader){
- cout << "Error opening file" << endl;
- return -1;
- }else{
- //use for loop in this situation to read every character till the end of tghe line
- for(int i = 0; ! reader.eof(); i++){
- reader.get(letter);
- cout << letter;
- }
- cout << endl;
- reader.close();
- }
- //EXCPETION HANDLING
- int numb = 0;
- try{
- if(number != 0) {
- cout << 2/number << endl;
- } else throw(number); //This "throw" will look in the catch!!!
- }
- catch(int number){
- cout << number << "is not valid" << endl;
- }
- //POINTERS!!!!!!!!!!****
- //Remember the sizeof(variable) see how many bytes she have**
- int myAge = 39;
- char myGrade = 'A';
- cout << "Size of int" << sizeof(myAge) << endl; //Size of int 4 bytes
- cout << "Size of char" << sizeof(myGrade) << endl; //Size of char 1 byte
- cout << "myAge is locatae at " << &myAge << endl; //myAge is locate at 0x7fff61de8948
- int sum = 5;
- addThese(sum);
- int myAge = 39;
- int* agePtr = &myAge;
- cout << "Address of pointer " << agePtr << endl; //Address of pointer 0x7fff61522034
- cout << "Data at memory address " << *agePtr << endl; //Data at memory address 39
- //ARRAY IS AN ADRESS TO THE VALUES!!
- return 0; //always end with this //void és tu?
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement