C++?????? - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

C++??????

Description:

C++ 1. (cout, cin) cout.width(n); setw( ); cout.precision( ); setprecision ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 34
Provided by: webMathI
Category:
Tags: c

less

Transcript and Presenter's Notes

Title: C++??????


1
C??????
  • ????

2
??
  • 1. ?????(cout, cin)
  • ?????- cout.width(n) setw( )
  • ?????-cout.precision( ) setprecision( )
  • ??????-cout.setf( )
  • ???????????
  • 2. ????
  • ??????- if if else switch
  • ??????- while do while for
  • 3. ??
  • 4. ??

3
C??????
  • 1. ?????(cout, cin)

4
?? cout
  • cout(??c-out)?C?????(Object) ?
  • cout???ltiostreamgt?????,????????????(include
    ltiostreamgt)?cout???????ltlt?????????,?????
  • cout ltlt ??
  • cout ltlt ????
  • cout ltlt ??
  • cout ltlt (???)
  • ???????
  • ltlt????(Insert)????,?????????????cout?

5
?? cin
  • cin ?siin
  • ???????
  • ?????
  • cingtgt????gtgt????gtgt
  • ???????gtgt?cin???????,????????????
  • ???gtgt??????????????????????,?
  • cin gtgt a gtgt a //??,???a
  • cin gtgt a gtgt a //??,???a

6
?????-cout.width(n) setw(n)
  • ???????????
  • cout.width(n)
  • setw(n)
  • ??n????,???n ?
  • ??setw(n)???????
  • include ltiomanipgt
  • ???????????????????????
  • ??n,??????n???????????,???????n??????????? ,?
  • cout.width(4)
  • cout ltlt 23 //??? 23 ( ???)
  • cout ltlt setw(4) ltlt12345 // ???12345

7
????????-cout.fill() setfill ()
  • ??????????????
  • cout.fill( )
  • setfill( )
  • ( )???????? ,?
  • cout.fill()??????????
  • ??setfill( )???????
  • include ltiomanipgt
  • ????cout.width()?setw()???
  • ???????

8
?????-setprecision()
  • ???????,C????????6?(???????),?setprecision(n)???
    ??????????????
  • ??? setprecision(n)
  • ???????
  • include ltiomanipgt
  • ???????

9
?????-cout.precision()
  • cout.precision?setprecision??????,????????????????
    ?????????cout????????(????ltiomanipgt)
  • ???cout.precision(n)
  • ???????
  • ??????????????????,??n???????
  • ?11.156??????5?,?????

10
??????????-cout.setf()
  • ???????
  • cout.setf (ios fixed, ios floatfield)
  • cout.precision(n) ???????n?
  • fixed?floatfield????ios????
  • ??????????ios??????()(Scope Resolution
    Operator)????
  • fixed ??????n
  • floatfield ?????
  • ???????? cout.unsetf (ios floatfield)

11
2.2.5 ??????????-cout.setf()
  • include ltiostreamgt //cout
  • include ltiomanipgt //setw
  • int main( )
  • const int f6 //??????
  • float pi3.14159
  • cout.fill()
  • cout ltlt ????2?,??
  • ltlt f ltlt " ????" ltlt endl
  • cout.setf(iosfixed, iosfloatfield)
    //????
  • cout.precision(2) //??????2?
  • cout ltlt setw(f) ltlt pi ltlt endl
  •   cout ltlt setw(f) ltlt pi/4 ltlt endl
  • coutltltsetw(f)ltlt pi1010ltltendl
  • return 0
  • ????
  • ????2?,??6 ????
  • 3.14
  • 0.78
  • 314.16

12
??????-cout.setf()
  • ??????-cout.setf(iosscientific,
    iosfloatfield)
  • ????????????-
  • cout.setf(iosscientific, iosfloatfield)
  • cout.precision(n)
  • ?????????-cout.unsetf(iosfloatfield)

13
????????
  • ???????????,????
  • ofstream ???(??)
  • ????????????,????
  • ifstream ???(??)
  • ?????data.txt
  • ofstream outfile(data.txt)
  • ??????
  • ofstream outfile (Adata.txt)
  • ?????????
  • ofstream outfile(A\\data\\data.txt)

14
???0?90??sin???,??????
  • includeltiostreamgt
  • include ltfstreamgt
  • include ltmath.hgt //sin( )
  • const double PI3.14159
  • int main( )
  • double d
  • ofstream outfile("fdata.txt")
  • outfile ltlt "deg sin(deg)\n" //???
  • for (int i0ilt90ii5)
  • d sin(PI/180i) //?sin???
  • outfile ltlt i ltlt " " ltlt d ltlt"\n" //????????
  • return 0

15
??????????sine()???????
  • double d
  • int degree
  • char buffer80
  • ifstream infile("fdata.txt")
  • infile.getline(buffer,80) //??????
  • cout ltlt buffer ltlt endl //??????
  • while (infile)
  • infile gtgt degree gtgt d //????????
  • cout ltlt setw(3)ltlt degree ltlt " " ltlt d ltlt endl

16
???????????
  • ????
  • fstream file //????
  • file.open(????,????)
  • file.close() ????
  • ??????

17
?????????
  • includeltiostreamgt
  • includeltfstreamgt //??????
  • using namesapce std
  • int main()
  • fstream file //????
  • file.open(c\\test.txt, ios_baseout)
    //????????
  • file ltlt TEST1 ltlt endl //??
  • file.close() //????
  • file.open(c\\test.txt, ios_baseapp)
    //???????????
  • file ltlt TEST2 ltlt endl //??

18
C??????
  • 2. ????
  • ??????- if if else switch
  • ??????- while do while for

19
???? if
  • if(???)
  • ??1
  • ??2
  • ????????????1 ??????2?

20
???? if else
  • if(???)
  • ??1
  • else
  • ??2
  • ??3
  • ????????????1???????2??????3?

21
???? switch
  • switch(???)
  • case ??1 ??1 break
  • case ??2 ??2 break
  • default ??3 break
  • switch???????????,???????case????????????????break
    ???????switch?????default???????,?????????????,???
    ??????default??????

22
while ??
  • while(???)
  • ??
  • while?????????????????????????????????????,???????
    ??????????????????????????,????????,?????

23
do while ??
  • do
  • ??
  • while(???)
  • do/while?while?????
  • ?????do/while????????,?????????while??????????????
    ?do/while????????????

24
for ??
  • for(???1???2???3)
  • ??
  • for?????while??,???????????????
  • ???1????????????????
  • ???2????????????????
  • ???3????????????????

25
C??????
  • 3. ??

26
??
  • ??????
  • 1.??
  • 2.????
  • 3.??
  • ??????

27
??
  • ???? ????(??)
  • ????
  • return ???
  • ?????????,?????????????????????????
  • return????????????

28
?? ???????????
  • include ltiostreamgt //cout
  • using namespace std
  • int fact(int ) //??????
  • int main( )
  • int sum, n
  • cout ltlt "n" cin gtgt n
  • sum fact(n) //????
  •   cout ltlt n ltlt"!"ltlt sum
  • return 0
  • int fact(int n) //????
  • int i, product1
  • for (i1 iltn i)
  • product product i
  • return product

29
C??????
  • 4. ??

30
????
  • ?? ???? N
  • int V3
  • ??????V???3???,???V0, V1, V2?
  • int V30,1,2
  • ??????V???3???,???V00, V11, V22?

31
????
  • ?? ???? NM
  • int A33 0,1,2,3,4,5, 6,7,8
  • ??????A???33???,?????
  • A000, A011, A022,
  • A103, A114, A125,
  • A206, A217, A228?

32
????
  • ?????????????0?
  • ???????? void ?????????
  • ?????????????????,????????????????????? 0?
  • ?????????????x ,x3 ?
  • ?????????????? x 4,x34???? x
    ,x3 ,xnm (n,m??)?

33
???
  • ??????double SIN(double x, int n), ????sin(x)
    ????????????SIN(1,n) ????,???????sin(1)???,??SIN(1
    ,n)-sin(1), n1,2,3,,5?
  • ??????(data.txt),?????100??????A,???trac(A),A?
    ?????,AA???????????data.txt?
Write a Comment
User Comments (0)
About PowerShow.com