您好,欢迎来到汇意旅游网。
搜索
您的当前位置:首页C语言程序设计(谭浩强)第八章详细课后答案

C语言程序设计(谭浩强)第八章详细课后答案

来源:汇意旅游网


#if 0 //8.1

#include

#include

void main()

{

int Common_divisor(int a,int b);

int Common_mutiple(int m,int n);

int result_1,result_2;

int number_1,number_2;

printf(\"Enter number_1 and number_2 value:\");

scanf(\"%d,%d\

result_1 = Common_divisor(number_1,number_2);

result_2 = Common_mutiple(number_1,number_2);

printf(\"最大公约数:%d\\n最小公倍数:%d\\n\

}

int Common_divisor(int a,int b)

{

int r = 1;

while(r != 0)

{

if(a>b)

{

r = a%b;

a = b;

b = r;

if(r == 0)

{

return a;

break;

}

}

else

{

r = b%a;

b = a;

if(r == 0)

{

return b;

break;

}

a = r;

}

}

}

int Common_mutiple(int m,int n)

{

int Common_divisor(int a,int b);

int temp = 0,result = 0;

temp = Common_divisor(m,n);

result = (m*n)/temp;

return result;

}

#endif

#if 0 //8.2

#include

#include

float result_1 =0.0,result_2 = 0.0;

void main()

{

void Funvtion_1(float a,float b,float temp);

void Function_2(float a,float b);

float a,b,c;

float temp;

printf(\"Enter a,b,c value:\");

scanf(\"%f,%f,%f\

temp = b*b-4*a*c;

if(temp > 0)

{

Funvtion_1(a,b,temp);

printf(\"x1 = %.3f,x2 = %.3f\\n\

}

else if(temp < 0)

{

printf(\"此函数没有根!\\n\");

}

else

{

Function_2(a,b);

printf(\"x1 = x2 = %.3f\\n\

}

}

void Funvtion_1(float a,float b,float temp)

{

result_1 = (-b+temp)/(2*a);

result_2 = (-b-temp)/(2*a);

}

void Function_2(float a,float b)

{

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

}

#endif

#if 0 //8.3

#include

#include

void main()

{

void Prime(int n);

int m;

printf(\"Enter m value:\");

scanf(\"%d\

Prime(m);

}

void Prime(int n)

{

int i = 0 ,k;

k = (int)sqrt(n);

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

{

if(n%i == 0)

{

break;

}

}

if(i > k)

{

printf(\"The number %d is a prime!\\n\

}

else

{

printf(\"The number %d isn't a prime!\\n\

}

}

#endif

#if 0

#include

#include

int c[3][3];

void main()

{

void Function(int b[3][3]);

int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

int i,j;

// int d[3][3];

int k = sizeof(a[2])/sizeof(int); //列数

int n = sizeof(a)/sizeof(int);

for(i = 0; i < n/k;i++)

{

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

{

printf(\"%d \

}

printf(\"\\n\");

}

Function(a);

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

{

for(j = 0;j < n/k;j++)

{

printf(\"%d \j]);

}

printf(\"\\n\");

}

}

void Function(int b[3][3])

{

int i = 0,j = 0;

int k = sizeof(b[3])/sizeof(int);

int n = sizeof(b)/sizeof(int);

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

{

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

{

c[j][i] = b[i][j];

}

}

}

#endif

#if 0

#include

#include

#include

void main()

{

void Function(char a[100],char b[100]);

char a[100];

extern int b[100];

int n = sizeof(a);

printf(\"Enter a Array:\");

gets(a);

Function(a,b);

}

void Function(char a[],char b[])

{

int i = 0;

int n = sizeof(a)/sizeof(char);

int m = strlen(a);

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

{

b[i] = a[m-1-i];

}

//printf(\"put b Array:\");

//puts(b);

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

{

putchar(b[i]);

}

}

#endif

#if 0 //8.6

#include

#include

void main()

{

char a[20];

char b[10];

printf(\"Enter string a value:\");

gets(a);

printf(\"Enter string b value:\");

gets(b);

strcat(a,b);

puts(a);

}

#endif

#if 0 //8.6

#include

#include

#include

int a[20];

void main()

{

void Connect(char a[],char b[]);

char b[10];

printf(\"Enter string a value:\");

gets(a);

printf(\"Enter string b value:\");

gets(b);

Connect(a,b);

// puts(a);

printf(\"%s\

}

void Connect(char a[],char b[])

{

int i = 0;

int j = 0;

while(a[i] != '\\0')

{

i++;

}

while(b[j] != '\\0')

{

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

}

// a[i] = '\\0';

}

#endif

#if 0//8.7

#include

#include

#include

char b[100];

void main()

{

void Research_vowel(char a[]);

char a[50];

printf(\"Enter string a value:\");

gets(a);

Research_vowel(a);

puts(b);

}

void Research_vowel(char a[])

{

int i = 0,j = 0;

for(; i < strlen(a);i++)

{

if(a[i] == 'a'|| a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u')

{

b[j] = a[i];

j++;

}

}

}

#endif

#if 0//8.8

#include

#include

char a[4];

void main()

{

void Divide(int n);

int n = 0,i = 0;

printf(\"Enter n value(<10000):\");

scanf(\"%d\

Divide(n);

for(;i<4;i++)

{

printf(\"%d \

}

printf(\"\\n\");

}

void Divide(int n)

{

int i = 0,c = 0,b = 0,m = 1000;

while(m >= 1)

{

c = n/m;

b = n%m;

m = m/10;

a[i] = c;

n = b;

i++;

}

}

#endif

#if 0//8.9

#include

#include

#include

int m = 0,n = 0,r = 0,l = 0;

void main()

{

void Research_number(char a[]);

char a[50];

int i = 0;

printf(\"enter string value:\");

gets(a);

/*for(;i{

printf(\"%c\

}*/

puts(a);

puts(\"\");

Research_number(a);

printf(\"字母个数是:%d,数字个数是:%d,空格个数是:%d,其它:%d\\n\

}

void Research_number(char a[])

{

int i = 0;

for(i = 0;i{

if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z'))

{

m++;

}

else if(a[i] > 47 && a[i] < 58)

{

n++;

}

else if(a[i] == 32)

{

r++;

}

else

{

l++;

}

}

}

#endif

#if 0 //8.10

#include

#include

#include

void main()

{

void Array(char a[]);

char b[20];

printf(\"Enter b string(<20):\");

gets(b);

printf(\"Longest word is : \");

Array(b);

}

void Array(char a[])

{

int i = 0,place = 0,point = 0,length = 1; //point表示最长单词的开始位置 place表示最长单词最后的一个位置

int max = 0,j = 0;

int flag = 0; //为0 时是空格 为1时是单词

max = length;

for(i = 0;i<=strlen(a);i++)

{

if(a[i] != ' ' && a[i] != '\\0')

{

if(0 == flag)

{

j = i;

flag = 1;

}

else

length++; //这个题让我做的非常的难过 以后要好好的复习

}

else

{

flag = 0;

if(max < length)

{

max = length;

point = j;

place = i;

}

length = 1;

}

}

for(i = point;i <= place;i++)

{

printf(\"%c\

}

printf(\"\\n\");

}

/*

int judge_words(char b[20])

{

int i = 0,flag = 1;

for(i = 0;i < strlen(b); i++)

{

if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z'))

{

flag = 1;

}

else

{

flag = 0;

}

}

}*/

#endif

#if 0 //8.11

#include

#include

void main()

{

void bubbling(char a[]);

char a[10];

printf(\"enter string a value:\");

gets(a);

bubbling(a);

puts(a);

}

void bubbling(char a[])

{

int i = 0,j = 0;

char temp;

for(i = 0;i{

for( j = 0;j{

if(a[j] > a[j+1])

{

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

}

}

#endif

#if 0 //8.12

#include

#include

void main()

{

float Function(float x);

float x = 1.5,x1 = 0.0;

x1 = Function(x);

printf(\"%.3f\\n\

}

float Function(float x)

{

float x1;

float y,y1;

while(fabs(y1)>=1e-6)

{

x = x1;

y1 = x*x*x+2*x*x+3*x+4;

y = 3*x*x+4*x+3;

x1 = x - (y1/y);

}

return x1;

}

#endif

#if 0//8.13

#include

#include

void main()

{

float p(float x,int n);

float x = 0.0,result = 0.0;

int n = 0;

printf(\"Enter x and n value:\");

scanf(\"%f,%d\

result = p(x,n);

printf(\"p(x) value :%.3f\\n\

}

float p(float x,int n)

{

float y = 0.0;

if(n == 0)

{

y = 0;

}

else if(n == 1)

{

y = x;

}

else

{

y = ((2*n-1)*x-p(x,n-1)-(n-1)*p(x,n-2))/n;

}

return y;

}

#endif

#if 0//8.14

#include

#include

int r = 0,l = 0;

void main()

{

float* EStudent_aver(float scores[10][5]);

float* EClass_aver(float scores[10][5]);

float Maxscore(float scores[10][5]);

float variance(float scores[10][5]);

float scores[10][5] =

{{76,43,46,73,46},{34,67,,32,47},{31,68,73,16,87},{31,67,31,37,63},{16,37,61,67,86},

{31,34,60,49,29},{,,87,87,98},{97,34,,42,},{45,67,34,18,},{32,16,87,42,15}};

float *Student_aver,*Class_aver,Student_max = 0.0,aver_variance = 0.0;

int i = 0,j = 0,k = 0;

/* char Student_name[] = {\"Zhanghua Xiaoming Ligang Xiaotao Zhangsan Xiaoqiang CBC Baixu Gongcheng BOBO \

char score_name[] = {\"Chinese Math English Chemitry WuLi\

printf(\"\\");

for(i = 0;i < strlen(score_name);i++)

{

printf(\"%c\

}

puts(\"\");

for(i = 0;i < strlen(Student_name);i++)

{

printf(\"%c\

if(Student_name[i] == 32)

{

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

{

printf(\" %.0f\\j]);

}

k++;

puts(\"\");

}

}*/

/* printf(\"\\n\");

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

{

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

{

printf(\" %.0f\\j]);

}

puts(\"\");

}*/

// float scores[10][5];

printf(\"NO. course1 course2

course3 course4 course5\\n\");

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

{

printf(\"No.%2d\\

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

{

printf(\"%.2f\ \j]);

}

puts(\"\");

}

Student_aver = EStudent_aver(scores);

Class_aver = EClass_aver(scores);

Student_max = Maxscore(scores);

aver_variance = variance(scores);

printf(\"Student_aver:\\n\");

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

{

printf(\"%.2f \

}

printf(\"\\nClass_aver:\\n\");

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

{

printf(\"%.2f \

}

printf(\"\\n\");

printf(\"The Max Score:\\n %.2f Name: No.%d course%d\\n\

printf(\"Student average Variance: \\n %.2f\\n\

, Course:

}

float * EStudent_aver(float scores[10][5])

{

static float aver[10],sum = 0.0;

int i = 0,j = 0;

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

{

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

{

sum += scores[i][j];

}

aver[i] = sum/5;

sum = 0;

}

return aver;

}

float* EClass_aver(float scores[10][5])

{

static float aver_1[5],sum = 0.0;

int i = 0,j = 0;

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

{

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

{

sum +=scores[i][j];

}

aver_1[j] = sum/10;

sum = 0;

}

return aver_1;

}

float Maxscore(float scores[10][5])

{

int i = 0,j = 0;

float max = scores[0][0];

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

{

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

{

if(max < scores[i][j])

{

max = scores[i][j];

r = i + 1;

l = j + 1;

}

}

}

return max;

}

float variance(float scores[10][5])

{

float* EStudent_aver(float scores[10][5]);

float s = 0.0;

float sum_1 = 0.0,sum_2 = 0.0;

float number_1 = 0.0,number_2 = 0.0;

float* student_aver;

int i = 0,j = 0;

student_aver = EStudent_aver(scores);

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

{

sum_1 += student_aver[i]*student_aver[i];

sum_2 += student_aver[i];

}

number_1 = sum_1/10;

number_2 = (sum_2/10)*(sum_2/10);

s = number_1 - number_2;

return s;

}

#endif

#if 0 //测试 static

#include

#include

static j;

void f1()

{

static i = 0;

i++;

}

void f2()

{

j = 0;

j++;

}

void main()

{

int k = 0;

for(k = 0;k < 10;k++)

{

f1();

f2();

}

}

#endif

#if 0//8.16

#include

#include

void main()

{

void Change(int n);

int m = 0;

printf(\"Enter ox number:\");

scanf(\"%x\

// printf(\"%d\\n\

Change(m);

}

void Change(int n)

{

//int m;

printf(\"十进制是: %d\\n\

}

#endif

#if 0//8.17

#include

#include

void main()

{

void Function(int n);

int m;

printf(\"Input a integer:\");

scanf(\"%d\

Function(m);

}

void Function(int n)

{

int i;

/* if(n!=0)

{

ch[i] = n%10;

temp = n/10;

i++;

Function(temp);

}

else

{

ch[i] = 0;

}

return ch;*/

i = n/10;

if(i != 0)

{

Function(i);

}

putchar(n%10+'0');

putchar(32);

}

#endif

#if 0 //今天是今年的多少天

#include

#include

void main()

{

int i = 1,d = 0,sum = 0;

int year,month,day;

printf(\"Enter year and month and day(2012/08/19):\");

scanf(\"%d/%d/%d\

sum = day;

for(;i{

switch(i)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: d = 31; break;

case 2:

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

{

d = 29;

}

else

{

d = 28;

}

break;

case 4:

case 6:

case 9:

case 11: d = 30;break;

}

sum += d;

}

printf(\"The %d of The year dates!\\n\

}

#endif

#if 0 //8.15

#include

#define N 10

void main()

{

void Enter_massage(char staff_name[N][10],int staff_number[N]);

void Sort(char staff_name[N][10],int staff_number[N]);

int middle_search(int staff_number[N],int number);

int number,temp = 0;

char staff_name[N][10];

int staff_number[N];

Enter_massage(staff_name,staff_number);//输入信息

Sort(staff_name,staff_number);

printf(\"Enter search staff_number:\");

scanf(\"%d\

temp = middle_search(staff_number,number);

printf(\"The staff number is : %d\\n\

printf(\"The staff name is : %s\\n\

}

void Enter_massage(char staff_name[N][10],int staff_number[N])

{

int i;

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

{

printf(\"Enter Staff number No.:\");

scanf(\"%d\

fflush(stdin);

printf(\"Input staff name:\");

gets(staff_name[i]);

}

}

void Sort(char staff_name[N][10],int staff_number[N])

{

int i,j;

int temp1;

char temp[N][10];

for(i = 0;i < N - 1; i++)

{

for(j = 0;j < N - 1 - i; j++)

{

if(staff_number[j]>staff_number[j+1])

{

temp1 = staff_number[j];

strcpy(temp[j],staff_name[j]);

staff_number[j] = staff_number[j+1];

strcpy(staff_name[j],staff_name[j+1]);

staff_number[j+1] = temp1;

strcpy(staff_name[j+1],temp[j]);

}

}

/*if(staff_number[i]{

temp1 = staff_number[i];

staff_number[i] = staff_number[i+1];

staff_number[i+1] = temp1;

}

*/

}

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

{

printf(\"staff number is : %d\\n\

printf(\"staff name is %s\\n\

}

}

int middle_search(int staff_number[N],int number) {

int min = 0,high = 0,mid = 0;

int i = 0;

min = 0;

high = N - 1;

while(min <= high)

{

mid = (min+high)/2;

//折半查找法!

if(staff_number[mid] > number)

{

high = mid - 1;

}

else if(staff_number[mid] < number)

{

min = mid + 1;

}

else

{

return mid;

}

}

}

#endif

/*fasdfa the world hello

int flag=0;

flag=1;//letter

flag=0;//space*/

#if 0 //有问题 输出的是不对的! 为什么指针值temp+1的值不是指向的name[1]这一行的首地址 而是++后指向的是name[0]下一列的地址呢?

#include

void main()

{

char name[5][10];

char temp[5][10];

int i = 0;

// temp = &name[0];

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

{

gets(name[i]);

strcpy(temp[i],name[i]);

}

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

{

//puts(name[i]);

puts(temp[i]);

}

}

#endif

#if 0 //看不懂的程序 8.15

#define N 10

find(a,b)

int a[],b[];

{

int i,j,s,t,c[N][2];

for(i=0;i{

c[i][1]=a[i];c[i][1]=i;

} for(i=0;ifor(j=0;jif(c[i][0]>c[i+1][0])

{

t=c[i][0];

c[i][0]=c[i+1][0];

c[i+1][0]=t;

s=c[i][1];

c[i][1]=c[i+1][1];

c[i+1][1]=s;

}

for(i=0;ib[i]=c[i][1];

return;

}

lookfor(h,k)

int h[],k;

{

int i,j;

for(i=0;iif(h[i]-k==0) j=i;

return j;

}

main()

{

int number[N],x[N],i,j,u,p;

char name[N][20];

for(i=0;i{

gets(name[i]);

scanf(\"%d\

} scanf(\"%d\

find(number,x);

u=lookfor(number,p);

for(i=0;i{

printf(\"%d\

puts(name[x[i]]);

}

puts(name[x[u]]);

}

#endif

#if 1 //16进制转换成10进制

#include

#include

#define N 10

int flag = 1;

void main()

{

extern int sum;

int convert(char str[],int n);

char str[N];

int s = 0,n = 0;

puts(\"Enter 16 OX number:\");

gets(str);

n = strlen(str);

s = convert(str,n);

if(1 == flag)

{

printf(\"10 d number is : %d\\n\

}

else

{

printf(\"The number isn't OX \\n\");

}

}

int convert(char str[],int n)

{

int i,j = 0;

int sum = 0;

for(i = n - 1;i > -1;i--,j++)

{

if(str[i] > 47 && str[i] < 58)

{

sum = sum + (str[i] - 48) * (int)pow(16,j);

}

else if(str[i] > && str[i] < 71)

{

sum = sum + (str[i] - 55)* (int)pow(16,j);

}

else if(str[i] > 96 && str[i] < 103)

{

sum = sum + (str[i] - 87) * (int)pow(16,j);

}

else

{

flag = 0;

}

}

return sum;

}

#endif

#if 0

#include

#include

void main()

{

printf(\"%d\\n\

}

#endif

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- hids.cn 版权所有 赣ICP备2024042780号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务