本文目錄一覽:
求的答案
(第一題)#include stdio.h
void main()
{
int gy(int m, int n);
int x,y,max, min;
printf(“請輸入兩個大於0的正整數,以空格或者回車間隔:\n”);
scanf(“%d%d”,x,y);
while(x1 || y1)
{
printf(“輸入數據不正確,請重新輸入。\n”);
printf(“請輸入兩個大於0的正整數,以空格或者回車間隔:\n”);
scanf(“%d%d”,x,y);
}
max=gy(x,y);
min=x*y/max;
printf(“%d與%d的最大公約數是:%d,最小公倍數是:%d\n”,x,y,max,min);
}
int gy(int m, int n)
{
int max, t;
while(m%n != 0) // while(m%n)
{
t=n; n=m%n; m=t;
}
max=n;
return max;
}
第二題#include stdio.h
#include math.h
void main()
{
void root(double a, double b, double c);
double a, b, c;
printf(“請輸入一元二次方程的係數,用空格或者回車間隔:\n”);
scanf(“%lf%lf%lf”,a,b,c);
root(a,b,c);
}
void root(double a, double b, double c)
{
double disc, x1, x2, real , imag;
disc = b*b-4*a*c;
if(disc0)
{
x1 = (-b+sqrt(disc))/(2*a);
x2 = (-b-sqrt(disc))/(2*a);
printf(“方程有兩個實根,分別是:%f 與 %f\n”, x1, x2);
}
else if(disc==0)
{
x1 = (-b)/(2*a);
printf(“方程有一個實根,它是:%f\n “, x1);
}
else
{
real=(-b)/(2*a);
imag=sqrt(-disc)/(2*a);
printf(“方程有兩個虛根,分別是:%f+%fi, %f-%fi\n”, real,imag,real,imag);
}
}
測試1:請輸入一元二次方程的係數,用空格或者回車間隔:1 2 1
方程有一個實根,它是:-1.000000
測試2:請輸入一元二次方程的係數,用空格或者回車間隔:1 6 5
方程有兩個實根,分別是:-1.000000 與 -5.000000
測試3:請輸入一元二次方程的係數,用空格或者回車間隔:1 1 1
方程有兩個虛根,分別是:-0.500000+0.866025i, -0.500000-0.866025i
第三題#include stdio.h
#include math.h
void main()
{
int isprime(int n);
int n;
scanf(“%d”,n);
while(n2)
{
printf(“Please reinput(n=2):”);
scanf(“%d”,n);
}
if(isprime(n))
printf(“%d是素數。\n”,n);
else
printf(“%d不是素數。\n”,n);
}
int isprime(int n)
{
int i,k=sqrt(n);
for(i=2;i=k;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
第四題#include stdio.h
#include math.h
void main()
{
void printA(int a[3][3]);
void reverse(int a[3][3]); //轉置函數的聲明
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
printf(“原二維數組:\n”);
printA(a);
reverse(a); //函數轉置
printf(“轉置後的數組:\n”);
printA(a);
}
void printA(int a[3][3])
{
int i,j;
for(i=0;i3;i++)
{
for(j=0;j3;j++)
{
printf(“%6d”,a[i][j]);
}
printf(“\n”);
}
}
void reverse(int a[3][3]) //函數轉置的定義
{
int i,j,t;
for(i=0;i3;i++)
{
for(j=0;ji;j++)
{
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
}
}
第五題#include stdio.h
#include string.h
void main()
{
char str[30];
void fanxu(char *p);
printf(“請輸入一個字符串:”);
gets(str);
printf(“進行反序……\n”);
fanxu(str);
printf(“反序後的字符串:”);
puts(str);
}
void fanxu(char *p)
{
int len=strlen(p);
char *q,c;
q=p+len-1; //字符串最後一個字符位置
for(;pq;p++,q–)
{
c=*p; *p=*q; *q=c;
}
}
第六題#include stdio.h
#include string.h
void main()
{
char str1[60],str2[30];
void str_copy(char *p, char *q);
printf(“請輸入第一個字符串:”);
gets(str1);
printf(“請輸入第二個字符串:”);
gets(str2);
str_copy(str1, str2);
printf(“連接後的字符串:”);
puts(str1);
}
void str_copy(char *p, char *q)
{
for(;*p!=’\0′;p++);
for(;*q!=’\0′;p++,q++)
{
*p=*q;
}
*p=’\0′;
}
(0808)#include stdio.h
#include string.h
void main()
{
char str[80];
void insert(char str[]); //插入空格的函數
printf(“請輸入一個字符串:”);
gets(str);
insert(str); //插入空格
puts(str); //輸出字符串
}
void insert(char str[])
{
int len, i;
len=strlen(str);
for(i=len; i0; i–) // 設置空格
{
str[2*i]=str[i];
str[2*i-1]=’ ‘;
}
}
(0809)#include stdio.h
int letter; //字母個數
int digit; //數字個數
int space; //空格個數
int others; //其它字母個數
void main()
{
void count(char str[]); //統計個數的函數的聲明
char s[81];
printf(“請輸入一個字符串:”);
gets(s);
letter=0; digit=0;
space=0; others=0;
count(s);
printf(“字符串中共有 %d 個字母,%d 個數字,%d 個空格,%d個其它字母。\n”,letter,digit,space,others);
}
void count(char str[]) //統計個數的函數的定義
{
int i;
char c;
for(i=0; str[i]!=’\0′;i++)
{
c=str[i];
if(c=’a’ c=’z’ || c=’A’ c=’Z’)
{ letter++; }
else if(c=’0′ c=’9′)
{ digit++; }
else if(c==’ ‘)
{ space++; }
else
{ others++; }
}
}
(0810)#include stdio.h
#include string.h
void main()
{
int i;
char line[81];
int alphabetic(char c); //判斷一個字符是空格還是其它字母
int longest(char str[]);//尋找最長單詞的起始位置
printf(“請輸入一行字符串:\n”);
gets(line);
printf(“最長的字符串是:”);
for(i=longest(line); alphabetic(line[i]); i++)
{ printf(“%c”,line[i]);}
printf(“\n”);
}
int alphabetic(char c) //如果為空格返回0,其它字母返回1
{
if(c!=’ ‘)
return 1;
else
return 0;
}
int longest(char str[])
{
int len=0; // 記錄每一個單詞的長度
int length=0; // 記錄最長單詞的長度
int flag=1; // 其值為0時表示當前位置處於字符串中,為1時表示當前位置為空格
int place=0; // 記錄最長字符串(單詞)的起始位置
int point; // 每個字符串的起始位置
for(int i=0; i=strlen(str); i++)
{
if(alphabetic(str[i])) //如果當前位置為非空格
{
if(flag) //如果前一字符為空格
{
point = i; // 設置當前單詞的起始位置
flag = 0; // flag設為0,表示處於單詞中
}
else //如果前一字符為非空格
{ len++; } // 單詞的長度加1
}
else //當前位置為空格
{
flag = 1; //flag設為1,表示當前位置為空格
if(len = length) //如果最近單詞的長度大於最長長度
{
length = len;
place = point; //設置最長單詞的起始位置
len = 0; //len歸0,重新開始計算單詞的長度
}
}
}
return place;
}
(0811)#include stdio.h
#include string.h
void main()
{
void inputNum(int a[], int n);
void outputNum(int a[], int n);
void bubble(int a[], int n);
int a[11], n;
printf(“請輸入你要排序的數的個數:”);
scanf(“%d”,n);
inputNum(a,n);
outputNum(a,n);
bubble(a,n);
printf(“從小到大排序為:\n”);
outputNum(a,n);
}
void inputNum(int a[], int n)
{
int i;
for(i=1;i=n;i++)
{
printf(“a[%d]=”,i);
scanf(“%d”,a[i]);
}
}
void outputNum(int a[], int n)
{
int i;
for(i=1;i=n;i++)
{
printf(“%6d”,a[i]);
}
printf(“\n”);
}
void bubble(int a[], int n)
{
int i,j,t;
for(i=1;i=n-1;i++)
{
for(j=1; j=n-i; j++)
{
if(a[j]a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
}
}
(0813)#include stdio.h
void main()
{
double lrd(int, double);
double x;
int n;
printf(“請輸入n階勒讓德多項式的階數:\n”);
scanf(“%d”,n);
printf(“請輸入x的值:\n”);
scanf(“%lf”,x);
printf(“參數為%f 的 %d 階勒讓德多項式的值為 %f.\n”,x,n,lrd(n,x));
}
double lrd(int n, double x)
{
if(n==0)
return 1;
else if(n==1)
return x;
else
return ((2*n-1)*x – lrd(n-1,x) -(n-1)*lrd(n-2,x))/n;
}
/*
測試1:請輸入n階勒讓德多項式的階數:0請輸入x的值:99.99參數為99.990000 的 0 階勒讓德多項式的值為 1.000000.
測試2:請輸入n階勒讓德多項式的階數:1請輸入x的值:99.99
參數為99.990000 的 1 階勒讓德多項式的值為 99.990000.
測試3:請輸入n階勒讓德多項式的階數:2
請輸入x的值:99.99 參數為99.990000 的 2 階勒讓德多項式的值為 99.490000.
測試4:請輸入n階勒讓德多項式的階數:10
請輸入x的值:1.1
參數為1.100000 的 10 階勒讓德多項式的值為 0.888677.
(0817)#include stdio.h
void main()
{
int num;
void convert(int num); // 轉換為字符串的函數的聲明
printf(“請輸入一個整數:\n”);
scanf(“%d”,num);
printf(“轉換成字符串:”);
if(num0)
{
putchar(‘-‘);
num = -num;
}
convert(num);
printf(“\n”);
}
void convert(int n)
{
int i;
if((i = (n/10)) != 0)
{
convert(i);
}
putchar(n%10 + ‘0’);
}
(0818)#include stdio.h
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void main()
{
int year,month,day;
int days;
int count(int year,int month, int day);
int leap(int y);
printf(“請輸入年份:”);
scanf(“%d”,year);
while(year=0)
{
printf(“年份不能為負,請重新輸入:”);
scanf(“%d”,year);;
}
if(leap(year)) //如果為閏年,2月份的天數為29
{ d[2]=29; }
printf(“輸入月份:”);
scanf(“%d”,month);
while(month1 || month12)
{
printf(“月份在1月和12月之間,你的輸入有誤,請重新輸入:”);
scanf(“%d”,month);
}
printf(“輸入日數:”);
scanf(“%d”,day);;
while(day1 || day d[month])
{
printf(“日數應在1和%d之間,請重新輸入:”,d[month]);
scanf(“%d”,day);
}
days=count(year,month,day);
printf(“%d年%d月%d日是該年的第%d天\n”,year,month,day,days);
}
int leap(int y)
{
if(y%4==0 y%100!=0 || y%400==0)
return 1;
else
return 0;
}
int count(int year,int month, int day)
{
int i,days=0;
for(i=1; imonth; i++)
{ days += d[i]; }
days+=day;
return days;
}
(08050)#include stdio.h
#include string.h
void main()
{
char str[30];
void fanxu(char str[]);
printf(“請輸入一個字符串:”);
gets(str);
printf(“進行反序……\n”);
fanxu(str);
printf(“反序後的字符串:”);
puts(str);
}
void fanxu(char str[])
{
int len=strlen(str);
int i,j;
char c;
i=0; j=len-1;//i,j分別存儲第一個字符和最後一個字符的下標
for(;ij; i++,j–)
{
c=str[i]; str[i]=str[j]; str[j]=c;
}
}
(08051)#include stdio.h
#include string.h
void main()
{
char str[30];
void fanxu(char s[]);
printf(“請輸入一個字符串:”);
gets(str);
printf(“進行反序……\n”);
fanxu(str);
printf(“反序後的字符串:”);
puts(str);
}
void fanxu(char s[])
{
int len=strlen(s);
int i;
char c;
for(i=0;i=len/2;i++)
{
c=s[i];
s[i]=s[len-i-1];
s[len-i-1]=c;
}
}
(08061)#include stdio.h
#include string.h
void main()
{
char str1[60],str2[30];
void str_copy(char s1[], char s2[]);
printf(“請輸入第一個字符串:”);
gets(str1);
printf(“請輸入第二個字符串:”);
gets(str2);
str_copy(str1, str2);
printf(“連接後的字符串:”);
puts(str1);
}
void str_copy(char s1[], char s2[])
{
int i,j;
for(i=0;s1[i];i++); // s1[i]!=’\0′
for(j=0;s1[i]=s2[j];i++,j++);
// (s1[i]=s2[j])!=’\0′
}
(08110)#include stdio.h
#include string.h
void main()
{
void inputNum(char a[], char n);
void outputNum(char a[], char n);
void bubble(char a[], char n);
char a[11], n;
printf(“請輸入你要排序的數的個數:”);
scanf(“%d”,n);
inputNum(a,n);
outputNum(a,n);
bubble(a,n);
printf(“從小到大排序為:\n”);
outputNum(a,n);
}
void inputNum(char a[], char n)
{
char i;
printf(“請連續輸入%d個字符:”,n);
fflush(stdin); //一般在輸入字符或者字符串之前要清空輸入緩衝區
for(i=1;i=n;i++)
{
scanf(“%c”,a[i]);
}
}
void outputNum(char a[], char n)
{
char i;
for(i=1;i=n;i++)
{
printf(“%2c”,a[i]);
}
printf(“\n”);
}
void bubble(char a[], char n)
{
char i,j,t;
for(i=1;i=n-1;i++)
{
for(j=1; j=n-i; j++)
{
if(a[j]a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
}
}
(08112)#include iostream.h
#include iomanip.h
void main()
{
void inputNum(int a[], int n);
void outputNum(int a[], int n);
void bubble(int a[], int n);
int a[11], n;
cout”請輸入你要排序的數的個數:”;
cinn;
while(n1 || n10)
{
cout”請重新輸入,(1=n=10):”;
cinn;
}
inputNum(a,n);
outputNum(a,n);
bubble(a,n);
cout”從小到大排序為:\n”;
outputNum(a,n);
}
void inputNum(int a[], int n)
{
int i;
for(i=1;i=n;i++)
{
cout”a[“i”]=”;
cina[i];
}
}
void outputNum(int a[], int n)
{
int i;
for(i=1;i=n;i++)
{
coutsetw(6)a[i];
}
coutendl;
}
void bubble(int a[], int n)
{
int i,j,t;
for(i=1;i=n-1;i++)
{
for(j=1; j=n-i; j++)
{
if(a[j]a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
}
}
(08181)#include iostream.h
#include iomanip.h
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void main()
{
int year,month,day;
int days;
int count(int year,int month, int day);
int leap(int y);
cout”請輸入年份:”;
cinyear;
while(year=0)
{
cout”年份不能為負,請重新輸入:”;
cinyear;
}
if(leap(year)) //如果為閏年,2月份的天數為29
{ d[2]=29; }
cout”輸入月份:”;
cinmonth;
while(month1 || month12)
{
cout”月份在1月和12月之間,你的輸入有誤,請重新輸入:”;
cinmonth;
}
cout”輸入日數:”;
cinday;
while(day1 || day d[month])
{
cout”日數應在1和”d[month]”之間,請重新輸入:”endl;
cinday;
}
days=count(year,month,day);
coutyear”年”month”月”day”日是該年的第”days”天\n”;
}
int leap(int y)
{
if(y%4==0 y%100!=0 || y%400==0)
return 1;
else
return 0;
}
int count(int year,int month, int day)
{
int i,days=0;
for(i=1; imonth; i++)
{ days += d[i]; }
days+=day;
return days;
}
C語言程序設計教程答案~有追加懸賞100分!
1 【C語言】《C語言程序設計教程(第二版)》習題答案
說 明
1. 本文所指的《C語言程序設計教程(第二版)》是李鳳霞主編、北京理
工大學出版社出版的,綠皮。
2 第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 chick\n”);
for(i=1;i=20;i++)
for(j=1;j=33;j++)
for(k=1;k=33;k++)
if (i+j+k*3==100i*5+j*3+k==100)
printf(” %d %d %d\n”,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 1 2 3 5 8 13 21 34 55
89 144 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(deltax1e-12);
printf(“%.10f\n”,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(ab){m=a;a=b;b=m;}
if(ac){m=a;a=c;c=m;}
if(bc){m=b;b=c;c=m;}
printf(“%d %d %d\n”,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”);}
執行結果:
42
Yes
3 第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);}
執行結果:
12 34
34 12
4 第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.執行結果:
11
12
1
5 第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(“%d\n”,h*60+m);}
執行結果:
9:23
563
3.源程序:
main()
{int a[]={-10,0,15,34},i;
for(i=0;i=3;i++)
printf(“%d\370C=%g\370F\t”,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.14159265358979,r=5;
printf(“r=%lg A=%.10lf S=%.10lf\n”,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+bca+cbb+ca)
{double s=(a+b+c)/2;
printf(“SS=%.10lf\n”,sqrt(s*(s-a)*(s-b)*(s-c)));}
else printf(“Data error!”);}
執行結果:
4 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=**%d\nd=%g\ne=%6.2f\nf=%-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 %d\n”,a,b,c);}
執行結果:
5 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 %.2f\n”,a,b,c,(a+b+c)/3.);
執行結果:
6 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=%d\n”,a,b,c);
printf(“x=%d,y=%d\n”,x,y);}
6 第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=1x=10||x=200x=210)x1)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=%d\n”,ab?bc?c:b:ac?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×0)y=x;
if (x=0x5)y=x-1;
if (x=5×10)y=x+1;
printf(“%d\n”,y);}
(2)源程序:
main()
{int x,y;
scanf(“%d”,x);
if(x10) if(x-5) if(x=0) if(x=5)y=x+1;
else y=x-1; else y=x;
printf(“%d\n”,y);}
(3)源程序:
main()
{int x,y;
scanf(“%d”,x);
if(x10) if(x=5)y=x+1;
else if(x=0)y=x-1;
else if(x-5)y=x;
printf(“%d\n”,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(“%d\n”,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下)
5.源程序:
main()
{unsigned a,b,c;
printf(“請輸入三個整數:”);
scanf(“%d %d %d”,a,b,c);
if(abca==ba==c)printf(“構成等邊三角形\n”);
else if(a+bca+cbb+ca)
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(x20)y=1;
else switch(x/60)
{case 0:y=x/10;break;
default:y=6;}
printf(“x=%d,y=%d\n”,x,y);}
7.源程序:
main()
{unsigned m;float n;
scanf(“%d”,m);
if(m100)n=0;
else if(m600)n=0.06;
else n=(m/100+0.5)/100;
printf(“%d %.2f %.2f\n”,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 %d\n”,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(“%ld\n”,i);}
執行結果:
6987
8109
(註:要加密的數值不能是0或以0開頭。如果要以0開頭需用字符串而不能是整數。)
7 第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 ②xamin
三、編程題(第145頁)
1. 源程序:
main()
{int i=1,sum=i;
while(i101){sum+=i=-i-2;sum+=i=-i+2;}
printf(“%d\n”,sum);}
執行結果:
51
2.源程序:
main()
{double p=0,n=0,f;int i;
for(i=1;i=10;i++)
{scanf(“%lf”,f);
if (f0)p+=f; else n+=f;}
printf(“%lf %lf %lf\n”,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;ib;i++)c=c*a%1000;
if(c100)printf(“0”);
if(c10)printf(“0”);
printf(“%ld\n”,c);}
執行結果:
129 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=%.14lf\n”,e);}
執行結果:
e=2.71828182845905
(2)源程序:
main()
{double e=1,f=1;int n;
for(n=1;f1e-4;n++){f/=n;e+=f;}
printf(“e=%.4f\n”,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));}
執行結果:
15
1 2 3 6 11 20 37 68
125 230 423 778 1431 2632 4841
8.源程序:
main()
{int i;
for(i=1010;i=9876;i+=2)
if(i/100%11i%100%11i/10%100%11i/1000!=i%10i/1000!=i/10%10i/100%10!=i%10)printf(” %d”,i);}
執行結果:
1024 1026 1028 1032 1034 1036 …… …… 9874 9876
9.源程序:
main()
{int i,j,k;
printf(“apple watermelon pear\n”);
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%9d\n”,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;im;printf(“\n”),i++)
for(j=1;jm;
putchar(N-abs(i-N)=abs(j++-N)?’ ‘:’*’));}
如果把N值改為5,則執行結果如下:
*
***
*****
*******
*********
*******
*****
***
*
作者:寧西貫通 2006-5-7 23:41 回復此發言
——————————————————————————–
8 說明
注意:上面最後一題的輸出結果應該是由星號組成的一個菱形,
9 第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;i26
3. ①break ②i==8
4. ①a[i]b[j] ②i3 ③j5
5. ①b[j]=a[j][0] ②b[j]a[j][k] 6.a[k++]=a[j]
三、編程題(第196頁)
1.源程序:
main()
{int a[4][4],i,j,s=0;
for(i=0;i4;i++)
for(j=0;j4;j++)
scanf(“%d”,a[i][j]);
for(i=0;i4;i++)
for(j=0;j4;j++)
if(i==j||i+j==3)s+=a[i][j];
printf(“%d\n”,s);} /* 注:5×5矩陣不能照此計算! */
執行結果:
1 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(“%d\t”,a[i]);}
執行結果:
2 4 6 8 10 12 14 16 18 20
22 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(ma[i])m=a[i];
printf(“%d\t”,a[i]);}
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(“%d\t”,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++]=i1;
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(i1|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;iM;printf(“\n”),t[i++]=0)
for(j=0;jN;j++)
printf(“%4d”,a[i][j]=random(50));
/*找出每行的最小數,t[M]是第M行的最小數所在的列數*/
for(i=0;iM;i++)
for(j=0;jN;j++)
if(a[i][t[i]]a[i][j])t[i]=j;
/*比較每個最小數在其所在的列上是否也是最小*/
for(j=0;jM;j++)
for(i=0;iM;i++)
{if(i==j)continue;
if(a[j][t[j]]a[i][t[j]])
{t[j]=-1;break;}
}
printf(“——————-\n”);
/*輸出在行和列上均為最小的數*/
for(i=0;iM;i++)
if(t[i]!=-1)
printf(“a[%d,%d]=%d\n”,i,t[i],a[i][t[i]]);
}
執行結果:
13 19 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;iM;i++)
{a[i][N-1]=0;
for(j=0;jN-1;j++)
{printf(“%4d”,a[i][j]=random(91)+10);
a[i][N-1]+=a[i][j];}
printf(“%4d\n”,a[i][N-1]);}
for(i=1;iM;i++)
if(a[i][N-1]a[t][N-1])t=i;
if(t)for(j=0;jN;j++)
{i=a[0][j];a[0][j]=a[t][j];a[t][j]=i;}
printf(“—————–\n”);
for(i=0;iM;printf(“\n”),i++)
10 第7章 數 組
for(j=0;jN;j++)
printf(“%4d”,a[i][j]);
}
執行結果:
89 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
—————–
84 94 97 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;iM;printf(“\n”),i++)
for(j=0;jN;j++)
{printf(“%4d”,a[i][j]=random(100)+1);
if(max.valuea[i][j])
{max.value=a[i][j];max.x=i;max.y=j;}
if(min.valuea[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;iM;printf(“\n”),i++)
for(j=0;jN;j++)
printf(“%4d”,a[i][j]);
}
執行結果:
51 53 74 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
—————–
51 53 74 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?”Yes\n”:”No\n”);}
執行結果:
Input a string:level
Yes
10. 源程序:
main()
{char s[255],t,max=0,min=0,l,i;
printf(“Input a string(length4):”);
gets(s);
l=strlen(s);
for(i=0;il;i++)
{if(s[max]s[i])max=i;if(s[min]s[i])min=i;}
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(“%s\n”,s);}
執行結果:
Input a string(length4):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, %s\n”, 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
11 第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;i999;i++)
if((a=i/100)*a*a+(b=i/10%10)*b*b+(c=i%10)*c*c==i)
printf(“%d\t”,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
1 9 36 84 126 126 84 36 9 1
1 10 45 120 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
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=n3)printOCT(i);
putchar((n7)+48);}
main()
{unsigned long i;
scanf(“%ld”,i);
printOCT(i);}
執行結果:
1234567890
11145401322
本題也可以不用遞歸算法,源程序請參考第7章第三題4。
12 回復:【C語言】《C語言程序設計教程(第二版)》習題答案
但是不同時間印刷的版本課後題不太一樣呢,象我們的是1999年12月第2版,2005年12月第69次印刷的。沒有選擇填空,應用題和樓主不知道有多少相同的,因為看不到原題。這個比較麻煩呢。
作者:210.77.204.* 2006-5-9 18:38 回復此發言
——————————————————————————–
13 回復:【C語言】《C語言程序設計教程(第二版)》習題答案
你對照一下主編和出版社,看看對嗎?(見說明的第一條。)
我不是說叫你有問題另發帖子嗎?
14 第9章 指 針
一、單項選擇題(第276頁)
1-5.DCDAC 6-10.CCABC 11-16.AABBB 16-20.DCDBD
二、填空題(第278頁)
1.①int * ②*z
2.*p++
3.①’\0′ ②++
4.①q=p+1 ②qp+10 ③*qmax ④*qmin
三、編程題(第280頁)
7.源程序:
main()
{int i=0;char c[20];
do{scanf(“%s”,c);i++;}
while(strcmp(c,”stop”));
printf(“%d\n”,i);}
執行結果:
This car ran form Nanyang
to Luoyang without a stop
10
9.源程序:
main()
{char s[255],c[255]={0};int i;
gets(s);
for(i=0;s[i];c[s[i++]]++);
for(i=0;i255;i++)
if(c[i])printf(“%c=%d\t”,i,c[i]);}
執行結果:
abcedabcdcd
a=2 b=2 c=3 d=3 e=1
c語言複習題
以下答案僅作參考,希望通過你的思考後對你有所幫助。
一、
1、函數
2、源程序一般是.c或者.cpp
頭文件是.h
編譯後是.obj
鏈接後是.exe
3、分支結構、循環結構
4、從主函數開始執行,即main()函數開始
5、分隔符為分號 即 ;
6、float
7、把12賦值給變量S
8 基本單位是函數
9、0
10、60
11、stdio.h
12、4
13、16
14、%
15、非0
16、3
17、8
18、8
19、int 、char 、float
20、main()
21、10
22、與運算符:;或運算符:||;非運算符:!
23、getchar(); putchar();
24、輸入與編輯源程序–對源程序進行編譯–與庫函數連接–運行可執行的目標程序
25、分支結構、循環結構
26、邏輯表達式、條件表達式
27、?:
28、文件包含、條件編譯
二、
1、結果:2 因為:if(ab)不成立,所以下面語句都不執行。結果為C的初值,也就是2。
2、結果:K 因為A+10後,也就是到了K
3、a ,97 因為‘a’所對應的整數是97
4、沒有結果輸出。原因兩個條件都不成立。
三、
1、
#include stdio.h
void main()
{ int a,b,c;
printf(“請輸入三個整數:”);
scanf(“%d%d%d”,a,b,c);
if(ab) a=b;
if(ac) a=c;
printf(“最大的數:%d”,a);
}
2、
#include stdio.h
void main()
{ int sum=0,i;
for(i=1;i=50;i++)
sum+=i;
printf(“1—50的和為:%d”,sum);
}
3、
#include stdio.h
void main()
{ char c;
printf(“請輸入一個大寫字母:”);
scanf(“%c”,c);
printf(“\n它的小寫字母為:%c”,c+32);
}
4、
#include stdio.h
void main()
{ float sum=0,temp;
int i=1;
while(i=100)
{temp=1/i;
sum+=temp;
i++;
}
printf(“結果為:%f”,sum);
}
5、
#include stdio.h
void main()
{int a,b;
printf(“請輸入兩個整數:”);
scanf(“%d%d”,a,b);
printf(“大的數是:%d”,ab? a:b);
}
6、
#include stdio.h
void main()
{int i=1;
float sum=0,temp=0;
while(i=99)
{temp=1/i;
sum+=temp;
i=i+2;
}
printf(“結果為:%f”,sum);
}
求c語言程序設計第三版和c語言程序設計實驗與習題指導答案
c語言程序設計第三版指導答案
附錄F 課後題參考答案
習 題 1
1.1 填空題
1.函數
2.主函數main();主函數main() 3.主函數main() 4.函數首部;函數體 5.{;}
6.順序結構;選擇結構;循環結構 7..c;.obj;.exe
1.2 思考題
1.答:結構化程序設計是指,為使程序具有一個合理的結構以保證程序正確性而規定的一套如何進行程序設計的原則。順序結構,選擇結構,循環結構
2.答:算法是對具體問題求解步驟的一種描述。計算機算法的表達工具通常採用以下幾種方法:①用自然語言表示算法;②用流程圖表示算法;③用偽代碼表示算法;④用程序設計語言表示算法。
3.略 4.略 5.略 1.3 編程題 1.答:
#include “stdio.h” main()
{ float a=10, b=20, h=5, s; s=(a+b)*h/2;
printf(“s=%f ” , s ); }
2.答:
#include “stdio.h”
main()
{ printf(“******************************”); printf(“* hello world *”); printf(“******************************”); }
習 題 2
2.1 單選題
DBDCA DCABB CA
2.2 填空題
1.2.000000
2.1;0.500000
3.9;2 4.6 5.100;d 6.(1)20
(2)0
(3)60 7.(1)10;6;4
(2)6;9;15
(3)3;60;83
8.55或 ‘7’
9.x=4;y=6;z=3;m=463
2.3 改錯題(略)
習 題 3
3.1 單選題
BDABC ADCAC BBA
3.2 填空題
1.3 2.261 3.10
4.2, 1;互換a,b的值 5.6.6 6.003 7.7
8.5.0,4,c=3Enter
9.i=10,j=20Enter
10. (1)65 (2)65,A (3)3.14,123.46
(4)3.141600e+000,1.234560e+002 (5)8765.432100 (6)8.765432e+003
11.a=2b=5x=8.8y=76.34c1=65c2=97 12.%d/%d;%d/%d=%.2f\n
3.3 改錯題(略) 3.4 編程題
1.答:
#include “stdio.h” main() {
int x,y;
scanf(“%d%d”,x,y); printf(“\t\tx\ty\n”);
printf(“十進制數\t%d\t%d\n”,x,y); printf(“八進制數\t%o\t%o\n”,x,y); printf(“十六進制數\t%X\t%x\n”,x,y); }
2.答:
#include “stdio.h” main() {
char ch;
printf(“請輸入一個大寫英文字母”); scanf(“%c”,ch);
printf(“大寫英文字母是%c\n”,ch); printf(“它的前導字符是%c\n”,ch-1); printf(“它的後續字符是%c\n”,ch+1); }
3.答:
#include “stdio.h” main() {
int x,a,b,c,y;
printf(“請輸入一個三位整數\n”); scanf(“%d”,x); a=x/100;
b=(x-a*100)/10; c=x%10;
y=c*100+b*10+a;
printf(“反向輸出該整數:%d\n”,y); } }
4.答:
#include “stdio.h” main()
{ int hour;
double salary, salaryday;
scanf(“%d,%lf”, hour, salaryday );
salary=hour*salaryday- hour*salaryday*0.1;
printf(“%8.2lf\n”, salary); }
5.答:
#include “stdio.h” main() {
int a,b,c,t;
printf(“請輸入三個整數\n”); scanf(“%d%d%d”,a,b,c);
printf(“交換前a=%d,b=%d,c=%d\n”,a,b,c); t=a;a=c;c=b;b=t;
printf(“交換後a=%d,b=%d,c=%d\n”,a,b,c); }
習 題 4
4.1 單選題
BADDD ACBBB BA
4.2 填空題
1.1
2.(1)a0 || b0
(2)x0 x=10 (3)a==1.5 b==1.5 c==1.5
(4)pa || pb || pc
3.(1)0 (2)1 (3)1 (4)0 (5)1
4.c=1 5.-4 6.1 7.5, 0, 3 8.5 9.123
10.( cvb= =’y’||cvb= =’Y’)(work=3||college==’y’|| college==’Y’)age=35
4.3 改錯題(略) 4.4 編程題
1.答
#include “stdio.h”
#include “math.h” main() {
double a,b,c,p,area;
scanf(“%lf%lf%lf”,a,b,c);
printf(“三角形的三邊為:%.llf,%.1lf,%.1lf\n”,a,b,c); if (a+bca+cbb+ca) {p=(a+b+c)/2;
area=sqrt(p*(p-a)*(p-b)*(p-c));
printf(“三角形的面積為%.2lf\n”,area); } else
printf(“不能構成三角形\n”); }
2.答:
#include “stdio.h” main()
{ int x,y;
scanf(“%d,%d”,x,y); if(x*x+y*y1000)
printf(“%d\n”,(x*x+y*y)/100); else
printf(“%d\n”,x+y); }
3.答:
#include “stdio.h” #include “math.h” main()
{ double x,y;
scanf(“%lf”,x); if(x-2) y=x*x-sin(x); else if (x=2) y=pow(2,x)+x; else y=sqrt(x*x+x+1);
printf(“x=%.2lf y=%.2lf\n”,x,y); }
4.答:
#include “stdio.h” main( )
{ long ge,shi,qian,wan,x; scanf(“%ld”,x); wan=x/10000;
qian=x%10000/1000; shi=x%100/10;
ge=x%10;
if (ge==wanshi==qian) /*個位等於萬位並且十位等於千位*/ printf(“this number is a huiwen\n”); else
printf(“this number is not a huiwen\n”);
}
5.答:
#include “stdio.h” main()
{ float p,w,s,d,f;
scanf(“%f,%,%f”,p,s,w); if (s3000) d=0.15 else if( s=2000) d=0.1; else if(s=1000) d=0.08; else if(s=500) d=0.05; else if(s=250) d=0.02; else d=0 f=p*w*s*(1-d); printf(“%f”,f); }
6.答:
#include “stdio.h” main()
{ int year,money; char x;
printf(“是否是本公司產品(y/n):”); scanf(“%c”,x);
if(x==’y’||x==’Y’)
{printf(“產品使用的年限:”); scanf(“%d”,year);
if(year=1) money=0;
else if(year8) money=50; else money=100;
printf(“產品保修額是:%d\n”,money);
}
else
{ money=200;
printf(“不是本公司產品,產品保修額是:%d\n”,money); } }
7.答:
#include “stdio.h” main()
{ int money,num1,num2;
printf(“請輸入取款額(≤2000):”); scanf(“%d”,money);
if(money2000) printf(“請輸入取款額數≤2000!\n”); else if(money%50==0) { num1=money/100; num2=(money-num1*100)/50; printf(“需支付100元:%d張\n”,num1); printf(“需支付50元:%d張\n”,num2); } else printf(“輸入錢數必須是50的倍數!\n”); }
習 題 5
5.1 單選題
CDABA ABDDB DBCB
5.2 填空題
1.2 0 2.333
3.(1)i10 或 i=9 (2)j%3!=0
4.(1)flag*(float)k/(k+1) 或1.0*flag*k/(k+1) (2)flag=-flag 5.(1)max=x
(2)x!=-1 (3)scanf(“%d”, x)
6.(1)x=9或x10
(2)y=9-x
5.3 改錯題(略) 5.4 編程題
1.答:
(1)for循環,其他略
#include “stdio.h”
main()
{ int i,s=0;
for(i=1;i=100;i++) s+=i*i;
printf(“%d\n”,s); }
(2)for循環,其他略
#include “stdio.h” main()
{ int i=1,p=1; double s=1; do {
s+=1.0/p; p*=++i;
}while(1.0/p1e-6); printf(“%lf”,s); }
2.答:
#include “stdio.h” main()
{ int m,n,t,a,b;
scanf(“%d,%d” ,m,n) if (mn)
{ t=m m=n n=t } a=m; b=n; t=m%n while(t)
{ m=n n=t t=m%n;} printf(“%d”,n); }
3.答:
#include “stdio.h” main()
{ int x,y,s=1;
scanf(“%d,%d”,x,y) for( y0 y–)s*=x
printf(“%d,%d,%d\n “,s%10,s/10%10,s/100%10); }
4.答:
#include “stdio.h” main()
{ int x,y,z;
for( x=1 x20 x++) for( y=1 y33 y++) { z=100-x-y
if ((z%3)==0 (5*x+3*y+z/3)==100) printf(“x=%d,y=%d,z=%d\n”,x,y,z) } }
5.答: (a)
#include “stdio.h” main()
{ int j,k
for( j=1 j=4 j++)
{ for(k=1;k=4-j;k++)printf(” “); printf(“****”) printf(“\n”) } }
(b)
#include “stdio.h” main()
{ int j,k
for( j=1 j=4 j++)
{for(k=1;k=4-j;k++)printf(” “); for(k=1 k=2*j-1 k++) printf(“*”) printf(“\n”) } }
6.答:
程序分析:利用for循環控制在100~999之間,對每個數分解出個位、十位、百位。
#include stdio.h main() { int i,j,k,n; printf(“water flower’number is:”); for(n=100;n1000;n++) { i=n/100;/*分解出百位*/ j=n/10%10;/*分解出十位*/ k=n%10;/*分解出個位*/ if(n==i*i*i+j*j*j+k*k*k) { printf(“%-5d”,n); } } printf(“\n”); }
7.答:
#include stdio.h main() { int x; for(x=1000;x=3;x–) if(x%3==1x%5==2x%7==3) {
printf(“該校的學生人數是:%d人\n”,x); break; } }
8.答:
#include stdio.h main() { int x=12,i=1; while(1)
{ if((x+20+i)==2*(x+i)) break; i++; } printf(“小明母親在%d年後比小明的年齡大一倍\n”,i); printf(“那時小明年齡是:%d歲,小明母親年齡是:%d歲\n”,x+i,x+20+i); }
習 題 6
6.1 單選題
DBCCB BDC
C語言程序設計教程(第3版)
278
6.2 填空題
1.c 2.60 3.1000 10 4.16
6.3 編程題
1.答:
#include “stdio.h” #include “math.h”
#define F(a) a*a+ sqrt(3*a*a+2*a+1) main()
{ float x, f;
scanf(“%f”, x );
f=4.5/F(exp(x))+F(cos(x))+F(sqrt(x))/F(x*x) printf(“%f\n”, f); }
習 題 7
7.1 單選題
BCADA CCCDA BCBDB
7.2 填空題
1.(1)2 3 4 5 (2)10010 (3)QuickC
(4)10000 01000 00100 00010 00001 (5)Language
(6)Language Programming 2.(1)j+=2 (2)a[i]a[j] 3.(1)i=1 (2)x[i-1]
7.3 改錯題(略) 7.4 編程題
1.答:
#define N 10
#include “stdio.h” main()
{ int a[N]={1,2,3,4,5,6,7,8,9,10,osum=0, qsum=0,j; for(j=0;j10;j++)
if( j%2) qsum+=a[j];
else osum+=a[j];
printf(“osum=%d,qsum=%d\n”, osum,qsum); }
2.答:
#define N 10
#include “stdio.h” main()
{ int a[N]={10,20,30,40,50,60,70,80,90}, j, k, x; scanf(“%d”,x); for(j=0;jN;j++)
if (xa[j]) break; if(j==N) a[N-1]=x; else
{for(k=N-1; kj; k–) a[k]=a[k-1]; a[j]=x;}
for(j=0;jN;j++)
printf(“%d “,a[j]); }
3.答:
#define M 3
#include “stdio.h” main()
{ int a[M][M]={{1,2,3},{2,4,5},{3,5,6}},j,k,flag=1;; for( j=0;jM;j++)
for(k=0;kM;k++) if (a[j][k]!=a[k][j]) { flag=0; break;} if (flag) printf(“ok”); else printf(“NO”); }
4.答:
#include “stdio.h” #include “string.h” main()
{ char c1[10],c2[10],j; gets(c1); gets(c2);
for(j=0; (c1[j]==c2[j]) c1[j] c2[j]; j++); if (c1[j]c2[j]) printf(“%d\n”,1); if (c1[j]c2[j]) printf(“%d\n”,-1); if (c1[j]==c2[j]) printf(“%d\n”,0); }
5.答:
#include “stdio.h” #include “string.h” #define M 3 #define N 80 main()
{ char a[M][N],j,k,n[5]={0}; for(j=0;jM;j++) gets(a[j]);
for(j=0;jM;j++)
for(k=0;a[j][k];k++)
if( a[j][k]=’A’ a[j][k]=’Z’) n[0]++;
else if (a[j][k]=’a’ a[j][k]=’z’) n[1]++; else if (a[j][k]=’0′ a[j][k]=’9′) n[2]++; else if (a[j][k]==’ ‘ ) n[3]++; else n[4]++;
for(j=0;j5;j++) printf(“%4d”, n[j]); }
習 題 8
8.1 單選題
DBDAC BACCC
8.2 填空題
1.(1)2, 1 (2)10#30# (3)FOUR, P (4)60
2.(1)49
(2)2
(3)2
(4)
(5)
8.3 改錯題(略) 8.4 編程題
1.答:
#include “stdio.h”
main()
{ int n1,n2,n3,t; int *p1,*p2,*p3;
printf(“please input 3 number:n1,n2,n3:”); scanf(“%d,%d,%d”,n1,n2,n3); p1=n1;
p2=n2; p3=n3;
if(*p1* p2) { t=*p1;*p1=*p2;*p2=t;}
if(*p1*p3) { t=*p1;*p1=*p3;*p3=t;} if(*p2*p3) { t=*p2;*p2=*p3;*p3=t;}
printf(“the sorted numbers are:%d,%d,%d\n”,n1,n2,n3); }
2.答:
#include “stdio.h” #define N 3 main()
{ int a[N],*p=a; for(;p-aN; p++) scanf(“%d”,p); p=a+N-1;
for(;p-a=0; p–) printf(“%d “,*p); }
3.答:
#include “stdio.h” main()
{ int a[10];
int j,minl=0,maxl=0; for(j=0;j10;j++)
scanf(“%d”, a+j); for(j=0;j10;j++)
{ if(a[maxl]*(a+j)) maxl=j; if(a[minl]*(a+j)) minl=j; }
j=a[0]; a[0]=a[minl];a[minl]=j; j=a[9];a[9]=a[maxl];a[maxl]=j; for(j=0;j10;j++) printf(“%d “, *(a+j)); }
4.答:
輸入陣列如下: 1 2 3 4 5 6 7 8 9 10 11 12 輸出陣列如下:
12 11 10 9 8 7 6 5 4 3 2 1
#define M 3
#define N 4
#include “stdio.h” main()
{ int a[M][N]={1,2,3,4,5,6,7,8,9,10,11,12},k,j,*p=a[0][0],t; for(k=0,j=M*N-1;kj;k++,j–)
{ t=*(p+k); *(p+k)=*(p+j); *(p+j)=t;} for (k=0 kM k++) { for(j=0 jN j++)
printf(“%4d “,a[k][j]); printf(“\n”);
} }
5.答:
#include “stdio.h” main() {
int len;
char str[20],*p=str;
printf(“please input a string:\n”); scanf(“%s”,str); len=0;
while(*p!=’\0′) {
len++; p++; }
printf(“the string has %d characters.\n”,len); }
6.答:
#include “string.h” #include “stdio.h” main() {
char *str1[5],ch[5][20],k,t,j,*c; void sort(char **); for(k=0;k5;k++) {str1[k]=ch[k]; gets(str1[k]);} for(k=0;k7;k++)
{ t=k;
for(j=k+1;j5;j++) if(strcmp(*(str1+t),*(str1+j))0) t=j; c=*(str1+t);
*(str1+t)=*(str1+k) *(str1+k)=c }
for(k=0;k5;k++) puts(str1[k]); }
習 題 9
9.1 單選題
CBBAD DBCCD DCABC BCCBA DCDAB
9.2 填空題
1.120 2.x 3.3,2,2,3 4.fac /i 5.8,17 6.9 7.1.0/(i*i) 8.
fun-in:30,20,10 fun-end:1015,35,1050 10,20,30 9.012345 10.93636 11.(1)r+b[k] (2)*x
12.7 5 3 1 9 13.15
14.(1)*x (2)t 15.(1)’\0′ (2)n++ 16.024
9.3 改錯題(略) 9.4 編程題
1.答:
void zhuan( )
{ char ch;
while((ch=getchar())!=’\n’)
{ if(ch=’a’ ch=’z’) ch=ch-32; putchar(ch); } }
2.答:
double expp(int n) { int k, fac=1; double sum=1;
for(k=1; k=n; k++) { fac*=k;
sum+=1.0/fac }
return(sum); }
3.答:
int xy3( int x, int y)
{ int k, num=1;
for(k=1;k=y k++) num*=x
num=num%1000 return num }
4.答:
int age( int n) { int c;
if(n==1) c=10
else c=age(n-1)+2 return c }
5.答:
#include “stdio.h”
main()
{ int a,b,c,d;
void fun(int a,int b,int *c, int *d); scanf(“%d%d”,a,b); fun(a,b,c,d);
printf(“%d %d”,c,d);
}
void fun(int a,int b,int *c, int *d) { if (b)
{ *c=a/b; *d=a%b;} }
6.答:
#include “stdio.h”
main(int argc,char *argv[]) { int k;
printf(“argc=%d\n”,argc); for (k=1;kargc; k++) printf(“%s\n”,argv[k]); }
習 題 10
10.1 單選題
CDBBB BBBAD CCBDC
10.2 填空題
1.所有結構體成員所佔存儲空間的總和 2.與佔用存儲空間最大的那個成員相等
附錄F 課後題參考答案
285
3.(1)struct satype (2)3 (3)sa.a (4)9 (5)psa=sa 4.80 5.struct node 6.0
10.3 編程題
1.答:
#include “stdio.h”
struct student {
long num;
char name[20]; char sex; float score; }; main()
{ struct student s[20], temp; int j,k, man=0, woman=0;
float summan=0,sumwoman=0, aveman, avewoman; for(k=0; k20; k++)
{ scanf("%ld %s %c%f",s[k].num,s[k].name,s[k].sex,s[k].score); if(s[k].sex==’m’)
{ summan+=s[k].score; man++;} else
{ sumwoman+=s[k].score;woman++ } }
aveman=summan/man;
avewoman=sumwoman/woman;
printf("%d\t%f\t%d\t%f\n",man,aveman,woman,avewoman); for(k=0; k19; k++)
for(j=0;j20-k;j++)
if(s[j].scores[j+1].score)
{ temp=s[j];s[j]=s[j+1];s[j+1]=temp;} printf("the sorted numbers:\n"); for(k=0;k20;k++)
printf("%ld\t%s\t%c\t%5.1f\n",s[k].num,s[k].name,s[k].sex,s[k].score); }
習 題 11
11.1 單選題
BADD
11.2 填空題
1.3d3d330 2.(1)28
(2)20 (3)0 (4)–9
3.(1)251
(2)42
(3)209
(4)–295 (5)848
習 題 12
12.1 單選題
BCDCA ADA
12.2 填空題
1.rewind(文件指針) 2.”d1.dat”,”rb” 3.stdin
4.文本文件;二進制文件 5.(1)”w”
(2)str[i]–32
(3)”r”
6.fopen 7.Hello 8.(1)”r”
(2)fgetc(fp)
(3)time++
C語言程序設計實驗與習題指導課後程序設計答案
P18
(1)
#includestdio.h int main(void) { intcelsius,fahr; fahr=150; celsius=5*fahr/9-5*32/9; printf(“fahr=%d,celsius=%d\n”,fahr,celsius); return 0; }
(2)
#includestdio.h int main(void) { intcelsius,fahr; celsius=26; fahr=9*celsius/5+32; printf(“celsius=%d,fahr=%d\n”,celsius,fahr); return 0; }
(3)
#includestdio.h int main(void) { intaverage,math,eng,comp; math=87; eng=72; comp=93; average=(math+eng+comp)/3; printf(“math=%d,eng=%d,comp=%d,average=%d\n”,math,eng,comp,average); return 0; }
(4)
#includestdio.h int main(void) { intn,a,b,c; n=152
c=n%10;
b=(n/10)%10; a=n/100;
printf(“整數%d的個位數字是%d,十位數字是%d,百位數字是%d\n”,n,c,b,a); return 0;
}
P27
(1)
#includestdio.h #includemath.h int main(void) { intcelsius,fahr; printf(“Enter celsius:”); scanf(“%d”,celsius); fahr=9*celsius/5+32; printf(“fahr%d\n”,fahr); return 0; }
(2)
#includestdio.h #includemath.h int main(void) { intmoney,year; doublerate,interest; printf(“Enter money,year,rate:”); scanf(“%d%d%lf”,money,year,rate); interest=money*pow(1+rate,year)-money; printf(“interest=%.2f\n”,interest); return 0; }
(3)
#includestdio.h #includemath.h int main(void) { doublex,y; printf(“Enter x:”); scanf(“%lf”,x);
if(x0){ y=pow(x+1,2)+2*x+1/x; } else{ y=sqrt(x); }
printf(“y=f(%f)=%.2f\n”,x,y); return 0;
}
(4)
#includestdio.h int main(void) { intx,y; printf(“Enter num1:”); scanf(“%d”,x); printf(“Enter num2:”); scanf(“%d”,y); printf(“%d+%d=%d\n”,x,y,x+y); printf(“%d-%d=%d\n”,x,y,x-y); printf(“%d*%d=%d\n”,x,y,x*y); printf(“%d/%d=%d\n”,x,y,x/y); printf(“%d%%%d=%d\n”,x,y,x%y); return 0; }
10的階乘
#includestdio.h int main(void) { inti,n,product; printf(“Enter n:”); scanf(“%d”,n); product=1; for(i=1;i=n;i++){ product=product*i; } printf(“product=%d\n”,product); return 0; }
原創文章,作者:RUYV,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/131247.html