南昌大學c語言程序設計第六章循環控制答案
A. 南昌大學c語言期末考試有題庫嗎
沒有題庫的, 不過每年都是類似的題目,考察的知識點都一樣.
B. 譚浩強著C語言第二版清華大學出版社第六章習題答案
||第五章 循環控制
5.1
main()
{int a,b,num1,num2,temp;
scanf("%d,%d",&num1,&num2);
if(num1<num2){temp=num1;num1=num2;num2=temp;}
a=num1;b=num2;
while(b!=0)
{temp=a%b;
a=b;
b=temp;}
printf("%d\n",a);
printf("%d\n",num1*num2/a);
}
5.2
#include"stdio.h"
main()
{char c;
int letters=0,space=0,digit=0,other=0;
while((c=getchar())!='\n')
{if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++;
else if(c==' ')space++;
else if(c>='0'&&c<='9')digit++;
else other++;
}
printf("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other);
}
5.3
main()
{int a,n,count=1,sn=0,tn=0; scanf("%d,%d",&a,&n);
while(count<=n)
{tn+=a;
sn+=tn;
a*=10;
++count;
}
printf("a+aa+aaa+...=%d\n",sn);
}
5.4
main()
{float n,s=0,t=1;
for(n=1;n<=20;n++)
{t*=n;
s+=t;
}
printf("s=%e\n",s);
}
5.5
main()
{int N1=100,N2=50,N3=10;
float k;
float s1=0,s2=0,s3=0;
for(k=1;k<=N1;k++)s1+=k;
for(k=1;k<=N2;k++)s2+=k*k;
for(k=1;k<=N3;k++)s3+=1/k;
printf("s=%8.2f\n",s1+s2+s3);
}
5.6
main()
{int i,j,k,n;
for(n=100;n<1000;n++)
{i=n/100;
j=n/10-i*10;
k=n%10;
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
printf("n=%d\n",n);
}
}
5.7
#define M 1000
main()
{int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;
int i,j,n,s; for(j=2;j<=M;j++)
{n=0;
s=j;
for(i=1;i<j;i++)
{if((j%i)==0)
{n++;
s=s-i;
switch(n)
{case 1:k0=i;break;
case 2:k1=i;break;
case 3:k2=i;break;
case 4:k3=i;break;
case 5:k4=i;break;
case 6:k5=i;break;
case 7:k6=i;break;
case 8:k7=i;break;
case 9:k8=i;break;
case 10:k9=i;break;
}
}
}
if(s==0)
{printf("j=%d\n",j);
if(n>1)printf("%d,%d",k0,k1);
if(n>2)printf(",%d",k2);
if(n>3)printf(",%d",k3);
if(n>4)printf(",%d",k4);
if(n>5)printf(",%d",k5);
if(n>6)printf(",%d",k6);
if(n>7)printf(",%d",k7);
if(n>8)printf(",%d",k8);
if(n>9)printf(",%d\n",k9);
}
}
}
main()
{static int k[10];
int i,j,n,s;
for(j=2;j<=1000;j++)
{n=-1;
s=j;
for(i=1;i<j;i++)
{if((j%i)==0)
{n++; s=s-i;
k[n]=i;
}
}
if(s==0)
{printf("j=%d\n",j);
for(i=0;i<n;i++)
printf("%d,",k);
printf("%d\n",k[n]);
}
}
}
5.8
main()
{int n,t,number=20;
float a=2;b=1;s=0;
for(n=1;n<=number;n++)
{s=s+a/b;
t=a,a=a+b,b=t;
}
printf("s=%9.6f\n",s);
}
5.9
main()
{float sn=100.0,hn=sn/2;
int n;
for(n=2;n<=10;n++)
{sn=sn+2*hn;
hn=hn/2;
}
printf("sn=%f\n",sn);
printf("hn=%f\n",hn);
}
5.10
main()
{int day,x1,x2;
day=9;
x2=1;
while(day>0)
{x1=(x2+1)*2;
x2=x1;
day--;
}
printf("x1=%d\n",x1); }
5.11
#include"math.h"
main()
{float a,xn0,xn1;
scanf("%f",&a);
xn0=a/2;
xn1=(xn0+a/xn0)/2;
do
{xn0=xn1;
xn1=(xn0+a/xn0)/2;
}
while(fabs(xn0-xn1)>=1e-5);
printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1);
}
5.12
#include"math.h"
main()
{float x,x0,f,f1;
x=1.5;
do
{x0=x;
f=((2*x0-4)*x0+3)*x0-6;
f1=(6*x0-8)*x0+3;
x=x0-f/f1;
}
while(fabs(x-x0)>=1e-5);
printf("x=%6.2f\n",x);
}
5.13
#include"math.h"
main()
{float x0,x1,x2,fx0,fx1,fx2;
do
{scanf("%f,%f",&x1,&x2);
fx1=x1*((2*x1-4)*x1+3)-6;
fx2=x2*((2*x2-4)*x2+3)-6;
}
while(fx1*fx2>0);
do
{x0=(x1+x2)/2;
fx0=x0*((2*x0-4)*x0+3)-6;
if((fx0*fx1)<0)
{x2=x0; fx2=fx0;
}
else
{x1=x0;
fx1=fx0;
}
}
while(fabs(fx0)>=1e-5);
printf("x0=%6.2f\n",x0);
}
5.14
main()
{int i,j,k;
for(i=0;i<=3;i++)
{for(j=0;j<=2-i;j++)
printf(" ");
for(k=0;k<=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i<=2;i++)
{for(j=0;j<=i;j++)
printf(" ");
for(k=0;k<=4-2*i;k++)
printf("*");
printf("\n");
}
}
5.15
main()
{char i,j,k;
for(i='x';i<='z';i++)
for(j='x';j<='z';j++)
{if(i!=j)
for(k='x';k<='z';k++)
{if(i!=k&&j!=k)
{if(i!='x'&&k!='x'&&k!='z')
printf("\na--%c\tb--%c\tc--%c\n",i,j,k);
}
}
}
}
第六章 數組
6.1 #include <stdio.h>
#include <math.h>
#define N 101
void main()
{ int i,j,line,a[N];
for (i=2;i<N;i++) a[i]=i;
for (i=2;i<sqrt((double)N);i++)
for (j=i+1;j<N;j++)
{if(a[i]!=0 && a[j]!=0)
if (a[j]%a[i]==0)
a[j]=0; }
printf("\n");
for (i=2,line=0;i<N;i++)
{ if(a[i]!=0)
{ printf("%5d ",a[i]);
line++; }
if(line==10)
{ printf("\n");
line=0; }
}
}
6.2
#define N 10
main()
{int i,j,min,temp,a[N];
for(i=0;i<N;i++)
scanf("%d",&a);
for(i=0;i<N-1;i++)
{min=i;
for(j=i+1;j<N;j++)
if(a[min]>a[j])min=j;
temp=a;
a=a[min];
a[min]=temp;
}
for(i=0;i<N;i++)
printf("%5d",a);
}
6.3
main()
{float a[3][3],sum;
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++) {scanf("%f",&sum);
a[j]=sum;
}
for(i=0;i<3;i++)
sum=sum+a;
printf("sum=%f",sum);
}
6.4
main()
{int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number, end,i,j;
scanf("%d",&number);
end=a[9];
if(number>end) a[10]=number;
else
{for(i=0;i<10;i++)
{if(a>number)
{temp1=a;
a=number;
for(j=i+1;j<11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;i<11;i++)
printf("%6d",a);
}
6.5
#define N 5
main()
{int a[N]={8,6,5,4,1},i,temp;
for(i=0;i<N/2;i++)
{temp=a;
a=a[N-i-1];
a[N-i-1]=temp;
}
for(i=0;i<N;i++)
printf("%4d",a);
}
6.6 #define N 11
void main()
{int i,j,a[N][N]={0};
for(i=1;i<N;i++)
{a[i][1]=1;
a[i][i]=1;
}
for(i=3;i<N;i++)
for(j=2;j<i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=1;i<N;i++)
{for(j=1;j<=i;j++)
printf("%6d",a[i][j]);
printf("\n");
}
}
6.7
main()
{int a[16][16],i,j,k,p,m,n;
p=1;
while(p==1)
{scanf("%d",&n);
if((n!=0)&&(n<=15)&&(n%2!=0))p=0;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[j]=0;
j=n/2+1;
a[1][j]=1;
for(k=2;k<=n*n;k++)
{i=i-1;
j=j+1;
if((i<1)&&(j>n))
{i=i+2;
j=j-1;
}
else
{if(i<1)i=n;
if(j>n)j=1;
}
if(a[j]==0)a[j]=k;
else
{i=i+2;
j=j-1; a[j]=k;
}
}
for(i=1;i<=n;i++)
{for(j=1;j<=n;j++)
printf("%3d",a[j]);
printf("\n");
}
}
6.8
#define N 10
#define M 10
main()
{int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj;
scanf("%d,%d",&n,&m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[j]);
flag2=0;
for(i=0;i<n;i++)
{max=a[0];
for(j=0;j<m;j++)
if(max<a[j])
{max=a[j];
maxj=j;
}
for(k=0,flag1=1;k<n&&flag1;k++)
if(max>a[k][maxj])flag1=0;
if(flag1)
{ printf("\na[%d][%d]=%d\n",i,maxj,max);
flag2=1;
}
}
if(!flag2) printf("NOT");
}
6.9
#include<stdio.h>
#define N 15
main()
{int i,j,number,top,bott,min,loca,a[N],flag;
char c;
for(i=0;i<=N;i++)
scanf("%d",&a);
flag=1; while(flag)
{scanf("%d",&number);
loca=0;
top=0;
bott=N-1;
if((number<a[0])||(number>a[N-1]))
loca=-1;
while((loca==0)&&(top<=bott))
{min=(bott+top)/2;
if(number==a[min])
{loca=min;
printf("number=%d,loca=%d\n",number,loca+1);
}
else if(number<a[min])
bott=min-1;
else
top=min+1;
}
if(loca==0||loca==-1)
printf("%d not in table\n",number);
printf("continue Y/N or y/n\n");
c=getchar();
if(c=='N'||c=='n')flag=0;
}
}
6.10
main()
{int i,j,uppn,lown,dign,span,othn;
char text[3][80];
uppn=lown=dign=span=othn=0;
for(i=0;i<3;i++)
{gets(text);
for(j=0;j<80&&text[j]!='\0';j++)
{if(text[j]>='A'&&text[j]<='Z')
uppn++;
else if(text[j]>='a'&&text[j]<='z')
lown++;
else if(text[j]>='0'&&text[j]<='9')
dign++;
else if(text[j]==' ')
span++;
else
othn++;
} }
for(i=0;i<3;i++)
printf("%s\n",text);
printf("uppn=%d\n",uppn);
printf("lown=%d\n",lown);
printf("dign=%d\n",dign);
printf("span=%d\n",span);
printf("othn=%d\n",othn);
}
6.11
main()
{static char a[5]={'*','*','*','*','*'};
int i,j,k;
char space=' ';
for(i=0;i<=5;i++)
{printf("\n");
for(j=1;j<=3*i;j++)
printf("%1c",space);
for(k=0;k<=5;k++)
printf("%3c",a[k]);
}
}
6.12
#include<stdio.h>
main()
{int i,n;
char ch[80],tran[80];
gets(ch);
i=0;
while(ch!='\0')
{if((ch>='A')&&(ch<='Z'))
tran=26+64-ch+1+64;
else if((ch>='a')&&(ch<='z'))
tran=26+96-ch+1+96;
else
tran=ch;
i++;
}
n=i;
for(i=0;i<n;i++)
putchar(tran);
}
6.13
main() {char s1[80],s2[40];
int i=0,j=0;
scanf("%s",s1);
scanf("%s",s2);
while(s1!='\0')i++;
while(s2[j]!='\0')s1[i++]=s2[j++];
s1='\0';
printf("s=%s\n",s1);
}
6.14
#include<stdio.h>
main()
{int i,resu;
char s1[100],s2[100];
gets(s1);
gets(s2);
i=0;
while((s1==s2)&&(s1!='\0'))i++;
if(s1=='\0'&&s2=='\0')resu=0;
else
resu=s1-s2;
printf("s1=%s,s2=%s,resu=%d\n",s1,s2,resu);
}
6.15
#include"stdio.h"
main()
{char from[80],to[80];;
int i;
scanf("%s",from);
for(i=0;i<=strlen(from);i++)
to=from;
printf("%s\n",to);
}
C. 求C語言程序設計實例教程課後習題答案,人民郵電出版社,主編管銀枝,胡銀輝
答案沒有,不過我有視頻,不知道你需要不,因為我是自學,我現在專就在看,感覺還不屬錯。一聽就能聽懂的那種,感覺比較適合我這樣入門剛學的人。最吸引的地方就是講的難理解的概念講的特別好,用形象比喻舉例講概念,比如變數比喻成裝東西的盒子,我一下子就理解了變數賦值為什麼是從右往左。還有很多這樣類似的比喻。比我之前看的什麼郝斌曾怡金文的晦澀難懂的都好多了。
D. c語言 譚浩強的《c程序設計》第六章,6.9節程序距離的例6.10
它是讓你在循環的時候把字元輸入輸入流裡面,getchar()的確是只能就收一個字元,但是如回果你沒有按下回答車鍵,就不會把輸入流裡面的內容送入內存,當且僅當你回車時,程序才強制將輸入流的內容送入內存,系統內存就會從輸入流逐個讀字元並輸出一個個字元。
E. 這是我們的課堂作業,是C語言程序設計第六章 函數 方面的題目,有誰會解的請解一下吧,十分感謝了!!!
這個其實也沒什麼好去分析的,按照題目的要求去做就可以了。
比如說這個題目,題干已經很明確的告訴你要去你寫一個函數完成兩個字元串的連接(不使用庫函數strcat),即把p2所指的字元串連接到p1所指的字元串後。
那麼你按要求寫就好了
給個例子
void fun(char p1[], char p2[])
{
int i=0,j=0;
while(p1[i]!='\0')
{
i++;
}/*因為是p2所指的字元串連接到p1所指的字元串後。所以找到p1字元串的結尾*/
while(p2[j]!='\0')
{
p1[i]=p2[j];
i++;
j++;
}/*從p1字元串結尾開始依次用p2的字元填充直到p2中的內容為'\0'*/
p1[i]='\0';
/*要注意給p1[]字元串結束標志,這個地方很容易忘記。因為當p2[j]是'\0'時已經退出循環了,所以p1[]是沒有結束標志的,可能會導致程序崩潰哦。*/
}
2級C是很基礎的 考來考去就那麼幾個上機題 說實話 你那幾種題型一樣寫幾個就OK了 呵呵 但是要細心 2級C考的就是你細不細心的問題 加油 祝你考試順利
I never think of the future. It comes soon enough.
F. c語言教程習題答案
第一章
1.1 EXE
1.2 C OBJ EXE
1.3 順序 選擇 循環
第二章
一. 選擇題
2.1 B 2.2 D 2.3 B 2.4 A 2.5 C 2.6 A 2.7 B
2.8 B 2.9 D 2.10 C 2.11 B 2.12 B 2.13 A
二. 填空題
2.14 11 12
2.15 4.2 4.2
2.16 { } 定義 執行語句
2.17 關鍵字 用戶標識符
2.18 int float double
2.19 float a1=1; float a2=1;
2.20 存儲單元
2.21 3.5
2.22 (a*b)/c a*b/c a/c*b
2.23 把常量10賦給變數s
2.24 位 1或0
2.25 8 127 0111111 -128 10000000
2.26 32767 -32768 1000000000000000
2.27 10 8 16
三. 上機改錯題
2.28
#include "stdio.h"; 刪除行尾的";"
main(); / * main function * / 刪除")"後的";",注釋中的*要緊靠「/」,即應為「/*」和「*/」
函數開始處遺失了一個「{」
float r,s ; /*/*r is radius*/,/* s is area of circuilar*/*/ 注釋符號不可嵌套使用
r = 5.0 ;
s = 3.14159 * r * r ;
printf("%f\n",s) 行尾遺失了「;」
函數結束處遺失了一個「}」
2.29
#include "stdio.h"
main /* main function */ main後遺失了「()」
{
float a,b,c,v; /*a,b,c are sides, v is volume of cube */
a=2.0; b=3.0; c=4.0 行尾遺失了「;」
v=a*b*c;
printf("%f\n", v) 行尾遺失了「;」
}
第三章
一. 選擇題
3.1 C 3.2 C 3.3 D 3.4 C 3.5 D 3.6 B 3.7 C 3.8 D 3.9 A 3.10 B
3.11 C 3.12 D 3.13 D 3.14 A 3.15 C 3.16 C 3.17 C 3.18 無答案 3.19 C 3.20 B
二. 填空題
3.21 (1)-2002500(2)i=-200,j=2500
(3)i=-200
j=2500
3.22 12 0 0
3.23 一條語句 ;
3.24 ;
3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.89234
3.26 x=127,x= 127,x= 177,x= 7f,x= 127
3.27 x=127,x=127 ,x=$127 ,x=$000127,x=d
3.28 a=513.789215,a= 513.79,a= 513.78921500,a= 513.78921500
三. 編程題和改錯題
3.29 修改後的程序如下:
main()
{
double a,b,c,s,v;
printf("input a,b,c:");
scanf("%lf%lf%lf",&a,&b,&c);
s =a*b;
v=a*b*c;
printf("a=%f,b=%f,c=%f\n", a,b,c);
printf("s=%f,v=%f\n",s,v);
}
3.30
#include
main()
{
int a=560,b=60;
printf("560 minute is %d hour and %d minute.\n",a/b,a%b);
}
3.31
#include
main()
{
int a,b;
a=1500;b=350;
printf("a div b is : %d\n",a/b);
printf("a mod b is : %d\n",a%b);
}
3.32
#include
main()
{
double a,b,c,ave;
printf ("input 3 double number : \n");
scanf ("%lf%lf%lf",&a,&b,&c);
printf ("%.1f\n",(a b c)/3);
}
3.33
#include
void main()
{
int a,b,c,t;
printf("請依次輸入整數a,b,c:");
scanf("%d%d%d",&a,&b,&c);
printf("\n你輸入的值是: a=%d,b=%d,c=%d\n",a,b,c);
t=b;b=a;a=c;c=t;
printf("交換之後的值是:a=%d,b=%d,c=%d\n",a,b,c);
}
第四章
一. 選擇題
4.1 A 4.2 A 4.3 A 4.4 D 4.5 C 4.6 A 4.7 B 4.8 C 4.9 D 4.10 C
二. 填空題
4.11 非0 0
4.12 < > >= <=同級 == !=同級
4.13 ! && ||
4.15 !
4.16 a == b || a < c x > 4 || x < -4
4.17 1
4.18 x <= 0 1 > 0
4.19 3 2 2
4.20 *#
三. 編程題
4.21 略
4.22
#include
/* 檢查日期的合法性 */
int checkdate(int year, int month, int day)
{
if(year < 1900 || year > 2005)
{
printf("輸入的年份無效!\n");
return 0;
}
else if(month < 0 && month > 12)
{
printf("輸入的月份無效!\n");
return 0;
}
else if(day <= 0 && day > 31)
{
printf("輸入的日期無效!\n");
return 0;
}
else
{
switch(month)
{
case 4:
case 6:
case 9:
case 11:
if(day > 30)
{
printf("輸入的日期無效!\n");
return 0;
}
break;
case 2:
if((year%4 == 0 && year0 != 0) || year@0 == 0)
{
if(day > 29)
{
printf("輸入的日期無效!\n");
return 0;
}
}
else
{
if(day > 28)
{
printf("輸入的出生日期無效!\n");
return 0;
}
}
break;
}/* end of switch(m0)*/
}
return 1;
}
void main()
{
int y0, m0, d0; /* 生日 */
int y1, m1, d1; /* 當前日期 */
int years, months, days; /* 實足年齡*/
printf("請輸入學生的生日:");
scanf("%d%d%d", &y0,&m0,&d0);
if(checkdate(y0, m0, d0))
{
printf("請輸入當前日期:");
scanf("%d%d%d", &y1,&m1,&d1);
/*當前日期合法性檢查*/
if(!checkdate(y1, m1, d1))
{
return;
}
else if(y0 > y1)
{
printf("出生年份比當前年份晚!\n");
return;
}
else if(y0 == y1)
{
if(m0 > m1)
{
printf("出生年月比當前年月晚!\n");
return;
}
else if(m0 == m1)
{
if(d0 > d1)
{
printf("出生年月日比當前年月日晚!\n");
return;
}
}
}
}
/* 計算實足年齡 */
years = y1 - y0;
months = m1 - m0;
days = d1 - d0;
/* 修正實足年齡天數*/
if(days < 0)
{
months--;
switch(m1)
{
case 1:
case 5:
case 7:
case 10:
case 12:
days = 30;
break;
case 2:
case 4:
case 6:
case 8:
case 9:
case 11:
days = 31;
break;
case 3:
if((y1%4 == 0 && y10 != 0) || y1@0 == 0)
{
days = 29;
}
else
{
days = 28;
}
break;
}/* end of switch(m1) */
}/* end of if(days < 0) */
/* 修正實足年齡月數*/
if(months < 0)
{
months = 12;
years--;
}/* end of if(months < 0) */
printf("出生日期: %d年%d月%d日\n", y0, m0, d0);
printf("當前日期: %d年%d月%d日\n", y1, m1, d1);
printf("實足年齡: %d年%d月%d日\n", years, months, days);
return;
}
4.23
#include
void main()
{
int a;
printf ("請輸入一個整數 :");
scanf ("%d",&a);
if (a%2==0)
{
printf ("%d 是偶數\n", a);
}
else
{
printf ("%d 是奇數\n", a);
}
}
4.24
#include
void main()
{
int a,b,c,temp,max;
printf ("請輸入三個整數 :");
scanf ("%d %d %d",&a,&b,&c);
temp=(a>b)? a:b;
max=(temp>c)? temp:c;
printf ("\n");
printf ("你輸入的數中最大的是 %d.\n",max);
}
4.25
(1)不嵌套的if語句
#include
void main()
{
int x,y;
printf("input x :");
scanf("%d",&x);
if ( x>-5 && x<0 )
{
printf("y is %d\n",y=x);
}
if ( x==0 )
{
printf("y is %d\n",y=x-1);
}
if ( x>0 && x<10 )
{
printf("y is %d\n",y=x 1);
}
if ( x>=10 || x<=-5)
{
printf("error\n");
}
}
(2)嵌套的if語句
#include
void main()
{
int x,y;
printf("input x :");
scanf("%d",&x);
printf("\n");
if(x < 0)
{
if(x > -5)
{
printf("y is %d.\n",y=x);
}
else
{
printf("error!\n");
}
}
if(0 == x)
{
printf("y is %d.\n",y=x-1);
}
if(x > 0)
{
if(x < 10)
{
printf("y is %d.\n",y=x 1);
}
else
{
printf("error!\n");
}
}
}
(3)if_else語句
#include
void main()
{
int x,y;
printf("input x :");
scanf("%d",&x);
if( x>-5 && x<0 )
{
printf("y is %d.\n",y=x);
}
else if( x==0 )
{
printf("y is %d.\n",y=x-1);
}
else if( x>0 && x<10 )
{
printf("y is %d.\n",y=x 1);
}
else
{
printf("error!\n");
}
}
(4)switch語句
#include
void main()
{
int x,y;
printf("input x : ");
scanf("%d",&x);
switch (x)
{
case -4:
case -3:
case -2:
case -1:
printf("y is %d.\n",y=x);
break;
case 0:
printf("y is %d.\n",y=x-1);
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
printf("y is %d.\n",y=x 1);
break;
default:
printf("error!\n");
}
}
第五章
一. 選擇題
5.1 D 5.2 C 5.3 B 5.4 C 5.5 C 5.6 B 5.7 D 5.8 A 5.9 D 5.10 D
二. 填空題
5.11 5 4 6
5.12 死循環
5.13 -1
5.14 11
5.15 d=1.0 k k<=n
5.16 x>=0 x
三. 編程題
5.17
#include
void main()
{
int i;
int sig = 1;
int sum = 0;
for(i=1; i<=101; i ,i )
{
sum = sig*i;
sig *= -1;
}
printf("sum=%d\n", sum);
}
5.18
(1)
#include
void main()
{
int i;
double m=1.0;
double e = 1.0;
for(i=1; i<50; i )
{
m *= i;
e = 1/m;
}
printf("e=%f\n",e);
}
(2)
#include
void main()
{
int i=1;
double m=1.0;
double e = 1.0;
while(1/m >= 0.0004)
{
m *= i;
e = 1/m;
i ;
}
printf("e=%f\n",e);
}
5.19
#include
void main()
{
int year;
int col = 0;
for(year=1600; year<=2000; year )
{
if((year%4 == 0 && year0 != 0) || year@0 == 0)
{
printf("%d\t", year);
col ;
if(col%5 == 0)
{
printf("\n");
}
}
}
printf("\n");
}
5.20
#include
#define N 7
void main()
{
int i;
int j;
int m;
int k = N/2;
for(i=0; i {
m = i-k;
if(m < 0)
{
m *= -1;
}
for(j=0; j {
printf(" ");
}
for(j=0; j<2*(k-m) 1; j )
{
printf("*");
}
printf("\n");
}
}
第六章
一. 選擇題
6.1 B 6.2 D 6.3 A 6.4 A 6.5 B 6.6 D 6.7 D 6.8 B 6.9 A 6.10 A 6.11 C
二. 填空題
6.12 -1
6.13 1
6.14 ctype.h
6.15 1
6.16 10A 20B 30C 40D
6.17 7.29 101.298AB
6.18 A7.29B101.298
6.19 A B C (每個字元後有三個空格)
三. 編程題
6.20
#include
#define N 80
void main()
{
char str[N];
int iLoop = 0;
gets(str);
while(str[iLoop])
{
printf("%c-%d\t", str[iLoop],str[iLoop]);
iLoop ;
if(iLoop%3 == 0)
{
printf("\n");
}
}
printf("\n");
}
6.21
#include
#define N 80
void main()
{
char str[N];
int num = 0;
int iLoop = 0;
gets(str);
while(str[iLoop])
{
if(str[iLoop] >= '0' && str[iLoop] <= '9')
{
num = 10*num (str[iLoop] - '0');
}
iLoop ;
}
printf("%d\n",num);
}
6.22
#include
#include
#define N 80
void main()
{
char str[N];
int num = -1;
do
{
gets(str);
num ;
}while(strcmp(str, "EOF"));
printf("您輸入了%d行字元!\n",num);
}
6.23
#include
#define N 80
void main()
{
char str[N];
int iLoop = 0;
int num = 0;
gets(str);
while(str[iLoop] && iLoop < N)
{
if(str[iLoop] >= 'a' && str[iLoop] <= 'z')
{
num ;
}
iLoop ;
}
printf("您輸入了字元中有%d個小寫字母!\n",num);
}
6.24
#include
void main()
{
int line;
int iLoop1;
int iLoop2;
printf("請輸入圖案的行數(不大於26):");
scanf("%d", &line);
for(iLoop1 = 0; iLoop1 < line; iLoop1 )
{
for(iLoop2 = 0; iLoop2 < line - iLoop1; iLoop2 )
{
printf(" ");
}
for(iLoop2 = 0; iLoop2 < 2*iLoop1 1; iLoop2 )
{
printf("%c",iLoop1 'A');
}
printf("\n");
}
}
第七章
一. 選擇題
7.1 C 7.2 C 7.3 B 7.4 C 7.5 A 7.6 D 7.7 A
二. 填空題
7.8 12
7.9 9.000000
7.10 4
7.11 n=1 s
7.12 <=y z*x
7.13 1 s*i 0 f(k)
三. 程序調試和編程題
7.14
fun(int n)
{ int k,yes;
for(k=2; k<=n/2; k )
{
if(n%k == 0) { yes = 0; break;}
else yes = 1;
}
return yes;
}
7.15
int mymod(int a, int b)
{
return a%b;
}
7.16
double fun(int n)
{
double sum = 0;
int iLoop;
int sig = -1;
for(iLoop=1; iLoop<=n; iLoop )
{
sig *= -1;
sum = sig*1.0/iLoop;
}
return sum;
}
7.17
double fun(int n)
{
double t = 1.0;
int iLoop;
long tmp;
for(iLoop=2; iLoop<=n; iLoop )
{
tmp = iLoop*iLoop;
t -= 1.0/tmp;
}
return t;
}
7.18
#include
#include
double fun(double x)
{
return x*x 5*x 4;
}
void main()
{
int x = 2;
printf("y1=%f\n", fun(x));
printf("y2=%f\n", fun(x 15));
printf("y3=%f\n", fun(sin(x)));
}
2005-7-29 17:53 myhome1702
第八章
一. 選擇題
8.1 A 8.2 B 8.3 B 8.4 C 8.5 B 8.6 B 8.7 C 8.8 D 8.9 B 8.10 C 8.11 C 8.12 C
二. 填空題
8.13 110
8.14 7 1
8.15 (1)char *p=&ch; (2) p=&ch; (3)scanf("%c",p); (4)*p='A'; (5)printf("%c",*p);
8.16 (1)s=p 3; (2)s=s-2 (3)50 (4)*(s 1) (5)2 (6)10 20 30 40 50
三. 編程題
8.17
void fun(double x, double y, double *sum, double *div)
{
*sum = x y;
*div = x - y;
return;
}
8.18
void fun(double x, double y, double z, double *max, double *min)
{
*max = x;
*min = x;
if(*max < y)
{
*max = y;
}
if(*max < z)
{
*max = z;
}
if(*min > y)
{
*min = y;
}
if(*min > z)
{
*min = z;
}
return;
}
第九章
一. 選擇題
9.1 D 9.2 A 9.3 A 9.4 C 9.5 C 9.6 A 9.7 B 9.8 D 9.9 C 9.10 C
9.11 C 9.12 D 9.13 D 9.14 A 9.15 A 9.16 A 9.17 C 9.18 C
二. 填空題
9.19 9 0
9.20 6
9.21 12
9.22 3
9.23 2721
9.24 -850,2,0
9.25 k=p k
9.26 (c=getchar()) c-'A'
三. 編程題
9.27
#include
#define N 81
int main()
{
int counter[10] = {0};
int iLoop = 0;
char str[N];
gets(str);
while(str[iLoop])
{
if(str[iLoop] >= '0' && str[iLoop] <= '9')
{
counter[str[iLoop] - '0'] ;
}
iLoop ;
}
for(iLoop=0; iLoop < 10; iLoop )
{
printf("%d - %d\n", iLoop, counter[iLoop]);
}
return 0;
}
9.28
void fun(int array[], int arraysize, int start)
{
int iLoop;
if(start < arraysize-1)
{
if(start <=0)
{
start = 1;
}
for(iLoop = start; iLoop < arraysize; iLoop )
{
array[iLoop-1] = array[iLoop];
}
}
for(iLoop = 0; iLoop < arraysize; iLoop )
{
printf("No.%d = %d\n", iLoop, array[iLoop]);
}
}
9.29
int fun(int arry1[], int arry2[], int arrysize)
{
int iLoop;
int counter = 0;
for(iLoop = 0; iLoop < arrysize; iLoop )
{
if(arry1[iLoop] % 2)
{
arry2[counter ] = arry1[iLoop];
}
}
return counter;
}
9.30
void fun(char array[], int arraysize)
{
int iLoop1;
int iLoop2;
char temp;
/* 冒泡排序 */
for(iLoop1 = 0; iLoop1 < arraysize - 1; iLoop1 )
{
for(iLoop2 = 0; iLoop2 < arraysize - 1 - iLoop1; iLoop2 )
{
if(array[iLoop2] < array[iLoop2 1])
{
temp = array[iLoop2];
array[iLoop2] = array[iLoop2 1];
array[iLoop2 1] = temp;
}
}
}
}
9.31
#include
void fun(int array[], int arraysize, int inertNumber)
{
int iLoop;
int iLoop2;
if(array[0] < array[arraysize-1])
{
for(iLoop = 0; iLoop< arraysize; iLoop )
{
if(array[iLoop] > inertNumber)
{
for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--)
{
array[iLoop2 1] = array[iLoop2];
}
array[iLoop] = inertNumber;
break;
}
}
if(iLoop >= arraysize)
{
array[arraysize] = inertNumber;
}
}
else
{
for(iLoop = 0; iLoop< arraysize; iLoop )
{
if(array[iLoop] < inertNumber)
{
for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--)
{
array[iLoop2 1] = array[iLoop2];
}
array[iLoop] = inertNumber;
break;
}
}
if(iLoop >= arraysize)
{
array[arraysize] = inertNumber;
}
}
}
int main()
{
int iLoop;
int a[20] = {7,6,5,3,2,1};
for(iLoop = 0; iLoop < 6; iLoop )
{
printf("%d ", a[iLoop]);
}
printf("\n");
fun(a, 6, 0);
for(iLoop = 0; iLoop < 7; iLoop )
{
printf("%d ", a[iLoop]);
}
printf("\n");
fun(a, 7, 4);
for(iLoop = 0; iLoop < 8; iLoop )
{
printf("%d ", a[iLoop]);
}
printf("\n");
fun(a, 8, 8);
for(iLoop = 0; iLoop < 9; iLoop )
{
printf("%d ", a[iLoop]);
}
printf("\n");
return 0;
}
9.32
int fun(int number, int array[])
{
int iLoop = 0;
int iLoop2;
int binLen;
int midNumber;
int div;
int remain;
midNumber = number;
do
{
div = midNumber/2;
remain = midNumber%2;
midNumber = div;
array[iLoop ] = remain;
}while(midNumber);
binLen = iLoop;
for(iLoop2 = 0, iLoop = binLen - 1; iLoop2 < iLoop; iLoop2 , iLoop--)
{
midNumber = array[iLoop2];
array[iLoop2] = array[iLoop];
array[iLoop] = midNumber;
}
return binLen;
}
9.33
#include
#include
#define N 15
void fun(int array[], int arraysize)
{
int x;
int iLoop;
int iLoop2;
for(iLoop = 0; iLoop < arraysize; iLoop )
{
iLoop2 = 0;
x = rand() ;
do
{
if(x == array[iLoop2] && iLoop > 0)
{
x = rand() ;
iLoop2 = 0;
}
iLoop2 ;
}while(iLoop2 < iLoop);
array[iLoop] = x;
}
}
int main()
{
int a[N];
int iLoop;
fun(a, N);
for(iLoop = 0; iLoop < N; iLoop )
{
printf("%d\n", a[iLoop]);
}
return 0;
}
第十章
一. 選擇題
10.1 C 10.2 B 10.3 C 10.4 B 10.5 C 10.6 A 10.7 C 10.8 A 10.9 C 10.10 C
二. 填空題
10.11 GFEDCB
10.12 XYZ
10.13 SO
10.14 10
10.15 Itis
10.16 strlen(str)-1 j--
10.17 3
10.18 goodgood!
三. 編程題
10.19
char* mygets(char *str)
{
int iLoop = 0;
char ch;
while((ch=getchar()) != '\n')
{
str[iLoop ] = ch;
}
str[iLoop] = '\0';
return str;
}
char * myputs(char *str)
{
int iLoop = 0;
while(str[iLoop])
{
putchar(str[iLoop ]);
}
putchar('\n');
return str;
}
10.20
#include
#include
int fun(char *str)
{
int len;
int iLoop1;
int iLoop2;
int result = 1;
len = strlen(str);
for(iLoop1 = 0, iLoop2 = len - 1; iLoop1 < iLoop2; iLoop1 , iLoop2--)
{
if(str[iLoop1] != str[iLoop2])
{
result = 0;
break;
}
}
return result;
}
int main()
{
char a[20] = "ABCDCBA";
char b[20] = "ABCDEBA";
printf("%d\n", fun(a));
printf("%d\n", fun(b));
return 0;
}
10.21
char fun(char *str, int pos)
{
int len;
int iLoop;
char ch;
len = strlen(str);
if(pos > len)
{
return NULL;
}
ch = str[pos];
for(iLoop = pos; iLoop < len - 1; iLoop )
{
str[iLoop] = str[iLoop 1];
}
str[len-1] = '\0';
return ch;
}
2005-7-29 17:54 myhome1702
第十一章
一. 選擇題
11.1 D 11.2 B 11.3 A 11.4 C
二. 填空題
11.5 IJKLEFGHABCD
11.6 7
11.7 8
11.8 *(s j) i 1 i
11.9 17
11.10 (*fun)() (*fun)(a i*h)/h mypoly
三. 編程題
11.11
#include
#include
#define N 81
int main(int argc, char **argv)
{
char sig;
int dig;
int pos;
char str[N] = {'\0'};
char outStr[N] = {'\0'};
if(argc < 2)
{
sig = '-';
dig = 10;
}
else
{
sig = argv[1][0];
dig = argv[1][1] - '0';
}
printf("請輸入一個字元串:");
gets(str);
if(sig == '-')
{
pos = strlen(str) - dig;
if(pos <= 0)
{
pos = 0;
}
strcpy(outStr, str pos);
}
else if(sig == ' ')
{
strcpy(outStr, str);
pos = strlen(outStr);
if(pos > dig)
{
pos = dig;
}
outStr[pos] = '\0';
}
printf("處理後的字串為:");
printf("%s\n", outStr);
return 0;
}
11.12
#include
#include
void movebin(char *bin)
{
int len;
int iLoop;
len = strlen(bin);
for(iLoop = len; iLoop > 0; iLoop--)
{
bin[iLoop] = bin[iLoop - 1];
}
return;
}
void fun(int n, char *bin)
{
int pos;
pos = strlen(bin);
if(n == 0)
{
return;
}
if(n == 1)
{
movebin(bin);
bin[0] = '1';
return;
}
movebin(bin);
bin[0] = (n%2) '0';
n /= 2;
fun(n, bin);
return;
}
int main()
{
int a = 4;
char bin[50] = {""};
fun(a, bin);
printf("%s\n", bin);
return 0;
}
11.13
#include
long fun(int n)
{
if(n == 1)
{
return n;
}
else
{
return fun(n-1) n;
}
}
int main()
{
int num;
int sum;
printf("請輸入一個自然數:");
scanf("%d", &num);
sum = fun(num);
printf("結果是:%d\n", sum);
return 0;
}
11.14
#include
int fun(int n)
{
if(n == 0 || n == 1)
{
return 1;
}
else
{
return fun(n-1) fun(n-2);
}
}
int main()
{
int num;
int result;
printf("請輸入一個自然數:");
scanf("%d", &num);
result = fun(num);
printf("斐波拉契級數為:%d\n", result);
return 0;
}
第十二章
一. 選擇題
12.1 B 12.2 B 12.3 A 12.4 C 12.5 D 12.6 B 12.7 A 12.8 A
二. 填空題
12.9 2,5,1,2,3,-2
12.10 2468
第十三章
一. 選擇題
13.1 A 13.2 C 13.3 B 13.4 C 13.5 D 13.6 D 13.7 D
二. 填空題
13.8 ar=9 ar=9 ar=11
13.9 int* s *b
三. 編程題
13.10
#define MYALPHA(C) ((C>='A' && C<='Z') || (C>='a' && C<='z')) ? 1 : 0
13.11
#define SWAP(t,x,y) {t tmp; tmp=x; x=y; y=tmp;}
13.12
#include
#include
int main()
{
int *p;
int tmp;
int iLoop;
int iLoop2;
p = (int *)malloc(sizeof(int)*3);
scanf("%d%d%d", p,p 1,p 2);
for(iLoop = 0; iLoop < 2; iLoop )
{
for(iLoop2 = 0; iLoop2 < 2 - iLoop; iLoop2 )
{
if(*(p iLoop2) > *(p iLoop2 1))
{
tmp = *(p iLoop2);
*(p iLoop2) = *(p iLoop2 1);
*(p iLoop2 1) = tmp;
}
}
}
printf("%d %d %d\n", *p, *(p 1), *(p 2));
free(p);
p = NULL;
return 0;
}
字太多了 粘不完
G. 南昌大學C語言的實驗報告答案在那裡可以找到
親,我覺得,你直接告訴大家你的實驗課題更好一些
H. c語言期末考試試題
去學校抄復印室找找吧。網上其它學校的期末考試題也有,比如南昌大學的:http://wenku..com/view/71eaec8271fe910ef12df886.html
I. C語言程序設計教程(第二版) 周宇 課後答案
二、 1. I love China! printf("we are students.\n") 2. 6 項目實訓題參考答案 1.編寫一個C程序,輸出以下信息: * * * * * * * * * * * * * * * * * * * * I am a student! * * * * * * * * * * * * * * * * * * * * main() { printf("********************\n"); printf(" I am a student!\n "); printf("********************\n"); } 2222....已知立方體的長、寬、高分別是10cm、20cm、15cm,編寫程序,求立方體體積。 解: main() { int a,b,c,v; a=10; b=20; c=15; v=a*b*c; printf("v=%d",v); } 本程序運行結果為: v=3000 第第第第2章章章章 編制編制編制編制C程序的基礎知識程序的基礎知識程序的基礎知識程序的基礎知識 一 選擇題 C B A B A C C 二 操作題 2 21. 3,2,-8,2 3.000000,2.500000,-8.000000 2. ABC DE FGH why is 21+35 equal 52 3. 3 1 4 3 2 3 1 2 4. aa bb cc abc A N 項目實訓題 1.定義一個符號常量M為5和一個變數n值為2,把它們的乘積輸出。 #define M 5 main() { int n,c; n=2; c=M*n; printf("%d\n",c); } 2.編程求下面算術表達式的值。 (1)x+a%3*(int)(x+y)%2/4,設x=2.5,a=7,y=4.7; (2)(float)(a+b)/2+(int)x%(int)y,設a=2,b=3,x=3.5,y=2.5。 (1)main() { int a=7; float x=2.5,y=4.7; printf("%f\n",x+a%3*(int)(x+y)%2/4); } (2)main() { int a=2,b=3; float x=3.5,y=2.5; printf("%f\n",(float)(a+b)/2+(int)x%(int)y); 第三章第三章第三章第三章 順序結構程序設計順序結構程序設計順序結構程序設計順序結構程序設計 一 選擇題 A C D C C 二 操作題 1. x=3,a=2,b=3 2. z=12.700000 3. 1 2 1 a 2 1 2 三三三三....編程題 編程題編程題編程題編程題 1. 某工種按小時計算工資,每月勞動時間(小時)×每小時工資=總工資,總工資中扣除10%公積金,剩餘的為應發工資。編寫一個程序從鍵盤輸入勞動時間和每小時工資,列印出應發工資。 解: #include <stdio.h> main() { float sj,gz,yfgz; printf("time,salary:"); scanf("%f,%f",&sj,&gz); yfgz=sj*gz*0.9; printf("total salary:%f\n",yfgz); } 本程序運行結果為: time,salary:4,3<CR> total salary:10.800000 2.編寫一個程序求出任意一個輸入字元的ASCII碼 解: #include <stdio.h> main() { char c; printf("Input a string:"); scanf("%c",&c); printf("%c ASCII is %d\n",c,c); } 本程序運行結果為: Input a string:a<CR> a ASCII is 97 3、編寫一個程序用於水果店售貨員算帳:已知蘋果每斤2.50元,鴨梨每斤1.80元,香蕉每斤2元,橘子每斤1.6元,要求輸入各類水果的重量,列印出應付第四章第四章第四章第四章 選擇結構程序設計選擇結構程序設計選擇結構程序設計選擇結構程序設計 一、略 二、B B A B C B A 三、1. 1 0 2. 2 3 2 2 3. 10 20 0 4. ch>=』A』&&ch<=』Z』||ch>=』a』&&ch<=』z』 ch>=』0』&&ch<=』9』 ch==』 』 5. -1 四、上機操作 1. 從鍵盤輸入一個英文字母,如果是大寫字母,則將它變為小寫字母輸出;如果是小寫字母,則將其變為大寫字母輸出。 #include<stdio.h> main() {char ch; ch=getchar(); if(ch>='A'&&ch<='Z') ch+=32; else if(ch>='a'&&ch<='z') ch-=32; putchar(ch); putchar('\n'); } 2. 根據輸入的x值依據下列表達式,計算y的值。 2x (x>-1) y = 3 (x=-1) 4+x (x<-1) 解: main() { float x,y; scanf("%f",&x); if(x>-1) y=2*x; else if(x==1) y=3; else y=4+x; printf("y=%f",y); } 本程序運行結果為: -2<CR> y=2.000000 3.編寫程序,輸入一個整數,判斷它是奇數還是偶數,若是奇數,輸出「Is Odd「;若是偶數,輸出「Is Even「。 main() { int x; scanf("%d",&x); if(x%2==0) printf("Is Even\n"); else printf("Is Odd\n"); } 4.設計應用程序,求二次方程ax2+bx+c=0的解。 #include<math.h> main() { float a,b,c,disc,x1,x2,p,q; scanf("%f,%f,%f",&a,&b,&c); if(fabs(a)<=1e-6) printf(" The equation is not a quadratic\n"); else { disc=b*b-4*a*c; if(fabs(disc)< 1e-6) printf("x1=x2=%8.4f\n",-b/(2*a)); else if(disc>1e-6) {x1=(-b+sqrt(disc)/(2*a)); x2=(-b-sqrt(disc)/(2*a)); printf("x1=%8.4f,x2=%8.4f\n",x1,x2); } else { p=-b/(2*a); q=sqrt(-disc/(2*a)); printf("%8.4f+%x8.4fi\n",p,q); printf("%8.4f-%8.4fi\n",p,q);} } } 5555....按托運規則,行李不超過50公斤時,運費為0.15元/公斤,如超過50公斤,超過部分的運費為0.22元/公斤,現有行李w公斤,編寫一個程序計算運費。 解: #include <stdio.h> main() { float w,f,x; printf("weight:"); scanf("%f",&w); if(w<=50) x=0.15*w; else x=0.15*50+0.22*(w-50); printf("money:%6.2f yuan\n",x); } 本程序運行結果為: weight:20<CR> money:3.00 yuan weight:60<CR> money:9.70 yuan 6. 某商場給與顧客購物的折扣率如下: 購物金額<200元 不打折 500元>購物金額>=200元 9折 1000元>購物金額>=500元 8折 購物金額>=1000元 7.5折 輸入一個購物金額,輸出打折率、購物實際付款金額。 #include<stdio.h> main() { float x,y,realx; scanf("%f",&x); if(x<=0) { printf("Error! You input a worry number!\n"); y=0;} else { if(x<200) y=1.0; else if(x<500) y=0.9; else if(x<1000) y=0.8; else y=0.75;} if(y!=0) {realx=x*y; printf("y=%f, the realx=%5.2f\n", y,realx);} } 第五章第五章第五章第五章 循環結構程序設計循環結構程序設計循環結構程序設計循環結構程序設計 一、選擇題 C C A A D D第六章第六章第六章第六章 數組數組數組數組 、選擇題 D A D A A C C A D 二、程序閱讀題 13 13 13 13 13 13第七章第七章第七章第七章 函數函數函數函數 一、選擇題 B D C B B D A A D第第第第8888章章章章 指針指針指針指針 一、選擇題 D A C C(D) D C D 二、填空題 1. m 2. 指針數組名 3. ABCDCD 4.49 5. 25
