Programming is a skill that is described in the simplest words. As a programmer, you are the listener and the advisor, both the interpreter and the one who commands the orders. Try to capture the elusive needs and find the right expression; try to make your work modular so that others build on your basis. Whether you're a developer or a member of a project team, believe in creating a small miracle every day.
Programming is the need to constantly practice, small blog will continue to publish some programming ideas and practical guidance in programming, hope to be able to help, at the same time about the programming of some thinking and algorithm research, welcome guidance and Mutual exchange. Today I mainly introduce 2 programs, focusing on C + + programming Foundation.
1. Solving 1-1/2+1/3-1/4+......+1/99-1/100
Programming thought: From the calculation can be seen, there are obvious rules, when n is odd, plus 1/n, when n is odd even when the 1/n minus. For obvious repetitive operations, loop structure implementation is required. Write the following procedure:
1#include <iostream>2 3 using namespacestd;4 5 intMain ()6 {7 intn= -;//Cycle Cap8 Doublesum=0;9 for(intI=1; i<=n;i++)Ten { One if(i%2==0)//The even number is subtracted A { -sum=sum-(1.0/i);//pay special attention to the need for parentheses, parentheses can be added, and more can not be wrong - } the Else - { -sum=sum+ (1.0/i);//odd, plus . - } + } -cout<<"1-1/2+1/3-1/4+......+1/99-1/100="<<sum<<Endl; +cout<<"sum="<<sum<<Endl; A return 0; at}
2, using the character interface to implement the menu program, by receiving different parameter values to print different operation names
Programming Idea: The menu is used for function navigation, which has obvious branching ability. Can choose the structure to implement, can choose Switch-case Statement implementation.
1#include <iostream>2 3 using namespacestd;4 5 intMain ()6 {7 intnum=0;8cout<<"Please enter the number [1-3] Menu selection:"<<Endl;9 DoTen { One //displays the menu, which is displayed again after each operation Acout<<"1. New"<<Endl; -cout<<"2. Save"<<Endl; -cout<<"3. Exit"<<Endl; the //Get user Input -Cin>>num; - Switch(num) - { + //determine user input and response - Case 1: +cout<<"you have selected the new"<<Endl; A Break; at Case 2: -cout<<"you have selected to save"<<Endl; - Break; - Case 3: -cout<<"you have chosen to exit, the program ends"<<Endl; - Break; in default: -cout<<"Invalid operation, re-enter the number [1-3]!"<<Endl; to Break; + } -} while(num!=3);//when num equals 3 o'clock, the exit is selected and the program ends the return 0; *}
Reprint please specify the source:
C + + Blog Park: godfrey_88
http://www.cnblogs.com/gaobaoru-articles/
C + + Basic Practice Guidance