Course 1 (Part 1)

Source: Internet
Author: User

In addition to ppt, we also wrote an example Program

// PlayWithC ++ _ Lesson1.cpp: defines the entry point of the console application. /***************************************: The first lesson of playing C ++ with the basic programming knowledge program: Guo Ming. creation Time: Final modification: 2012-10-6 *************************************** * ******************/# include "stdafx. h "# include <time. h> // time () # include <windows. h> // HANDLE # include <iostream> # include <vector> // vector # include <algorithm> // sort # include <deque> // deque using namespace std; // int _ tmain (int argc, _ TCHAR * argv []) // {// return 0; //}/* _ tmain () ****************************** _ tmain () only int argc // number of parameters char * argv [] // string array, each unit of the string array is of the char * type, point to a c-style string. _ The TCHAR type is a string of the wide character type, which is different from the string we commonly use. It is a 32-bit or higher type used in the operating system. refer: *************************************** **///! Function declaration ----------------------------------- intAdd (int val1, int val2); floatAdd (float val1, float val2); intAdd (int val1, int val2, int val3 ); // function overload voidchangeColor (int F = 0, int B = 0); // Finally, boolcompareInt (int val1, int val2) is introduced; // sort voidswapInt (int & val1, int & val2); // reference //! Main function ------------------------------------- int main () {for (int I = 0; I <100; I ++); // for replacement of almost all while loop structures int flag = 10; // to make the display clearer, "=" is added with spaces on the left and right. // if there are many conditions, you can save some spaces. // put the constant on the left side of the equal sign, to avoid flag = 10, you should determine whether the results are the same. if (10 = flag) {// pay attention to code alignment and layout. cout <"first lesson" <endl ;}else {;} // bitwise computation cout <"-------- bitwise computation -----" <endl; cout <"and computation, 3 & 5 =" <(3 & 5) <endl; cout <"or operation, 3 | 5 =" <(3 | 5) <endl; cout <"XOR operation, 071 ^ 052 = "<(071 ^ 052) <endl; cout <" anti-calculation ,~ 025 = "<(~ 025) <endl; cout <"Left shift operation, 5 <1 =" <(5 <1) <endl; cout <"right shift operation, 5> 2 = "<(5> 2) <endl; // function call cout <" -------- add () function call ----- "<endl; int x = 3, y = 7; int ans = Add (x, y); cout <"current ans is:" <ans <endl; int z = 10; ans = Add (x, y,-z); cout <"current ans is:" <ans <endl; // reference the form parameter cout <"-------- reference the form parameter -----" <endl; cout <"current x is:" <x <", and y is: "<y <endl; swapInt (x, y); cout <" after swap, now x is :" <X <", and y is:" <y <endl; // rand () function changeColor (11,0 ); // green cout <"-------- rand () function usage -----" <endl; srand (time (0); for (int I = 0; I <3; I ++) {int iTemp = rand () % 100; cout <"rand number" <I + 1 <"is:" <iTemp <endl ;} changeColor (1234); // green // cout <"-------- character -----" <endl; int iTemp = atoi "); cout <"string 1234 is converted to a number:" <iTemp <endl; cout <"determines whether it is a number:" <isdigit ('1') <endl; cout <"determines whether it is a number:" <isdigit ('+') <Endl; cout <"determines whether it is a number:" <isdigit ('3') <endl; cout <"determines whether it is a number: "<isdigit ('@') <endl; char strNum [2]; strNum [0] = '8'; strNum [1] = '7 '; iTemp = atoi (strNum); cout <"a '87 'string change to num:" <iTemp <endl; // container cout <"-------- container -----" <endl; vector <int> vTemp; // declare vTemp. push_back (10); // load data vTemp. push_back (-100); vTemp. push_back( 125); vTemp. push_back (45); cout <"Before sorting:" <endl; vector <int>: iterator it _ Start = vTemp. begin (); vector <int >:: iterator it_end = vTemp. end (); iTemp = 0; // start traversing for (vector <int>: iterator it = it_start; it! = It_end; it ++, iTemp ++) cout <"vTemp [" <iTemp <"] =" <* it <endl; // sort (vTemp. begin (), vTemp. end (), compareInt); cout <"after sorting:" <endl; // another Traversal method for (int I = 0; I <vTemp. size (); I ++) cout <"vTemp [" <I <"] =" <vTemp [I] <endl; vTemp. insert (vTemp. begin () + 2nd); // insert to vTemp. erase (vTemp. end ()-1); // Delete the last one ---- equivalent to vTemp. pop_back (); cout <"after insertion or deletion:" <endl; // another Traversal method for (int I = 0; I <vTemp. size (); I ++) cout <"vTemp [" <<I <"] =" <vTemp [I] <endl;/* Remember that the following table of the array starts from 0, this is different from matlab and other languages * // clear the current result vTemp. clear (); return 0 ;}//! Function Definition ----------------------------------- // integer addition, two parameters int Add (int val1, int val2) {return val1 + val2;} float Add (float val1, float val2) {return val1 + val2;} // integer addition, three parameters int Add (int val1, int val2, int val3) {// return Add (val1, val2 ), val3); // return val1 + val2 + val3;} void changeColor (int F, int B) {HANDLE hCon = GetStdHandle (STD_OUTPUT_HANDLE); SetConsoleTextAttribute (hCon, F | B);} // changeColor () Green // changeColor (); bright green // changeColor (); original color // changeColor (); bright white // changeColor) red // changeColor (); Lake Blue // changeColor (); blue bottom yellow // changeColor (); // gold bottom yellow // changeColor ); pink/* ---- changeColor introduction -----------------------------------/* windows is used. the two functions GetStdHandle and SetConsoleTextAttribute in h. HANDLE GetStdHandle (DWORD nStdHandle); GetStdHandle () returns the HANDLE of the standard input, output, or wrong device, that is, the HANDLE to obtain the input, output, or error screen buffer. the value of nStdHandle is one of the following types: STD_INPUT_HANDLE standard input handle STD_OUTPUT_HANDLE standard output handle STD_ERROR_HANDLE standard error handle function SetConsoleTextAttribute () set the text color and background color of the input or output text in the console. color text can only be displayed after this function is set. the function prototype is BOOL SetConsoleTextAttribute (HANDLE hConsoleOutput, // the HANDLE of the console screen buffer WORD wAttributes // text and background color ). To call this function, you can easily change the current foreground and background scenes of the screen. Optional */bool compareInt (int val1, int val2) {return val2> val1;} voidswapInt (int & val1, int & val2) {int temp = val1; val1 = val2; val2 = temp;}/* vector vs deque vector is suitable for insert and delete operations at the end. Deque ----- double-ended-queue in the following cases, it is best to use deque: 1) You need to insert and delete elements at both ends. 2) You do not need to reference elements in the container. 3) the container is required to release elements that are no longer in use. The operations of deque are only different from those of the vector below: 1) deque does not provide capacity operations (capacity () and reserve ()). 2) deque directly provides functions to insert and delete header elements (push_front () and pop_front ()). * // * When a large object is passed to a function, you can use the reference parameter to improve the parameter transfer efficiency. Because the reference does not generate a copy of the object, that is, when the parameter is passed, objects do not need to be copied. */

Program content is a bit messy, but it is a very important method. In order to cultivate good habits, the first course will show you how many things are common and common mistakes you have.

The requirements for this question are listed below. This question should be difficult, but not very difficult.

Job 1: Calculate 24 points
Addition, subtraction, multiplication, division, to 1 ~ Perform 24-point calculation on the number 4 of 13
For example, 4 2 3 1
User input: 4*2
Return: Current result: 8
User input: * 3
Return: Current result: 24
Used for input:/1
Return: the final result is 24, and the notification is successful. The number of successes increases by one. If the error occurs, start the game again.
Input: c (indicates clean), then clear the input and solve the problem again.
Input: r (restart), clear all the input and regenerate the question.
Input: s (show), which indicates the number of wins and the number of wins.
Input: e (end) to exit the game.
Input: g (give up). The result is displayed. The number of failures increases by one.
You may also need other input commands.
Possible method: Random Number (1 ~ 13), sorting, input and output, and determining whether the character is a number or an operator.
Note:
0. Game introduction required
1. When a question is generated, the system can determine whether the question can be solved and cannot be re-generated.
2. When you enter a non-existent number or a number that cannot be used again, an error is prompted to continue the game. (The error command should also be considered)
3. Advanced requirement 1: Color Display
4. Advanced requirement 2: add to ranking function
5. Advanced requirement 3. The user enters two numbers, and the system adds two numbers. After completing the supplement, the user starts to play.

I also gave them a reference for my previous jobs. I wrote 300 lines of code in the job content, which should be a great challenge for them. After two weeks, it should be said that it is also adequate. I hope they can learn something, and I can also sort out my knowledge.

Click here to download the relevant documentation ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.