版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、面向?qū)ο蟪绦蛟O(shè)計(jì)從C到C+C語言的特點(diǎn)面向過程的:結(jié)構(gòu)化和模塊化的語言既有高級(jí)語言的優(yōu)點(diǎn),也有低級(jí)語言的許多特點(diǎn)C+是對(duì)C的增強(qiáng)對(duì)C語言的功能做了不少擴(kuò)充。增加了面向?qū)ο蟮臋C(jī)制。對(duì)象:把數(shù)據(jù)和處理數(shù)據(jù)的過程當(dāng)成一個(gè)整體。最簡(jiǎn)單的C+程序#include using namespace std;int main( )coutThis is a C+ program.endl;return 0;1、如何輸出“hello world!”2、如何輸出* * *輸出串輸出換行另一個(gè)簡(jiǎn)單的C+程序#include using namespace std;int main( )int a, b;coutpl
2、ease input a and bab;if(b!=0)couta/b=a/bendl;elsecoutb should not be zeroendl;return 0;思考:如何輸出以下圖形(高度為N20)P16習(xí)題6#include using namespace std;int main( )int a, b, c;a = 10;b = 23;c = a + b;couta+b=;coutc;couta+b=a+b;return 0;g:tempex1.cpp(3) : error C2065: c : undeclared identifierg:tempex1.cpp(4) :
3、error C2065: cout : undeclared identifierg:tempex1.cpp(4) : error C2297: : illegal, right operand has type char 5程序的調(diào)試int main( )int a,b,c;c = a+b;couta+b=a+b;return 0;g:tempex1.cpp(4) : error C2065: cout : undeclared identifierg:tempex1.cpp(4) : error C2297: : illegal, right operand has type char 5
4、#include using namespace std;int main( )int a,b,c;c = a+b;couta+b=a+b;return 0;g:tempex1.cpp(6) : error C2676: binary : class std:basic_ostreamchar,struct std:char_traits does not define this operator or a conversion to a type acceptable to the predefined operator#include using namespace std;int mai
5、n( )int a,b,c;c = a+b;couta+b=a+b;return 0;g:tempex1.cpp(5) : warning C4700: local variable a used without having been initializedg:tempex1.cpp(5) : warning C4700: local variable b used without having been initialized#include using namespace std;int main( )int a=15,b=17,c;c = a+b;couta+b=a+b;return
6、0;包含類的C+程序#include using namespace std;class Studentprivate:int num; float score;public:void setdata( )cinnumscore;void display( ) coutnum=numendlscore=scorestud1.numstud1.score 不可替代分析以下程序的輸出#include using namespace std;class Bookpublic:int id;public:void display( )switch(id)case 1: coutGone with th
7、e windendl; break;case 2: coutThe thorn birdsendl; break;case 3: coutPride and prejudiceendl; break;default: coutUnknownendl; break;int main( )Book mybook;mybook.id=1;mybook.display( );mybook.id=3;mybook.display( );mybook.id=10;mybook.display( );return 0;常量數(shù)值常量:也稱常數(shù)整型常量短整數(shù):-32768 32767長(zhǎng)整數(shù):-214748364
8、8 2147483647unsigned:非負(fù)表示方法:十進(jìn)制(20)、八進(jìn)制(020)、十六進(jìn)制(0X20)浮點(diǎn)數(shù)十進(jìn)制小數(shù):21.456, 21.456f, 21.456L指數(shù):0.314159e1, 3.14159E0#include using namespace std;int main( )int a=030, b=30, c=0 x30, d=0 x1f;couta=a,b=b,c=c;cout,d=dendl;return 0;思考:int a=038; 對(duì)不對(duì)?常量(2)字符常量普通字符常量:用單撇號(hào)括起來的一個(gè)字符轉(zhuǎn)義字符常量:以開頭的字符序列以ASCII碼存儲(chǔ)字符串常量用
9、雙撇號(hào)括起來的部分在字符串最后會(huì)自動(dòng)加上0作為字符串的結(jié)束標(biāo)志符號(hào)常量:以標(biāo)識(shí)符形式出現(xiàn)的常量#include using namespace std;int main( )int i, j;char c1, c2;i = A;j = B;c1 = i+32;c2 = j+32;couti jn;coutc1 c2endl;cout(int) A (char) (i+32)endl;return 0;將(char) (i+32) 改成:(char ) i+32會(huì)怎么樣?變量在程序運(yùn)行期間,其值可以改變的量有一個(gè)名字在內(nèi)存中占據(jù)一定的存儲(chǔ)單元,以存放值常變量(只讀變量):定義變量時(shí),加上關(guān)鍵字c
10、onst,在程序運(yùn)行期間值不能改變的量。P40 習(xí)題4#include using namespace std;int main( )char c1=C, c2=+, c3=+;coutI say: c1c2c3;coutttHe says:C+ is very interesting! n;return 0;char前能否加const,會(huì)有什么影響?P40 習(xí)題7#include using namespace std;int main()int i,j,m,n;i=8;j=10;m=+i+j+;n=(+i)+(+j)+m;coutitjtmtnendl;return 0;1.輸出什么?2.
11、將j+改成(j+)有什么影響?C+程序和語句一個(gè)程序包含一個(gè)或多個(gè)程序單位,每個(gè)程序單位由以下部分組成預(yù)處理命令: #include, #define 聲明部分:對(duì)數(shù)據(jù)類型和函數(shù)的聲明,對(duì)變量的聲明和定義函數(shù):包括函數(shù)首部和函數(shù)體,在函數(shù)體中可以包含若干聲明和執(zhí)行語句。#include using namespace std;#define MAX 10int main( )const int K=5;int aMAX, bK, i, j ; for(i=0; iMAX; i+) ai = i;for(i=0; iK; i+) bi = i*i;for(i=0; iMAX; i+)for(j=
12、0; jK; j+)coutai+bj;if(j = K-1)coutendl; elsecoutt;return 0;輸出什么?C+的輸入與輸出在C語言中,I/O功能是通過scanf函數(shù)和printf函數(shù)來實(shí)現(xiàn)的。非類型安全:所期望的參數(shù)個(gè)數(shù)與類型取決于包含在第一個(gè)參數(shù)中的信息,而這一信息對(duì)編譯器是沒有用的。不可擴(kuò)充性:只能輸入和輸出已知的基本數(shù)據(jù)類型值,但C+中大量的類對(duì)象,其輸入輸出格式是未預(yù)先定義的。#include void main( )int j=10;float f=2.3;printf(%dn, f);scanf(%d, &f);scanf(%d,j);printf(%dn,
13、abcde);非類型安全問題C+的輸入與輸出(2)在C+中,I/O功能是通過輸入輸出流庫(kù)中的流對(duì)象cin和cout實(shí)現(xiàn)的。流:來自設(shè)備或者傳給設(shè)備的一個(gè)數(shù)據(jù)流。cin是輸入流對(duì)象的名稱,是流提取運(yùn)算符cout是輸出流對(duì)象的名稱,是流插入運(yùn)算符使用cin,cout和流運(yùn)算符時(shí),必須: #include using namespace std;輸入流與輸出流的基本操作cout語句:一般格式: cout 表達(dá)式1 表達(dá)式2 變量1 變量2 變量n;系統(tǒng)會(huì)根據(jù)變量的類型從輸入流中提取相應(yīng)長(zhǎng)度的字節(jié)。一個(gè)提取運(yùn)算符只能提取一項(xiàng)。在輸入流和輸出流中使用控制符程序中應(yīng)當(dāng)包含iomanip文件。常用控制符se
14、tprecision(n): 設(shè)置浮點(diǎn)數(shù)的精度為n位。setw(n): 設(shè)置字段寬度為n位。浮點(diǎn)數(shù)的表示:setiosflags(ios:fixed), setiosflags(ios:scientific)對(duì)齊:setiosflags(ios:left), setiosflags(ios:right)進(jìn)制: dec, hex, oct#include #include using namespace std;int main()double a=123.456789012345;coutaendl;coutsetprecision(9)aendl;coutsetprecision(5)set
15、iosflags(ios:fixed)a;coutresetiosflags(ios:fixed)endl;coutsetiosflags(ios:scientific)aendl;coutsetiosflags(ios:scientific)setprecision(4)aendl;return 0;能不能去除resetiosflags#include #include using namespace std;int main()float a=123.456; int b=123456;coutb hexbendl;coutsetiosflags(ios:uppercase)b;cout
16、setw(10)bendl;coutsetw(10)a setw(10)bendl;coutsetiosflags(ios:left)setw(10)a setw(10)bendl;coutb octb decbendl;return 0;P58 例3.5#include #include using namespace std;int main()float a,b,c,x1,x2;cinabc;x1=(-b+sqrt(b*b-4*a*c)/(2*a);x2=(-b-sqrt(b*b-4*a*c)/(2*a);coutx1=x1endl;coutx2=x2endl;return 0;本程序可
17、能存在什么問題?ab=pow(a,b)ln(a)=log(a)#include #include using namespace std;int main( ) int s=1;double n=1, t=1, pi=0;while(fabs(t)1e-7) pi += t;n += 2;s *= -1;t = s/n;pi *= 4;coutpi=piendl;return 0;P81 例3.12n可以為int型嗎?#include using namespace std;int main( ) long s=1;long sum=0;for(int i=1; i21; i+) s*=i;sum += s;coutsumendl;return 0;P86 練習(xí)18求#include using namespace std;bool check(int id) int a3;for(int i=2; i=0; i-)ai=id % 10;if(ai=0 | ai3) return false;id /= 10;if(a0=a1 | a0=a2 | a1=a2) return false;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年大學(xué)地理(平原地貌)試題及答案
- 2025年中職機(jī)電設(shè)備(機(jī)電安裝調(diào)試)試題及答案
- 2025年高職第三學(xué)年(語文教育)現(xiàn)代漢語教學(xué)階段測(cè)試題及答案
- 2025年高職電工電子技術(shù)(電路裝調(diào))試題及答案
- 2025年中職中藥資源與開發(fā)(種植技術(shù))試題及答案
- 2025年中職計(jì)算機(jī)應(yīng)用(辦公自動(dòng)化應(yīng)用)試題及答案
- 2025年中職(大數(shù)據(jù)與會(huì)計(jì))稅務(wù)申報(bào)實(shí)訓(xùn)階段測(cè)試題及答案
- 2025年中職土木建筑(建筑構(gòu)造基礎(chǔ))試題及答案
- 2025年大學(xué)大三(護(hù)理)兒科護(hù)理技術(shù)試題及答案
- 2025年中職烹飪工藝與營(yíng)養(yǎng)(面包制作基礎(chǔ))試題及答案
- 《建筑工程定額與預(yù)算》課件(共八章)
- (完整版)設(shè)備安裝工程施工方案
- 跨區(qū)銷售管理辦法
- 超聲年終工作總結(jié)2025
- 鉆井工程施工進(jìn)度計(jì)劃安排及其保證措施
- 管培生培訓(xùn)課件
- 梗阻性黃疸手術(shù)麻醉管理要點(diǎn)
- 民用機(jī)場(chǎng)場(chǎng)道工程預(yù)算定額
- 重生之我在古代當(dāng)皇帝-高二上學(xué)期自律主題班會(huì)課件
- 膀胱切開取石術(shù)護(hù)理查房
- 混凝土試塊標(biāo)準(zhǔn)養(yǎng)護(hù)及制作方案
評(píng)論
0/150
提交評(píng)論