第一篇:C#應用程序設計教程 第二版+課后習題答案
第一章
1.判斷題
(1)×(2)√(3)√(4)×(5)×
(6)√ 2.選擇題
(1)C(2)B(3)B(4)C(5)D
(6)C 3.編程題
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args){ Console.WriteLine(“Hello C#”);} } }
第二章
1.判斷下列符號哪些是合法的C#變量名稱
北京 abc _xyz x_y 2.根據要求寫出C#表達式(1)N%3==0 & N%5==0(2)(char)new Random().Next(67,74)(3)Math.Pow(x,3)*y/(x+y)(4)Math.Sqrt(b*b-4*a*c)3.判斷題
(1)×(2)√(3)√(4)√(5)√
(6)×(7)√(8)√(9)√(10)×(11)√(12)× 4.選擇題
(1)B(2)C(3)B(4)B
(5)C
(6)B(7)D(8)B(9)C(10)A 5.分析并寫出下列程序運行結果(1)a=21,b=9,c=16
y=4(2)b=41(3)s=6(4)a[0]=28
a[1]=15
a[2]=39
a[3]=48
a[4]=39 6.程序填空
(1)① 3 ② a[i] ③ a[i] ④ a[5-i] ⑤ a[5-i](2)① i % 5 == 0 | i % 7 == 0 ② s+ i 7.編程題(1)static void Main(string[] args){ int s,i,j;for(i = 2;i < 1000;i++){ s = 0;for(j = 1;j < i;j++){ if(i % j == 0)s += j;} if(i == s)Console.WriteLine(“{0}”,i);} }(2)static void Main(string[] args){ int[,] a = {{2,5,18,4 },{3,4,9,2},{4,1,16,8},{5,2,14,6}};int i,j,k,max,maxj;for(i = 0;i < 4;i++){ max = a[i,0];maxj = 0;for(j = 1;j < 4;j++){ if(max < a[i,j]){max = a[i,j];maxj = j;} } for(k = 0;k < 4;k++){ if(a[k, maxj] < max)break;} if(k == 4)Console.WriteLine(“鞍點:第{0}行,第{1}列”,i+1,maxj+1);} }(3)
static void Main(string[] args){ int a = 20, b = 16,i=20;do { if(i % a == 0 & i % b == 0)break;i++;} while(true);Console.WriteLine(“最小公倍數:{0}”,i);i = 16;do { if(a % i == 0 & b % i == 0)break;i--;} while(true);Console.WriteLine(“最大公約數:{0}”,i);}(4)
static void Main(string[] args){ int i, j,k;float s = 1.0f;for(i = 2;i < 11;i++){ k=1;for(j = 2;j <= i;j++)k += j;s += 1.0f / k;} Console.WriteLine(“s={0}”,s);}(5)
static void Main(string[] args){ String s = “a,b2>4[AG6p(”;int i, n1=0,n2=0,n3=0,n4=0;for(i=0;i
static void Main(string[] args){ int i,s = 1;for(i = 0;i < 5;i++){ s = 2 *(s + 1);} Console.WriteLine(“小猴子第一天摘了{0}個桃子”,s);}
說明:由最后一天往前推算。后一天吃了前一天桃子數量的一半多1個,剩余桃子數量是前一天桃子數量的一半減1,則,前一天的桃子數量是后一天的桃子數量加1的2倍。
第三章
1.判斷題
(1)√(2)×(3)√
(4)√(5)√(6)×(7)√(8)×(9)×
(10)√
(11)√(12)√(13)√(14)×(15)√(16)√(17)×(18)√(19)√(20)×(21)×(22)√(23)√(24)√(25)×(26)×(27)× 2.選擇題
(1)C(2)B(3)C(4)D
(5)C
(6)B(7)D(8)C(9)A(10)A(11)B(12)A(13)D(14)B
(15)C
3.分析下列程序的運行結果(1)s=32,s1=32,s2=34
x=11,y=21(2)x1=3,x2=4
y1=1,y2=4 4.編程題(1)
class Student { public string studentid;public string studentname;private string birthplace;private DateTime birthdate;public Student(string id, string name){ studentid = id;studentname = name;} public string StudentId { get { return studentid;} set { studentid = value;} } public string StudentName { get { return studentname;} set { studentname = value;} } public string BirthPlace { get { return birthplace;} set { birthplace = value;} } public DateTime BirthDate { get { return birthdate;} set { birthdate = value;} } public int Age { get { return DateTime.Now.Year1, p1.yp1.x)*(p2.xp1.y)*(p2.yv2;if(radioButton3.Checked ==true)v = v1 * v2;if(radioButton4.Checked ==true){ if(v2!= 0)v = v1 / v2;} textBox3.Text = v.ToString();} 3.利用4.6節知識,參考圖4-29設計具有菜單、工具欄和狀態欄的Windows窗口應用程序。
第五章
1.判斷題
(1)√(2)×(3)√(4)√(5)×
(6)√(7)×
2.選擇題
(1)A(2)無答案,應該是TextReader(3)A(4)C
(5)A(6)C 3.編程題
創建Windows窗口用于程序,在窗口上放置一個Button控件,名稱為button1,編寫其Click事件處理方法:
private void button1_Click(object sender, EventArgs e){ int i, j;int[,] a=new int[3,3];int[,] b = new int[3, 3];int[,] c = new int[3, 3];string s1;string[] ss;FileStream fs = new FileStream(@“d:data1.txt”,FileMode.Open);StreamReader sr=new StreamReader(fs,Encoding.Default);StreamWriter sw;BinaryWriter bw;for(i = 0;i < 3;i++){ s1 = sr.ReadLine();ss = s1.Split(' ');for(j = 0;j < 3;j++){ a[i, j] = int.Parse(ss[j]);} } for(i = 0;i < 3;i++){ s1 = sr.ReadLine();ss = s1.Split(' ');for(j = 0;j < 3;j++){ b[i, j] = int.Parse(ss[j]);} } sr.Close();fs.Close();for(i = 0;i < 3;i++)for(j = 0;j < 3;j++)c[i, j] = a[i, j] + b[i, j];SaveFileDialog sfd = new SaveFileDialog();sfd.Filter = “文本文件(*.txt)|*.txt”;if(sfd.ShowDialog()== DialogResult.OK){ fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate);sw = new StreamWriter(fs,Encoding.Default);for(i = 0;i < 3;i++){ for(j = 0;j < 3;j++){ sw.Write(c[i, j]);sw.Write(“ ”);} sw.Write(“n”);} sw.Close();fs.Close();} sfd.Filter = “二進制文件(*.bin)|*.bin”;if(sfd.ShowDialog()== DialogResult.OK){ fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate);bw = new BinaryWriter(fs);for(i = 0;i < 3;i++)for(j = 0;j < 3;j++)bw.Write(c[i, j]);bw.Close();fs.Close();} } 說明:data1.txt文件中共9行整數,每行3個整數,每個數據之間以一個空格隔開。
第六章
1.判斷題
(1)√(2)×(3)×(4)×(5)√
(6)√(7)√(8)×(9)×(10)√
2.選擇題
(1)C(2)D(3)C(4)A
(5)B(6)C(7)B(8)D 3.編程題
創建Windows窗口應用程序,在窗口上放置一個toolStrip控件,并在其中添加兩個組合框,名稱分別為toolStripComboBox1和toolStripComboBox2,給toolStripComboBox1添加選項:短劃線、點劃線、雙點劃線、虛線、實線,給toolStripComboBox2添加選項:1、2、3、4。給窗口添加如下私有字段:
private Graphics g1;private DashStyle dashstyle=DashStyle.Solid;private float penwidth=1;private Pen pen1;private bool beDraw = false;private Point p1, p2;編寫comboBox1和comboBox2的SelectedIndexChanged事件處理方法:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){ dashstyle =(DashStyle)comboBox1.SelectedIndex;}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e){ penwidth =float.Parse(comboBox2.Text);} 編寫Form3的Load、MouseDown、MouseMove和MouseUp以及工具欄上兩個組合框的SelectedIndexChanged事件處理方法:
private void Form3_Load(object sender, EventArgs e){ toolStripComboBox1.SelectedIndex = toolStripComboBox1.Items.Count-1;toolStripComboBox2.SelectedIndex = 0;} private void Form3_MouseDown(object sender, MouseEventArgs e){ pen1 = new Pen(Color.Red, penwidth);pen1.DashStyle = dashstyle;beDraw = true;p1 = e.Location;g1 = this.CreateGraphics();} private void Form3_MouseMove(object sender, MouseEventArgs e){ if(beDraw == true){ p2 = e.Location;g1.DrawLine(pen1, p1, p2);p1 = p2;} } private void Form3_MouseUp(object sender, MouseEventArgs e){ beDraw = false;} private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e){ dashstyle =(DashStyle)toolStripComboBox1.SelectedIndex;} private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e){ penwidth = float.Parse(toolStripComboBox2.Text);}
第七章
1.判斷題
(1)√(2)×(3)×(4)×(5)√
(6)×(7)√(8)×(9)√(10)×(11)√(12)×(13)√(14)√(15)√(16)√
2.選擇題
(1)A(2)D(3)C(4)A
(5)D(6)B(7)C(8)D(9)B(10)C(11)C 12)C 13)A 14)B 15)D 16)D 3.編程題
(1)創建Windows窗口應用程序,在窗口上放置兩個Button控件,名稱分別為button1和button2,再放置一個ComboBox控件和DataGridView控件,名稱分別為comboBox1和dataGridView1。
編寫button1的Click事件處理方法:
private void button1_Click(object sender, EventArgs e){ OleDbConnection con = new OleDbConnection();OleDbCommand com = new OleDbCommand();con.ConnectionString =@“Provider=microsoft.jet.oledb.4.0;data source=d:aaa.mdb”;con.Open();com.CommandType = CommandType.Text;com.CommandText = “select * from Score where score>80”;com.Connection = con;OleDbDataReader dr = com.ExecuteReader();comboBox1.Items.Clear();while(dr.Read()== true){ if(dr.IsDBNull(dr.GetOrdinal(“StudentID”))== false)
comboBox1.Items.Add(dr[“StudentID”]);} } 說明:為了使用ADO.NET的OLEDB訪問方式,需要添加下列引用:
using System.Data.OleDb;(2)界面與(1)相同。
編寫button2的Click事件處理方法:
private void button2_Click(object sender, EventArgs e){ OleDbConnection con = new OleDbConnection();OleDbCommand com = new OleDbCommand();con.ConnectionString = @“Provider=microsoft.jet.oledb.4.0;data source=d:aaa.mdb”;con.Open();OleDbDataAdapter da = new OleDbDataAdapter(“select Class as 班級,avg(Score)as平均成績 from Score group by Class”, con);DataSet ds = new DataSet();da.Fill(ds, “平均成績表”);dataGridView1.DataSource =ds.Tables[“平均成績表”];}(3)參考7.7節的學生信息管理程序,編寫通訊錄管理程序,細節略。
第二篇:C語言程序設計教程課后習題答案
C語言程序設計教程課后習題答案
第一章 C語言程序設計概述 -習題答案 算法的描述有哪些基本方法?
答
1、自然語言
2、專用工具C語言程序的基本結構是怎樣的?舉一個例子說明。
答
1、C語言程序由函數構成;
2、“/*”與“*/”之間的內容構成C語言程序的注釋部分;
3、用預處理命令#include、#define可以包含有關文件或預定義信息;
4、大小寫字母在C語言中是有區別的;
5、除main()函數和標準庫函數外,用戶也可以自己編寫函數,應用程序一般由多個函數組成,這些函數指定實際所需要做的工作。C語言有什么特點?
答
1、具有結構語言的特點,程序之間很容易實現段的共享;
2、主要結構成分為函數,函數可以在程序中被定義完成獨立的任務,獨立地編譯代碼,以實現程序的模塊化;
3、運算符豐富,包含的范圍很廣;
4、數據類型豐富;
5、允許直接訪問物理地址,即可直接對硬件進行損傷,實現匯編語言的大部分功能;
6、限制不太嚴格,程序設計自由度大,這樣使C語言能夠減少對程序員的束縛;
7、生成的目標代碼質量,程序執行效率高,同時C語言編寫的程序的可移植性好。★指出合法與不合法的標識符命名。
答
AB12--√ leed_3--a*b2--× 8stu--× D.K.Jon--× EF3_3--√ PAS--√ if--× XYZ43K2--√ AVE#XY--× _762--√ #_DT5--× C.D--×說明下列Turbo C熱鍵的功能。
答
F2:源文件存盤 F10:調用主菜單 F4:程序運行到光標所在行(用于調試程序)Ctrl+F9:編譯并鏈接成可執行文件 Alt+F5:將窗口切換到 DOS 下,查看程序運行結果。說明下列Turbo C方式下輸入并運行下列程序,記錄下運行結果。
①main()
{printf(“********************n”);printf(“ welcome you n”);printf(“ very good n);printf(”********************n“);} ②main()
{ int a,b,c,t;printf(”please input three numbers;“);scanf(”%d,%d,%d“,&a,&b,&c);/*教材S是錯誤的*/ t=max(max(a,b),c);printf(”max number is:%dn“,t);} int max(int x, int y){ int z;if(x>y)z=x;else z=y;return(z);} 答
運行結果:
******************** welcome you very good ******************** 運行結果:
please input three numbers;3,1,4 /*左側下劃線內容為鍵盤輸入*/ max number is:4 7 一個C程序是由若干個函數構成的,其中有且只能有一個___函數。
答
main()8 在Turbo C環境下進行程序調試時,可以使用Run下拉菜單的___命令或按___鍵轉到用戶屏幕查看程序運行結果。
答
1、User screen
2、Alt+F5 9 ★C語言對標識符與關鍵字有些什么規定?
答
1、標識符用來表示函數、類型及變量的名稱,它是由字母、下劃線和數字組成,但必須用字母或下劃線開頭。
2、關鍵字是一種語言中規定具有特定含義的標識符,其不能作為變量或函數名來使用,用戶只能根據系統的規定使用它們。C源程序輸入后是如何進行保存的?
答
是以C為擴展名保存的純文本文件。
第二章 C語言程序的基本數據類型與表達式 -習題答案 ★指出下列常數中哪些是符合C語法規定的。
答
''--× '101'--× ”“--× e3--× 019--√ 0x1e--√ ”abn“--√ 1.e5--×(2+3)e(4-2)--× 5.2e2.5--×請找出下列程序中的錯誤,改正后寫出程序運行結果。
①void main(){int x,y=z=5,aver;x=7 AVER=(x+y+z)/3 printf(”AVER=%dn“,aver);} ②void main()
{ char c1='a';c2='b';c3='c';int a=3.5,b='A' printf(”a=%db='“endn”,a,b);printf(“a%cb%cbc%ctabcn”,c1,c2,c3);} 答
main(){int x,y=5,z=5,aver;x=7;aver=(x+y+z)/3;printf(“AVER=%dn”,aver);}
運行結果:AVER=5 ②main()
{ char c1='a', c2='b', c3='c';int a=3,b='A';printf(“a=%d,b='%c'”end“n”,a,b);printf(“a%cb%cbc%ctabcn”,c1,c2,c3);}
運行結果:a=3,b='A'“end”
aabcc abc 3 寫出下列賦值的結果,表格中寫了數值的是要將它賦給其他類型的變量,將所有的空格填上賦值后的數據(實數保留到小數點后兩位)。int 99
-1 char 'h'
unsigned int
float
55.78
long int
答 int 99 104 66 55 68-1 char 'c' 'h' 'B' '7' 'D'
unsigned int 99 104 66 55 68 65535
float 99.00 104.00 66.00 55.78 68.00-1.00
long int 99 104 66 55 68-1
★寫出程序運行結果。
①void main(){int i,j;i=8,j=10;printf(“%d,%d,%d,%dn”,i,j,++i,j++);} ②main()
{ int a=1,b=2,c=30;;printf(“%d,%d,%d,%dn”,a=b=c,a=b==c,a==(b=c),a==(b==c));} 注意:a=b=c,a=b==c之間應為逗號,教材有誤 答
運行結果: 9,11,9,10 運行結果: 30,1,0,0
③void main()
{int a=10,b=20,c=30,d;d=++a<=10||b-->=20||c++;printf(“%d,%d,%d,%dn”,a,b,c,d);}
答
運行結果: 11,19,30,1
★寫出下面表達式的值(設a=10,b=4,c=5,d=1,x=2.5,y=3.5)。⑴a%=(b%=3)
⑵n++,a+=a-=a*=a ⑶(float)(a+c)/2+(int)x%(int)y ⑷a*=b+c ⑸++a-c+b++ ⑹++a-c+++b ⑺a
⑼a+b,18+(b=4)*3,(a/b,a%b)
⑽x+a%3*(int)(x+y)%2/4+sizeof(int)⑾a
答
⑴0 ⑵0 ⑶9.500000 ⑷90 ⑸10 ⑹10 ⑺'A' ⑻2 ⑼4.5 ⑽1 ⑾0 ⑿20 ⒀0 下列每組表達式中,被執行后結果完全等價的是哪些(設a、b、m是已被賦值的整型變量)?
①m=(a=4,4*5)與m=a=4,4*5 ②(float)(a/b)與(float)a/b ③(int)a+b與(int)(a+b)④m%=2+a*3與m=m%2+a*3 ⑤m=1+(a=2)+(b=3)與a=2,b=3,m=1+a+b 答
①前面是賦值表達式,而后面的是一個逗號表達式,所以一定不同;
②前面的表達式中a/b結果為一整數,結果已經取整,精度可能受到影響,之后強制float后才為浮點型,后面的是先將a轉換為float后再與b相除,其值保證了精度,所以不同。
③因為a、b均為整數,其前后兩個表達式的計算結果是一致的。
④前一表達式是一算術表達式,而后者為一賦值表達式,此為一點不同;另外,前一表達式的m只被賦過一次值,后一表達式中的m曾兩次被賦值,第一次賦值時與第一表達式中的值一致,第二次賦值后即不再相同。⑤前后表達式的計算結果應該是一致的:a=2, b=3, m=6 7 條件表達式x>0?x:-x的功能是什么?
答
如果x的值是一正數,則表達式的值為x值;如果x的值是一非正數,則表達式的值為-x。其實該表達式的值即為x的絕對值,C語言中提供了一個函數fabs(x)即可完成此功能,該函數包含在math.h頭文件中。用一個條件表達式描述從a、b、c中找出最大都賦給max.答
max=a>(b>c?b:c)?a:(b>c?b:c);9 ★若x為int型變量,則執行以下語句后x的值為()。x=6;x+=x-=x*x;A.36 B.-60 C.60 D.-24 答 B.10 ★若有以下類型說明語句: char w;int x;float y;double z;則表達式w*x+z-y的結果為()類型。A.float B.char C.int D.double 答 D.第三章 順序結構程序設計 -習題答案 變量k為float類型,調用函數scanf(“%d”,&k),不能使變量k得到正確數值的原因是___。
答
格式修飾符與變量類型不一致。因為%d輸入的數據類型應該為十進制整數,而&k為占用4個字節的float類型變量的地址。★a=1234,b=12,c=34,則執行“printf(“|%3d%3d%-3d|n”,a,b,c);”后的輸出是___。
答
|1234 1234 |
分析如下:
①%3d為右對齊輸出變量,且指定輸出變量的值寬度為3個字符位,如果變量實際位數小于3,則左端補空格,如果變量實際位數大于3,則按實際長度輸出,不受限制。
②%-3d為左對齊輸出變量,在輸出變量時,如是變量實際位數小于3,則在右端補空格,否則按實際輸出。★設有“int a=255,b=8;”,則“printf(“%x,%on”,a,b);”輸出是___。答 ff,10 ①如果“printf(“%X,%on”,a,b);”則輸出為FF,10。說明在輸出十六進制字母時,其大小寫受格式修飾符的限制,如果是“%x”則輸出小寫,如果是“%X”則輸出大寫。
②如果希望在輸出十六進制時輸出前導符0x或0X,則以上輸出語句應改“printf(“%#x,%on”,a,b);”為或“printf(“%#X,%on”,a,b);”。本條解釋不必須掌握。★以下程序輸出的結果是___。main(){ int a1=1,a2=0,a3=2;printf(“%d,%d,%dn”,a1,a1+a2+a3,a3-a1);} 答 1,3,1 5 printf函數中用到格式符%5s,其中5表示輸出字符占用5列。如果字符串長度大于5,則按___輸出;如果字符串長度小于5,則按___輸出。
答
①實際 ②左端補空格 6 ★已定義變量如下: int a1,a2;char c1,c2;若要求輸入a1、a2、c1和c2的值,正確的輸入函數調用語句是___。
答
scanf(“%d,%d,%c,%c”,&a1,&a2,&c1,&c2);7 輸入兩個整型變量a、b的值,輸出下列算式以及運算結果___。a+b、a-b、a*b、a/b、(float)a/b、a%b 每個算式占一行。如a=10,b=5,a+b輸出為:10+5=15 答
設int a=10,b=5;以下為輸出語句及結果: ①printf(“%d+%d=%dn”,a,b,a+b);10+5=15 ②printf(“%d-%d=%dn”,a,b,a-b);10-5=5 ③printf(“%d*%d=%dn”,a,b,a*b);10*5=50 ④printf(“%d/%d=%dn”,a,b,a/b);10/5=2 ⑤printf(“%(float)d/%d=%fn”,a,b,(float)a/b);(float)10/5=2.000000 ⑥printf(“%d%%%d=%dn”,a,b,a%b);10%5=0 8 ★輸入一個非負數,計算以這個數為半徑的圓周長和面積。答
#define PI 3.1415926 main(){ float r,l,area;printf(“Input a positive:”);scanf(“%f”,&r);l=2*PI*r;area=PI*r*r;printf(“l=%ftarea=%fn”,l,area);} 9 輸入任意一個3位數,將其各位數字反序輸出(例如輸入123,輸出321)。
答
main(){ int x,y;printf(“Input a number(100-999):”);scanf(“%d”,&x);y=100*(x%10)+10*(x/10%10)+x/100;/*注意分析此處算法*/
第三篇:完整的delphi程序設計教程課后習題答案
1.239頁1題小時鐘 procedure TForm1.Timer1Timer(Sender: TObject);var xt,yt:integer;h,m,s,ms:word;begin decodetime(time,h,m,s,ms);xt:=paintbox1.clientwidth div 2;yt:=paintbox1.clientheight div 2;paintbox1.refresh;paintbox1.canvas.moveto(xt,yt);paintbox1.canvas.pen.width:=3;paintbox1.canvas.lineto(xt+round(sin((60*h+m)/(12*60)*2*pi)*100), yt-round(cos((60*h+m)/(12*60)*2*pi)*100));paintbox1.canvas.moveto(xt,yt);paintbox1.canvas.pen.width:=2;paintbox1.canvas.lineto(xt+round(sin(m/60*2*pi)*140), yt-round(cos(m/60*2*pi)*140));paintbox1.canvas.moveto(xt,yt);paintbox1.canvas.pen.width:=1;paintbox1.canvas.lineto(xt+round(sin(s/60*2*pi)*180), yt-round(cos(s/60*2*pi)*180));end;end.2.239頁3題求兩點間的距離 procedure TForm1.Button1Click(Sender: TObject);begin paintbox1.Refresh;formpaint(sender);end;procedure TForm1.FormPaint(Sender: TObject);var xt,yt:integer;ax,ay,bx,by:integer;begin xt:=paintbox1.ClientWidth div 2;yt:=paintbox1.Clientheight div 2;paintbox1.canvas.MoveTo(xt,0);paintbox1.canvas.lineTo(xt,2*yt);paintbox1.canvas.MoveTo(0,yt);paintbox1.canvas.lineTo(2*xt,yt);ax:=strtoint(edit1.text);ay:=strtoint(edit2.text);bx:=strtoint(edit3.text);by:=strtoint(edit4.text);
edit5.Text:=floattostr(sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)));
paintbox1.canvas.MoveTo(xt+ax*10,yt-ay*10);
paintbox1.canvas.lineTo(xt+bx*10,yt-by*10);end;end.3.226頁5題輸入學生成績 var
Form1: TForm1;type
studentrecord=record xh,xm:string[6];xb:boolean;yy,sx,dz:integer;end;
implementation {$R *.dfm} procedure
TForm1.Button1Click(Sender: TObject);
var t:studentrecord;
f,f1:file of studentrecord;p1,p2:boolean;begin
assignfile(f,'c:my documentsstu.dat');reset(f);
assignfile(f1,'c:my documentsstu1.dat');rewrite(f1);
while not eof(f)do begin read(f,t);
p1:=(t.sx>=85)and(t.yy>=85)and(t.dz>=85);
p2:=t.sx+t.yy+t.dz>=270;if p1 or p2 then write(f1,t);end;
closefile(f);closefile(f1);end;
procedure
TForm1.FormCreate(Sender: TObject);
var t:studentrecord;f:file of studentrecord;xingbie:string;begin
assignfile(f,'c:my documentsstu.dat');reset(f);
while not eof(f)do begin read(f,t);if t.xb then xingbie:='男' else
xingbie:='女';
listbox1.Items.Add(t.xh+' '+t.xm+' '+xingbie+' '+inttostr(t.sx)+
' '+inttostr(t.yy)+' '+inttostr(t.dz));end;
closefile(f);end;
procedure
TForm1.Button2Click(Sender: TObject);begin close;end;end.4.226頁8題保存獲得獎學金者 var
Form1: TForm1;type
studentrecord=record xh,xm:string[6];xb:boolean;yy,sx,dz:integer;end;
implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);var t:studentrecord;f,f1:file of studentrecord;p1,p2:boolean;begin assignfile(f,'c:my documentsstu.dat');reset(f);assignfile(f1,'c:my documentsstu1.dat');rewrite(f1);while not eof(f)do begin read(f,t);p1:=(t.sx>=85)and(t.yy>=85)and(t.dz>=85);p2:=t.sx+t.yy+t.dz>=270;if p1 or p2 then write(f1,t);end;closefile(f);closefile(f1);end;procedure TForm1.FormCreate(Sender: TObject);var t:studentrecord;f:file of studentrecord;xingbie:string;begin assignfile(f,'c:my documentsstu1.dat');reset(f);while not eof(f)do begin read(f,t);if t.xb then xingbie:='男' else xingbie:='女';listbox1.Items.Add(t.xh+' '+t.xm+' '+xingbie+' '+inttostr(t.sx)+ ' '+inttostr(t.yy)+' '+inttostr(t.dz));end;closefile(f);end;procedure TForm1.Button2Click(Sender: TObject);begin close;end;end.5.166頁22題素數 procedure
TForm1.Button1Click(Sender: TObject);type
sushuset=set of byte;var
sushu:sushuset;i,j,n,m,k:longword;s,s0:string;begin
n:=strtoint(edit1.text);m:=strtoint(edit2.text);if n mod 2=0 then n:=n+1;k:=m-n;
if k>255 then
showmessage('范圍太大,請重新輸入區間端點!')else begin
sushu:=[0..k];for i:=2 to n-1 do for j:=n to m do
if j mod i =0 then sushu:=sushu-[j-n];for i:=n to m div 2 do for j:= i+1 to m do
if j mod i =0 then sushu:=sushu-[j-n];s:='';s0:='';
for i:=n to m do if i-n in sushu then begin
if length(s0+' '+inttostr(i))>60 then begin
s:=s+s0+chr(13);s0:='' end;
s0:=s0+' '+inttostr(i);end;s:=s+s0;label1.caption:=s;
groupbox1.Top:=(panel1.Top-groupbox1.height)div 2;end;end;
procedure
TForm1.FormCreate(Sender: TObject);begin
groupbox1.Top:=(panel1.Top-groupbox1.height)div 2;end;
procedure
TForm1.Edit1KeyPress(Sender: TObject;var Key: Char);var j:set of char;begin
j:=['0'..'9',#8];
if not(key in j)then key:=#0;end;end.6.148頁12題整除的數 implementation {$R *.dfm}
function d7(x:integer):boolean;begin
if x mod 7=0 then d7:=true else d7:=false;end;
function d17(x:integer):boolean;begin
if x mod 17=0 then d17:=true else d17:=false;end;
procedure
TForm1.FormActivate(Sender: TObject);var
n:integer;begin
for n:=1000 to 4000 do if d7(n)and d17(n)then memo1.Lines.Add(inttostr(n));end;end.7.148頁17題求斐波那契數列問題 implementation {$R *.dfm} function fib(n:integer):int64;begin case n of 1,2:fib:=1;else fib:=fib(n-1)+fib(n-2);end;end;procedure TForm1.Button1Click(Sender: TObject);var k:integer;begin memo1.lines.clear;for k:=1 to strtoint(edit1.text)do memo1.lines.add(format('%2d %3d',[k,fib(k)]));end;end.8.120頁10題,判斷是否是閏年 procedure TForm1.Button1Click(Sender: TObject);var y,m:integer;begin y:=strtoint(edit1.text);m:=strtoint(edit2.text);label3.Caption:='';if((y mod 4 = 0)and(y mod 100 <> 0))or(y mod 400=0)then label3.Caption:=edit1.text+'年是閏年,' else label3.Caption:=edit1.text+'年不是閏年,';case m of 3,4,5:Label3.Caption:=Label3.Caption+edit2.Text+'月份是春天,';6,7,8:Label3.Caption:=Label3.Caption+edit2.Text+'月份是夏天,';9,10,11:Label3.Caption:=Label3.Caption+edit2.Text+'月份是秋天,';else Label3.Caption:=Label3.Caption+edit2.Text+'月份是冬天,';end;
case m of
1,3,5,7,8,10,12:Label3.Caption:=Label3.Caption+'共有31天,';
4,6,9,11:Label3.Caption:=Label3.Caption+'共有30天,';else
if((y mod 4 = 0)and(y mod 100 <> 0)or(y mod 400=0))then Label3.Caption:=Label3.Caption+'共有29天,' else
Label3.Caption:=Label3.Caption+'共有28天,';end;end;end.9.120頁11題求和的 procedure
TForm1.Button1Click(Sender: TObject);var
n,k,j:integer;s:int64;begin
n:=strtoint(edit1.text);j:=0;s:=0;
for k:=1 to n do begin j:=j+k;s:=s+j;end;
panel2.Caption:=format('s = %d',[s]);edit1.setfocus;end;
procedure
TForm1.Edit1KeyPress(Sender: TObject;var Key: Char);var b:boolean;begin
b:=(key<>#8)and(key<#48)or(key>#57);
if b then key:=#0;end;end.10.120頁12題男人,女人,小孩各有多少人 procedure
TForm1.Button1Click(Sender: TObject);var
x,y,z:integer;p:string;begin
memo1.clear;for x:=0 to 16 do for y:=0 to 24 do begin
z:=30-x-y;
if 3*x+2*y+z/1=50 then begin
p:=format('%3d %3d %3d',[x,y,z]);
memo1.lines.add(p);end;end;end;end.11.98頁2題求長方體的表面積 procedure
TForm1.Button1Click(Sender: TObject);var
s,l,k,h:double;begin
l:=strtofloat(edit1.text);k:=strtofloat(edit2.text);h:=strtofloat(edit3.text);s:=2*l*k+2*k*h+2*l*h;label1.Caption:=floattostr(s);end;end.12.98頁5題計算圓的面積和周長 procedure
TForm1.Edit1KeyPress(Sender: TObject;var Key: Char);var r:real;b:boolean;begin b:=(key<>#8)and(key<>'.')and(key<>#13)and(key<#48)or(key>#57);if b then key:=#0;if key =#13 then begin if edit1.text='' then edit1.text:='0';r:=strtofloat(edit1.text);if radiobutton1.Checked then label1.Caption:=format('圓的面積為: %f',[pi*r*r]);if radiobutton2.Checked then label1.Caption:=format('圓的周長為: %f',[2*pi*r]);if radiobutton3.Checked then label1.Caption:=format('圓的面積為: %f'+chr(13)+ '周長為: %f',[pi*r*r,2*pi*r]);edit1.SelectAll;end;end;procedure TForm1.RadioButton1Click(Sender: TObject);var sender0:tobject;key:char;begin sender0:=edit1;key:=#13;form1.edit1keypress(sender0,key);end;procedure TForm1.RadioButton2Click(Sender: TObject);var sender0:tobject;key:char;begin sender0:=edit1;key:=#13;form1.edit1keypress(sender0,key);end;procedure TForm1.RadioButton3Click(Sender: TObject);var sender0:tobject;key:char;begin
sender0:=edit1;key:=#13;
form1.edit1keypress(sender0,key);end;end.13.98頁6題統計奇數和偶數的個數
procedure
TForm1.Button1Click(Sender: TObject);var
k,j,n:word;begin k:=0;
n:=combobox1.items.count;for j:=1to n do
if strtoint(combobox1.items[j-1])mod 2 = 0 then k:=k+1;
label1.caption:=format('共有: '+#13+'%d個偶數,'+#13+'%d個奇數。',[k,n-k]);end;
procedure
TForm1.Button2Click(Sender: TObject);begin
combobox1.items.clear;end;
procedure
TForm1.ComboBox1KeyPress(Sender: TObject;var Key: Char);begin if(key =#13)and(combobox1.text<>'')then
//按
combobox1.items.insert(0,combobox1.text);
//按
combobox1.selstart:=0;combobox1.text:='';end;
if key = #27 then
if combobox1.itemindex<>-1 then
combobox1.items.Delete(combobox1.itemindex);end;end.14.98頁8題設置倒計時的時間,并進行倒計時 procedure
TForm1.Timer1Timer(Sender: TObject);var
m,n,s,h:integer;begin
timer1.Tag:=timer1.Tag-1;m:=timer1.Tag;if m<0 then begin
timer1.enabled:=false;
showmessage('預定的時間到了!');edit1.Text:='0';end else begin s:=m;
n:=s div 60;s:=s mod 60;h:=n div 60;n:=n mod 60;
maskedit1.text:=format('%d:%d:%d',[h,n,s]);
if n <> strtoint(edit1.text)then edit1.Text:=inttostr(n);end;end;
procedure
TForm1.Button1Click(Sender: TObject);begin
timer1.enabled:=True;
timer1.Tag:=strtoint(edit1.Text)*60;end;end.
第四篇:C#程序設計教程期末復習題及答案
習題 1
一、選擇題
1.在C#中 B 都是對象。
A 任何類型 B 任何事物 C 任何代碼 D 任何技術 2.對象包含數據和 A 的方法。
A 在該數據上工作 B 調用 C 函數調用 D 傳遞參數 3.一個類是 D 的藍本。
A 數據集合 B 函數集合 C 方法集合 D 給定功能集合 4..NET構架包含公用語言運行時期和 B。5..NET的核心是 A。
A CLR B Windows2000 C DNA D 分解平臺 6.C#程序以 B 擴展名保存編寫的程序。A.CS B.PS C.CSS D.SC 7.System是 B 的命名空間。
A 存儲系統類 B 控制臺類 C I/O操作 D 新項目 8.namespace用于聲明 B。
A 新項目 B 一個命名空間 C 類與方法 D 指令 9.每個C#程序必須有一個 D 方法。A 類方法 B 構造方法 C Main D 重載方法
二、問答題
1.面向對象編程的三大原則是什么? 答:封裝、繼承和多態性。2.封裝是什么?
答:封裝是用于隱藏對象實際的制作細節。3.繼承是什么?
答:繼承是在建立新的特定對象時,可以使用現有對象的功能性。4.多態性是什么?
答:多態性是程序代碼能夠依據實際對象所需而進行不同的行為。5..NET的核心構件包括哪些? 答:(1).NET構造塊服務或有計劃的訪問某些服務。
(2)將在新的Internet設備上運行的.NET設備軟件。(3).NET用戶經驗。6.CLR的作用是什么?
答:CLR是.NET的核心,它是一個運行時期環境,在該環境中,以不同語言編寫的應用程序均能始終運行。
三、編程題
使用.NET代碼編輯器編寫一個C#應用程序,以在屏幕打印出:
C# is the Component-oriented language in C and C++ family of language.要求:
(1)使用using System命名空間,即定位System命名空間的Console類。(2)不使用using System命名空間,即System命名空間的Console類。(3)使用using指令的別名,即使用using創建using的別名。答案:(1)
//Example1.cs Using System;Class Example1 { Public static void Main(){ Console.Write(“C# is the Component-oriented language ”);Console.WriteLine(“in C and C++ family of language.”);} }(2)
//Example2.cs Class Example1 { Public static void Main(){ System.Console.Write(“C# is component-oriented language”);System.Console.WriteLine(“in C and C++ family language.”);
} }(3)Example3.cs Using output=System.Console;Class Example1
Public static void Main(){ Output.Write(“C# is component-oriented language”);}
習題2
一、選擇題
1.C#的數據類型有 A 和 C 兩種。
A 值類型 B 調用類型 C 引用類型 D 關系類型 2.C#的值類型包括 A、B 和 D 三種。A 枚舉 B 基本類型 C 整形 D 結構 E浮點型 F 字符型
3.C#的引用類型包括 A、B、C、F、G 和 H 六種。
A string B object C 類 D float E char F 數組G 代表 H 4.裝箱是把值類型轉換到 B 類型。
A 數組 B 引用 C char D string 5.拆箱是引用類型返回到 C 類型。
A string B char C 值 D 數組 6. A 類型是所有類型的根。
接口 A System.Object B object C string D System.Int32 7.從派生類到基類對象的轉換是 B 類型轉換。A 顯示 B 隱式 C 自動 D 專向 8.從基類到派生類對象的轉換是 D 類型轉換。A 隱式 B 自動 C專向 D 顯示 9.強制轉換對象可以使用 B 關鍵字實現。A is B as C this D object 10.命名空間用于定義 A 的作用域。
A 應用程序 B 有關類型 C 多重源代碼 D 層次結構 11.using關鍵字用于 B 命名空間中的Console對象。A Console B System C Object D Int32
二、填空題
1.下列程序的運行結果是 99.44。//Exam1.cs using System;class Using { public static void Main(){ int i=918;float f=10.25f;short sh=10;double d=11.19;Console.WriteLine(i+f+sh+d);} } 2.下列程序的運行結果是 25.5。//Exam2.cs using System;class Using { public static void Main(){ int i=5;float f=5.1f;Console.WriteLine(i*f);} }
二、編程題
1. 已知a=1,b=2,c=3,x=2,計算y=ax+bx+c之值。2. 已知圓的半徑Radius=2.5,計算圓的面積。(PI=3.14159)要求:
(1)使用基本方法;(2)使用裝箱與拆箱;
(3)輸出以double,float,int,decimal,short表示;(4)使用object類與類型轉換;(5)使用派生類與as。答案: 1. 方案一: //YValue.cs using System;class Using { public static void Main(){ int a=1,b=2,c=3,x=2,y;y=(a*x+b)*x+c;Console.WriteLine(“y={0}”,y);} } 方案二:
//YValue1.cs using System;class Using { public static void Main(){ int a=1,b=2,c=3,x=2;Console.WriteLine(“y={0}”,(a*x+b)*x+c);} } 2.
(1)使用基本方法 方案一:
//CircleAreaApp.cs using System;class CircleAreaApp { public static void Main(){ double Radius=2.5,Area;Area=3.14159*Radius*Radius;Console.WriteLine(“Area={0}”,Area);} } 方案二:
//CircleAreaApp1.cs using System;class CircleAreaApp { public static void Main(){ double Radius=2.5;Console.WriteLine(“Area={0}”,3.14159*Radius*Radius);} }(2)使用裝箱與拆箱 //CircleAreaApp2.cs using System;class CircleAreaApp { public static void Main(){ double Radius=2.5;double Area=3.14159*Radius*Radius;Console.WriteLine(“Area={0}”,Area);object obj=Area;Console.WriteLine(“Area={0}”,(double)obj);} }(3)輸出以double,float,int,decimal,short表示 //CircleAreaApp3.cs using System;class CircleAreaApp { public static void Main(){ double Radius=2.5;double Area=3.14159*Radius*Radius;Console.WriteLine(“Area={0}”,Area);Console.WriteLine(“Area={0}”,(float)Area);Console.WriteLine(“Area={0}”,(int)Area);Console.WriteLine(“Area={0}”,(decimal)Area);Console.WriteLine(“Area={0}”,(short)Area);} }(4)使用object類與類型轉換 //CircleAreaApp4.cs using System;class Circle { public double Radius=2.5;} class CircleAreaAPP { public static void Main(){ Circle cir=new Circle();double Area=3.14159*cir.Radius*cir.Radius;Console.WriteLine(“Area={0}”,Area);Console.WriteLine(“Area={0}”,(float)Area);object obj=(float)Area;Console.WriteLine(“Area={0}”,(float)obj);} }(5)使用派生類與as //CircleAreaApp5.cs using System;class Circle {} class CircleAreaAPP:Circle { public static void Main(){ double Radius=2.5;double Area=3.14159*Radius*Radius;Console.WriteLine(“Area={0}”,Area);Console.WriteLine(“Area={0}”,(float)Area);object obj=(float)Area;Console.WriteLine(“Area={0}”,(float)obj);Circle cir=new Circle();Console.WriteLine(“cir={0}”,cir==null?“null”:cir.ToString());CircleAreaAPP cirA=new CircleAreaAPP();cirA=cir as CircleAreaAPP;Console.WriteLine(“cirA={0}”,cirA==null?“null”:cirA.ToString());} }
習題3
一、選擇題
1.字符串的輸入使用 B 方法。
A)Cosole.Read()B)Cosole.ReadLine()C)Cosole.Write()D)Cosole.In.read()2.用于格式化輸出十進制數的符號是 C。
A)C B)E C)D D)G E)N F)X 3.用于格式化輸出浮點數的符號是 D。A)C B)D C)G D)F E)N F)X 4.用于格式完整日期/時間模式(長時間)的符號是 A。A)D B)F C)G D)M E)R F)S 5.用于格式完整日期/時間模式(短時間)的符號是 C。A)D B)f C)g D)d E)F F)G
二、編程題
1.從鍵盤輸入一個小寫字母,然后輸出所輸入的小寫字母后其對應單代碼值。2.從鍵盤輸入兩個浮點數,然后輸出這兩個數相加的結果(要求小數后取4位)。3.從鍵盤輸入年、月、日的數值,然后用完整的日期事件格式化輸出。答案: 1.
//CharValue.cs using System;public class CharValue { public static void Main(){ Console.Write(“Enter an char:”);char ch = char.Parse(Console.ReadLine());//or char ch=(char)Console.Readline();Console.WriteLine(ch);Console.WriteLine((int)ch);} }
2.//TwoFloatAddition.cs using System;public class TwoFloatAddition { public static void Main(){ Console.Write(“Enter a float:”);float f1= float.Parse(Console.ReadLine());Console.Write(“Enter a float:”);float f2 = float.Parse(Console.ReadLine());Console.WriteLine(“Result of addition for two float is: {0:F4}”,f1+f2);} }
3.//DateTimeFormat.cs using System;using System.Globalization;public class DateTimeFormat { public static void Main(String[] args){ Console.Write(“Enter year month day: ”);string s = Console.ReadLine();DateTime s1 = DateTime.Parse(s);Console.WriteLine(“d {0:d}”,s1);Console.WriteLine(“D {0:D}”, s1);Console.WriteLine(“f {0:f}”, s1);Console.WriteLine(“F {0:F}”, s1);Console.WriteLine(“g {0:g}”, s1);Console.WriteLine(“G {0:G}”, s1);Console.WriteLine(“m {0:m}”, s1);Console.WriteLine(“M {0:M}”, s1);Console.WriteLine(“r {0:r}”, s1);Console.WriteLine(“R {0:R}”, s1);Console.WriteLine(“s {0:s}”, s1);} }
習題4 1.以下運算符的運算符優先級,D 最高,E 最低。A)+ B)<< C)| D)()E)|| F)++ 2.以下運算符中,A 是三目運算符。
A)?: B)--C)= D)<= 3.在堆棧上創建對象和調用構造函數時,通常使用 B 關鍵字。A)typeof B)new C)as D)is 4. A 用于獲取系統的System.Type類型。
A)typeof B)new C)sizeof D)is
二、寫出下列程序執行結果。1.運行結果。//Increment1.cs using System;public class Increment1 { public static void Main(){ int i1=1993,i2=11,i3=19;Console.WriteLine(“i1={0},i2={1},i2={2}”,i1,i2,i3);i1=i3;Console.WriteLine(“i1={0},i2={1},i2={2}”,i1,i2,i3);i3+=i2;Console.WriteLine(“i1={0},i2={1},i2={2}”,i1,i2,i3);i1=i2+i3;Console.WriteLine(“i1={0},i2={1},i2={2}”,i1,i2,i3);i1++;++i2;i3=i1++ + ++i2;Console.WriteLine(“i1={0},i2={1},i2={2}”,i1,i2,i3);} }
2.運行結果:。//Increment2.cs using System;public class Increment2 { public static void Main(){ int a,b;a = b = 1;b = a / ++b;Console.WriteLine(“a={0} b={1}”,a,b);b = a++-i1);Console.WriteLine(---i1);Console.WriteLine(i2---i3);Console.WriteLine(i4---i5);Console.WriteLine(-i6---i7);Console.WriteLine(i8++/ ++i9*--i10);Console.WriteLine(++i11/i12++ *--i13);Console.Read();} }
三、編程題
1.輸入兩個整數,輸出它們(實數除)的商,并輸出商的第2位小數位(例如:5/18.0=1.875, 1.875的第二位小數是7)。
2.輸入圓球的半徑,計算圓球的表面積(4πr)和體積(4πr/3),其中π=3.14159。3.輸入秒數,把它轉換為用小時、分、秒表示。例如,輸入7278秒,則輸出2小時1分18秒。4.計算x=ab+5ln(1+c)要求:
(1)輸出結果以科學表示法、定點表示法(小數點后保留兩位)和普通表示法表示。(2)輸出結果以整數表示并指明當前工作的日期和時間。
5.計算答案: 1.
//RealDivide.cs using System;public class RealDivide
3223 { public static void Main(){ Console.WriteLine(“Enter two integers:”);string[] s = Console.ReadLine().Split();;int a = int.Parse(s[0]);int b = int.Parse(s[1]);float f = 1.0f * a / b;int c=(int)(f*100)%10;Console.WriteLine(“Result of real divide is : {0}”,f);Console.WriteLine(“Second place of decimals is : {0}”,c);} }
2.//SphereA.cs using System;public class Sphere { public static void Main(){ Console.Write(“Enter the radius of sphere: ”);string s = Console.ReadLine();double radius = double.Parse(s);double surfaceArea = 4 * Math.PI * radius * radius;double Volume = 4 * Math.PI * radius * radius * radius / 3;Console.WriteLine(“SurfaceArea={0}”,surfaceArea);Console.WriteLine(“Volume={0}”,Volume);} }
3.//HourMinuteSecond.cs using System;public class HourMinuteSecond { public static void Main(){ int hour, minute, second;Console.Write(“Enter numbers of second:”);string s = Console.ReadLine();second = int.Parse(s);hour = second / 3600;second %= 3600;minute = second / 60;second = second % 60;Console.WriteLine(“{0} hour {1} minute {2} second”,hour,minute,second);} }
4. //ValueX.cs using System;using System.Globalization;public class ValueX { public static void Main(){ double a, b, c, x;Console.Write(“Enter three numbers: ”);string[] s = Console.ReadLine().Split();a = double.Parse(s[0]);b = double.Parse(s[1]);c = double.Parse(s[2]);x = a * Math.Pow(b, 3)+ 5 * Math.Log(1 + c * c);Console.WriteLine(“x={0:E}ttx={1:F2}ttx={2:G}”, x, x, x);Console.WriteLine(“x={0:D}”,(int)x);DateTime NowTime = DateTime.Now;Console.WriteLine(“{0:D}”, NowTime);} }
5.//MathTestA.cs using System;public class MathTestA { public static void Main(){ double alpha, beta, y;Console.Write(“Enter value of alpha: ”);string s = Console.ReadLine();alpha = double.Parse(s);Console.Write(“Enter value of beta: ”);s = Console.ReadLine();beta = double.Parse(s);y = Math.Pow(Math.Abs(Math.Log(Math.Sqrt(1 + alpha * alpha))a)*(sc)));Console.WriteLine(“Area of triangle is :{0:F2}n”, Area);} else { Console.WriteLine(“can't construct triangle!n”);} Console.ReadLine();}
} 運行結果:
2.//Prime.cs
class Prime { public static void Main(){ int a, n, m = 0, i, j;bool flag;for(i = 2;i <= 50;i++){ flag = true;j = 2;a =(int)Math.Sqrt((double)i);while(flag && j <= a){ if(i % j == 0)flag = false;j++;} if(flag){ Console.Write(“{0:D2} ”, i);m++;if(m % 4 == 0)Console.WriteLine();} } Console.WriteLine();Console.ReadKey();} } 運行結果:
3.//CountDigit.cs class CountDigit { public static void Main(){ int num = 0;char ch;Console.Write(“Enter chars:”);while(true){ if((ch =(char)Console.Read())== 'b')if((ch =(char)Console.Read())== 'y')if((ch =(char)Console.Read())== 'e')break;if(ch >= '0' && ch <= '9')num++;} Console.WriteLine(“Numbers of digit is:{0}n”, num);Console.Read();Console.ReadKey();} } 運行結果:
4.//ForSinCosTan.cs class ForSinCosTan { public static void Main(){ float sinx, cosx, tanx;double x;Console.WriteLine(“xtsinxtcosxttanx”);for(int i = 2;i <= 10;i += 2){ x = i * Math.PI / 180;sinx =(float)Math.Sin(x);cosx =(float)Math.Cos(x);tanx =(float)Math.Tan(x);Console.WriteLine(“{0}tt{1:F6}tt{2:F6}tt{3:F6}”, i, sinx, cosx, tanx);} Console.Read();Console.ReadKey();} } 運行結果:
5.//Factorial.cs
class Factorial { static int n, Fact;public static void Main(){ n = 0;Fact = 1;Console.WriteLine(“Use while loop:”);while(++n <= 5){ Fact *= n;Console.WriteLine(“ {0}!= {1}”, n, Fact);} Console.WriteLine(“Use do-while loop:”);n = 1;Fact = 1;do { Fact *= n;Console.WriteLine(“ {0}!= {1}”, n, Fact);} while(++n <= 5);Console.WriteLine(“Use for loop: ”);Fact = 1;for(n = 1;n <= 5;n++){ Fact *= n;Console.WriteLine(“ {0}!= {1}”, n, Fact);} Console.ReadKey();} } 運行界面:
習題6
一、填空題
1.C#類的成員包括 域、方法、屬性、常量、索引、事件與運算符
2.用于指定類的成員是否可訪問的修飾符有public、protected、private及internal。3.類最常用的方法是Main 4.構造方法實例化對象的形式是 類名 對象=new 類名(構造方法參數)5.從另一個類,繼承一個類的語法是 class 派生類:基類 6.sealed類用于 確保一個類永不作為基類
二、編程題
1輸入一個數值作為正方形的邊長,計算正方形的面積,并輸出到屏幕上。要求:
(1)定義一個類,在類中定義無參的構造方法和主方法。(2)定義一個類,在類中定義帶參的構造方法和主方法。
2、重復輸入數據,計算分段函數
?|x|?r?0y=?
22|x|?r??r?x要求:
(1)定義兩個類,在一個類中定義無參的構造方法,在另一個類中定義主方法。(1)定義兩個類,在一個類中定義帶參的構造方法,在另一個類中定義主方法。
3.從鍵盤讀入邊數(side),然后按輸入的邊數畫出一組由排列緊湊的星號組成的正方形。例如,side為4則畫出: * * * * * * * * * * * * 要求:
(1)定義一個類,在類中定義無參的構造方法。
(2)定義有兩個類,在一個類中定義帶參的構造方法,在另一個類中定義主方法。4.打印一個ASCⅡ碼表。
要求定義兩個類,在一個類中定義無參的構造方法,在另一個類中定義主方法。5.重復輸入數據計算正方形、長方形與任意三角形面積(要求使用單一繼承)。
答案 1.(1)答案
namespace ConsoleApplication1 { class Square { Square(){ Console.Write(“Enter length of side for square:”);double len = double.Parse(Console.ReadLine());Console.WriteLine(“Area={0}”, len * len);} public static void Main(){ for(;;){ Square obj = new Square();Console.Write(“Do you want to continue?(y/n)”);string s = Console.ReadLine();if(s.Equals(“n”))break;}
} } }
運行結果:
(2)答案 namespace ConsoleApplication1 { class Square { public static readonly int N = 3;public Square(double len){ Console.WriteLine(“Area={0}”, len * len);} public static void Main(){ for(int i=1;i<=N;i++){ Console.Write(“Enter length of side for square: ”);string s=Console.ReadLine();double length=double.Parse(s);Square obj=new Square(length);} Console.ReadKey();} } } 運行結果:
2.題(1)答案
namespace ConsoleApplication3 { class Function { public Function(){ Console.Write(“Enter value of x and r;”);string[] s = Console.ReadLine().Split();double x = double.Parse(s[0]);double r = double.Parse(s[1]);double y = Math.Abs(x)>= r ? 0 : Math.Sqrt(r * ry;} } static void Main(string[] args){ int x = 30, y = 50, a, b;fun(x, y out a ,out b)Console.WriteLine(“a=”+a +“b=”+b);} } A)50,30 B)30,50 C)80,—20 D)80,20
二、填空題
下面程序的執行結果是()//FunApp2.cs Using System;class FunApp2 { int x = 888, y = 777, z = 666;public FunApp2(){ x++;y++;z++;} public FunApp2(int a, int b, int c){ x = a;y = b;z = c;} } public class FunApp { static void Main();FunApp2 obj1=new FunApp2();Console.Write Line(obj1.x);Console.Write Line(obj1.y);Console.Write Line(obj1.z);FunApp2 obj2=new FunApp2();Console.Write Line(obj2.x);Console.Write Line(obj2.y);Console.Write Line(obj2.z);} }
二、編程題
1,重復輸入任意數據,計算y=
??0
???
r2?x2
|x|??r
|x|?r要求:
(1)使用值參數方法:(2)使用ref參數方法;(3)使用out參數方法;(4)使用ref與out參數方法;(5)使用重載方法;(6)使用重載構造方法;(7)使用可變參數方法;(8)使用靜態方法。
(1)答案: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1 { //YestA
class YTest { public void yMethod(double x, double r){ Console.WriteLine(“y={0}”, Math.Abs(x)> r ? 0 : Math.Sqrt(r * rx * x);} } class YTest {
第五篇:《C語言程序設計教程(第二版)》習題答案
第1章 程序設計基礎知識
一、單項選擇題(第23頁)1-4.CBBC 5-8.DACA
二、填空題(第24頁)
1.判斷條件 2.面向過程編程 3.結構化 4.程序 5.面向對象的程序設計語言 7.有窮性 8.直到型循環 9.算法 10.可讀性 11.模塊化 12.對問題的分析和模塊的劃分
三、應用題(第24頁)2.源程序:
main()
{int i,j,k;/* i:公雞數,j:母雞數,k:小雞數的1/3 */ printf(“cock hen chickn”);for(i=1;i<=20;i++)for(j=1;j<=33;j++)for(k=1;k<=33;k++)
if(i+j+k*3==100&&i*5+j*3+k==100)printf(“ %d %d %dn”,i,j,k*3);} 執行結果:
cock hen chick 4 18 78 8 11 81 12 4 84
3.現計算斐波那契數列的前20項。
遞推法 源程序:
main()
{long a,b;int i;a=b=1;
for(i=1;i<=10;i++)/*要計算前30項,把10改為15。*/ {printf(“%8ld%8ld”,a,b);a=a+b;b=b+a;}}
遞歸法 源程序:
main(){int i;
for(i=0;i<=19;i++)printf(“%8d”,fib(i));} fib(int i)
{return(i<=1?1:fib(i-1)+fib(i-2));}
執行結果:
1 2 3 5 8 13 21 34 55
233 377 610 987 1597 2584 4181 6765 4.源程序:
#include “math.h”;main()
{double x,x0,deltax;x=1.5;
do {x0=pow(x+1,1./3);deltax=fabs(x0-x);x=x0;
}while(deltax>1e-12);printf(“%.10fn”,x);} 執行結果:
1.3247179572
5.源程序略。(分子、分母均構成斐波那契數列)結果是32.66026079864 6.源程序:
main()
{int a,b,c,m;
printf(“Please input a,b and c:”);scanf(“%d %d %d”,&a,&b,&c);if(a
printf(“%d %d %dn”,a,b,c);} 執行結果:
Please input a,b and c:123 456 789 789 456 123 7.源程序:
main(){int a;
scanf(“%d”,&a);
printf(a%21==0?“Yes”:“No”);} 執行結果:
Yes 第2章 C語言概述
一、單項選擇題(第34頁)1-4.BDCB 5-8.AABC
二、填空題(第35頁)
1.主 2.C編譯系統 3.函數 函數 4.輸入輸出 5.頭 6..OBJ 7.庫函數 8.文本
三、應用題(第36頁)
5.sizeof是關鍵字,stru、_aoto、file、m_i_n、hello、ABC、SIN90、x1234、until、cos2x、s_3是標識符。
8.源程序: main(){int a,b,c;
scanf(“%d %d”,&a,&b);c=a;a=b;b=c;
printf(“%d %d”,a,b);} 執行結果:34 34 12 第3章 數據類型與運算規則
一、單項選擇題(第75頁)
1-5.DBACC 6-10.DBDBC 11-15.ADCCC 16-20.CBCCD 21-25.ADDBC 26-27.AB
二、填空題(第77頁)
1.補碼 2.±(10^-308~10^308)3.int(整數)4.單目 自右相左 5.函數調用 6.a或b 7.1 8.65,89
三、應用題(第78頁)1.10 9
2.執行結果: 0 0 12 1 第4章 順序結構程序設計
一、單項選擇題(第90頁)1-5.DCDAD 6-10.BACBB
二、填空題(第91頁)
1.一 ;2.5.169000 3.(1)-2002500(2)I=-200,j=2500(3)i=-200 j=2500 4.a=98,b=765.000000,c=4321.000000 5.略 6.0,0,3 7.3 8.scanf(“%lf%lf%lf”,&a,&b,&c);9.13 13.000000,13.000000 10.a=a^c;c=c^a;a=a^c;(這種算法不破壞b的值,也不用定義中間變量。)
三、編程題(第92頁)
1.仿照教材第27頁例2-1。
2.源程序:
main(){int h,m;
scanf(“%d:%d”,&h,&m);printf(“%dn”,h*60+m);} 執行結果:
9:23 563
3.源程序:
main()
{int a[]={-10,0,15,34},i;for(i=0;i<=3;i++)
printf(“%d370C=%g370Ft”,a[i],a[i]*1.8+32);} 執行結果:
-10℃=14°F 0℃=32°F 15℃=59°F 34℃=93.2°F 4.源程序:
main()
{double pi=3.14***9,r=5;
printf(“r=%lg A=%.10lf S=%.10lfn”,r,2*pi*r,pi*pi*r);} 執行結果:
r=5 A=31.4159265359 S=49.3480220054 5.源程序:
#include “math.h”;main()
{double a,b,c;
scanf(“%lf%lf%lf”,&a,&b,&c);if(a+b>c&&a+c>b&&b+c>a){double s=(a+b+c)/2;
printf(“SS=%.10lfn”,sqrt(s*(s-a)*(s-b)*(s-c)));} else printf(“Data error!”);} 執行結果:5 6
SS=9.9215674165 6.源程序:
main()
{int a=3,b=4,c=5;float d=1.2,e=2.23,f=-43.56;
printf(“a=%3d,b=%-4d,c=**%dnd=%gne=%6.2fnf=%-10.4f**n”,a,b,c,d,e,f);} 7.源程序:
main()
{int a,b,c,m;
scanf(“%d %d %d”,&a,&b,&c);m=a;a=b;b=c;c=m;
printf(“%d %d %dn”,a,b,c);} 執行結果:6 7 6 7 5 8.源程序:
main(){int a,b,c;
scanf(“%d %d %d”,&a,&b,&c);
printf(“average of %d,%d and %d is %.2fn”,a,b,c,(a+b+c)/3.);執行結果: 7 9
average of 6,7 and 9 is 7.33 9.不能。修改后的源程序如下:
main()
{int a,b,c,x,y;
scanf(“%d %d %d”,&a,&b,&c);x=a*b;y=x*c;
printf(“a=%d,b=%d,c=%dn”,a,b,c);printf(“x=%d,y=%dn”,x,y);} 第5章 選擇結構程序設計
一、單項選擇題(第113頁)1-4.DCBB 5-8.DABD
二、填空題(第115頁)1.非0 0 2.k==0
3.if(abs(x)>4)printf(“%d”,x);else printf(“error!”);
4.if((x>=1&&x<=10||x>=200&&x<=210)&&x&1)printf(“%d”,x);5.k=1(原題最后一行漏了個d,如果認為原題正確,則輸出k=%。)6.8!Right!11 7.$$$a=0 8.a=2,b=
1三、編程題(第116頁)1.有錯。正確的程序如下:
main(){int a,b,c;
scanf(“%d,%d,%d”,&a,&b,&c);
printf(“min=%dn”,a>b?b>c?c:b:a>c?c:a);} 2.源程序:
main()
{unsigned long a;scanf(“%ld”,&a);
for(;a;printf(“%d”,a%10),a/=10);} 執行結果:
12345 54321
3.(1)源程序: main(){int x,y;
scanf(“%d”,&x);if(x>-5&&x<0)y=x;if(x>=0&&x<5)y=x-1;if(x>=5&&x<10)y=x+1;printf(“%dn”,y);}(2)源程序:
main(){int x,y;
scanf(“%d”,&x);
if(x<10)if(x>-5)if(x>=0)if(x>=5)y=x+1;else y=x-1;else y=x;printf(“%dn”,y);}(3)源程序:
main(){int x,y;
scanf(“%d”,&x);
if(x<10)if(x>=5)y=x+1;else if(x>=0)y=x-1;else if(x>-5)y=x;printf(“%dn”,y);}(4)源程序:
main(){int x,y;
scanf(“%d”,&x);switch(x/5)
{case-1:if(x!=-5)y=x;break;case 0:y=x-1;break;case 1:y=x+1;} printf(“%dn”,y);}
4.本題為了避免考慮每月的天數及閏年等問題,故采用面向對象的程序設計。
現給出Delphi源程序和C++ Builder源程序。
Delphi源程序:
procedure TForm1.Button1Click(Sender: TObject);begin
edit3.Text:=format('%.0f天',[strtodate(edit2.text)-strtodate(edit1.text)]);end;
procedure TForm1.FormCreate(Sender: TObject);begin
Edit2.Text:=datetostr(now);button1click(form1)end;
C++ Builder源程序: void __fastcall TForm1::Button1Click(TObject *Sender){
Edit3->Text=IntToStr(StrToDate(Edit2->Text)-StrToDate(Edit1->Text))+“天”;}
void __fastcall TForm1::FormCreate(TObject *Sender){
Edit2->Text=DateToStr(Now());Button1Click(Form1);}
執行結果:(運行于Windows下)http://img378.photo.163.com/nxgt/41463572/1219713927.jpg
5.源程序:
main()
{unsigned a,b,c;
printf(“請輸入三個整數:”);
scanf(“%d %d %d”,&a,&b,&c);
if(a&&b&&c&&a==b&&a==c)printf(“構成等邊三角形n”);else if(a+b>c&&a+c>b&&b+c>a)
if(a==b||a==c||b==c)printf(“構成等腰三角形n”);else printf(“構成一般三角形n”);else printf(“不能構成三角形n”);} 執行結果:
請輸入三個整數:5 6 5 構成等腰三角形
6.源程序:
main(){int x,y;
scanf(“%d”,&x);if(x<20)y=1;else switch(x/60)
{case 0:y=x/10;break;default:y=6;}
printf(“x=%d,y=%dn”,x,y);} 7.源程序:
main()
{unsigned m;float n;scanf(“%d”,&m);if(m<100)n=0;
else if(m>600)n=0.06;else n=(m/100+0.5)/100;
printf(“%d %.2f %.2fn”,m,m*(1-n),m*n);} 執行結果:
450 450 429.75 20.25
8.2171天(起始日期和終止日期均算在內)
本題可利用第4小題編好的程序進行計算。把起始日期和終止日期分別打入“生日”和“今日”欄內,單擊“實足年齡”按鈕,將所得到的天數再加上1天即可。
9.源程序:
#include “math.h”;main()
{unsigned long i;scanf(“%ld”,&i);
printf(“%ld %dn”,i%10,(int)log10(i)+1);} 執行結果:
99887 7 5
10.源程序:
main()
{unsigned long i;unsigned j[10],m=0;scanf(“%ld”,&i);
for(;i;){j[m++]=(i+2)%10;i/=10;} for(;m;m--)i=i*10+j[m-1];printf(“%ldn”,i);} 執行結果:
6987 8109
(注:要加密的數值不能是0或以0開頭。如果要以0開頭需用字符串而不能是整數。)第6章 循環結構程序設計
一、單項選擇題(第142頁)1-4.BCCB 5-8.CBCA
二、填空題(第143頁)
1.原題可能有誤。如無誤,是死循環 2.原題有誤。如果把b=1后面的逗號改為分號,則結果是8。3.20 4.11 5.2.400000 6.*#*#*#$ 7.8 5 2 8.①d=1.0 ②++k ③k<=n 9.①x>=0 ②x 三、編程題(第145頁)1.源程序: main() {int i=1,sum=i; while(i<101){sum+=i=-i-2;sum+=i=-i+2;} printf(“%dn”,sum);} 執行結果: 51 2.源程序: main() {double p=0,n=0,f;int i;for(i=1;i<=10;i++){scanf(“%lf”,&f); if(f>0)p+=f;else n+=f;} printf(“%lf %lf %lfn”,p,n,p+n);} 3.源程序: main() {unsigned a;scanf(“%ld”,&a); for(;a;printf(“%d,”,a%10),a/=10);printf(“b n”);} 執行結果: 23456 6,5,4,3,2 4.源程序: main() {unsigned long a,b,c,i;scanf(“%ld%ld”,&a,&b);c=a%1000; for(i=1;i 57 009 5.略 6.原題提供的計算e的公式有誤(前面漏了一項1)。正確的公式是e= 1 + 1 + 1/2!+ 1/3!+ … + 1/n!+ …(1)源程序: main() {double e=1,f=1;int n; for(n=1;n<=20;n++){f/=n;e+=f;} printf(“e=%.14lfn”,e);} 執行結果: e=2.7***05(2)源程序: main() {double e=1,f=1;int n; for(n=1;f>1e-4;n++){f/=n;e+=f;} printf(“e=%.4fn”,e);} 執行結果: e=2.7183 7.源程序: main() {unsigned long a=0,b=1,c=0;int i,d;scanf(“%d”,&d); for(i=1;i<=(d+2)/3;i++) printf(“%10ld%10ld%10ld”,a,b,(a+=b+c,b+=c+a,c+=a+b));} 本題還可以用遞歸算法(效率很低),源程序如下: unsigned long fun(int i) {return i<=3?i:fun(i-1)+fun(i-2)+fun(i-3);} main() {int i,d;scanf(“%d”,&d);for(i=1;i<=d;i++) printf(“%10ld”,fun(i));} 執行結果: 68 230 423 778 1431 2632 4841 8.源程序: main(){int i; for(i=1010;i<=9876;i+=2) if(i/100%11&&i%100%11&&i/10%100%11&&i/1000!=i%10&&i/1000!=i/10%10&&i/100%10!=i%10)printf(“ %d”,i);} 執行結果: 1024 1026 1028 1032 1034 1036 …… …… 9874 9876 9.源程序: main(){int i,j,k; printf(“apple watermelon pearn”);for(i=1;i<=100;i++)for(j=1;j<=10;j++) if((k=100-i-j)*2==400-i*4-j*40)printf(“%4d%7d%9dn”,i,j,k);} 執行結果: apple watermelon pear 5 5 90 24 4 72 43 3 54 62 2 36 81 1 18 10.源程序: #include “stdio.h”; #define N 4 /* N為階數,可以改為其他正整數 */ main(){int m=N*2,i,j; for(i=1;i putchar(N-abs(i-N)<=abs(j++-N)?' ':'*'));} 如果把N值改為5,則執行結果如下: * *** ***** ******* ********* ******* ***** *** * 作者:寧西貫通 2006-5-7 23:41 回復此發言 ------------------說明 注意:上面最后一題的輸出結果應該是由星號組成的一個菱形,第7章 數 組 一、單項選擇題(第192頁)1-4.BBCC 5-8.AABA 二、填空題(第194頁) 1.1 2 4 8 16 32 64 128 256 512 2.①a[age]++ ②i=18;i<26 3.①break ②i==8 4.①a[i]>b[j] ②i<3 ③j<5 5.①b[j]=a[j][0] ②b[j] 三、編程題(第196頁)1.源程序: main() {int a[4][4],i,j,s=0;for(i=0;i<4;i++)for(j=0;j<4;j++)scanf(“%d”,&a[i][j]);for(i=0;i<4;i++)for(j=0;j<4;j++) if(i==j||i+j==3)s+=a[i][j]; printf(“%dn”,s);} /* 注:5×5矩陣不能照此計算!*/ 執行結果: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 68 2.源程序: main() {int i,a[36];a[0]=2; for(i=1;i<=29;i++)a[i]=a[i-1]+2;for(;i<=35;i++)a[i]=a[(i-30)*5+2];for(i=0;i<=35;i++)printf(“%dt”,a[i]);} 執行結果: 4 6 8 10 12 14 16 18 20 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 6 16 26 36 46 56 3.源程序: #include “stdlib.h” #include “time.h” main() {int a[30],i,m=0;randomize(); for(i=0;i<=29;i++){a[i]=rand();if(m for(i=0;i<=29;i++)if(a[i]==m)a[i]=-1;printf(“n-----------------n”);for(i=0;i<=29;i++) if(~a[i])printf(“%dt”,a[i]);printf(“n”);} 執行結果: 20679 29377 18589 9034 27083 4959 3438 5241 32278 23344 32499 29305 22340 5927 13031 2161 2583 31855 22977 14283 4851 22038 6992 11394 20887 27381 6293 18347 16414 10210----------------- 20679 29377 18589 9034 27083 4959 3438 5241 32278 23344 29305 22340 5927 13031 2161 2583 31855 22977 14283 4851 22038 6992 11394 20887 27381 6293 18347 16414 10210 4.源程序: main() {int i,n=0,b[16];scanf(“%d”,&i); for(;i;i>>=1)b[n++]=i&1;for(;n;)printf(“%d”,b[--n]);} 執行結果: 9876 10011010010100 本題也可以不用數組。源程序如下: #include “stdio.h” main(){int i,n; scanf(“%d”,&i);for(n=16;n;n--){asm ROL i,1 putchar(i&1|48);} } /* ROL是循環左移的匯編指令 */ 5.源程序: #include “stdlib.h” #include “time.h” #define M 5 #define N 6 main() {int a[M][N],i,j,t[M];randomize(); /*生成M行N列隨機數*/ for(i=0;i printf(“%4d”,a[i][j]=random(50)); /*找出每行的最小數,t[M]是第M行的最小數所在的列數*/ for(i=0;i if(a[i][t[i]]>a[i][j])t[i]=j; /*比較每個最小數在其所在的列上是否也是最小*/ for(j=0;j printf(“-------------------n”); /*輸出在行和列上均為最小的數*/ for(i=0;i printf(“a[%d,%d]=%dn”,i,t[i],a[i][t[i]]);} 執行結果: 13 20 0 1 20 41 6 16 35 30 3 5 37 8 23 15 6 36 24 29 18 1 1 5 28 21 46 34-------------------a[0,4]=0 a[1,2]=6 a[3,5]=1 a[4,0]=1 6.源程序: #include “stdlib.h” #include “time.h” #define M 5 #define N 7 main() {int a[M][N],i,j,t=0;randomize();for(i=0;i for(j=0;j {printf(“%4d”,a[i][j]=random(91)+10);a[i][N-1]+=a[i][j];} printf(“%4dn”,a[i][N-1]);} for(i=1;i if(a[i][N-1]>a[t][N-1])t=i;if(t)for(j=0;j {i=a[0][j];a[0][j]=a[t][j];a[t][j]=i;} printf(“-----------------n”);for(i=0;i 第7章 數 組 for(j=0;j 執行結果: 17 32 95 35 20 288 39 48 22 27 73 22 231 51 87 39 71 84 46 378 84 94 97 77 27 26 405 69 50 56 89 37 46 347----------------- 77 27 26 405 39 48 22 27 73 22 231 51 87 39 71 84 46 378 89 17 32 95 35 20 288 69 50 56 89 37 46 347 7.源程序: #include “stdlib.h” #include “time.h” #define M 5 #define N 6 main() {int a[M][N],i,j; struct data{int value,x,y;}max,min;max.value=0;min.value=100;randomize(); for(i=0;i {printf(“%4d”,a[i][j]=random(100)+1);if(max.value {max.value=a[i][j];max.x=i;max.y=j;} if(min.value>a[i][j]) {min.value=a[i][j];min.x=i;min.y=j;} } printf(“-----------------n”); i=a[0][N-1];a[0][N-1]=max.value;a[max.x][max.y]=i;i=a[M-1][0];a[M-1][0]=min.value;a[min.x][min.y]=i;for(i=0;i 執行結果: 65 30 40 30 26 50 6 61 27 47 16 54 58 76 19 57 74 44 92 71 48 73 57 60 32 73 67----------------- 65 30 92 30 26 50 73 61 27 47 16 54 58 76 19 57 74 44 40 71 48 6 57 60 32 73 67 9.源程序: main() {char s[255];int i,j,b=1;printf(“Input a string:”);scanf(“%s”,s);i=strlen(s); for(j=1;j<=i/2;j++)b=b&&(s[j-1]==s[i-j]);printf(b?“Yesn”:“Non”);} 執行結果: Input a string:level Yes 10.源程序: main() {char s[255],t,max=0,min=0,l,i;printf(“Input a string(length>4):”);gets(s);l=strlen(s); for(i=0;i {if(s[max] t=s[1];s[1]=s[max];s[max]=t;if(min==1)min=max;t=s[l-2];s[l-2]=s[min];s[min]=t;printf(“%sn”,s);} 執行結果: Input a string(length>4):C++Builder Cu+Beild+r 11.源程序: main() {char m[13][10]={“****”,“January”,“February”,“March”, “April”,“May”,“June”,“July”,“August”,“September”, “October”,“November”,“December”};int i,j,k,a,s,n; printf(“Please input an integer(100..999):”);scanf(“%d”,&n); printf(“%d:%d+%d+%d=%d, %d%%13=%d, %sn”, n,i,j,k,s,s,a,m[a=((s=(i=n/100)+(j=n/10%10)+(k=n%10))%13)]);} 執行結果: Please input an integer(100..999):539 539:5+3+9=17, 17%13=4, April 第8章 函 數 一、單項選擇題(第241頁) 1-5.BCCAA 6-10.CCDDD 11-15.ACACB 二、填空題(第243頁) 1.看不出原題的意圖。因為要計算1~n的累加和,n應是一個≥1的正整數。可是題目中卻出現了n=0的情況。除非另加規定當n=0時1~n的累加和為0,或者把原題中的計算式改為計算0~n的累加和。據此猜測,原題應填為:①return(0)②return(n+sum(n-1))根據題意,如下程序較為合理: int sum(int n) {if(n<=0)return(-1);/*-1是出錯標志 */ else if(n==1)return(1);else return(n+sum(n-1));} 2.①return(1)②return(n*facto(n-1)) 三、編程題(第244頁)3.源程序: main() {int i,a,b,c; for(i=100;i<999;i++) if((a=i/100)*a*a+(b=i/10%10)*b*b+(c=i%10)*c*c==i)printf(“%dt”,i);} 執行結果: 153 370 371 407 8.源程序(非遞歸算法): #define P 13 /* P可以改為其他正整數 */ main() {int a[P],r,c; for(r=0;r<=P;r++){a[r]=1; for(c=r-1;c>=1;a[c--]+=a[c-1]);printf(“%*d”,(P-r)*3+1,a[0]); for(c=1;c<=r;printf(“%6d”,a[c++]));printf(“n”);} } 執行結果: (應該排列成一個三角形,是貼吧造成現在這個樣子的,不是程序有問題)1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 126 84 36 9 1 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 1 12 66 220 495 792 924 792 495 220 66 12 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 9.源程序(遞歸算法): #include “stdio.h” void printOCT(unsigned long n){unsigned long i; if(i=n>>3)printOCT(i);putchar((n&7)+48);} main() {unsigned long i;scanf(“%ld”,&i);printOCT(i);} 執行結果: 1234567890 11145401322 本題也可以不用遞歸算法,源程序請參考第7章第三題4。回復:【C語言】《C語言程序設計教程(第二版)》習題答案 但是不同時間印刷的版本課后題不太一樣呢,象我們的是1999年12月第2版,2005年12月第69次印刷的。沒有選擇填空,應用題和樓主不知道有多少相同的,因為看不到原題。這個比較麻煩呢。 作者:210.77.204.* 2006-5-9 18:38 回復此發言 ------------------回復:【C語言】《C語言程序設計教程(第二版)》習題答案 你對照一下主編和出版社,看看對嗎?(見說明的第一條。)14 第9章 指 針 一、單項選擇題(第276頁) 1-5.DCDAC 6-10.CCABC 11-16.AABBB 16-20.DCDBD 二、填空題(第278頁)1.①int * ②*z 2.*p++ 3.①'
主站蜘蛛池模板:
国产精品免费精品自在线观看|
日日摸夜夜添狠狠添欧美|
亚洲熟妇色xxxxx欧美老妇y|
国产七十六+老熟妇|
天天日天天干天天操|
国产美女在线精品免费观看网址|
yw尤物av无码国产在线观看|
久久精品99久久香蕉国产|
中文字幕人成无码免费视频|
国产亚洲欧美日韩一区图片|
欧美人与动牲交aⅴ|
777精品出轨人妻国产|
五月丁香六月综合缴清无码|
99er热精品视频|
久久久精品94久久精品|
国产精品青草久久福利不卡|
日韩做a爰片久久毛片a片|
亚洲色大成网站www在线|
少妇裸交aa大片|
无码人妻丰满熟妇啪啪|
无码中文字幕乱码三区日本视频|
国产无遮挡无码视频在线观看|
99久久精品免费观看国产|
美女视频黄频大全免费|
亚洲日韩国产精品第一页一区|
久久久噜噜噜久久|
亚洲成_人网站图片|
国产999精品成人网站|
亚洲成熟丰满熟妇高潮xxxxx|
欧美国产日韩a在线视频|
精品麻豆一卡2卡三卡4卡乱码|
午夜精品久久久久久久久日韩欧美|
亚洲已满18点击进入在线看片|
国产亚洲人成网站在线观看|
亚洲无亚洲人成网站9999|
国产精品一区二区 尿失禁|
久草日b视频一二三区|
久久精品国产只有精品66|
国产清纯在线一区二区|
刺激一区仑乱|
人妻有码中文字幕|
s[i])min=i;}