C++ 實驗二 基本數(shù)據(jù)類型與輸入輸出.doc_第1頁
C++ 實驗二 基本數(shù)據(jù)類型與輸入輸出.doc_第2頁
C++ 實驗二 基本數(shù)據(jù)類型與輸入輸出.doc_第3頁
C++ 實驗二 基本數(shù)據(jù)類型與輸入輸出.doc_第4頁
C++ 實驗二 基本數(shù)據(jù)類型與輸入輸出.doc_第5頁
免費預覽已結束,剩余1頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

實驗二 基本數(shù)據(jù)類型與輸入輸出2.1 實驗目的1.掌握C語言基本數(shù)據(jù)類型以及常量的表示方法、變量的定義與使用規(guī)則。2.掌握C語言的算束運算、逗號運算的運算規(guī)則與表達式的書寫方法。3.掌握各種輸入輸出函數(shù)的使用方法。 2.2 實驗內(nèi)容1.上機調試(需作出必要的注釋?。?) 請說明以下程序的功能,然后上機驗證。(1.1.1)軟件操作。#include void main() printf(t*n); printf(tb*n); printf(tbb*n); (1.1.2)運行結果(1.1.3)程序分析。 該程序主要功能是以給定形式輸出幾個簡單的字符。(2) 請說明以下程序的功能,然后上機驗證。(1.2.1)軟件操作。# includevoid mian() int x=010,y=10,z=0x10;char c1=M,c2=x4d,c3=115,c4=77 ,c; printf(x=%o,y=%d,z=%xn,x,y,z); printf(x=%d,y=%d,z=%dn,x,y,z); printf(c1=%c,c2=%c,c3=%c,c4=%cn,c1,c2,c3,c4);printf(c1=%d,c2=%d,c3=%d,c4=%dn,c1,c2,c3,c4);c=c1+32;print(c=%c,c=%dn,c,); (1.2.2)程序運行結果.c:documents and settingsvm272sy3.cpp(5) : error C2018: unknown character 0xa3c:documents and settingsvm272sy3.cpp(5) : error C2065: c : undeclared identifierc:documents and settingsvm272sy3.cpp(11) : error C2065: print : undeclared identifierc:documents and settingsvm272sy3.cpp(11) : error C2059: syntax error : )(1.2.3)錯誤程序分析。 第一:void mian()中“mian”書寫錯誤,應為“main”. 第二:char c1=M,c2=x4d,c3=115,c4=77 ,c;中最后一個“c”前面的“,”為中文符號,應用英文符號“,”第三:在print(c=%c,c=%dn,c,);中“print”應為“printf” 第四:在print(c=%c,c=%dn,c,);中最后只有一個“c”,本應由兩個,所以應該改為“c,c”。正確的應為:printf(c=%c,c=%dn,c,c);(1.2.4)改正后程序。# includevoid main() int x=010,y=10,z=0x10; char c1=M,c2=x4d,c3=115,c4=77 ,c; printf(x=%o,y=%d,z=%xn,x,y,z); printf(x=%d,y=%d,z=%dn,x,y,z); printf(c1=%c,c2=%c,c3=%c,c4=%cn,c1,c2,c3,c4);printf(c1=%d,c2=%d,c3=%d,c4=%dn,c1,c2,c3,c4);c=c1+32;printf(c=%c,c=%dn,c,c);(1.2.5)改正后程序的運行結果。(1.2.6)正確程序的意義。 主要是將所給證書和字符以各類進制形式輸出(3) 請說明以下程序的功能,然后上機驗證。(1.3.1)軟件操作。#include void main() int m=18,n=13; float a=27.6,b=5.8,x,; x=m/2+n*a/b+1/4; printf(%fn,x); (1.3.2)程序運行結果。C:Documents and SettingsAdministratorsu3.cpp(6) : warning C4305: initializing : truncation from const double to floatC:Documents and SettingsAdministratorsu3.cpp(6) : warning C4305: initializing : truncation from const double to floatC:Documents and SettingsAdministratorsu3.cpp(6) : error C2059: syntax error : ;(1.3.3)錯誤程序分析。 第一:float a=27.6,b=5.8,x,;中“float”是單精度型的關鍵字,而在這里應為雙精度型“double” 第二:float a=27.6,b=5.8,x,;在“x”后的“,”應去掉。正確的應該為double a=27.6,b=5.8,x;(1.3.4)改正后的程序。#include void main() int m=18,n=13; double a=27.6,b=5.8,x; x=m/2+n*a/b+1/4; printf(%fn,x);(1.3.5)改正后程序的運行結果。(1.3.6)改正后程序的意義。該程序是輸入兩不同的整數(shù)m,n,然后將運算m/2+n*a/b+1/4結果賦值給x,最后以小數(shù)形式輸出單雙精度實數(shù)。(4)當輸入是8.5,2.5,5,分析程序運行結果,并上機驗證。(1.4.1)軟件操作。#include void main() float x,y; Int z; scanf(%f,%f,%d,&x,&x,&z); y=x-z%2*(int)(x+17)%4/2; printf(x=%f,y=%f,z=%dn,x,y,z); (1.4.2)程序運行結果。c:documents and settingsadministratorsy5.cpp(6) : error C2065: Int : undeclared identifierc:documents and settingsadministratorsy5.cpp(6) : error C2146: syntax error : missing ; before identifier zc:documents and settingsadministratorsy5.cpp(8) : error C2146: syntax error : missing ) before identifier y (1.4.3)錯誤程序分析。第一:Int z;中“Int”第一個字母應為小寫,應改正為“int”。第二:scanf(%f,%f,%d,&x,&x,&z);中在地址符前少了一個符號“”,應該為scanf(%f,%f,%d”,&x,&x,&z); (1.4.4)改正后的程序。#include void main() float x,y; int z; scanf(%f,%f,%d,&x,&x,&z); y=x-z%2*(int)(x+17)%4/2; printf(x=%f,y=%f,z=%dn,x,y,z); (1.4.5)改正后程序的運行結果(1.4.6)該程序的意義。 說明函數(shù)scanf()的格式及作用。2.填空題(1) 以下程序輸入3個整數(shù)值給a,b,c,程序把b中的值給a,把c中的值給b,把a中的值給c,交換后輸出a,b,c的值。例如,輸入a=10,b=20,c=30,交換后a=20,b=30,c=10。#include void main() int a,b,c,t; printf(Enter a,b,c: ); scanf(%d%d%d,&a,&b,&c); t=a; a=b; b=c; c=t; printf(%d,%d,%dn,a,b,c);(2) 以下程序輸入一個大寫字母,要求輸出對應的小寫字母。#include void main() char upperc,lowerc; upperc=getchar(); lowerc=upperc+32; printf(大寫字母);putchar(upperc); printf(小寫字母);putchar(lowerc);putchar(n);注:該程序中printf(小寫字母);putchar(lowerc);putchar(n);中小寫字母后的第一個后括號為中文符號,應改正為英文符號,改正后為printf(小寫字母);putchar(lowerc);putchar(n);3.思考題(2) 分析程序,寫出運行結果,并上機驗證 (3.2.1)軟件操作。#include void main() int i=3,j=5,k,l,m=19,n=-56; k=+i; l=j+; m=i+; n-=-j; printf(%d,%d,%d,%d,%d,%d,n,ij,k,l,m,n)(3.2.2)程序運行結果。C:Documents and SettingsAdministratorsy3.cpp(9) : error C2065: ij : undeclared identifierC:Documents and SettingsAdministratorsy3.cpp(10) : error C2143: syntax error : missing ; before (3.2.3)錯誤程序分析。 第一:printf(%d,%d,%d,%d,%d,%d,n,ij,k,l,m,n)中i和j中間應該還有一個符號“,”,改正后為printf(%d,%d,%d,%d,%d,%d,n,i,j,k,l,m,n). 第二:printf(%d,%d,%d,%d,%d,%d,n,ij,k,l,m,n)最后結尾處少了一個結束符“;”,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論