C语言苏小红版第六章习题答案[推荐]

时间:2019-05-12 16:51:23下载本文作者:会员上传
简介:写写帮文库小编为你整理了多篇相关的《C语言苏小红版第六章习题答案[推荐]》,但愿对你工作学习有帮助,当然你在写写帮文库还可以找到更多《C语言苏小红版第六章习题答案[推荐]》。

第一篇:C语言苏小红版第六章习题答案[推荐]

第六章习题 1.(1)#include main(){

int i,j,k;

char space=' ';

for(i=1;i<=4;i++)

{

for(j=1;j<=i;j++)

{

printf(“%c”,space);} for(k=1;k<=6;k++){

printf(“*”);}

printf(“n”);

} }

(2)#include main(){ int k = 4, n;for(n = 0;n < k;n++){

if(n % 2 == 0)continue;

k--;} printf(“k = %d,n = %dn”,k,n);}

(3)#include main(){

int k = 4, n;

} for(n = 0;n < k;n++){ if(n % 2 ==0)

break;k--;} printf(“k = %d, n = %dn”, k, n);2.

(1)计算1+3+5+……99+101=

#include main(){

int i,sum=0;

for(i=1;i<=101;i=i+2)

{

sum=sum+i;

}

printf(“sum=%dn”,sum);}

(2)计算1*2*3+3*4*5+...+99*100*101= #include main(){

long i;

long term,sum=0;

for(i=1;i<=99;i=i+2)

{ term=i*(i+1)*(i+2);

sum=sum+term;

}

printf(“sum=%ld”,sum);}

(3)计算a+aa+aaa+...+aa...a(n个a)的值

#include main(){

long term=0,sum=0;

int a,i,n;

printf(“input a,n:”);

scanf(“%d,%d”,&a,&n);

for(i=1;i<=n;i=i++)

{ term=term*10+a;

sum=sum+term;

}

printf(“sum=%ldn”,sum);}(4)计算1—1/2+1/3-...+1/99-1/100+...,直到最后一项的绝对值小于10#include #include main(){

int n=1;

float term=1.0,sign=1,sum=0;

while(fabs(term)>=1e-4)

{

term=sign*1.0/n;

sum=sum+term;

sign=sign*(-1);

n++;

}

printf(“sum=%fn”,sum);}

-4。

(5)#include #include main(){

int n=1,count=1;

float x;

double sum,term;

printf(“input x:”);

scanf(“%f”,&x);

sum=x;

term=x;

do{

term=(-1)*term*x*x/((n+1)*(n+2));

sum=sum+term;

n=n+2;

count++;

}while(fabs(term)>=1e-5);

printf(“sin(x)=%f,count=%dn”,sum,count);}

3.#include main(){ int x = 0,find=0;

while(!find)

/*当没找到时继续执行循环*/ {

x++;

if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)

{

printf(“x=%dn”,x);

find=1;

} } }

4. #include main(){ int i,n;long m=1,p=1;printf(“input n:”);scanf(“%d”,&n);for(i=1;i<=n;i++){

m=i*i;

p=i*i*i;

printf(“m=%ld,p=%ldn”,m,p);} }

5.#include main(){ float c,f;for(c=-40;c<=110;c=c+10){

f=9/5*c+32;

printf(“f=%fn”,f);

}

}

6.#include #include main(){ int n;double c=0.01875,x;do{

x=x*pow(1+c,12)-1000;

n++;}while(x>0);

} printf(“x=%dn”,x);7.#include main(){ int n=0;float x=100.0,a;printf(“input a:”);scanf(“%f”,&a);do {

x=x*(1+a);

n++;}while(x<=200.0);printf(“n=%dn”,n);}.#include #include main(){

int n=1,count=1;

float sum=0,term=1.0,sign=1;

do{

term=sign*1.0/n;

sum=sum+term;

sign=sign*(-1);

n=n+2;

count++;

}while(fabs(term)>=1e-4);

sum=4*sum;

printf(“sum=%f,count=%dn”,sum,count);}

9. #include #include main(){

int n=1,count=1;

float sum=1,term=1.0;

do{

term=term/n;

sum=sum+term;

n=n+1;

count++;

}while(fabs(term)>=1e-5);

printf(“e=%f,count=%dn”,sum,count);}

10.#include #include main(){

int m;

for(m=100;m<=999;m++)

{

if(m==pow(m/100,3)+pow(m/10%10,3)+pow(m%10,3))

printf(“m=%dn”,m);

} }

11.#include main(){

int i=0,n;

long sum=0,term=1;

printf(“Inputn:”);

scanf(“%d”,&n);

do{

i++;

term=term*i;

sum=sum+term;

}while(sum

printf(“%dn”,i-1);}

12.#include main(){

int i,n,m,sum=0,count=0;

printf(“input n:”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

printf(“please enter m:”);

scanf(“%d”,&m);

if(m>0)

{

sum=sum+m;

count++;

}

else

{

break;

}

printf(“sum=%d,count=%dn”,sum,count);}

printf(“program is over!n”);}

13.#include main(){

int i,n,m,sum=0,count=0;

printf(“input n:”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

printf(“please enter m:”);

scanf(“%d”,&m);

if(m>0)

{

sum=sum+m;

count++;

}

else if(m<0)

{

continue;

}

else

{

break;

}

printf(“sum=%d,count=%dn”,sum,count);}

printf(“program is over!n”);}

14.#include main(){ int x,y,z;for(x=0;x<17;x++){

for(y=0;y<=25;y++)

{

for(z=0;z<=30;z++)

}

{

if(x+y+z==30&&3*x+2*y+1*z==50)printf(“x=%d,y=%d,z=%dn”,x,y,z);

}

} }

15.#include main(){ int x,y;for(x=0;x<98;x++){

for(y=0;y<97;y++)

{

if(x+y==98&&2*x+4*y==386)

printf(“x=%d,y=%dn”,x,y);

} } }

16.#include main(){ int x,y,z;for(x=0;x<=20;x++){

for(y=0;y<=33;y++)

{

for(z=0;z<=100;z++)

{

if(x+y+z==100&&5*x+3*y+z/3.0==100)

}

printf(“x=%d,y=%d,z=%dn”,x,y,z);

}

} }

17.#include main(){ int x,y,z;for(x=0;x<=10;x++){

for(y=0;y<=20;y++)

{

for(z=0;z<=100;z++)

{

if(x+y+z==50&&10*x+5*y+z==100)

printf(“x=%d,y=%d,z=%dn”,x,y,z);

}

} } }

18.#include #include main(){

int i,j,k,c=0;

for(i=1;i<=9;i++)

printf(“%6d”,i);

printf(“n”);

for(i=1;i<=9;i++)

printf(“

-”);

printf(“n”);

for(int x=1;x<=9;x++){

for(int y=1;y<=9;y++)

printf(“%6d”,x*y);

printf(“n”);

} }

#include #include main(){

int i,j,k,c=0;

for(i=1;i<=9;i++)

printf(“%6d”,i);

printf(“n”);

for(i=1;i<=9;i++)

printf(“

-”);

printf(“n”);

for(int x=1;x<=9;x++){

for(int y=1;y<=x;y++)

printf(“%6d”,x*y);

printf(“n”);

} }

#include #include main(){

int i,j,k,c=0;

for(i=1;i<=9;i++)

printf(“%6d”,i);

printf(“n”);

for(i=1;i<=9;i++)

printf(“

-”);

printf(“n”);

for(i=1;i<=9;i++)

{

for(k=1;k<=c;k++)

printf(“

”);

c++;

j=0;

while(j<9)

{

j++;

if(j

printf(“%6d”,i*j);

}

printf(“n”);

} }

第二篇:C语言程序设计苏小红版答案

2.2 #include main(){ float x=2.5,y=2.5,z=2.5;printf(“x=%fn”,x);printf(“y=%fn”,y);printf(“z=%fn”,z);} 3.1(1)

#include main(){ int a=12,b=3;float x=18.5,y=4.6;printf(“%dn”,(float)(a*b)/2);printf(“%dn”,(int)x%(int)y);} 3.2

#include main(){ int x,b0,b1,b2,s;printf(“Inputx:”);scanf(“%d”,&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf(“s=%dn”,s);} 3.3

#include #include main(){ float rate=0.0225;float n,capital,deposit;printf(“Input n,capital:”);scanf(“%f,%f”,&n,&capital);deposit=capital*pow(1+rate,n);printf(“deposit=%fn”,deposit);} 3.4 #include #include main(){ float a,b,c;double x,y;printf(“Inputa,b,c:”);scanf(“%f,%f,%f”,&a,&b,&c);x=(-b+sqrt(b*b-4*a*c))/(2*a);y=(-b-sqrt(b*b-4*a*c))/(2*a);printf(“x=%f,y=%fn”,x,y);} 4.1(1)#include main(){ char c1='a',c2='b',c3='c';printf(“a%cb%cc%cn”,c1,c2,c3);} 4.1(2)#include main(){ int a=12,b=15;printf(“a=%d%%,b=%d%%n”,a,b);} 4.1(3)#include main(){ int a,b;scanf(“%2d%*2s%2d”,&a,&b);printf(“%d,%dn”,a,b);} 4.2 #include main(){ long a,b;float x,y;scanf(“%d,%dn”,&a,&b);scanf(“%f,%fn”,&x,&y);printf(“a=%d,b=%dn”,a,b);printf(“x=%f,b=%fn”,x,y);} 5.1 #include main(){ float a;printf(“Innputa:”);scanf(“%f”,&a);if(a>=0){

a=a;

printf(“a=%fn”,a);} else {

a=-a;

printf(“a=%fn”,a);} } 5.2 #include main(){ int a;printf(“Inputa:”);scanf(“%d”,&a);if(a%2==0){

printf(“a是偶数”);} else {

printf(“a是奇数”);} } 5.3 #include #include main(){ float a,b,c,s,area;printf(“Inputa,b,c:”);scanf(“%f,%f,%f”,&a,&b,&c);if(a+b>c&&a+c>b&&b+c>a){

s=(a+b+c)/2;

area=(float)sqrt(s*(s-a)*(s-b)*(s-c));

printf(“area=%fn”,area);} else {

printf(“不是三角形”);} } 5.4 #include #include main(){ float a,b,c,x,y;printf(“Inputa,b,c:”);scanf(“%f,%f,%f”,&a,&b,&c);if(a==0){

printf(“该方程不是一元二次方程n”);} if(b*b-4*a*c>0){

x=(-b+sqrt(b*b-4*a*c))/(2*a);

y=(-b-sqrt(b*b-4*a*c))/(2*a);

printf(“x=%f,y=%fn”,x,y);} else if(b*b-4*a*c==0){

x=-b/(2*a);

y=-b/(2*a);

printf(“x=%f,y=%fn”,x,y);} else {

printf(“该方程无实根n”);} } 5.5

#include main(){ int year,flag;printf(“Input a year:”);scanf(“%d”,&year);if(year%4==0&&year%400!=0||year%400==0){

flag=1;} else {

flag=0;} if(flag==1){

printf(“%d is a leap year!n”,year);} else {

printf(“%d is not a leap year!n”,year);} } 5.6 #include main(){ int year,flag;printf(“Input a year:”);scanf(“%d”,&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){

printf(“%d is a leap year!n”,year);}

else {

printf(“%d is not a leap year!n”,year);} } 5.7

#include main(){ char ch;printf(“Inputch:”);scanf(“%c”,&ch);if(ch>='a'&&ch<='z'){

ch=getchar();

ch=ch-32;

printf(“%c,%dn”,ch,ch);}

else if(ch>='A'&&ch<='Z'){

ch=getchar();

ch=ch+32;

printf(“%c,%dn”,ch,ch);} else {

printf(“%c”,ch);} } 5.8 #include main(){ char ch;printf(“Inputch:”);scanf(“%c”,&ch);

if(ch>=48&&ch<=57)

{

printf(“ch是数字字符n”);

}

else if(ch>=65&&ch<=90)

{

printf(“ch是大写字母n”);

}

else if(ch>=97&&ch<=122)

{

printf(“ch是小写字母n”);

}

else if(ch==32)

{

printf(“ch是空格n”);

}

else

{

printf(“ch是其他字符n”);

} } 5.9 #include main(){ int score,grade;printf(“Input score:”);scanf(“%d”,&score);

grade=score/10;if(score<0||score>100){

printf(“Input errorn”);}

if(score>=90&&score<=100)

{

printf(“%d--An”,score);

}

else if(score>=80&&score<90)

{

printf(“%d--Bn”,score);

}

else if(score>=70&&score<80)

{

printf(“%d--Cn”,score);

}

else if(score>=60&&score<70)

{

printf(“%d--Dn”,score);

}

else if(score>=0&&score<60)

{

printf(“%d--En”,score);

} } 5.10 #include main(){ int year,month;printf(“Input year,month:”);scanf(“%d,%d”,&year,&month);

if(month>12||month<=0){

printf(“error monthn”);}

else

{ switch(year,month){ case 12: case 10: case 8: case 7: case 5: case 3: case 1:

printf(“31天n”);

break;case 11: case 9: case 6: case 4:

printf(“30天n”);

break;case 2:

if(year%4==0&&year!=0||year%400==0)

{

printf(“29天n”);

}

else

{

printf(“28天n”);

}

break;

default:

printf(“Input errorn”);}

} } 6.1(1)#include main(){ int i,j,k;char space=' ';for(i=1;i<=4;i++){

for(j=1;j<=i;j++)

{

printf(“%c”,space);

}

for(k=1;k<=6;k++)

{

printf(“*”);

}

printf(“n”);} } 6.1(2)

#include main(){ int k=4,n;for(n=0;n

if(n%2==0)

continue;

k--;} printf(“k=%dn,n=%dn”,k,n);} 6.1(3)#include main(){ int k=4,n;for(n=0;n

if(n%2==0)

break;

k--;} printf(“k=%d,n=%dn”,k,n);} 6.2(1)

#include main(){ int i,sum=0;for(i=1;i<=101;i++){

sum=sum+i;} printf(“sum=%dn”,sum);} 6.2(2)#include main(){ long i;long term,sum=0;for(i=1;i<=101;i=i+2){

term=i*(i+1)*(i+2);

sum=sum+term;} printf(“sum=%ldn”,sum);} 6.2(4)

#include #include main(){ int n=1;float term=1.0,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){

term=1.0/sign;

sum=sum+term;

sign=sign+n;

n++;} printf(“sum=%fn”,sum);} 6.2(5)

#include #include main(){ int n=1,count=1;float x;double sum,term;printf(“Input x:”);scanf(“%f”,&x);sum=x;term=x;do{

term=-term*x*x/((n+1)*(n+2));

sum=sum+term;

n=n+2;

count++;}while(fabs(term)>=1e-5);printf(“sin(x)=%f,count=%dn”,sum,count);} 6.3

#include main(){ int x=1,find=0;

while(!find){

if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)

{

printf(“x=%dn”,x);

find=1;

x++;

} } } /* int x,find=0;for(x=1;!find;x++){

if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)

{

printf(“x=%dn”,x);

find=1;

} } }*/ 6.4

#include main(){ int i,n;long p=1,m=1;printf(“Input n:”);scanf(“%d”,&n);for(i=1;i<=n;i++){

p=i*i;

m=i*i*i;

printf(“p=%d,m=%dn”,i,p,i,m);} } 6.5 #include main(){ float c,f;for(c=-40;c<=110;c=c+10){

f=9/5*c+32;

printf(“f=%fn”,f);}

} 6.6

#include #include main(){ int n;double c=0.01875,x;do{

x=x*pow(1+c,12)-1000;

n++;}while(x>0);

printf(“x=%dn”,x);} 6.7

#include main(){ int n=0;float a=100.0,c;printf(“Inputc:”);scanf(“%f”,&c);do {

a=a*(1+c);

n++;}while(a<=200);

printf(“n=%dn”,n);} 6.8 #include #include main(){ int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){

term=pow(-1,count)*(1.0/(n+2));

sum=sum+term;

n=n+2;

count++;}

sum=4*sum;printf(“sum=%f,count=%dn”,sum,count);} 6.9 #include #include main(){ int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){

term=term*(1.0/n);

sum=sum+term;

n++;

count++;} printf(“sum=%f,count=%dn”,sum,count);} 6.10 #include #include main(){ int x;for(x=100;x<=999;x++){

if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))

printf(“x=%dn”,x);}

} 6.11 #include main(){ int i=0,n;long sum=0,term=1;printf(“Inputn:”);scanf(“%d”,&n);do{

i++;

term=term*i;

sum=sum+term;}while(sum main(){ int i,n,m,count=0,sum=0;printf(“Input n:”);scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

printf(“Inputm:”);

scanf(“%d”,&m);

if(m>0)

{

sum=sum+m;

count++;

}

else

{

break;

}

printf(“sum=%d,count=%dn”,sum,count);

} } 6.13 #include main(){ int i,n,m,count=0,sum=0;printf(“Inputn:”);scanf(“%d”,&n);for(i=1;i<=n;i++){

printf(“Inputm:”);

scanf(“%d”,&m);

if(m>0||m<0)

{

sum=sum+m;

count++;

}

else

{

break;

}

printf(“sum=%d,count=%dn”,sum,count);} } 6.14

#include main(){ int x,y,z;for(x=0;x<=17;x++){

for(y=0;y<=25;y++)

{

3*x+2*y+z==50;

z=30-x-y;

if(3*x+2*y+z==50&&x+y+z==30)

printf(“x=%d,y=%d,z=%dn”,x,y,z);

} } } 6.15 #include main(){ int x,y;for(x=0;x<=98;x++){

y=98-x;

2*x+4*y==386;

if(x+y==98&&2*x+4*y==386)

{

printf(“x=%d,y=%dn”,x,y);

} } } 6.16

#include main(){ int x,y,z;for(x=0;x<=20;x++){

for(y=0;y<=33;y++)

{

3*y+5*x+z/3.0==100;

z=100-x-y;

if(5*x+3*y+z/3.0==100&&z+x+y==100)

{

printf(“x=%d,y=%d,z=%dn”,x,y,z);

}

}

}

} 6.17

#include main(){ int x,y,z;for(x=1;x<=9;x++){

for(y=1;y<=17;y++)

{

10*x+5*y+z==100;

z=50-x-y;

if(10*x+5*y+z==100&&x+y+z==50&&z>0)

{

printf(“x=%d,y=%d,z=%dn”,x,y,z);

}

} } } 7.1

#include int Square(int i){ return i*i;} int main(){ int i=0;i=Square(i);for(;i<3;i++){

static int i=1;

i+=Square(i);

printf(“%d,”,i);} printf(“%dn”,i);return 0;} 7.2 #include int hour,minute,second;void update(){ second++;if(second==60){

second=0;

minute++;} if(minute==60){

minute=0;

hour++;} if(hour==24)

hour=0;} void display(){ printf(“%d,%d,%dn”,hour,minute,second);

} void delay(){ int t;for(t=0;t<100000000;t++);} int main(){ int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){

update();

display();

delay();}

return 0;} 7.3

#include int GetMax(int a,int b);int main(){ int x,y,max;printf(“Inputx,y:”);scanf(“%d,%d”,&x,&y);max=GetMax(x,y);printf(“max=%dn”,max);return 0;} int GetMax(int m,int n){ if(m>=n)

return m;else

return n;} 7.4

#include int LCM(int n,int m);int main(){ int a,b;printf(“Inputa,b:”);scanf(“%d,%d”,&a,&b);printf(“%dn”,LCM(a,b));return 0;} int LCM(int n,int m){ int x;int find=0;for(x=1;!find;x++){

if(x%n==0&&x%m==0)

{

find=1;

}

} return x-1;} 7.5

#include long Fact(int n);int main(){ int m,a;printf(“Inputm:”);scanf(“%d”,&m);for(a=1;a<=m;a++){

printf(“%d!=%ldn”,a,Fact(a));} return 0;} long Fact(int n){ int i;long result=1;for(i=2;i<=n;i++)

result*=i;return result;} 7.6 #include long Fact(int n);int main(){ int m;long ret;printf(“Inputm:”);scanf(“%d”,&m);ret=Fact(m);printf(“ret=%dn”,ret);return 0;} long Fact(int n){ int i;long result=1,sum=0;for(i=2;i<=n;i++){

result*=i;

sum=sum+result;} return sum;} 7.7(1)#include int Gcd(int a,int b);int main(){ int m,n;printf(“Inputm,n:”);scanf(“%d,%d”,&m,&n);printf(“%dn”,Gcd(m,n));return 0;} int Gcd(int a,int b){ int t,min,find=0;min=a

a%t==0;

b%t==0;

if(a%t==0&&b%t==0)

return t;}

find=1;} 7.7(2)

#include int Gcd(int a,int b);int main(){ int m,n;printf(“Inputm,n:”);scanf(“%d,%d”,&m,&n);printf(“%dn”,Gcd(m,n));return 0;} int Gcd(int a,int b){ int r,temp;r=a%b;if(r==0)

return b;else do{

temp=b;

b=r;

a=temp;

r=a%b;

}while(r!=0);

return b;} 8.1(1)#include void Func(int x){ x=2;} int main(){ int x=10;Func(x);printf(“%d”,x);return 0;} 8.1(2)

#include void Func(int b[]){ int j;for(j=0;j<4;j++){

b[j]=j;} } int main(){ static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){

printf(“%d”,a[i]);} return 0;} 8.2(1)

int PositiveNum(int a[],int n){ int i,count=0;for(i=0;i

if(a[i]>0)

count++;} return 0;} 8.2(2)

void Fib(long f[],long n){ int i;f[0]=0;f[1]=1;for(i=2;i

f[i]=f[i-1]+f[i-2];} } 8.2(3)

#include int main(){ int a[10],n,max,min,maxPos,minPos;for(n=0;n<10;n++){

scanf(“%d”,&a[n]);} max=min=a[0];maxPos=minPos=0;for(n=0;n<10;n++){

if(a[n]>max)

{

max=a[n];

maxPos=n;

}

else if(a[n]

{

min=a[n];

minPos=n;

} } printf(“max=%d,pos=%dn”,max,maxPos);printf(“min=%d,pos=%dn”,min,minPos);return 0;} 8.3 void DivArray(int *pArray,int n){ int i;for(i=0;i

pArray[i]/=pAttay[0];} } 8.4

#include #define N 40 int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){ int score[N],n;printf(“Input n:”);scanf(“%d”,&n);ReadScore(score,n);FailNum(score,n);printf(“FailNum students are %dn”,FailNum(score,n));return 0;} int FailNum(int score[],int n){ int i,count=0;for(i=0;i

if(score[i]<60)

count++;} return count;} void ReadScore(int score[],int n){ int i;printf(“Input score:”);for(i=0;i

scanf(“%d”,&score[i]);} } 8.5 #include #define N 40 int HighAver(int score[],int n);int ReadScore(int score[]);int main(){ int score[N],n;n=ReadScore(score);printf(“HighAver students are %dn”,HighAver(score,n));return 0;} int HighAver(int score[],int n){ int i,count=0,sum=0,aver;for(i=0;i

sum=sum+score[i];

aver=sum/n;} for(i=0;i

if(score[i]>aver)

count++;} return count;} int ReadScore(int score[]){ int i=-1;do{

i++;

printf(“Input score:”);

scanf(“%d”,&score[i]);}while(score[i]>=0);return i;} 8.6 #include #define N 40 int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){ int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf(“max=%d,num=%dn”,max,num);return 0;} int ReadScore(int score[],long num[]){ int i=-1;do{

i++;

printf(“Input student's ID and score:”);

scanf(“%ld%d”,&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;} int FindMax(int score[],long num[],int n){ int max,i;max=score[0];for(i=1;i

if(score[i]>max)

max=score[i];} return i;} 8.7

#include #define N 40 int Read(int a[]);int ChangeArry(int a[],int n);int main(){ int a[N],n;printf(“%dn”,ChangeArry(a,n));return 0;} int Read(int a[]){ int i;for(i=0;i<9;i++){

printf(“Input a:”);

scanf(“%d”,&a[i]);return i;} int ChangeArry(int a[],int n){ int max,min,i,maxpos,minpos,term;max=min=score[0];for(i=1;i

if(a[i]>max)

max=score[i];

maxpos=n;

else(a[i]

min=a[i];

minpos=n;

temp=maxpos;

maxpos=minpos;

minpos=temp;} return a[i];} 8.10

#include #define N 5 void main(){

int a[N][N];int sum = 0;int i, j;

printf(“请输入一个%d*%d的矩阵n”,N,N);for(i = 0;i < N;i++){

for(j = 0;j < N;j++)

{

scanf(“%d”,&a[i][j]);

} } for(i = 0;i < N;i++){

for(j = 0;j < N;j++)

{

if(i == j || i + j == N-1)

sum=sum+a[i][j];

} } printf(“对角线元素之和为:%dn”,sum);} 8.11

#include #define N 2 #define M 3 void main(){ int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf(“请输入一个%d*%d的矩阵n”,N,M);for(i = 0;i

for(j = 0;j< M;j++)

{

scanf(“%d”,&a[i][j]);

sum1=sum1+a[i][j];

} }

printf(“请输入一个%d*%d的矩阵n”,N,M);for(i = 0;i

for(j = 0;j

{

scanf(“%d”,&b[i][j]);

sum2=sum2+a[i][j];

} } sum=sum1+sum2;printf(“sum=%dn”,sum);} 8.12

#include #define N 7 void YangHui(int a[N][N],int n);void main(){ int i,j,n,b[N][N];printf(“Input n:”);scanf(“%d”,&n);YangHui(b,n);for(i=0;i

for(j=0;j<=i;j++)

{

printf(“%4d”,b[i][j]);

}

printf(“n”);} } void YangHui(int a[N][N],int n){ int i,j;for(i=0;i

a[i][0]=1;

a[i][i]=1;} for(i=2;i

for(j=1;j

a[i][j]=a[i-1][j-1]+a[i-1][j];} } 8.13

#include #define N 12 void main(){ int i;int f[N]={1,1};printf(“%d:%dn”,0,f[0]);printf(“%d:%dn”,1,f[1]);for(i=2;i

f[i]=f[i-1]+f[i-2];

printf(“%d:%dn”,i+1,f[i]);} } 8.14 #include #include #include #define N 6000 void main(){ int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,p2,p3,p4,p5,p6;srand(time(NULL));for(i=0;i

b1++;if(a[i]==2)

b2++;if(a[i]==3)

b3++;if(a[i]==4)

b4++;if(a[i]==5)

b5++;if(a[i]==6)

b6++;p1=(float)(b1/6000.0);p2=(float)(b2/6000.0);p3=(float)(b3/6000.0);p4=(float)(b4/6000.0);p5=(float)(b5/6000.0);p6=(float)(b6/6000.0);} printf(“骰子出现1的概率:%.2fn”,p1);printf(“骰子出现2的概率:%.2fn”,p2);printf(“骰子出现3的概率:%.2fn”,p3);printf(“骰子出现4的概率:%.2fn”,p4);printf(“骰子出现5的概率:%.2fn”,p5);printf(“骰子出现6的概率:%.2fn”,p6);} 9.1(1)

void Swap(int *x,int *y){ int *pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置

*pTemp=*x;*x=*y;*y=*pTemp;} 9.1(2)

void Swap(int *x,int *y){ int *pTemp;pTemp=x;x=y;y=pTemp;} //错误,x,y没有指向类型 9.2

#include int main(){ int a[]={1,2,3,4,5};int *p=a;printf(“%d,%d,%d,%d,%d,%d,%dn”,*p,*(++p),(*p)++,*p,*p--,--(*p),*p);return 0;} 9.3 #include void Swap(int *x,int *y);int main(){ int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};int i;printf(“Before Array n”);for(i=0;i<6;i++){

printf(“ %dn”,a[i]);} for(i=0;i<6;i++){

printf(“%dn”,b[i]);} for(i=0;i<6;i++){

Swap(&a[i],&b[i]);

}

printf(“After Array n”);for(i=0;i<6;i++)

{

printf(“%dn”,a[i]);

} for(i=0;i<6;i++)

{

printf(“%dn”,b[i]);

}

return 0;} void Swap(int *x,int *y){ int temp;

temp=*x;

*x=*y;

*y=temp;} 9.4 #include #define N 10 void FindMaxMin(int a[],int *pMaxa,int *pMaxnum,int *pMina,int *pMinnum);int main(){ int a[N];int i,maxa,mina,maxnum,minnum;printf(“Input a:”);for(i=0;i<10;i++){

scanf(“%d”,&a[i]);} FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);printf(“maxa=%d,maxnum=%d,mina=%d,minnum=%dn”,maxa,maxnum,mina,minnum);return 0;} void FindMaxMin(int a[],int *pMaxa,int *pMaxnum,int *pMina,int *pMinnum){ int i;*pMaxa=a[0];*pMina=a[0];*pMaxnum=0;*pMinnum=0;for(i=0;i<10;i++){

if(a[i]>*pMaxa)

{

*pMaxa=a[i];

*pMaxnum=i;

}

else if(a[i]<*pMina)

{

*pMina=a[i];

*pMinnum=i;

} } } 10.1 void MYStrcpy(char *dstStr,const char *srcStr){ while((*dstStr++=*srcStr++)!=''){ } } 10.2 #include main(){char *a=“main(){char *a=% c% s% c;printf(a,34,a,34);}”;printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char *p)main(){ unsigned int len;len=0;for(;*pa!='';pa++){

len++;} return len;} 10.3(2)

unsigned int MyStrlen(char s[]){ char *p=s;while(*p!=''){

p++;} return(p-*p);} 10.3(3)int MyStrcmp(char *p1,char *p2){ for(;*p1==*p2;p1++,p2++){

if(*p1=='')

return-1;} return 1;} 10.3(4)int MyStrcmp(char s[],char t[]){ int i;for(i=0;s[i]==t[i];i++){

if(s[i]=='')return 0;} return i;} 10.3(5)

#include #include int main(){ char password[7]=“secret”;char userInput[81];printf(“Input Password:”);scanf(“%s”,userInput);if(strcmp(userInput,password)==0)

printf(“Coorrect password!Welcome to the system...n”);else if(strcmp(userInput,password)<0)

printf(“Invalid password!user input

printf(”Invalid password!user input>password...n“);return 0;} 10.4

#include #define N 24 unsigned int CountLetter(char str[]);int main(){ char a[N];printf(”Input a letter:n“);gets(a);printf(”The length of the letter is:%dn“,CountLetter(a));return 0;} unsigned int CountLetter(char str[]){ char *p=str;int c=0,flag=0;while(*p!=''){

if(*p!=' ')

flag=1;

else if(flag==1)

{

c++;

flag=0;

}

p++;}

return c+1;} 12.1

#include typedef struct data { int year;int month;int day;}DATA;typedef struct work { char a[14];char b[10];char c[6];}WORK;typedef struct student { char studentName[10];char studentSex;DATA birthday;WORK profession;}STUDENT;int main(){ STUDENT stu={”王刚“,'M',{1991,5,19},{”信息工程系“,”学习人员“,”学生“}};printf(”stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4sn“,stu.studentName,stu.studentSex,stu.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return 0;} 12.4

#include #include #define N 10 typedef struct people { char peopleName[10];}PEOPLE;int main(){ int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={”zhang“,”li“,”wang“};printf(”Enter their name:n“);for(i=0;i<10;i++){

gets(name[i]);

if(strcmp(name[i],stu[0].peopleName)==0)

sum1++;

else if(strcmp(name[i],stu[1].peopleName)==0)

sum2++;

else if(strcmp(name[i],stu[2].peopleName)==0)

sum3++;

else

sum4++;} printf(”stu:%4s%2dn“,stu[i].peopleName,sum[j]);

printf(”stu:%4s%2dn“,stu[1].peopleName,sum2);printf(”stu:%4s%2dn“,stu[2].peopleName,sum3);printf(”%2dn",sum4);return 0;}

第三篇:C语言程序设计苏小红版第五六单元答案

第五章 5.1 #include void main(){ double x;printf(“请输入一个实数:n”);scanf(“%lf”,&x);if(x>0){

printf(“%fn”,x);} else {

printf(“%fn”,-x);} } 5.2 #include main(){ int a,b;printf(“请输入一个整数:”);scanf(“%d”,&a);b=a%2;if(b==0){

printf(“%d为偶数n”,a);} else {

printf(“%d为奇数n”,a);}

} 5.3 #include #include main(){ float a,b,c,s,area;printf(“请输入三边长:”);

scanf(“%f,%f,%f”,&a,&b,&c);s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));if((a+b>c)&&(a+c>b)&&(b+c>a)){ printf(“这个三角形的面积为%fn”,area);} else {

printf(“这三条边不能组成三角形n”);} } 5.4 #include #include void main(){

double a,b,c,d,e,x1,x2;

printf(“请输入a,b,c:”);

scanf(“%lf,%lf,%lf”,&a,&b,&c);

if(a==0)

{

printf(“该方程不是一元二次方程.n”);

}

else if(b*b-4*a*c>0)

{

d=sqrt(b*b-4*a*c);

x1=(-b+d)/(2*a);

x2=(-b-d)/(2*a);

printf(“x1=%f,x2=%fn”,x1,x2);

}

else if(b*b-4*a*c==0)

{

x1=(-b)/(2*a);

printf(“x1=x2=%fn”,x1);

}

else if(b*b-4*a*c<0)

{

printf(“该方程没有实数解.n”);

} } 5.5 #include main(){ int year,flag;printf(“输入年份:”);scanf(“%d”,&year);if(((year%4==0)&&(year%100!==0))||(year%400==0)){

flag=1;} else {

flag=0;} if(flag==1){

printf(“%d是闰年n”,year);} else {

printf(“%d不是闰年n”,year);} } 5.6 #include void main(){ int year,flag;printf(“输入年份:”);scanf(“%d”,&year);flag=(((year%4==0)&&(year%100!=0))||(year%400==0))?1:2;

if(flag==1){

printf(“%d年是闰年n”,year);} else {

printf(“%d年不是闰年n”,year);} } 5.7 #include main(){ char ch;printf(“请输入一个字母:”);ch=getchar();if((ch>=65)&&(ch<=90)){

ch=ch+32;

printf(“%c,%dn”,ch,ch);} else if((ch>=97)&&(ch<=122)){

ch=ch-32;

printf(“%c,%dn”,ch,ch);} else {

printf(“%cn”,ch);} } 5.8 #include main(){ char ch;

printf(“请输入一个字符:”);ch=getchar();if((ch>=48)&&(ch<=57)){ printf(“这是一个数字字符.n”);} else if((ch>=65)&&(ch<=90)){

printf(“这是一个大写字母.n”);} else if((ch>=97)&&(ch<=122)){

printf(“这是一个小写字母.n”);}

else if(ch==32){

printf(“这是一个空格字符.n”);} else {

printf(“这是一个其它字符.n”);} } 5.9 #include main(){ int score,mark;printf(“Please enter score:”);scanf(“%d”,&score);

if(score<0||score>100){

printf(“Input error!n”);} else if(score>=90&&score<=100){

printf(“%d--An”,score);} else if(score>=80&&score<90){

printf(“%d--Bn”,score);} else if(score>=70&&score<80){

printf(“%d--Cn”,score);} else if(score>=60&&score<70){

printf(“%d--Dn”,score);} else {

printf(“%d--En”,score);} } 5.10 #include #include main(){ int flag, year,month,day,day1=31,day2=30,day3=29,day4=28;printf(“请以2012,03这种形式输入某年某月 :”);scanf(“%d,%d”,&year,&month);if(month>12||month<1){

printf(“你输入的年份不在合法范围内n”);

exit(0);} else { flag=(year%4==0 && year%100!=0||year% 400 ==0)?1:0;switch(flag){ case 1:

printf(“闰年”);

break;case 0:

printf(“平年”);

break;} if(month==2&&flag==1)day=day3;else if(month==2&&flag==0)day=day4;switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12:

printf(“%d年,%d月拥有的天数是31.n”,year,month);

break;

case 4:

case 6:

case 9:

case 11:

printf(“%d年,%d月拥有的天数是30.n”,year,month);

break;

default:

printf(“%d年,%d月拥有的天数是%d: n”,year,month,day);} } } 本章实验题1 #include main(){

double faheight,maheight;char sex,sports,diet,F,M,Y,N;printf(“请输入父母的身高:n”);scanf(“%lf,%lf”,&faheight,&maheight);

printf(“请输入孩子是男孩还是女孩(F/M):n”);scanf(“ %c”,&sex);printf(“请输入孩子是否喜欢锻炼身体(Y/N):n”);scanf(“ %c”,&sports);printf(“请输入孩子是否有良好的饮食习惯(Y/N):n”);scanf(“ %c”,&diet);if(sex==77&&sports==89&&diet==89){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54*1.02*1.015);} else if(sex==77&&sports==89&&diet==78){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54*1.02);} else if(sex==77&&sports==78&&diet==89){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54*1.015);} else if(sex==77&&sports==78&&diet==78){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight+maheight)*0.54);} else if(sex==70&&sports==89&&diet==89){ printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2*1.02*1.015);} else if(sex==70&&sports==89&&diet==78){

printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2*1.02);} else if(sex==70&&sports==78&&diet==89){

printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2*1.015);} else if(sex==70&&sports==78&&diet==78){

printf(“该孩子成人时身高约为:%.2f.n”,(faheight*0.923+maheight)/2);} } 本章实验题2 #include main(){ double t,w,h;

printf(“请输入体重身高:n”);scanf(“%lf,%lf”,&w,&h);t=w/(h*h);if(t<18){

printf(“低体重.n”);}

if(t>=18&&t<25){

} printf(“正常体重.n”);} if(t>=25&&t<27){ printf(“超重体重.n”);} if(t>=27){ printf(“肥胖.n”);} 第六章 6.1(1)#include main(){ int i,j,k;char space=' ';for(i=1;i<=4;i++){

for(j=1;j<=i;j++)

{

printf(“%c”,space);

}

for(k=1;k<=6;k++)

{

printf(“*”);

}

printf(“n”);} } 6.1(2)#include main(){ int k=4,n;for(n=0;n

if(n%2==0)continue;

k--;} printf(“k=%dn,n=%dn”,k,n);} 6.1(3)#include main(){ int k=4,n;for(n = 0;n < k;n++){

if(n%2 ==0)break;

k--;} printf(“k=%d,n=%dn”,k,n);} 6.2(1)#include main(){ int i,sum = 0;for(i=1;i<=101;i=i+2){

sum=sum+i;} printf(“sum = %dn”,sum);} 6.2(2)#include main(){ long i;long term,sum = 0;for(i=1;i<=99;i=i+2){

term=i*(i+1)*(i+2);

sum=sum+term;} printf(“sum =%ldn”,sum);} 6.2(3)#include main(){ long term=0,sum=0;int a,i,n;printf(“Input a,n:”);scanf(“%d,%d”,&a,&n);for(i = 1;i<=n;i++){

term = term * 10 + a;

sum=sum+term;} printf(“sum = %ldn”,sum);} 6.2(4)#include #include main(){ int n = 1;float term = 1.0,sign = 1,sum = 0;while(fabs(term)>= 1e-4){

term = sign / n;

sum = sum + term;

sign =-sign;

n++;} printf(“sum = %fn”);} 6.2(5)#include #include main(){ int

n = 1,count = 1;float x;double sum,term;printf(“Input x:”);scanf(“%f”,&x);sum = x;term = x;do{

term =-term*x*x/((n+1)*(n+2));

sum = sum + term;

n = n + 2;

count++;}while(fabs(term)>=1e-5);printf(“sin(x)= %f,coumt = %d n”,sum,count);} 6.3 #include main(){ int x=1,find=0;while(!find){

if(x%2==1 && x%3==2 && x%5==4 && x%6==5 && x%7==0)

{

find=1;

}

x++;}

printf(“x=%dn”,x-1);} 6.4 #include main(){ int i,n;long p,j;printf(“Please enter n:”);scanf(“%d”,&n);for(i=1;i<=n;i++){

p=i*i;

j=i*i*i;

printf(“%d * %d = %ldn”,i,i,p);

printf(“%d * %d * %d= %ldn”,i,i,i,j);} } 6.5 #include main(){ double C,F,i;for(F =-40;F <= 110;F = F + 10){

C =(F-32)*5/9;

printf(“F=%f

C=%fn”,F,C);} } 6.6 #include main(){ double p = 1 + 0.01875*12,x4,x3,x2,benjin;x4 = 1000/p;x3 =(x4 + 1000)/p;x2 =(x3 + 1000)/p;benjin =(x2 + 1000)/p;printf(“本金为:%f”,benjin);} 6.7 #include main(){ double count = 0,c , d , sum = 100;printf(“Input c :”);scanf(“%lf”,&c);d = c / 100.0;do{

sum = sum *(1+d);

count++;}while(sum <= 200);printf(“count = %fn”,count);} 6.8 #include #include main(){ int count = 1;double sum = 1.0 ,term,n = 3.0,i = 1.0;do {

term =-i/n;

sum = sum + term;n = n + 2;i =-i;count++;}while(fabs(term)>= 1e-5);printf(“∏ = %lf,count = %d.n”,4 * sum,count);} 6.9 #include #include main(){ int count = 1;double sum = 1.0 , term = 1.0, n = 1.0;while(fabs(term)>= 1e-5){

term = term / n;

sum = sum + term;

n++;

count++;} printf(“e = %lf,count = %d.n”,sum,count);} 6.10 #include main(){ int i , a , b , c ,d;for(i = 100;i <= 999;i++){

a = i /100;

c = i % 10;

b =(ic)/ 10;

d = a * a * a + b * b * b + c * c * c;

if(i == d)

{

printf(“i = %dn”,i);

} } } 6.11 #include main(){ long m = 2 , n ,term = 1 ,sum = 1;printf(“Input n:”);scanf(“%ld”,&n);do {

term = term * m;

sum = sum + term;

m++;}while(sum <= n);printf(“m = %d.n”,m1);} 6.13 #include main(){ int term , sum = 0 , count = 0;do { begin:

printf(“Input term : ”);

scanf(“%d”,&term);

if(term > 0)

{

sum = sum + term;

count++;

}

else if(term < 0)

goto begin;

else

goto end;}while(term!= 0);end : printf(“sum = %d.count = %d.n”,sum,count);} 6.14 #include main(){ int a,b,c;for(a = 0;a < 17;a++)

for(b = 0;b <= 25;b++)

{

c = 30b;

if(a * 3 + b * 2 + c == 50)

printf(“男 = %d,女 = %d,孩子 = %d.n”,a,b,c);

} } 6.15 #include main(){ int a,b;for(a = 0;a <=98;a++){

b = 98xx-y;

if(x * 10 + y * 5 + z == 100)

printf(“x = %d, y = %d, z = %dn”,x ,y ,z);

} } } 6.18 上:

#include main(){ int i, j;for(i = 1;i <= 9;i++){

for(j = 1;j <= 9;j++)

{

printf(“%-5d”,i * j);

}

printf(“n”);} } 左:

#include main(){ int i, j;for(i = 1;i <= 9;i++){

for(j = 1;j <= i;j++)

{

printf(“%-5d”,i * j);

}

printf(“n”);} } 右:

#include main(){ int i, j, a = 1, k;char b = ' ';

for(i = 1;i <= 9;i++){

for(k = 1;k <= i;k++)

{

printf(“%5c”,b);

}

for(j = i;j <= 9;j++)

{

a=j * i;

printf(“%5d”,a);

}

printf(“n”);} } 6.19 #include main(){ long i,summo;double sumfu = 0,term = 0.01;summo = 100000 * 30;for(i = 1;i <= 30;i++){

term = term * 2.0;

sumfu = sumfu + term;

} printf(“陌生人付%ld,富翁付%lf.n”,summo,sumfu);} 本章实验题 2.1 #include #include #include main(){ int x1, x2, c, answer;begin: srand(time(NULL));x1 = rand()% 10 + 1;x2 = rand()% 10 + 1;c = x1 * x2;do {

printf(“%d * %d = n”,x1, x2);

scanf(“%d”,&answer);

if(c == answer)

{

printf(“Right!n”);

goto begin;

}

else

{

printf(“Wrrong!Please try again.n”);

} }while(c!= answer);} 2.2 #include #include #include main(){ int x1, x2, c, answer, count = 0;begin: srand(time(NULL));x1 = rand()% 10 + 1;x2 = rand()% 10 + 1;c = x1 * x2;do {

printf(“%d * %d = n”,x1, x2);

scanf(“%d”,&answer);

if(c == answer)

{

printf(“Right!n”);

goto begin;

}

else

{

printf(“Wrrong!Please try again.n”);

count++;

}

}while((c!= answer)&&(count <= 2));printf(“Wrong!You have tried three times!Test over!n”);} 2.3 #include #include #include main(){ int count1 = 0, count2 = 0;int x1, x2, c, answer ,i;

for(i = 1;i <= 10;i++){

srand(time(NULL));x1 = rand()% 10 + 1;

x2 = rand()% 10 + 1;

c = x1 * x2;

printf(“%d * %d =

n”,x1 , x2);

scanf(“%d”,&answer);

if(c == answer)

{

printf(“Right!n”);

count1++;

}

else

{

printf(“Wrong!n”);

count2++;

} } printf(“你得了%d分.n正确率为%d %%.n”,count1,count1 *10);}

第四篇:SQL语言习题及答案

数据库原理之关系数据库SQL语言课后习题及答案

http://www.xiexiebang.comAME,TEACHER)

试用SQL的查询语句表达下列查询:

(1)检索LIU老师所授课程的课程号和课程名。

SELECT C#,CNAME

FROM C

WHERE TEACHER=„LIU‟

(2)检索年龄大于23岁的男学生的学号和姓名。

SELECT S#,SNAME

FROM S

WHERE(AGE>23)AND(SEX=„M‟)

(3)检索至少选修LIU老师所授课程中一门课程的女学生姓名。

SELECT SNAME

FROM S

WHERE SEX=„F‟ AND S# IN

(SELECT S#

FROM SC

WHERE C# IN

(SELECT C#

FROM C

WHERE TEACHER=„LIU‟)

NOTICE:有多种写法,比如联接查询写法:

SELECT SNAME

FROM S,SC,C

WHERE SEX=„F‟ AND SC.S#=S.S#

AND SC.C#=C.C#

AND TEACHER='LIU' 但上一种写法更好一些。

(4)检索WANG同学不学的课程的课程号。

SELECT C#

FROM C

WHERE C# NOT IN

(SELECT C#

FROM SC

WHERE S# IN

(SELECT S#

FROM S

WHERE SNAME='WANG'))

(5)检索至少选修两门课程的学生学号。

SELECT DISTINCT X.SNO

FROM SC X,SC Y

WHERE X.SNO=Y.SNO AND X.CNO<>Y.CNO Notice:对表SC进行自连接,X,Y是SC的两个别名。

(6)检索全部学生都选修的课程的课程号与课程名。

SELECT C#,CNAME

FROM C

WHERE NOT EXISTS

(SELECT *

FROM S

WHERE S# NOT IN

(SELECT *

FROM SC

WHERE SC.C#=C.C#))

要从语义上分解:(1)选择课程的课程号与课程名,不存在不选这门课的同学。

其中,“不选这门课的同学”可以表示为:

或者

SELECT *

FROM S

WHERE NOT EXISTS

(SELECT *

FROM SC

WHERE S.S#=C.S#

AND SC.C#=C.C#)

SELECT *

FROM S

WHERE S# NOT IN

(SELECT *

FROM SC

WHERE SC.C#=C.C#)

(7)检索选修课程包含LIU老师所授课的学生学号。

SELECT DISTINCT S#

FROM SC

WHERE C# IN

(SELECT C#

FROM C

WHERE TEACHER='LIU'))

3.3 设有两个基本表R(A,B,C)和S(D,E,F),试用SQL查询语句表达下列关系代数表达式:

(1)πA(R)

(2)σB='17'(R)

(3)R×S

(4))πA,F(σC=D(R×S))

(1)SELECT A FROM R

(2)SELECT * FROM R WHERE B='17'(3)SELECT A,B,C,D,E,F FROM R,S

(4)SELECT A,F FROM R,S WHERE R.C=S.D

3.4 3.4 设有两个基本表R(A,B,C)和S(A,B,C)试用SQL查询语句表达下列关系代数表达式:

(1)R∪S

(2)R∩S

(3)R-S

(4)πA,B(R)πB,C(S)

(1)SELECT A,B,C

FROM R

UNION

SELECT A,B,C

FROM S

(2)SELECT A,B,C

FROM R

INTERSECT

SELECT A,B,C

FROM S

(3)SELECT A,B,C

FROM R

WHERE NOT EXISTS

(SELECT A,B,C

FROM S

WHERE R.A=S.A AND R.B=S.B AND R.C=S.C)

(4)SELECT R.A,R.B,S.C

FROM R,S

WHERE R.B=S.B

3.5 试叙述SQL语言的关系代数特点和元组演算特点。

(P61-62)

3.6 试用SQL查询语句表达下列对教学数据库中三个基本表S、SC、C的查询:

(1)统计有学生选修的课程门数。

SELECT COUNT(DISTINCT C#)FROM SC

(2)求选修C4课程的学生的平均年龄。

SELECT AVG(AGE)

FROM S

WHERE S# IN

(SELECT S#

FROM SC

WHERE C#='C4')或者,SELECT AVG(AGE)

FROM S,SC

WHERE S.S#=SC.S# AND C#='004'

(3)求LIU老师所授课程的每门课程的学生平均成绩。

SELECT CNAME,AVG(GRADE)

FROM SC ,C

WHERE SC.C#=C.C# AND TEACHER='LIU'

GROUP BY C#

(4)统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。

SELECT DISTINCT C#,COUNT(S#)

FROM SC

GROUP BY C#

HAVING COUNT(S#)>10

ORDER BY 2 DESC, C# ASC

(5)检索学号比WANG同学大,而年龄比他小的学生姓名。

SELECT X.SNAME

FROM S AS X, S AS Y

WHERE Y.SNAME='WANG' AND X.S#>Y.S# AND X.AGE

(6)检索姓名以WANG打头的所有学生的姓名和年龄。

SELECT SNAME,AGE

FROM S

WHERE SNAME LIKE 'WANG%'

(7)在SC中检索成绩为空值的学生学号和课程号。

SELECT S#,C#

FROM SC

WHERE GRADE IS NULL

(8)求年龄大于女同学平均年龄的男学生姓名和年龄。

SELECT SNAME,AGE

FROM S AS X

WHERE X.SEX='男' AND X.AGE>(SELECT AVG(AGE)FROM S AS Y WHERE Y.SEX='女')

(9)求年龄大于所有女同学年龄的男学生姓名和年龄。

SELECT SNAME,AGE

FROM S AS X

WHERE X.SEX='男' AND X.AGE>ALL(SELECT AGE FROM S AS Y WHERE Y.SEX='女')

3.7 试用SQL更新语句表达对教学数据库中三个基本表S、SC、C的各个更新操作:

(1)往基本表S中插入一个学生元组(„S9‟,„WU‟,18)。

INSERT INTO S(S#,SNAME,AGE)VALUES('59','WU',18)

(2)在基本表S中检索每一门课程成绩都大于等于80分的学生学号、姓名和性别,并把检索到的值送往另一个已存在的基本表STUDENT(S#,SANME,SEX)。

INSERT INTO STUDENT(S#,SNAME,SEX)

SELECT S#,SNAME,SEX

FROM S WHERE NOT EXISTS

(SELECT * FROM SC WHERE

GRADE<80 AND S.S#=SC.S#)

(3)在基本表SC中删除尚无成绩的选课元组。

DELETE FROM SC

WHERE GRADE IS NULL

(4)把WANG同学的学习选课和成绩全部删去。

DELETE FROM SC

WHERE S# IN

(SELECT S#

FROM S

WHERE SNAME='WANG')

(5)把选修MATHS课不及格的成绩全改为空值。

UPDATE SC

SET GRADE=NULL

WHERE GRADE<60 AND C# IN

(SELECT C#

FROM C

WHERE CNAME='MATHS')(6)把低于总平均成绩的女同学成绩提高5%。

UPDATE SC

SET GRADE=GRADE*1.05

WHERE GRADE<(SELECT AVG(GRADE)FROM SC)AND S# IN(SELECT S# FROM S WHERE SEX='F')

(7)在基本表SC中修改C4课程的成绩,若成绩小于等于75分时提高5%,若成绩大于75分时提高4%(用两个UPDATE语句实现)。

UPDATE SC

SET GRADE=GRADE*1.05

WHERE C#='C4' AND GRADE<=75

UPDATE SC

SET GRADE=GRADE*1.04

WHERE C#='C4' AND GRADE>75

3.8 在第1章例1.4中提到“仓库管理”关系模型有五个关系模式:

零件 PART(P#,PNAME,COLOR,WEIGHT)

项目 PROJECT(J#,JNAME,DATE)

供应商 SUPPLIER(S#,SNAME,SADDR)

供应 P_P(J#,P#,TOTOAL)

采购 P_S(P#,S#,QUANTITY)

(1)试用SQL DDL语句定义上述五个基本表,并说明主键和外键。

CREATE TABLE PART

(P# CHAR(4)NOT NULL,PNAME CHAR(12)NOT NULL,COLOR CHAR(10),WEIGHT REAL,PRIMARY KEY(P#))

CREATE TABLE PROJECT

(J# CHAR(4)NOT NULL,JNAME CHAR(12)NOT NULL,DATE DATE,PRIMARY KEY(J#))

CREATE TABLE SUPLIER

(S# CHAR(4)NOT NULL,SNAME CHAR(12),SADDR VARCHAR(20),PRIMARY KEY(S#))

CREATE TABLE P_P

(J# CHAR(4),P# CHAR(4),TOTAL INTEGER,PRIMARY KEY(J#,P#),FOREIGN KEY(J#)REFERENCE PROJECT(J#),FOREIGN KEY(P#)REFERENCE PART(P#))

CREATE TABLE P_S

(P# CHAR(4),S# CHAR(4),QUANTITY INTEGER,PRIMARY KEY(P#,S#),FOREIGN KEY(P#)REFERENCE PART(P#),FOREIGN KEY(S#)REFERENCE SUPLIER(S#))

(2)试将PROGECT、P_P、PART三个基本表的自然联接定义为一个视图VIEW1,PART、P_S、SUPPLIER 三个基本表的自然联接定义为一个视图VIEW2。

CREATE VIEW VIEW1(J#,JNAME,DATE,P#,PNAME,COLOR,WEIGHT,TOTAL)

AS SELECT PROJECT.J#,JNAME,DATE,PART.P#,PNAME,COLOR,WEIGHT,TOTAL

FROM PROJECT,PART,P_P

WHERE PART.P#=P_P.P# AND P_P.J#=PROJECT.J

#

CREATE VIEW VIEW2(P#,PNAME,COLOR,WEIGHT,S#,SNAME,SADDR,QUANTITY)

AS SELECT PART.P#,PNAME,COLOR,WEIGHT,SUPPLIER.S#,SNAME,SADDR,QUANTITY

FROM PART,P_S,SUPPLIER

WHERE PART.P#=P_S.P# AND P_S.S#=SUPPLIER.S#

(3)试在上述两个视图的基础上进行数据查询:

1)检索上海的供应商所供应的零件的编号和名字。

SELECT P#,PNAME FROM VIEW2 WHERE SADDR='SHANGHAI'

2)检索项目J4所用零件的供应商编号和名字。

SELECT S#,SNAME FROM VIEW2 WHERE P# IN(SELECT P# FROM VIEW1 WHERE J#='J4')

3.9 对于教学数据库中基本表SC,已建立下列视图:

CREATE VIEW S_GRADE(S#,C_NUM,AVG_GRADE)

AS SELECT S#,COUNT(C#),AVG(GRADE)

FROM SC

GROUP BY S#

试判断下列查询和更新是否允许执行。若允许,写出转换到基本表SC上的相应操作。

(1)SELECT *

FROM S_GRADE

允许

SELECT S#,COUNT(C#),AVG(GRADE)FROM SC GROUP BY S#

(2)SELECT S#,C_NUM

FROM S_GRADE

WHERE AVG_GRADE>80

允许

SELECT S#,COUNT(C#)FROM SC WHERE AVG(GRADE)>80

(3)SELECT S#,AVG_GRADE

FROM S_GRADE

WHERE C_NUM>(SELECT C_NUM

FROM S_GRADE

WHERE S#=„S4‟)

允许

SELECT S#,AVG(GRADE)

FROM SC AS X

WHERE COUNT(X.C#)>(SELECT COUNT(Y.C#)FROM SC AS Y WHERE Y.S#='S4')

GROUP BY S#

(4)UPDATE S_GRADE

SET C_NUM=C_NUM+1

WHERE S#=„S4‟

不允许

(5)DELETE FROM S_GRADE

WHERE C_NUM>

4不允许

3.10 预处理方式对于嵌入式SQL的实现有什么重要意义? 预处理方式是先用预处理程序对源程序进行扫描,识别出SQL语句,并处理成宿主语言的函数调用形式; 然后再用宿主语言的编译程序把源程序编译成目标程序。这样,不用扩充宿主语言的编译程序,就能处理SQL语句。

3.11 在宿主语言的程序中使用SQL语句有哪些规定?

在宿主语言的程序中使用SLQ语句有以下规定:

(1)在程序中要区分SQL语句与宿主语言语句

(2)允许嵌入的SQL语句引用宿主语言的程序变量(称为共享变量),但有两条规定:

1)引用时,这些变量前必须加“:”作为前缀标识,以示与数据库中变量有区别。

2)这些变量由宿主语言的程序定义,并用SQL的DECLARE语句说明。

(3)SQL的集合处理方式与宿主语言单记录处理方式之间要协调。需要采用游标机制,把集合操作转换成单记录处理方式。

3.12 SQL的集合处理方式与宿主语言单记录处理方式之间如何协调?

由于SQL语句处理的是记录集合,而宿主语言语句一次只能处理一个记录,因此需要用游标(cousor)机制,把集合操作转换成单记录处理方式。

2.13 嵌入式SQL语句何时不必涉及到游标?何时必须涉及到游标?

(1)INSERT、DELETE、UPDATE语句,查询结果肯定是单元组时的SELECT语句,都可直接嵌入在主程序中使用,不必涉及到游标。

(2)当SELECT语句查询结果是多个元组时,此时宿主语言程序无法使用,一定要用游标机制把多个元组一次一个地传送给宿主语言处理。

第五篇:大自然的语言习题及答案

阅读下面的语段,完成后面的练习,大自然的语言习题精选。

立春过后,大地渐渐地沉睡中_______(A.清醒B.苏醒;C.醒悟;D.复苏)过来。冰雪融化,草木萌发,各种花_________(A.次第;B.连续;C.陆续;D.全部)开放。再过两个月,燕子翩然归来。不久,布谷鸟也来了。于是转入炎热的夏季,这是植物孕育果实的时期。到了秋天,果实成熟,植物的叶子渐渐变黄,在秋风中_________(A.纷纷;B.刷刷;C.簌簌)的落下来。北雁南飞,活跃在田间草际的昆虫也都销声匿迹。到处呈现一片衰草连天的景象,准备迎接风雪载途寒冬。在地球上温带和亚热带区域里,年年如是,周而复始。

几千年来,劳动人民注意了草木荣枯、候鸟去来等自然现象同气候的关系,据以安排农事,语文试题《大自然的语言习题精选》。杏花开了,就好像大自然在传语要赶快耕地;桃花开了,又好像在暗示要赶快种谷子。布谷鸟开始唱歌,劳动人民懂得它在唱什么:“阿公阿婆,割去播禾。”这#者来,花香鸟语,草长莺飞,都是大自然的语言。

1.在语段中横线上填上恰当的词语。

2.解释词语:销声匿迹翩然

3.这两段文字的说明对象是()

A.物候现象;B.气候变化;C.物候规律;D.四季景色变化。

4.这两段文字的说明顺序是:____________________________,主要的说明方法是:_____________________________

5.去掉“温带和亚热带区域里”这几个词语行不行,为什么?

参考答案:

1.B A C

2.销声匿迹:这里指消失了鸣叫声,藏起了形迹。翩然:动作轻快的样子。

3.A

4.时间顺序;举例子

5.不行。以上情况只限于“地球上温带和亚热带区域里”。

下载C语言苏小红版第六章习题答案[推荐]word格式文档
下载C语言苏小红版第六章习题答案[推荐].doc
将本文档下载到自己电脑,方便修改和收藏,请勿使用迅雷等下载。
点此处下载文档

文档为doc格式


声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:645879355@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。

相关范文推荐

    C语言课后习题答案5篇

    C语言课后习题答案-第四版- 第一章 5、请参照本章例题,编写一个C语言程序,输出以下信息: **************************** V e r y G o o d ! **************************** #inc......

    c语言备考习题及部分答案

    一、 判断题 1. 格式字符%md中的m不能为负。( × ) 2. 实际上,可以用顺序、分支、循环三种结构构造任何算法。 3. printf("%d",sizeof(1234));其结果是4。( ×) 4. 在C语言......

    8下苏科版物理补充习题答案

    柯哀王道工作室真诚为您服务 Pageof 4 柯哀王道工作室真诚为您服务 Pageof 4 柯哀王道工作室真诚为您服务 Pageof 4 柯哀王道工作室真诚为您服务 Pageof 4......

    C语言程序设计教程课后习题答案

    C语言程序设计教程课后习题答案第一章 C语言程序设计概述 -习题答案 1 算法的描述有哪些基本方法?答 1、自然语言 2、专用工具2 C语言程序的基本结构是怎样的?举一个例子说明。......

    C语言程序设计教程第九章习题答案

    1、li 45 300.0 chang 30 200.0 chang 2、 #include struct students { char sid[100]; char name[100]; float score; }student; void main { int i; float j; pri......

    c语言备考习题及部分答案(精选五篇)

    一、判断题 1. 在函数定义中,形参变量有时也可以是全局变量。( × 2. 在C程序中,函数调用可以作为函数的形参。× 3. 在C程序中,函数调用可以作为函数的实参。√ 4. 在C程序......

    影视动画试听语言课本课后习题答案整理

    一、 名词解释 1、视听语言: 就是利用视觉和听觉的双重刺激向受众传播某种信息的一种感性语言,是电影的艺术手段,是利用镜头与镜头之间的组合来表达特殊意义,讲述故事,表达内容与......

    C语言习题

    第一章 C语言概述 1.1选择题 【题1.1】以下不是C语言的特点的是 A.C语言简洁、紧凑 B.能够编制出功能复杂的程序 C. C语言可以直接对硬件进行操作 D.C语言移植性好 【题1.2】......