补全下面的C++代码,用递归下降语法分析代码方法求行列式的值 #include<iostream> using namespace

当前位置: >>
c++程序设计-答案
第一章 1.5 题 #include &iostream& int main() { cout&&&This&&&&is&; cout&&&a&&&&C++&; cout&&&p
rogram.&; return 0; 1.6 题 #include &iostream& int main() { int a,b,c; a=10; b=23; c=a+b; cout&&&a+b=&; cout&&c; cout&& return 0; } 1.7 七题 #include &iostream& int main() { int a,b,c; int f(int x,int y,int z); cin&&a&&b&&c; c=f(a,b,c); cout&&c&& return 0; } int f(int x,int y,int z) { if (x&y) m=x; else m=y; if (z&m) m=z; return(m); } 1.8 题 #include &iostream&int main() { int a,b,c; cin&&a&&b; c=a+b;\&C++isvery cout&&setiosflags(ios::fixed) &&setiosflags(ios::right) &&setprecision(2); cout&&&l= &&&setw(10)&&l&& cout&&&s= &&&setw(10)&&s&&interesting!\&&&& '\n'; return 0; } 2.7 题 #include &iostream&cout&&&a+b=&&&a+b&& return 0; } 1.9 题 #include &iostream& int main() { int a,b,c; int add(int x,int y); cin&&a&&b; c=add(a,b); cout&&&a+b=&&&c&& return 0; } int main() {int i=8; j=10; m=++i+j++; n=(++i)+(++j)+m; i,j,m,n;cout&&&sq=&&&setw(10)&&sq &&cout&&&vq=&&&setw(10)&&vq &&cout&&i&&'\t'&&j&&'\t'&&m&&' \t'&&n&& return 0; } 2.8 题 #include &iostream& int main() } 3.3 题 #include &iostream& int main () {float c,f; cout&&& 请 输 入 一 个 华 氏 温度:&; cin&&f; c=(5.0/9.0)*(f-32); //注意 5 和 9 要用实型表示, 否则 5/9 值为 0 cout&&& 摄 氏 温 度 为:&&&c&& return 0; }; 3.4 题 #include &iostream& int main ( ) {char c1,c2; cout&&& 请 输 入 两 个 字 符 c1,c2:&; c1=getchar(); // 将输入的第一个字符赋给 c1 c2=getchar(); // 将输入的第二个字符赋给 c2 cout&&&vz=&&&setw(10)&&vz && return 0;int add(int x,int y) { z=x+y; return(z); } 2.3 题 #include &iostream& int main() {char c1='a',c2='b',c3='c',c4='\101' ,c5='\116'; cout&&c1&&c2&&c3&&'\n';{char c1='C', c2='h', c3='i', c4='n', c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; cout&&&password is:&&&c1&&c2&&c3&&c4&&c5& & return 0; } 3.2 题 #include &iostream&cout&&&\t\b&&&c4&&'\t'&&c5 &&'\n'; return 0; } 2.4 题 #include &iostream& int main() {char c1='C',c2='+',c3='+'; cout&&&I \&&&&c1&&c2&&c3&&'\&'; cout&&&\t\t&&&&He says: say:#include &iomanip& int main ( ) {float h,r,l,s,sq,vq, const float pi=3.1415926; cout&&&please enter r,h:&; cin&&r&&h; l=2*pi*r; s=r*r* sq=4*pi*r*r; vq=3.0/4.0*pi*r*r*r; vz=pi*r*r*h; cout&&&用 putchar 函数输 出结果为:&; putchar(c1); putchar(c2); cout&& cout&&&用 cout 语句输出 结果为:&; cout&&c1&&c2&& return 0; } 3.4 题另一解 #include &iostream& int main ( ) {char c1,c2; cout&&& 请 输 入 两 个 字 符 c1,c2:&; c1=getchar(); // } 将输入的第一个字符赋给 c1 c2=getchar(); // 将输入的第二个字符赋给 c2 cout&&&用 putchar 函数输 出结果为:&; putchar(c1); putchar(44); putchar(c2); cout&& cout&&&用 cout 语句输出 结果为:&; cout&&c1&&&,&&&c2&& return 0; } 3.5 题 #include &iostream& int main ( ) {char c1,c2; int //定义为整型? cout&&& 请 输 入 两 个 整 数 i1,i2:&; cin&&i1&&i2; c1=i1; c2=i2; cout&&& 按 字 符 输 出 结 果 为:&&&c1&&& , &&&c2&& i1,i2; }return 0; /* 3.8 题 #include &iostream& int main ( ) { int a=3,b=4,c=5,x,y; cout&&(a+b&c b==c)&& cout&&(a||b+c b-c)&& cout&&(!(a&b) && !c||1)&& cout&&(!(x=a) && (y=b) && 0)&& cout&&(!(a+b)+c-1 b+c/2)&& return 0; 3.9 题 include &iostream& int main ( ) {int a,b,c; cout&&&please enter && } && && */ /*temp=(a&b)?a:b; 将 a 和 b 中的大者存入 temp 中 */ max=(temp&c)?temp:c; 将 a 和 b 中的大者与 c 比较,最大者存入 maxof student:&; cin&& while (score&100||score&0) {cout&&&data error,enter data again.&; cin&& } switch(int(score/10))cout&&&max=&&&max&& return 0;{case 10: case grade='A'; case 8: 9:3.10 题 #include &iostream& int main ( ) {int x,y; cout&&&enter x:&; cin&&x; if (x&1) {y=x; cout&&&x=&&&x&&&, y=x=&&&y; } else // 1≤x<10 {y=2*x-1; cout&&&x=&&&x&&&, y=2*x-1=&&&y; } else // x≥10 {y=3*x-11; if (x&10)grade='B'; case grade='C'; case grade='D'; default:grade='E'; } cout&&&score &&&score&&&, &&&grade&& return 0; } 3.12 题 #include &iostream& int main () { int indiv,ten,hundred,thousand, ten_thousand, grade is is 6: 7:three integer numbers:&; cin&&a&&b&&c; if(a&b) if(b&c) cout&&&max=&&&c; else cout&&&max=&&&b; else if (a&c) cout&&&max=&&&c; else cout&&&max=&&&a; cout&& return 0; } 3.9 题另一解 #include &iostream& int main ( ) {int a,b,c,temp, cout&&&please enter/*分别代表个位,十位,百位, cout&&&x=&&&x&&&, y=3*x-11=&&&y; } cout&& return 0; } 3.11 题 #include &iostream& int main () { cout&&&please enter score else else else */ cout&&&enter integer(0~99999):&; cin&& if (num&9999) place=5; if (num&999) place=4; if (num&99) place=3; if (num&9) an 千位,万位和位数three integer numbers:&; cin&&a&&b&&c; place=2; else place=1; case//利润为 100 万元时的奖金 cout&&&enter i:&; cin&&i; if (i&=100000) bonus=i*0.1; //利润在 10 万元以内按 10%提成奖金bon2=bon1+.075; bon4=bon2+.05; bon6=bon4+.03;3:cout&&hundred&&&,&&&ten cout&&&place=&&&place&& //计算各位数字 order:&; &&&,&&&indiv&& cout&&&reversebon10=bon6+.015 ; cout&&&enter i:&; cin&&i; c=i/100000;ten_thousand=num/10000;cout&&indiv&&ten&&hundred &&else if (i&=200000)thousand=(int)(num-ten_th ousand*1; casebonus=bon1+(i-.0 75; //利润在 10 万元至 20 万时的奖金 else if (i&=400000)if (c&10) switch(c) { casec=10;2:cout&&ten&&&,&&&indiv&&e hundred=(int)(num-ten_tho usand*10000-thousand*10 00)/100; order:&; cout&&&reverse0:bonus=i*0.1;1:bonus=bon2+(i-.0 5; //利润在 20 万元至 40 万时的奖金 else if (i&=600000)bonus=bon1+(i-.0 75; case 2: case 3:ten=(int)(num-ten_thousan d*10000-thousand*1000-hu ndred*100)/10;cout&&indiv&&ten&& case 1:cout&&indiv&&bonus=bon2+(i-.0 bonus=bon4+(i-.0 3; //利润在 40 万元至 60 万时的奖金 else if (i&=; case 4: case 5:indiv=(int)(num-ten_thousa nd*10000-thousand*1000-h undred*100-ten*10); cout&&&original order:&; switch(place) {case } order:&;cout&&&reversebonus=bon4+(i-.0 3;cout&&indiv&& bonus=bon6+(i-.0 15; //利润在 60 万元至 100 万时的奖金 elsecase 6: case 7: case 8: case 9:return 0; 5:cout&&ten_thousand&&&,& &&thousand&&&,&&&hundred &&&,&&&ten&&&,&&&indiv&&en } 3.13 题 #include &iostream& cout&&&reverse order:&; int main () { //i 为利润 float cout&&indiv&&ten&&hundred &&thousand&&ten_thousand && case 4:cout&&thousand&&&,&&&hu ndred&&&,&&&ten&&&,&&&indi v&& cout&&&reverse order:&; bon2=bon1+.075; //利润为 20 万元时的奖金 bon4=bon2+.05; //利润为 40 万元时的奖金 bon6=bon4+.03; //利润为 60 万元时的奖金 cout&&indiv&&ten&&hundred &&thousand&& bon10=bon6+.015; bonus,bon1,bon2,bon4,bon 6,bon10; bon1=.1; //利润为 10 万元时的奖金bonus=bon6+(i-.0 bonus=bon10+(i-1000000)* 0.01; //利润在 100 万元 以上时的奖金 15; case 10:bonus=bon10+(i-1000000)* 0.01;cout&&&bonus=&&&bonus&& return 0; } 3.13 题另一解 #include &iostream& int main () { float bonus,bon1,bon2,bon4,bon 6,bon10; bon1=.1; }}cout&&&bonus=&&&bonus&& return 0; 3.14 题 #include &iostream& int main () {int t,a,b,c,d; cout&&&enter numbers:&; cin&&a&&b&&c&&d; cout&&&a=&&&a&&&, four b=&&&b&&&, c=&&&c&&&,d=&&&d&& if (a&b) {t=a;a=b;b=t;} if (a&c) {t=a; a=c; c=t;} if (a&d) {t=a; a=d; d=t;} if (b&c) {t=b; b=c; c=t;} if (b&d) {t=b; b=d; d=t;} if (c&d) {t=c; c=d; d=t;} cout&&&the sequence:&&& cout&&a&&&, &&&b&&&, sorted// p 是原来两个整数的乘 积 return 0; }之和 a=a*10; ++i; } int main() {const int m=1000; // 定义寻找范围 intcout&&&a+aa+aaa+...=&&&sn 3.16 题 #include &iostream& int main () { int letters=0,space=0,digit=0,ot her=0; cout&&&enter line::&&& while((c=getchar())!='\n') {if (c&='a' && one && return 0; } 3.18 题 #include &iostream& int main () {float s=0,t=1; for (n=1;n&=20;n++) { t=t*n; s=s+t; 累加 } // 求 n! // 将各项k1,k2,k3,k4,k5,k6,k7,k8,k9,k 10; int i,a,n,s; for (a=2;a&=m;a++) // a 是 2~1000 之间的整数, 检查它是否为完数 {n=0; // n 用来累计 a 的因子的个 数 s=a; // s 用来存放尚未求出的因 子之和,开始时等于 a for (i=1;i&a;i++) 检查 i 是否为 a 的因子 if (a%i==0) // 如果 i 是 a 的因子 {n++; // n 加 1,表示新找到一个 因子 s=s-i; // s 减去已找到的因子,s 的新值是尚未求出的因子 //&&&c&&&, &&&d&& return 0; } 3.15 题 #include &iostream& int main () {int p,r,n,m, cout&&&please enter two positive integer numbers n,m:&; cin&&n&&m; if (n&m) {temp=n; n=m; m= //把大数放在 n 中, 小数放 在m中 } p=n*m; //先将 n 和 m 的乘积保存在 p 中, 以便求最小公倍数时 用 while (m!=0) //求 n 和 m 的最大公约数 {r=n%m; n=m; m=r; } cout&&&HCF=&&&n&& cout&&&LCD=&&&p/n&&c&='z'||c&='A' && c&='Z') letters++; else if (c==' ') space++; else if (c&='0' && c&='9') digit++; else other++; }cout&&&1!+2!+...+20!=&&&s& & return 0; } 3.19 题cout&&&letter:&&&letters&&&, space:&&&space&&&, digit:&&&digit&&&,#include &iostream& int main () {int i,j,k,n;之和 switch(n) // 将 找 到 的 因 子 赋 给 k1,...,k10 {case 1: k1=i; // 找出的笫 1 个因子赋给 k1 case 2: k2=i; // 找出的笫 2 个因子赋给 k2 case 3:other:&&&other&& return 0; } 3.17 题 #include &iostream& int main () {int a,n,i=1,sn=0,tn=0;cout&&&narcissus numbers are:&&& for (n=100;n&1000;n++) {i=n/100; j=n/10-i*10; k=n%10; if (n == i*i*i + j*j*j + k*k*k) cout&&n&&& &; } cout&&cout&&&a,n=:&; cin&&a&&n; while (i&=n) {tn=tn+a; //赋值 } 3.20 题 // 赋 后的 tn 为 i 个 a 组成数的值 sn=sn+ 值后的 sn 为多项式前 i 项k3=i;// 找出的笫 3 个因子赋给 k3 case 4: k4=i; // 找出的笫 4 个因子赋给 k4return 0;#include &iostream& case 5: k5=i; // 找出的笫 5 个因子赋给 k5 case 6: k6=i; // 找出的笫 6 个因子赋给 k6 case 7: k7=i; // 找出的笫 7 个因子赋给 k7 case 8: k8=i;cout&&&,&&&k6; if cout&&&,&&&k7; if cout&&&,&&&k8; if cout&&&,&&&k9; if cout&&&,&&&k10; cout&&endl&& } } return 0; } 3.20 题另一解 #include &iostream& } are:&; (n&9) (n&8) (n&7) (n&6) ┅k[10] }s=s-i; k[n]=i; // 将找到的因子赋给 k[1]// 第 1 天的桃子数是第 2 天桃子数加 1 后的 2 倍 x2=x1; day--; }if (s==0) {cout&&a&&& is a 完数 &&& cout&&&its factors cout&&&total=&&&x1&& return 0; } 3.23 题 #include &iostream& #include &cmath& int main() {float a,x0,x1; cout&&&enter a positive number:&; 3.21 题 #include &iostream& int main() {int i,t,n=20; double a=2,b=1,s=0; for (i=1;i&=n;i++) {s=s+a/b; t=a; a=a+b; // 将前一项分子与分母之和 作为下一项的分子 b=t; 项的分母 } // } 3.24 题 #include &iostream& cout&&&sum=&&&s&& return 0; } 3.22 题 #include &iostream& int main() {int day,x1,x2; day=9; x2=1; while(day&0) {x1=(x2+1)*2; int main() {int i,k; for (i=0;i&=3;i++) // 输出上面 4 行*号 {for (k=0;k&=2*i;k++) cout&&&*&; // 输出*号 cout&& //输出完一行*号后换行 } for (i=0;i&=2;i++) // 输出下面 3 行*号 {for 将前一项的分子作为下一 cin&&a; 输入 a 的值 x0=a/2; x1=(x0+a/x0)/2; do {x0=x1; x1=(x0+a/x0)/2; } while(fabs(x0-x1)&=1e-5); cout&&&The square root of &&&a&&& is &&&x1&& return 0; //for (i=1;i&n;i++) cout&&k[i]&&& &; cout&&k[n]&& }// 找出的笫 8 个因子赋给 k8 case 9: k9=i; // 找出的笫 9 个因子赋给 k9 case 10: k10=i; // 找出的笫 10 个因子赋给 k10 } } if 到了 {cout&&a&&& is a 完数 &&& cout&&&its are:&; if cout&&k1&&&,&&&k2; (n&1) // factors (s==0) // s=0 表示全部因子都已找return 0; } int main() {int m,s,i; for (m=2;m&1000;m++) {s=0; for (i=1;i&m;i++) if ((m%i)==0) s=s+i; if(s==m) {cout&&m&&& is a 完 数&&& cout&&&its factors are:&; for (i=1;i&m;i++) if cout&&i&&& &; cout&& } } return 0; } 3.20 题另一解 #include &iostream& int main() (n&3) {int k[11]; int i,a,n,s; for (a=2;a&=1000;a++) {n=0; (n&4) // s=a; for (i=1;i&a;i++) if ((a%i)==0) (n&5) {n++; (m%i==0)n&1 表示 a 至少有 2 个因子 if (n&2) cout&&&,&&&k3; // n&2 表示 至少有 3 个因子, 故应再输 出一个因子 if cout&&&,&&&k4; // n&3 表示 至少有 4 个因子, 故应再输 出一个因子 if cout&&&,&&&k5; 以下类似 if (k=0;k&=4-2*i;k++) cout&&&*&; // 输出*号 cout&& // 输出完一行*号后换行 } return 0; }int hcf(int u,int v) {int t,r; if (v&u) {t=u;u=v;v=t;} while ((r=u%v)!=0) {u=v; v=r;} return(v); }{smaller_than_zero(a,b);cout&&&input an integer:&; cin&&n;cout&&&x1=&&&p&&&+&&&q&& &i&&&if (prime(n)) cout&&n&&& prime.&&& is acout&&&x2=&&&p&&&-&&&q&&& i&&& } return 0; } }else cout&&n&&& is not a prime.&&& return 0;3.25 题 #include &iostream& int main() {char 手;k 是 c 的对手*/ 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 && k!='X' && k!='Z') (i!='X' i,j,k; 4.2 题 #include &iostream& #include &math.h& float x1,x2,disc,p,q; int main() {void greater_than_zero(float,floa t); void equal_to_zero(float,float); cout&&&A--&&&i&&& B--&&&j&&& C--&&&k&& void smaller_than_zero(float,floa t); float a,b,c; cout&&&input a,b,c:&; 4.1 题 #include &iostream& int main() {int hcf(int,int); int lcd(int,int,int); int u,v,h,l; cin&&u&&v; h=hcf(u,v); cout&&&x1=&&&x1&&&,x2=&&& x2&& } cout&&&H.C.F=&&&h&& l=lcd(u,v,h); else if (disc==0) {equal_to_zero(a,b); cin&&a&&b&&c; disc=b*b-4*a*c; cout&&&root:&&& if (disc&0) { greater_than_zero(a,b); /* i 是 a 的对手;j 是 b 的对 int lcd(int u,int v,int h) {return(u*v/h); }void greater_than_zero(float a,float b) /* 定义一个函 数,用来求 disc&0 时方 程的根 */ {x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); }int prime(int n) {int flag=1,i; for (i=2;i&n/2 &&flag==1;i++) if (n%i==0) flag=0; return(flag); }voidequal_to_zero(float /* 定义一个4.4 题 #include &iostream& int main()a,float b)函数, 用来求 disc=0 时方程 的根 */ { x1=x2=(-b)/(2*a); }{int fac(int); int a,b,c,sum=0; cout&&&enter a,b,c:&; cin&&a&&b&&c;return 0; }void smaller_than_zero(float a,float b) /* 定义一个函 数,用来求 disc&0 时方 程的根 */ { p=-b/(2*a); q=sqrt(-disc)/(2*a); } 4.3 题 #include &iostream&sum=sum+fac(a)+fac(b)+fac( c);cout&&a&&&!+&&&b&&&!+&&&c &&&!=&&&sum&& return 0; }int fac(int n) {int f=1; for (int i=1;i&=n;i++) f=f*i; } 4.5 题cout&&&L.C.D=&&&l&& return 0; }cout&&&x1=&&&x1&&&,x2=&&& x2&& } elseint main() {int prime(int); /* 函数原型声明 */#include &iostream& #include &cmath& int main() {double e(double); double x, cout&&&enter x:&; cin&&x; sinh=(e(x)+e(-x))/2;4.7 题 #include &iostream& #include &cmath& int main() {void godbaha(int); }return 0;{cout&&x&&&--&&&&y&&} 4.10 题 #include &iostream&float p(int n,int x) {if (n==0) return(1); else if (n==1) return(x); else int main() {void convert(int n); cout&&&input an integer:&; cin&& cout&&&output:&&&cout&&&sinh(&&&x&&&)=&&&si nh&& return 0; } } double e(double x) {return exp(x);}cout&&&input n:&; cin&&n; godbaha(n); return 0;return(((2*n-1)*x*p((n-1),x)(n-1)*p((n-2),x))/n); } 4.9 题 #include &iostream& int main() {void hanoi(int n,char }if (number&0) {cout&&&-&; number=- } convert(number); cout&& return 0;void godbaha(int n) {int prime(int); int a,b; for(a=3;a&=n/2;a=a+2) {if(prime(a))4.6 题 #include &iostream& #include &cmath& int main() {double solut(double ,double ,doubl e ,double ); double a,b,c,d; cout&&&input a,b,c,d:&; cin&&a&&b&&c&&d; }{b=n-a; if (prime(b))one,char two,char three); cout&&n&&&=&&&a&&&+&&&b& &} } cout&&&input the number of diskes:&; cin&&m; cout&&&The int prime(int m) {int i,k=sqrt(m); for(i=2;i&=k;i++) if(m%i==0) moving disks:&&& hanoi(m,'A','B','C'); return 0; } steps ofvoid convert(int n) { if ((i=n/10)!=0) convert(i); c=n%10+'0'; cout&&& &&&c; } 4.11 题 #include &iostream&&&&m&&&cout&&&x=&&&solut(a,b,c,d)& & return 0; } }if (i&k) return 1; else return 0;voidhanoi(intn,charint main() {int f(int); int n,s; cout&&&input the number n:&; cin&&n; s=f(n); cout&&&The result isone,char two,char three) //将 n 个盘从 one 座 4.8 题 #include &iostream& int main() {int x,n; float p(int,int); cout&&&input n & x:&; {hanoi(n-1,one,three,two); move(one,three); 借助 two 座,移到 three 座 {void move(char x,char y); if(n==1) move(one,three); elsedoublesolut(doublea,double b,double c,double d) {double x=1,x0,f,f1; do {x0=x;&&&s&& return 0; }f=((a*x0+b)*x0+c)*x0+d; f1=(3*a*x0+2*b)*x0+c; x=x0-f/f1; } while(fabs(x-x0)&=1e-5); return(x); }cin&&n&&x; hanoi(n-1,two,one,three); cout&&&n=&&&n&&&,x=&&&x&&; } }int f(int n) {; if (n==1)cout&&&P&&&n&&&(x)=&&&p(n, x)&&void move(char x,char y)return 1; else return (n*n+f(n-1)); } #define CHANGE 1 int main() {char ch[40]; cout&&&input{int i,y=1; for(i=1;i&=n;i++) y*=a; }//输入 10 个数 } cout&&endl&&&The original numbers:&&&; for (i=1;i&=10;i++) cout&&a[i]&&& // 输出这 10 个数 &;4.12 题 #include &iostream& #include &cmath& #define S(a,b,c) #define (a+b+c)/2 AREA(a,b,c)text:&&&; gets(ch); #if (CHANGE) {for (int i=0;i&40;i++) {if (ch[i]!='\0') if (ch[i]&='a'&& 5.1 题 #include &iostream& #include &iomanip& #include &math.h& int main() ch[i]+=1; else (ch[i]=='z'||ch[i]=='Z') ch[i]-=25; } } #endif if {int i,j,n,a[101]; for (i=1;i&=100;i++) a[i]=i; a[1]=0; for (i=2;i&sqrt(100);i++) for (j=i+1;j&=100;j++) {if(a[i]!=0 a[j]!=0) cout&&&output:&&&endl&&ch && is not a } 4.16 题 file #include &iostream& {cout&&setw(5)&&a[i]&&& &; n++;} if(n==10) {cout&& n=0;} } cout&& return 0; } 5.2 题 #include &iostream& //#include &math.h& cout&&a&&&**&&&m&&&=&&& d&& return 0; } 4.16 题 file int main() {int i,j,min,temp,a[11]; cout&&&enter data:&&& for (i=1;i&=10;i++) {cout&&&a[&&&i&&&]=&; cin&&a[i]; } 5.4 题 5.3 题 } return 0; if (a[j]%a[i]==0) a[j]=0; } cout&& for (i=1,n=0;i&=100;i++) {if (a[i]!=0) && 序cout&&; for (i=1;i&=9;i++) //以下 8 行是对 10 个数排sqrt(S(a,b,c)*(S(a,b,c)-a)*(S( a,b,c)-b)*(S(a,b,c)-c)) int main() {float a,b,c; cout&&&input a,b,c:&; cin&&a&&b&&c; if (a+b&c && a+c&b && b+c&a)ch[i]&'z'||ch[i]&'A'&& ch[i]&'Z'){min=i; for (j=i+1;j&=10;j++) if min=j; temp=a[i]; //以下 3 行将 a[i+1]~a[10] 中最小者与 a[i] 对换 a[i]=a[min]; a[min]= } cout&&endl&&&The sorted numbers:&&& for (i=1;i&=10;i++) // 输出已排好序的 10 个数 cout&&a[i]&&& &; cout&& return 0; (a[min]&a[j])cout&&&area=&&&AREA(a,b,c) && else cout&&&It triangle!&&& return 0; } 4.14 题 #include &iostream& //#define LETTER 1 int main() { cin&&c; #if LETTER if(c&='a' && c&='z') c=c-32; #else if(c&='A' && c&='Z') c=c+32; #endif cout&&c&& return 0; } 4.15 题 #include &iostream& cout&&a&&&*&&&b&&&=&&&c& & d=power(m); int main() {extern int power(int); int b=3,c,d,m; cout&&&enter an integer a and its power m:&&& cin&&a&&m; c=a*b;#include &iostream& int main() {int a[3][3],sum=0; int i,j; cout&&&enter data:&&&; for (i=0;i&3;i++) for (j=0;j&3;j++) cin&&a[i][j]; for (i=0;i&3;i++) sum=sum+a[i][i];cout&&&sum=&&&sum&& return 0; int power(int n) #include &iostream& int main() {int a[11]={1,4,6,9,13,16,19,28,4 0,100}; int num,i,j; cout&&&array a:&&& for (i=0;i&10;i++) cout&&a[i]&&& &; cout&&; cout&&&insert data:&; cin&& if (num&a[9]) a[10]= else {for (i=0;i&10;i++) {if (a[i]&num) {for (j=9;j&=i;j--) a[j+1]=a[j]; a[i]= } } } cout&&&Now, a:&&& for (i=0;i&11;i++) cout&&a[i]&&& &; cout&& return 0; } 5.5 题 #include &iostream& int main() { const int n=5; int a[n],i, cout&&&enter a:&&& for (i=0;i&n;i++) cin&&a[i]; cout&&&array a:&&& for (i=0;i&n;i++) cout&&a[i]&&& &; for 元素的值互换 (i=0;i&n/2;i++) array } array }{ temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]= } cout&&endl&&&Now,array a:&&& for (i=0;i&n;i++) cout&&a[i]&&& &; cout&& return 0;for (i=0;i&n;i++) {max=a[i][0]; maxj=0; for (j=0;j&m;j++) //找出第 i 行中的最大数 if (a[i][j]&max) {max=a[i][j]; //将本行的最大数存放在 max 中 maxj=j; //将最大数所在的列号存 放在 maxj 中 }int main() { const int n=7; int i,number,top,bott,mid,loca,a [n]; bool flag=true, cout&&&enter data:&&&; cin&&a[0]; i=1; while(i&n) {cin&&a[i]; if (a[i]&=a[i-1]) i++; else cout&&&enter data again:&; } this5.6 题 #include &iostream& #include &iomanip& int main() {const int n=11; int i,j,a[n][n]; for (i=1;i&n;i++) {a[i][i]=1; a[i][1]=1; } for (i=3;i&n;i++) for (j=2;j&=i-1;j++) 假 相比flag= //先假设是鞍点,以 flag 为 真代表 for (int k=0;k&n;k++) if (max&a[k][maxj]) //将最大数和其同列元素{flag= //如果 max 不是同列最小, 表示不是鞍点令 flag1 为cout&& for (i=0;i&n;i++) cout&&a[i]&&& &; cout&& while(flag)} if(flag) //如果 flag1 为真表示是鞍 点 {cout&&&a[&&&i&&&][&& &&[&&&maxj&&&]=&&&max&&e 置{cout&&&input number to look for:&; cin&& sign= top=0; //top 是查找区间的起始位a[i][j]=a[i-1][j-1]+a[i-1][j]; for (i=1;i&n;i++) {for (j=1;j&=i;j++)cout&&setw(5)&&a[i][j]&&& &; cout&& } cout&& return 0;bott=n-1; //输出鞍点的值和所在行 列号 } } if(!flag) 存在 // 如果 flag 为假表示鞍点不 //bott 是查找区间的最末位 置 if ((number&a[0])||(number& a[n-1])) // 要 查的 数不 在 查找区间内 loca=-1; 表示找不到 cout&&&It exist!&&& return 0; } does not while (top&=bott)) {mid=(bott+top)/2; if (number==a[mid]) 5.8 题 #include &iostream& {loca= cout&&&Find &&&number&&&, its position ((!sign) && //5.7 题 #include &iostream& int main() { const int n=4,m=5; //假设数组为 4 行 5 列 int i,j,a[n][m],max, for //输入数组 for (j=0;j&m;j++) cin&&a[i][j]; (i=0;i&n;i++)//循环的作用是将对称的 is &&&loca+1&& sign= } else (number&a[mid]) bott=mid-1; else top=mid+1; } if(!sign||loca==-1) cout&&number&&& has not found.&&&; cout&&&continu not(Y/N)?&; cin&&c; if (c=='N'||c=='n') flag= } return 0; } 5.9 题 #include &iostream& int main() {int sum_day(int,int); int leap(int year); int year,month,day,days=0; cout&&&input date(year,month,day):&; or ifyear.&&& return 0; }else (text[i][j]&='0' text[i][j]&='9') digit++;if &&} 5.11 题另一解 #include &iostream& #include &string& int main()else if (text[i][j]==' int sum_day(int month,int day) { int day_tab[12]={31,28,31,30,3 1,30,31,31,30,31,30,31}; for (i=0;i&month-1;i++) day+=day_tab[i]; return(day); } } cout&&&upper case:&&&upper&& cout&&&lower case:&&&lower&& } //计算日期 ') space++; else other++;{ string stars=&*****&; int i,j; for // 输出 5 行 { cout&&& &; // 每行前面留 4 个空格 for (j=1;j&=i;j++) cout&&& // 每行再插入 i 个空格 cout&&stars&& // 输出 5 个*号 } return 0; &; (i=0;i&5;i++)intleap(intyear)cout&&&digit &:&&&digit&//判断是否为闰年 {cout&&&space leap=year%4==0&&year%10 0!=0||year%400==0; return(leap); } 5.10 题 #include &iostream& int main() {int i,j,upper,lower,digit,space,ot cout&&&other r&& return 0; } 5.11 题 e&&:&&&spac} 5.12 题 #include &iostream&:&&&othe int main() {int j,n; char ch[80],tran[80]; cout&&&input code:&; gets(ch); cout&&&cipher code:&&&ch&& j=0; while (ch[j]!='\0') cipher#include &iostream& int main() { char a[5]={'*','*','*','*','*'}; int i,j,k; char space=' '; for (i=0;i&5;i++) // 输出 5 行 { cout&& // 输出每行前先换行 input cout&&& // 每行前面留 4 个空格 for (j=1;j&=i;j++) && cout&& // 每行再留一个空格 for (k=0;k&5;k++) cout&&a[k]; // 每行输出 5 个*号 } cout&& return 0; &;cin&&year&&month&& char text[3][80];cout&&year&&&/&&&month&& &/&&& upper=lower=digit=space=o ther=0; days=sum_day(month,day); /* 调用函数一 for (i=0;i&3;i++) {cout&&&please line &&&i+1&& */ if(leap(year) month&=3) /* 调用函数二 && gets(text[i]); for (j=0;j&80{if((ch[j]&='A')&&(ch[j]&='Z')) tran[j]=155-ch[j]; else if ((ch[j]&='a') && (ch[j]&='z')) tran[j]=219-ch[j]; else tran[j]=ch[j]; j++; } n=j; cout&&&original text:&; for (j=0;j&n;j++) putchar(tran[j]); cout&&text[i][j]!='\0';j++) {if (text[i][j]&='A'&& text[i][j]&='Z')*/ days=days+1; cout&&& is theupper++; else if (text[i][j]&='a' && text[i][j]&='z') lower++;&&&days&&&th day in this return 0; } 5.12 题另一解 #include &iostream& int main() {int j,n; char ch[80]; cout&&&input code:&; gets(ch); cout&&&cipher code:&&&ch&& j=0; while (ch[j]!='\0') { if ((ch[j]&='A') && } cipher{if((ch[j]&='A')&&China next week.&; cout&&&cipher code:&&&ch&& j=0; while (j&=ch.size()) { if ((ch[j]&='A') && }cin&&s2; strcat(s1,s2); cout&&&The new string is:&&&s1&& return 0; 5.13 另一解 #include &iostream& #include &string& int main() { s1=&week&,s2=&end&; cout&&&s1=&&&s1&& cout&&&s2=&&&s2&& s1=s1+s2; cout&&&The new string is:&&&s1&& return 0; string(ch[j]&='Z')) tran[j]=155-ch[j]; else if ((ch[j]&='a') && (ch[j]&='z')) tran[j]=219-ch[j]; else tran[j]=ch[j]; j++; } cout&&&original text:&; cout&&tran&& return 0;(ch[j]&='Z')) ch[j]=155-ch[j]; else if ((ch[j]&='a') && (ch[j]&='z')) ch[j]=219-ch[j]; j++; } cout&&&original text:&; cout&&ch&& return 0;5.12 另一解 #include &iostream& #include &string& int main() { string ch=&I will visit}(ch[j]&='Z')) ch[j]=155-ch[j]; else if ((ch[j]&='a') && (ch[j]&='z')) ch[j]=219-ch[j]; else ch[j]=ch[j]; j++; } n=j; cout&&&original text:&; for (j=0;j&n;j++) putchar(ch[j]); cout&& return 0; }5.13 题 #include &iostream& #include &string& int main() {char s1[80],s2[40]; int i=0,j=0; cout&&&input string1:&; cin&&s1; && cout&&&input string2:&; cin&&s2; while (s1[i]!='\0') i++; while(s2[j]!='\0') s1[i++]=s2[j++]; s1[i]='\0'; cout&&&The new string is:&&&s1&& return 0; } 5.13 另一解}5.14 题 #include &iostream& #include &string& int main() { const int n=5; int i,j; string str[n], cout&&&please strings:&&& for(i=0;i&n;i++) cin&&str[i]; for(i=0;i&n-1;i++) for(j=0;j&n-i-1;j++) if(str[j]&str[j+1]) inputChina next week.&; cout&&&cipher code:&&&ch&& j=0; while (j&=ch.size()) { if ((ch[j]&='A')(ch[j]&='Z')) ch[j]=155-ch[j]; else if ((ch[j]&='a') && (ch[j]&='z')) ch[j]=219-ch[j]; j++;5.12 另一解 #include &iostream& #include &string& int main() { string ch=&I will visit }} cout&&&original text:&; cout&&ch&& return 0;{temp=str[j];str[j]=str[j+1];st r[j+1]=} cout&&endl&&&sorted strings:&&& for(i=0;i&n;i++) cout&&str[i]&& return 0; }#include &iostream& #include &iostream& #include &string& int main() { string ch=&I will visit int main() {char s1[80],s2[40]; cout&&&input string1:&; cin&&s1; cout&&&input string2:&;China next week.&, tran= cout&&&cipher code:&&&ch&& j=0; while (j&=ch.size()) 5.15 题 #include &iostream& #include &string& int main() { const int n=5; for(int i=0;i&n;i++) {cout&&&please input string:&; cin&& if(str[0]=='A') } cout&&str&&} return 0; }int i,n; cout&&&please string:&; cin&&a; n=a.size(); for(i=0;i&n/2;i++) 6.1 题 #include &iostream& #include &string& {temp=a[i];a[i]=a[n-i-1];a[n-i -1]=} cout&&a&& return 0; const int n=10; string name[n]; int num[n],score[n]; int main() { void input_data(); input_data(); cout&&endl&&&The list of 5.17 题 #include &iostream& #include &string& failed:&&& for(i=0;i&n;i++) if(score[i]&60) cout&&name[i]&&& &&&num[i]&&& &&&score[i]&& return 0; } } input agets(str3); if(strcmp(str1,str2)&0) swap(str1,str2); if(strcmp(str1,str3)&0) swap(str1,str3); if(strcmp(str2,str3)&0) swap(str2,str3); cout&&endl&&&Now,the order is:&&&cout&&str1&&endl&&str2&&e ndl&&str3&& return 0;void swap(char *p1,char *p2) 个字符串 */ {char p[20]; /* 交换两5.16 题 #include &iostream& int main() { const n=10; char a[n], cout&&&please string:&; for(i=0;i&n;i++) cin&&a[i]; for(i=0;i&n/2;i++) input a const int n=10; string name[n]; int num[n],score[n]; int main() { void input_data(); input_data(); cout&&endl&&&The list of failed:&&& for(i=0;i&n;i++) if(score[i]&60) cout&&name[i]&&&strcpy(p,p1);strcpy(p1,p2);st rcpy(p2,p); } 6.2 题另一解 #include &iostream&void input_data() { for (i=0;i&n;i++) {cout&&&input name,number and score of student &&&i+1&&&:&;#include &string& int main() {void &,string &); string &, str1=& change(string{temp=a[i];a[i]=a[n-i-1];a[n-i -1]=} for(i=0;i&n;i++) cout&&a[i]; cout&& return 0; }&&&num[i]&&& &&&score[i]&& return 0; }cin&&name[i]&&num[i]&&sco re[i];} } &; 6.2 题 char &,str2=&str3=&void input_data() { for (i=0;i&n;i++) {cout&&&input name,number and score of#include &iostream& #include &cstring& int main() {void swap(char *,char *); char str1[20],str2[20],str3[20];*p1=&str1[0],*p2=&str2[0], *p3=&str3[0]; cout&&&input line:&&& gets(p1); gets(p2); gets(p3); three5.16 题另一解 #include &iostream& #include &string& int main() {student &&&i+1&&&:&;cin&&name[i]&&num[i]&&sco re[i];} }cout&&&input line:&&& gets(str1); gets(str2);three if(str1&str2)change(str1,str2 ); if(str1&str3)change(str1,str3 );voidmax_min_value(int //求输入 n 个数 for (i=0;i&n;i++) cin&&number[i]; cout&&&how many places do you want move?&; 询问后移多少个位置 cin&&m; move(number,n,m); //调用 move 函数 cout&&&Now,they are:&&& for (i=0;i&n;i++) cout&&number[i]&&& &; cout&& //// k 为按 1,2,3 报数时的计 数变量 m=0; // m 为退出人数 while (m&n-1) // 当退出人数比 n-1 少时 (即未退出人数大于 1 时)执 行循 环体 {if (*(p+i)!=0) if k++; (k==3)*number) 交换函数if(str2&str3)change(str2,str3 ); cout&&endl&&&Now,the order is:&&&{ int *max,*min,*p, max=min= for (p=number+1;p&number+10 ;p++)cout&&str1&&endl&&str2&&e ndl&&str3&& return 0; } void &st1,string change(string &st2)if (*p&*max) max=p; // 将大数地址赋给 max else min=p; 小数地址赋给 min if (*p&*min) // 将// 将退出的人的编号置为 0 {*(p+i)=0; k=0; m++;/* 交换两个字符串 */ { st=st1;st1=st2;st2= }temp=number[0];number[0] =**min= 最小数与第一数交换 // 将 }return 0;void temp=number[9];number[9] // 将move(int*array,int //使} i++; if (i==n) i=0; 报数到尾后,i 恢复为 0 } while(*p==0) p++; cout&&&The last one is NO.&&&*p&& return 0; } 6.6 题 #include &iostream& int main() {int length(char *p); //n,int m) 循环后移一次的函数 {int *p,array_6.3 题 #include &iostream& int main() { void input(int *number); void max_min_value(int *number); void *number); int number[10]; input(number); // 调用输入 10 个数的函数 output(int=**max= 最小数与第一数交换 }array_end=*(array+n-1); forvoid output(int *number) // 输出函数 {int *p; cout&&&now,they &; for (p=p&number+10;p ++) cout&&*p&&& &; are:(p=array+n-1;p&p--) *p=*(p-1); *array=array_ m--; if (m&0) move(array,n,m); //递归调用,当循环次数 m 减至为 0 时,停止调用 } 6.5 题 #include &iostream& int main() {int i,k,m,n,num[50],*p; cout&&&input number of person: n=&; cin&&n; p= for (i=0;i&n;i++) *(p+i)=i+1; // 以 1 至 n 为序给每个人 编号 i=0; // i 为每次循环时计数变量 k=0; }max_min_value(number); // 调用交换函数 output(number); // 调用输出函数 return 0; }cout&& } 6.4 题 #include &iostream& int main() char str[20]; cout&&&input string:&; cin&& len=length(str); cout&&&The length ofstring is &&&len&& return 0;void input(int *number) // 输入 10 个数的函数 { cout&&&input numbers:&; for (i=0;i&10;i++) cin&&number[i]; } 10{void move(int *array,int n,int m); int number[20],n,m,i; cout&&&how numbers?&; // 询问共有多少个数 cin&&n; cout&&&input numbers:&&& &&&n&&& // 要 manyintlength(char*p)//求字符串长度的函数 { n=0; while (*p!='\0') {n++; p++; } return(n); } 6.7 题 #include &iostream& int main() {void copystr(char *,char *,int); char str1[20],str2[20]; cout&&&input string:&; gets(str1); cout&&&which character int main() {int upper=0,lower=0,digit=0,sp ace=0,other=0,i=0; char *p,s[20]; cout&&&input string:&; while ((s[i]=getchar())!='\n') i++; p=&s[0]; while (*p!='\n') {if (*p&='Z')) ++ else if (('a'&=*p) && (*p&='z')) ++ else if (*p==' ') ++ else if ((*p&='9') && (*p&='0')) ++ else ++ (('A'&=*p) && }move(p); } cout&&&Now,matrix:&&& for (i=0;i&3;i++) cout&&a[i][0]&&& &&&a[i][1]&&& &&&a[i][2]&& cout&& return 0;return 0;voidchange(int*p)//交换函数 {int i,j, int *pmax,* pmax=p; pmin=p; for (i=0;i&5;i++) 并赋给 pmax,pmin for (j=i;j&5;j++) {if (*pmax&*(p+5*i+j)) pmax=p+5*i+j; if (*pmin&*(p+5*i+j)) pmin=p+5*i+j; // 找最大值和最小值的地址,void move(int *pointer) {int i,j,t; for (i=0;i&3;i++) for (j=i;j&3;j++) {t=*(pointer+3*i+j);do you want begin to copy?&; cin&&m; if (strlen(str1)&m) cout&&&input error!&&& else {copystr(str1,str2,m);*(pointer+3*i+j)=*(pointer+ 3*j+i); 换} temp=*(p+12); //将最大值与中心元素互*(pointer+3*j+i)=t; } } 6.10 题 #include &iostream& int main() {void change(int *p); int a[5][5],*p,i,j; cout&&&input matrix:&&& for //输入矩阵 for (j=0;j&5;j++) (i=0;i&5;i++)*(p+12)=* *pmax=cout&&&result:&&&str2&& } return 0; } }p++;temp=*p; //将最小值与左上角元素 互换 *p=* *pmin=cout&&&upper case:&&&upper&&endl&&&low er case:&&&lower&& cout&&&space:&&&space&&envoid copystr(char *p1,char *p2,int m) 部分复制函数*/ { n=0; while (n&m-1) {n++; p1++; } while (*p1!='\0') {*p2=*p1; p1++; p2++; } *p2='\0'; } 6.8 题 #include &iostream& //字符串dl&&&digit:&&&digit&&endl&&& other:&&&other&& return 0; } 6.9 题 #include &iostream& int main() {void move(int *); int a[3][3],*p,i; cout&&&input matrix:&&& for (i=0;i&3;i++)pmin=p+1; // 将 a[0][1] 的地 址 赋 给 pmin,从该位置开始找 最小的元素 for 给 pmin for (j=0;j&5;j++) if (((p+5*i+j)!=p) (i=0;i&5;i++)cin&&a[i][j]; p=&a[0][0]; //使 p 指向 0 行 0 列元素 change(p); //调用函数,实现交换//找第二最小值的地址赋&& (*pmin & *(p+5*i+j))) pmin=p+5*i+j;cout&&&Now,matrix:&&& for (i=0;i&5;i++)temp=* //将第二最小值与右上角 元素互换 *pmin=*(p+4); *(p+4)=//输出已交换的矩阵 {for (j=0;j&5;j++)cin&&a[i][0]&&a[i][1]&&a[i][2] ; p=&a[0][0]; }cout&&a[i][j]&&& &; cout&&pmin=p+1; for (i=0;i&5;i++)//change(p); //调用函数,实现交换{if(i==0 if&&j==0)int main() {void sort(char s[][6]);找第三最小值的地址赋给 pmin for (j=0;j&5;j++) if(((p+5*i+j)!=(p+4)) && ((p+5*i+j)!=p)(*pmin& char str[10][6]; cout&&&input strings:&&& for (i=0;i&10;i++) cin&&str[i]; sort(str); cout&&&Now,the sequence is:&&& 10cout&&&Now,matrix:&&& for (i=0;i&5;i++)*(p+5*i+j)) pmin=p+5*i+j; } temp=* //将第二最小值与右上角 元素互换 *pmin=*(p+4); *(p+4)=//输出已交换的矩阵 {for (j=0;j&5;j++) cout&&a[i][j]&&& &; cout&&&&(*pmin&*(p+5*i+j))) pmin=p+5*i+j; temp=* // 将第三最小值与左下角 元素互换 *pmin=*(p+20); *(p+20)= }} return 0;pmin=p+1; for (i=0;i&5;i++) // 找第三最小值的地址赋给 pmin for (j=0;j&5;j++) {if((i==0 ||(i==0 // if(*pmin&*(p+5*i+j)) pmin=p+5*i+j; } temp=* // 将第三最小值与左下角 元素互换 *pmin=*(p+20); *(p+20)= && j==0) && j==4)) }for (i=0;i&10;i++) cout&&str[i]&& return 0;voidchange(int*p)//交换函数 {int i,j,pmin=p+1; for (i=0;i&5;i++) // 找第四最小值的地址赋给 pmin */ for (j=0;j&5;j++) if (((p+5*i+j)!=p) &&int *pmax,* pmax=p; pmin=p; for (i=0;i&5;i++) 并赋给 pmax,pmin for (j=i;j&5;j++) {if (*pmax&*(p+5*i+j)) pmax=p+5*i+j; 找最大值和最小值的地址,void sort(char s[][6]) {int i,j; char *p,temp[10]; p= for (i=0;i&9;i++) for (j=0;j&9-i;j++) if (strcmp(s[j],s[j+1])&0) {strcpy(p,s[j]); strcpy(s[j],s[+j+1]); strcpy(s[j+1],p); }&&((p+5*i+j)!=(p+4)) ((p+5*i+j)!=(p+20)) &&(*pmin&*(p+5*i+j))) pmin=p+5*i+j; temp=* //将第四最小值与右下角 元素互换 *pmin=*(p+24); *(p+24)= } 6.10 题另一解 #include &iostream& int main() {void change(int *p); int a[5][5],*p,i,j; cout&&&input matrix:&&& for //输入矩阵 for (j=0;j&5;j++) cin&&a[i][j]; p=&a[0][0]; //使 p 指向 0 行 0 列元素 (i=0;i&5;i++) 换if (*pmin&*(p+5*i+j)) pmin=p+5*i+j; } temp=*(p+12); //将最大值与中心元素互pmin=p+1; for (i=0;i&5;i++) // 找第四最小值的地址赋给} 6.11 题另一解 #include &iostream&*(p+12)=* *pmax=pmin for (j=0;j&5;j++) {if ((i==0 && j==0)int main() {void sort(char (*p)[6]); char str[10][6]; char (*p)[6]; cout&&&input strings:&&& for (i=0;i&10;i++) cin&&str[i]; p= sort(p); cout&&&Now,the sequence is:&&& for (i=0;i&10;i++) cout&&str[i]&& return 0; 10temp=*p; //将最小值与左上角元素 互换 *p=* *pmin=||(i==0&& j==4)||(i==4&& j==0)) if (*pmin&*(p+5*i+j)) pmin=p+5*i+j; } temp=*pmin=p+1; // 将 a[0][1] 的地 址 赋 给 pmin,从该位置开始找 最小的元素 for 给 pmin for (j=0;j&5;j++) (i=0;i&5;i++)//将第四最小值与右下角 元素互换 *pmin=*(p+24); *(p+24)= } 6.11 题 #include &iostream&//找第二最小值的地址赋 }int main() {void sort(char *[]);// 对 fexp 函数作声明 int n=20; cout&&&input 限和上限 //将 cin&&a1&&b1; cout&&&input 下限和上限 cin&&a2&&b2; cout&&&input // a3,b3:&; 输 入 求 #include float fexp(float x) // 计算 exp(x)的函数 {return exp(x);} 6.13 题 #include &iostream& #include &cmath& int main() of {float integral(float a2,b2:&; // 输入求 cos(x) 定积分的 float fcos(float x) // 计算 cos(x) 的函数 {return cos(x);} a1,b1:&; //输入求 sin(x) 定积分的下 float fsin(float x) // 计算 sin(x) 的函数 {return sin(x);}void sort(char (*s)[6]) {int i,j; char temp[6],*t= for (i=0;i&9;i++) for (j=0;j&9-i;j++) if (strcmp(s[j],s[j+1])&0) {strcpy(t,s[j]); strcpy(s[j],s[+j+1]); strcpy(s[j+1],t); } } 6.11 题另一解 #include &iostream& #include &string& int main() {void sort(string *); string str[10],*p= cout&&&input strings:&&& for (i=0;i&10;i++) cin&&str[i]; sort(p); cout&&&Now,the sequence is:&&& for (i=0;i&10;i++) cout&&str[i]&& return 0; } } 10 } char *p[10],str[10][20]; for (i=0;i&10;i++) p[i]=str[i]; 第 i 个字符串的首地址赋予 指针数组 p 的第 i 个元素 cout&&&input strings:&&& for (i=0;i&10;i++) cin&&p[i]; sort(p); cout&&&Now,the sequence is:&&& for (i=0;i&10;i++) cout&&p[i]&& return 0; 10&iostream& cin&&a3&&b3; p= c=integral(p,a1,b1,n); // 求出 sin(x)的定积分 cout&&&The integralsin(x) is :&&&c&& p=(*p)(float),float a,float b,int n); float a1,b1,a2,b2,a3,b3,c,(*p)(flo of at); float fsin(float); // 对 fsin 函数作声明 float fcos(float); // 对 fcos 函数作声明 of float fexp(float); // 对 fexp 函数作声明 int n=20; cout&&&input 限和上限 cin&&a1&&b1; cout&&&input a2,b2:&; // 输入求 cos(x) 定积分的 下限和上限 cin&&a2&&b2; cout&&&input // a3,b3:&; 输 入 求 #include a1,b1:&; //输入求 sin(x) 定积分的下void sort(char *s[]) {int i,j; char * for (i=0;i&9;i++) for (j=0;j&9-i;j++) if (strcmp(*(s+j),*(s+j+1))&0) {temp=*(s+j); *(s+j)=*(s+j+1); *(s+j+1)= } 6.13 题 #include &iostream& }c=integral(p,a2,b2,n); // 求出 cos(x)的 定积分 cout&&&The integralcos(x) is :&&&c&&; p= c=integral(p,a3,b3,n); // 求出 的定积分 cout&&&The integralexp(x) is :&&&c&& return 0;floatintegral(float(*p)(float),float a,float b,int n) //用矩形法求 定积分的通用函数void sort(string *s) {int i,j; for (i=0;i&9;i++) for (j=0;j&9-i;j++) if (s[j]&s[j+1]) {temp=s[j]; s[j]=s[+j+1]; s[j+1]= } } 6.12 题 #include &iostream&#include &cmath& int main() {float integral(float{ float x,h,s; h=(b-a)/n; x=a; s=0; for (i=1;i&=n;i++)(*p)(float),float a,float b,int n); float a1,b1,a2,b2,a3,b3,c,(*p)(flo at); float fsin(float); // 对 fsin 函数作声明 float fcos(float);&iostream& cin&&a3&&b3; p= c=integral(p,a1,b1,n); // 求出 sin(x)的定积分 cout&&&The integral of{x=x+h; s=s+(*p)(x)*h; } return(s); }// 对 fcos 函数作声明 float fexp(float);sin(x) is :&&&c&& p= c=integral(p,a2,b2,n); // 求出 cos(x)的 定积分 cout&&&The integral ofcout&&&input n:&; cin&&n; cout&&&please inputcourse[5][10],(*pcourse)[10 ]; cout&&&input course:&&& pcourse= for (i=0;i&5;i++) cin&&course[i]; cout&&&input scores:&&& cout&&&NO.&; for (i=0;i&5;i++) cout&&&,&&&course[i]; cout&& pscore=&score[0][0]; pnum=&num[0]; for (i=0;i&4;i++) } NO. and{sum=0.0; for (j=0;j&5;j++)cos(x) is :&&&c&&; p= c=integral(p,a3,b3,n); // 求出 的定积分 cout&&&The integral ofthese numbers:&&& for (i=0;i&n;i++) cin&&num[i]; p=&num[0]; sort(p,n); cout&&&Now,the sequence is:&&& for (i=0;i&n;i++) cout&&num[i]&&& &;sum=sum+(*(pscore+5*i+j)); //累计每个学生的各科成 绩 average=sum/5; //计算平均成绩 *(paver+i)= }exp(x) is :&&&c&& return 0; }floatintegral(floatcout&& return 0; }voidavcour1(char(*p)(float),float a,float b,int n) //用矩形法求 定积分的通用函数 { float x,h,s; h=(b-a)/n; x=a; s=0; for (i=1;i&=n;i++) {x=x+h; s=s+(*p)(x)*h; } return(s); } }(*pcourse)[10],float *pscore) // 求第一课程的平均成 绩的函数 { float sum,average1; sum=0.0; for (i=0;i&4;i++)void sort (char *p,int m) // 将 n 个数逆序排列函数 { char temp, *p1,*p2; for (i=0;i&m/2;i++) {p1=p+i; p2=p+(m-1-i); temp=*p1; *p1=*p2; *p2= }{cin&&*(pnum+i); for (j=0;j&5;j++) cin&&*(pscore+5*i+j); } paver=&aver[0]; cout&&endl&& avsco(pscore,paver); // 求出每个学生的平均成 绩 avcour1(pcourse,pscore); // 求出第一门课的平均成 绩 cout&&endl&&sum=sum+(*(pscore+5*i)); //累计每个学生的得分 average1=sum/4; //计算平均成绩 cout&&&course &&&*pcourse&&&,average score:&&&average1&& } 1:floatfsin(floatx)6.15 题 #include &iostream& int main() fali2(pcourse,pnum,pscore,p aver); // 找出两门// 计算 sin(x) 的函数 {return sin(x);}voidfail2(char课不及格的学生 cout&&endl&&course[5][10],int num[],float *pscore,float aver[4]) // 找 两 门 以 上课程不及格的学生的函 数 {int i,j,k,labe1; cout&&& ==========Student failed in two &&& &; who coursesfloatfcos(floatx){void avsco(float *,float *); void (*)[10],float *); void fali2(char avcour1(char// 计算 cos(x) 的函数 {return cos(x);}good(pcourse,pnum,pscore, paver); 绩好的学生 return 0; } // 找出成floatfexp(floatx)course[5][10],int num[],float *pscore,float aver[4]); void course[5][10],int good(char// 计算 exp(x)的函数 {return exp(x);} 6.14 题 #include &iostream& int main() { void sort (char *p,int m); int i,n; char *p,num[20];num[4],float aver[4]);*pscore,floatvoidavsco(float //=======*pscore,float *paver) 函数 {int i,j; float sum, for (i=0;i&4;i++)cout&&&NO. for (i=0;i&5;i++)int i,j,*pnum,num[4]; float score[4][5],aver[4],*pscore, * char求每个学生的平均成绩的cout&&course[i]&&& cout&&& average&&& for (i=0;i&4;i++)&; {labe1=0; for (j=0;j&5;j++) if (*(pscore+5*i+j)&60.0) labe1++; if (labe1&=2) {cout&&num[i]&&& &; for (k=0;k&5;k++)&; cout&&& &&&aver[i]&& } } } digit=digit+(*(pstr+i-1-k)-48) *e10; //将该位数的数值 累加于 digit k++; //位数 k 自增 } *pa= 6.16 题 #include &iostream& //将数值放在数组 a 中 ndigit++; pa++; //指针 pa 指向 a 数组下一 元素 j=0; } } a } if 后一个数据 {digit=*(pstr+i-1)-48; //将个数位赋予 digit k=1; while (k&j) 位的数值累加于 digit {e10=1; for (m=1;m&=k;m++) e10=e10*10; //e10 代表位数所应乘的因 && 子 // 将含有两位以上数的其它 //i 代表 //j 代表 } (j&0) i++; }pa=&a[0]; for //打印数据 cout&&*(pa+j)&& cout&& return 0; (j=0;j&j++)6.17 题 #include &iostream& int main() {int strcmp(char *p1,char *p2); char str1[20],str2[20],*p1,*p2; cout&&&input strings:&&& cin&&str1; cin&&str2; p1=&str1[0]; p2=&str2[0]; m=strcmp(p1,p2); cout&&&result:&&&m&& return 0; twocout&&*(pscore+5*i+k)&&& &; cout&&& &&&aver[i]&& } } } int main() {char str[50],* int i,j,k,m,e10,digit,ndigit,a[10], * cout&&&input string:&&&void course[5][10],int num[4],float aver[4])good(chargets(str); cout&& pstr=&str[0]; //字符指 针 pstr 指向数组 str 首元素 pa=&a[0]; //指针 pa 指向 a 数组首元素 ndigit=0; 代表有多少个整数 i=0; 字符串中的第几个字符/ //ndigit//以数字结尾字符串的最*pscore,float// 找成绩优秀学生(全 部课程成绩在 85 分以上或 平均成绩在 90 分以上)的函 数 {int i,j,k,n; cout&&& ======Students score good======&&& cout&&&NO. for (i=0;i&5;i++) cout&&course[i]&&& cout&&& average&&& for (i=0;i&4;i++) {n=0; for (j=0;j&5;j++) if (*(pscore+5*i+j)&85.0) n++; if ((n==5)||(aver[i]&=90)) {cout&&num[i]&&& &; for (k=0;k&5;k++) &; &; whose isj=0; 连续数字的位数int *p2)strcmp(char*p1,char//自已定义while(*(pstr+i)!='\0') {if((*(pstr+i)&='0') (*(pstr+i)&='9')) j++; else {if (j&0) {digit=*(pstr+i-1)-48; //将个数位赋予 digit k=1; while (k&j) 的数值累计于 digit {e10=1; for (m=1;m&=k;m++) e10=e10*10; //e10 代表该位数所应乘的 因子 //将 含有两位以上数的其它位字符串比较函数 { i=0;digit=digit+(*(pstr+i-1-k)-48) *e10; //将该位数的数值 k++; /*位数 K 自增 */ } *pa= //将数值放到数组 a 中 ndigit++; j=0; } printf(&There are %d 累加于 digitwhile(*(p1+i)==*(p2+i)) if return(0); (*(p1+i++)=='\0') //全部字符相同时返回结果 0 return(*(p1+i)-*(p2+i)); //不相同时返回结果为第 一对不相同字符的 ASCII 码 的差值 } 6.18 题 #include &iostream& int main()numbers in this line. They are:\n&,ndigit); j=0;cout&&*(pscore+5*i+k)&&& {char *month_name[13]={&illegal } month&,&January&,&February &,&March&,&April&, void 函数cout&&pstr[i]&& return 0;void sort(int **p,int n) {int i,j,* for (i=0;i&n-1;i++) {for (j=i+1;j&n;j++) case days=date.day+243; case 10: 9:sort(char**p){if(**(p+i)&**(p+j))//冒泡法对 5 个字符串排序 &May&,&June&,&July&,&August &,&September&,&October&,//比较后交换整数地址 {temp=*(p+i); *(p+i)=*(p+j); *(p+j)= } 7.1 题 #include &iostream& struct { } int main() { cout&&&input year,month,day:&;days=date.day+273; case 11:{int i,j; char *days=date.day+304; case 12:&November&,&December&}; cout&&&input month:&&& cin&&n; if ((n&=12) && (n&=1)) cout&&&It isfor (i=0;i&5;i++) {for (j=i+1;j&5;j++) {if (strcmp(*(p+i),*(p+j))&0) //比较后交换字符串地址 {temp=*(p+i); *(p+i)=*(p+j); *(p+j)= } } is } 6.20 题 #include &iostream& }days=date.day+334; } if ((date.year %4== 0 && date.year % 100 != 0 ||date.year % 400 == 0) && date.month &=3) days+=1;&&&*(month_name+n)&& else cout&&&It wrong&&& return 0; } 6.19 题 #include &iostream&cout&&date.month&&&/&&&d ate.day&&& is the &&&days &&&th day in&&&date.year&&&.&&& return 0; }cin&&date.year&&date.mont h&&date. switch(date.month) { case 1: days=date. case days=date.day+31; /*将 case days=date.day+59; case days=date.day+90; case days=date.day+120; case days=date.day+151; &; case days=date.day+181; case days=date.day+212; 8: 7: 6: cin&&date.year&&date.mont h&&date. days=0; for (i=1;i&date.i++) days+=day_tab[i]; days+=date. if ((date.year%4==0 5: 4: 3: 2: 7.1 题另一解 #include &iostream& struct { } int main() {int i, int day_tab[13]={0,31,28,31,30, 31,30,31,31,30,31,30,31}; cout&&&input year,month,day:&;int main() {void sort(int **p,int n); int i,n,data[10],**p,*pstr[10];int main() {void sort(char **p); const int m=20; 定义字符串的最大长度 char **p,*pstr[5],str[5][m]; for (i=0;i&5;i++) pstr[i]=str[i]; /*将第 i 个字符串的首地址赋予指 针数组 pstr 的第 i 个元素 */ cout&&&input strings:&&& for (i=0;i&5;i++) cin&&pstr[i]; p= sort(p); cout&&&strings sorted:&&& for (i=0;i&5;i++) } 5 //cout&&&input n:&; cin&&n; for (i=0;i&n;i++) pstr[i]=&data[i]; 第 i 个整数的地址赋予指针 数组 pstr 的第 i 个元素*/ cout&&&input &&&n&&&integer numbers:&&& for (i=0;i&n;i++) cin&&*pstr[i]; p= sort(p,n); cout&&&Now,the sequence is:&&& for (i=0;i&n;i++) cout&&*pstr[i]&&& cout&& return 0; && date.year%100!=0 || date.year%400==0) &&day_tab[13]={0,31,28,31,30, 31,30,31,31,30,31,30,31}; day_sum=0;score3&&& for (i=0;i&n;i++) {cout&&stu[i].num&&& &&&setw(10)&&stu[i].name&& & &; for (j=0;j&3;j++){int i,j; cout&&& NO. score1 score3&&& for (i=0;i&n;i++) {cout&&stu[i].num&&& &&&setw(10)&&stu[i].name&& name score2date.month&=3) days+=1;for (i=1;i&i++) day_sum+=day_tab[i]; day_sum+=cout&&date.month&&&/&&&d ate.day&&& is the &&&days &&&th day inif((year%4==0&& cout&&setw(3)&&stu[i].score [j]&&& &;year%100!=0 || year%4==0) && month&=3) day_sum+=1; return(day_sum); } 7.3 题 #include &iostream& #include &iomanip& const int n=5; struct student { char num[6]; char name[8]; int score[4]; }stu[n]; int main() {void print(student stu[6]); int i,j; for (i=0;i&n;i++) {cout&&&input scores of&&; for (j=0;j&3;j++)&&&date.year&&&.&&& return 0; } 7.2 题 #include &iostream& struct y_m_d { }; int main() {y_m_ int days(int,int,int); /* 对 days 函数的声明 */ int day_ cout&&&input year,month,day:&;cout&& } } 7.4 题 #include &iostream& #include &iomanip& const int n=5; struct student { char num[6]; char name[8]; int score[4]; }stu[n]; int main() {void input(student stu[]); void print(student stu[]); input(stu); print(stu); return 0; } int main() { int i,j,max,maxi, void input(student stu[]) {int i,j; for (i=0;i&n;i++) {cout&&&input scores of for (i=0;i&n;i++) {cout&&&input scores of student &&&i+1&&; cout&&&NO.:&; cin&&stu[i]. cout&&&name:&; cin&&stu[i]. for (j=0;j&3;j++) {cout&&&score &&&j+1&&&:&; 7.5 题 #include &iostream& #include &iomanip& const int n=10; struct student { char num[6]; char name[8]; int score[4]; } stu[n]; } cout&&setw(3)&&stu[i].score [j]&&& &;cout&& }student &&&i+1&&&:&&& cin&&date.year&&date.mont h&&date. cout&&&NO.: &; cin&&stu[i]. cout&&&name: &; day_sum=days(date.year,dat e.month,date.day); cin&&stu[i]. for (j=0;j&3;j++) {cout&&&score cout&&date.month&&&/&&&d ate.day&&& &&&day_sum &&&th day in } print(stu); return 0; } int days(int year,int /* void print(student stu[6]) {int i,j; cout&&& NO. score1 name score2 } is the } cout&& &&&j+1&&&:&; cin&&stu[i].score[j];student &&&i+1&&&:&&& cout&&&NO.: &; cin&&stu[i]. cout&&&name: &; cin&&stu[i]. for (j=0;j&3;j++) {cout&&&score &&&j+1&&&:&; cin&&stu[i].score[j]; } } }&&&date.year&& return 0; }cin&&stu[i].score[j]; } cout&&month,int day) 定义 days 函数 */ {int day_sum,i; intaverage=0; void print(student stu[]) max=0; maxi=0; for (i=0;i&n;i++) {sum=0; for (j=0;j&3;j++)struct student { struct student * };}; // 定义 n 为全局变量, 本文件 模块中各函数均可使用它 student *creat(void) 指向链表头的指针 {student * student *p1,*p2; int n=0; p1=p2=new p1,p2 指向它 cin&&p1-&num&&p1-& head=NULL; while(p1-&num!=0) {n=n+1; if(n==1) head=p1; else p2-&next=p1; p2=p1; p1= // 定义函数。 此函数带回一个&&&p-&score&& p=p-& }while(p!=NULL); } 7.7 题另一解 #include &iostream& #define NULL 0 struct student { student * }; student *del(studentsum+=stu[i].score[j]; stu[i].avr=sum/3.0; average+=stu[i]. if (sum&max) {max= maxi=i; } } average/=n; cout&&& name score2 score3 NO. score1int main() {student a,b,c,*head,*p; a.num=10001; a.score=89.5; b.num=10003; b.score=90; c.num=10007; c.score=85; //为结点的 num 和 score 成 员赋值 head=&a; //将结点 a 的起始地址赋给 头指针 head a.next=&b; //将结点 b 的起始地址赋给 a 结点的 next 成员 b.next=&c; //将结点 c 的起始地址赋给 b 结点的 next 成员 c.next=NULL; //c 结点的 next 成员不存放 其他结点地址 p= //使 p 指针指向 a 结点 do {cout&&p-&num&&&//开辟一个新单元,并使*head,long num) {student *p1,*p2; if //是空表 {cout&&&list return(head);} null!&&& (head==NULL)average&&& for (i=0;i&n;i++){cout&&setw(8)&&stu[i].num &&& &&&setw(10)&&stu[i].name&& & &; for (j=0;j&3;j++)cin&&p1-&num&&p1-& } p2-&next=NULL; return(head); }p1= //使 p1 指向第一个结点 while(num!=p1-&num && p1-&next!=NULL) //p1 指向 的不是所要找的结点且后 面还有cout&&setw(3)&&stu[i].score [j]&&& &;7.7 题 #include &iostream& #define NULL 0 struct student { student * }; void print(student *head)结点 {p2=p1; p1=p1-&} //p1 后移一个结点 if(num==p1-&num) //找到了 {if(p1==head) head=p1-& 结点地址赋予 //若 p1 指向的是首结点, 把第二个cout&&stu[i].avr&& }&&&p-&score&& //输出 p 指向的结点的数据 p=p-& //使 p 指向下一结点 }while(p!=NULL); //输出完 c 结点后 p 的值为 NULL return 0; } 7.6 题另一解 #include &iostream& #define NULL 0cout&&&average=&&&average && cout&&&The score is :&&&stu[maxi].name&&&, highestscorehead else p2-&next=p1-&total:&&&max&& return 0; } 7.6 题 #include &iostream& #define NULL 0{student *p; cout&&&Now &&&n&&& &&& p= if(head!=NULL) do {cout&&p-&num&&& } , These are ://否则将下一结点地址赋 给前一结点地址recordsstruct student { student *cout&&&delete:&&&num&& n=n+1; else cout&&&cannot find &&& 结点 return(head); } 7.8 题 #include &iostream& #define NULL 0 struct student { student * }; student *del(student //找不到该} else 结点 return(head); } cout&&&cannot find最后的结点之后 n=n+1; //结点数加1 return (head); } 7.10 题cout&&endl&&&input inserted record:&; 要插入的结点the//输入&&&//找不到该cin&&stu.num&&stu. head=insert(head,&stu); //返回地址 print(head); return 0; }7.9 题 #include &iostream& #define NULL 0 struct student { student* };#include &iostream& #define NULL 0 struct student { student * };student*creat(void)//建立链表的函数 {student * student *p1,*p2; n=0; p1=p2=new p1,p2 指向它//开辟一个新单元,并使 int main()*head,long num) {student *p1,*p2; if //是空表 {cout&&&list return(head);} p1= //使 p1 指向第一个结点 while(num!=p1-&num && p1-&next!=NULL) //p1 指 向的不是所要找的结点且 后面 还有结点 {p2=p1; p1=p1-&} //p1 后移一个结点 if(num==p1-&num) //找到了 {if(p1==head) head=p1-& 结点地址赋予 //若 p1 指向的是首结点, 把第二个 null!&&& (head==NULL) student *insert(student{ student *creat(void); student *,long); student *,student *); void print(student *); student *head, long del_ cout&&&input records:&&& head=creat(); //返回头指针 print(head); //输出全部结点 cout&&endl&&&input deleted number:&; cin&&del_ //输入要删除的学号 head=del(head,del_num); //删除后链表的头地址 print(head); //输出全部结点 cout&&endl&&&input inserted record:&; 要插入的结点 cin&&stu.num&&stu. head=insert(head,&stu); //返回地址 print(head); the student 结的函数 {student *p1,*p2; if //是空表 {cout&&&list return(head);} p1= //使 p1 指向第一个结点 while(num!=p1-&num && p1-&next!=NULL) //p1 指向 的不是所要找的结点且后 面还有 null!&&& (head==NULL) *del(student //删除 the cin&&p1-&num&&p1-& } p2-&next=NULL; return(head); } *insert(student *del(student cin&&p1-&num&&p1-& head=NULL; while(p1-&num!=0) {n=n+1; if(n==1) head=p1; else p2-&next=p1; p2=p1; p1=*head,student *stud) {student *p0,*p1,*p2; p1= //使 p1 指向第一个结点 p0= //指向要插入的结点 if(head==NULL) //原来的链表是空表 {head=p0;p0-&next=NULL;} //使 p0 指向的结点作为头 结点 else {while((p0-&num&p1-&num) && (p1-&next!=NULL)) {p2=p1; //使 p2 指向刚才 p1 指向的 结点 p1=p1-&} //p1 后移一个结点 if(p0-&num&=p1-&num) {if(head==p1) head=p0;*head,long num)head else p2-&next=p1-&//插到原来第一个结点之 前 else p2-&next=p0; //插到 p2 指向的结点之后 p0-&next=p1;}//输入//否则将下一结点地址赋 给前一结点地址cout&&&delete:&&&num&& n=n-1;else {p1-&next=p0; p0-&next=NULL;}} // 插 到//输出全部结点 {if(head==p1) 结点 {p2=p1; p1=p1-&} //p1 后移一个结点 if(num==p1-&num) //找到了 {if(p1==head) head=p1-& 结点地址赋予 //若 p1 指向的是首结点, 把第二个 / p0-&next=p1;} else {p1-&next=p0; p0-&next=NULL;}} 最后的结点之后 head else p2-&next=p1-& //否则将下一结点地址赋 给前一结点地址 } n=n+1; //结点数加1 return (head); 前 elsehead=p0;void print(student *); student *head,* long del_n=0; p1=p2=new p1,p2 指向它//插到原来第一个结点之//开辟一个新单元,并使p2-&next=p0;cout&&&input records:&&& head=creat(); //返回头指针 print(head); //输出全部结点//插到 p2 指向的结点之后*cin&&p1-&num&&p1-& head=NULL; while(p1-&num!=0) {n=n+1; the if(n==1) head=p1; else p2-&next=p1; p2=p1; p1=// 插 到cout&&endl&&&input deleted number:&; cin&&del_ //输入要删除的学号 while(del_num!=0){head=del(head,del_num); //删除后链表的头地址cin&&p1-&num&&p1-& } p2-&next=NULL; return(head);cout&&&delete:&&&num&& n=n-1; } else cout&&&cannot find &&& 结点 return(head); } //找不到该void print(student *head) //输出链表的函数 {student *p; cout&&&Now &&&n&&& &&& p= if(head!=NULL) do {cout&&p-&num&&& , These are : }print(head); //输出全部结点 cout&&&input deleted number:&; cin&&del_ the}recordsstudent 结点的函数*del(student //删除*head,long num) thecout&&endl&&&input inserted record:&; 要插入的结点 stu=new //开辟一个新结点//输入{student *p1,*p2; if (head==NULL)//是空表 {cout&&&list return(head);} null!&&&student*insert(student //插&&&p-&score&& p=p-& }while(p!=NULL); } 7.10 题另一解 #include &iostream& #define NULL 0 struct student { student * };*head,student *stud) 入结点的函数 {student *p0,*p1,*p2; p1=cin&&stu-&num&&stu-& while(stu-&num!=0) {head=insert(head,stu); //返回地址 print(head); //输出全部结点 cout&&endl&&&input the inserted record:&; 要插入的结点 stu= //输入p1= //使 p1 指向第一个结点 while(num!=p1-&num && p1-&next!=NULL) //p1 指向 的不是所要找的结点且后 面还有 结点 {p2=p1; p1=p1-&} //p1 后移一个结点 if(num==p1-&num)//使p1指向第一个结点 p0= //指向要插入的结点 if(head==NULL) //原来的链表是空表 {head=p0;p0-&next=NULL;} //使p0指向的结点作为 头结点 else {while((p0-&num&p1-&num) && (p1-&next!=NULL)) {p2=p1; //使p2指向刚才 p1指向 的结点 p1=p1-&} //p1 后移一个结点 if(p0-&num&=p1-&num)cin&&stu-&num&&stu-& } return 0;//找到了 {if(p1==head) head=p1-& 结点地址赋予 //若 p1 指向的是首结点, 把第二个int main() { student *creat(void); student *,long); student *,student *); *insert(student *del(student}student*creat(void) head else p2-&next=p1-& //否则将下一结点地址赋//建立链表的函数 {student * student *p1,*p2; 给前一结点地址 void print(student *head) cout&&&delete:&&&num&& n=n-1; } else cout&&&cannot find &&& 结点 return(head); } //找不到该 //输出链表的函数 {student *p; cout&&&Now &&&n&&& &&& p= if(head!=NULL) do {cout&&p-&num&&& &&&p-&score&& student *insert(student //插 } 8.1 题 #include &iostream& class Time {public: //成员改为公用的 }; T void set_time(void) } 8.3 题 #include &iostream& class Time {public: void show_time(void) void set_time(void); void show_time(void); private: cout&&t.hour&&&:&&&t.minut e&&&:&&&t.sec&& } // 插 到 int main() {set_time(); show_time(); return 0; } } void Time::set_time(void) {cin&& cin&& cin&& }; //在 main 函数之前定义 else p2-&next=p0; { } 8.4 题另一解 //xt8-4-2.cpp( student.cpp) #include 定义 #include &iostream& &xt8-4.h& 即 //在 main 函数之前定义 { cin&&t. cin&&t. cin&&t. } T int main() { t.set_time(); t.show_time(); return 0; 8.4 题 //xt8-4-1.cpp(main.cpp) #include &iostream& #include &xt8-4.h& int main() {S stud.set_value(); stud.display(); return 0; }; }; private: p=p-& }while(p!=NULL); {cout&&hour&&&:&&&minute& &&:&&&sec&&} } void show_time(void) 8.4 题 //xt8-4.h(student.h) class Student { public: void display( ); void set_value(); private: char name[20]; , These are : 8.2 题 #include &iostream& class Time {public: void set_time(void) {cin&& cin&& cin&& } T int main() { t.set_time(); t.show_time(); return 0; void Time::show_time(void) {cout&&hour&&&:&&&minute& &&:&&&sec&&}records*head,student *stud) 入结点的函数 {student *p0,*p1,*p2; p1=//使 p1 指向第一个结点 p0= //指向要插入的结点 if(head==NULL) //原来的链表是空表 {head=p0;p0-&next=NULL;} //使 p0 指向的结点作为头 结点 else {while((p0-&num&p1-&num) && (p1-&next!=NULL)) {p2=p1; //使 p2 指向刚才 p1 指向的 结点 p1=p1-&} //p1 后移一个结点 if(p0-&num&=p1-&num) {if(head==p1) 前 //插到 p2 指向的结点之后 p0-&next=p1;} else {p1-&next=p0; p0-&next=NULL;}} 最后的结点之后 n=n+1; //结点数加1 return (head); } head=p0;//插到原来第一个结点之//在此文件中进行函数的//不要漏写此行 void Student::display( ) { cout&&&num:&&&num&& cout&&&name:&&&name&& cout&&&sex:&&&sex&& }max=array[0]; for (i=1;i&10;i++) if(array[i]&max) max=array[i]; }box2.get_value(); cout&&&volmue of bax2 is &; box2.display(); box3.get_value(); cout&&&volmue of bax3 isbox2.volume(); cout&&&volmue of bax2 is &; box2.display(); box3.get_value(); box3.volume(); cout&&&volmue of bax3 is &; box3.display(); return 0; }void Student::set_value() { cin&& cin&& cin&& } 8.5 题 //xt8-5.h(arraymax.h) class Array_max {public: void set_value(); void max_value(); void show_value(); private: int array[10]; }; 8.5 题 //xt8-5-1.cpp(file1.cpp) #include &iostream& #include &xt8-5.h& int main() {Array_ void Box::get_value() { cout&&&please input lengh, width,height:&; cin&& cin&& cin&& } } 8.6 题 #include &iostream& class Box {public: void get_value(); float volume(); void display(); public: }; {cout&&&max=&&&max&& } } void Array_max::show_value()&; box3.display(); return 0;8.6 题另一解 #include &iostream& class Box {public: void get_value(); void volume(); void display(); public: }; 9.2 题 #include &iostream& class Date {public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: void Box::get_value() { cout&&&please input lengh, width,height:&; cin&& cin&& cin&& Date::Date(int d):month(m),day(d) void Box::volume() {year=2005;} m,int Date::Date(int m,int d,int y):month(m),day(d),year(y) {} };arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0; } 8.5 题 //xt8-5-2.cpp(arraymax.cpp) #include &iostream& #include &xt8-5.h& void Array_max::set_value() { for (i=0;i&10;i++) cin&&array[i]; }float Box::volume() { return(lengh*width*height );}{ vol=lengh*width*} Date::Date(int m):month(m) void Box::display() { cout&&vol&&} {day=1; year=2005; } int main() {Box box1,box2,box3; Date::Date() {month=1; day=1; year=2005; }void Box::display() { cout&&volume()&&}int main() {Box box1,box2,box3; box1.get_value();box1.get_value(); box1.volume(); cout&&&volmue of bax1 is &; box1.display(); box2.get_value();void Array_max::max_value() {cout&&&volmue of bax1 is &; box1.display();void Date::display() d3.display(); {cout&&month&&&/&&&day&& &/&&&year&&} } int main() { Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; } 9.3 题 #include &iostream& class Date {public: void Student::display() {cout&&num&&& &&&score&&} if(arr[i].score&max_score) {max_score=arr[i].k=i; } Date(int=1,int=1,int=2005); void display(); private: }; Student(104,100.0),Student( 105,95.5)}; Date::Date(int m,int d,int y):month(m),day(d),year(y) {} Student *p= for(int i=0;i&=2;p=p+2,i++) p-&display(); return 0; void Date::display() } Student(101,78.5),Student(1 02,85.5),Student(103,98.5), 9.6 题 #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void {cout&&month&&&/&&&day&& &/&&&year&&} 9.5 题 #include &iostream& int main() { Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); }; class Student {public: Student(int s):num(n),score(s){} n,float int main() {Student stud(101,78.5); stud.display(); stud.change(101,80.5); }; display(){cout&&num&&& &&&score&&} private: n,float } int main() {Student stud[5]={ cout&&arr[k].num&&& &&&max_score&& } 9.4 题 #include &iostream& class Student {public: Student(int s):num(n),score(s){} void display(); private: }; void max(Student *arr) {float max_score=arr[0]. int k=0; for(int i=1;i&5;i++) n,float Student(104,100.0),Student( 105,95.5)}; void max(Student* ); Student *p=&stud[0]; max(p); } Student(101,78.5),Student(1 02,85.5),Student(103,98.5), d4.display(); return 0; void main() {Student stud[5]={ }stud.display(); return 0;9.7 题 #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void {cout&&num&&& &&&score&&} //可改为:void display() const {cout&&num&&& display() n,float&&&score&&} private: };int main() {const stud(101,78.5); stud.display(); //stud.change(101,80.5); stud.display(); return 0; Student9.7 题另一解 #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) const {num=n;score=s;} display() const n,floatvoid{cout&&num&&& &&&score&&} private: };private:{num=n;score=s;} void {cout&&num&&& &&&score&&} private: display()9.9 题 #include &iostream& class Product {public: Product(int n,int q,float p):num(n),quantity(q),price( p){}; void total();int main() {const stud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return 0; } 9.7 题另一解 #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void {cout&&num&&& &&&score&&} private: }; display() n,float } Student };int main() {Student stud(101,78.5); const Student *p=& p-&display(); p-&change(101,80.5); p-&display(); return 0; };int main() {Student stud(101,78.5); Student * const p=& p-&display(); p-&change(101,80.5);static float average(); static void display();private: s9.7 题另一解 B #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() n,float }p-&display(); return 0;9.8 题 #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void {cout&&num&&& &&&score&&} private: } display() n,float };void Product::total() {float rate=1.0; if(quantity&10) rate=0.98*const{cout&&num&&& &&&score&&} private:sum=sum+quantity*price*r ate*(1-discount); n=n+int main() {Student stud(101,78.5); Student *p=& p-&display(); p-&change(101,80.5); p-&display(); return 0; } 9.7 题另一解 A #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void {cout&&num&&& &&&score&&} display() n,float } };int main() {Student stud(101,78.5); const Student *p=& p-&display(); stud.change(101,80.5); p-&display(); return 0; };void Product::display() {cout&&sum&& cout&&average()&& }int main() {Student stud(101,78.5); void fun(Student&); fun(stud); return 0; float Product::average() {return(sum/n);}9.7 题另一解 #include &iostream& class Student {public: Student(int s):num(n),score(s){} void change(int n,float s) n,float}float Product::discount=0.05;void fun(Student &stu) {stu.display(); stu.change(101,80.5); stu.display(); }float Product::sum=0; int Product::n=0;int main() { Product Prod[3]={ } Product(101,5,23.5),Product (102,12,24.56),Product(103, 100,21.5) }; for(int i=0;i&3;i++) Prod[i].total(); Product::display(); return 0; } 9.10 题 #include &iostream& class D class Time {public: Time(int,int,int); friend void display(const Date &,const Time &); private: }; 9.11 题 #include &iostream& Time::Time(int h,int m,int s) {hour=h; minute=m; sec=s; } class T class Date {public: Date(int,int,int); friend T private: class Date {public: Date(int,int,int); friend void display(const Date &,const Time &); private: }; class Time {public: Time(int,int,int); Date::Date(int m,int d,int y) {month=m; day=d; year=y; &); private: void display(const Date }; Date::Date(int m,int d,int y):month(m),day(d),year(y){ } }; } } int main() { Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; cout&&t.hour&&&:&&&t.minut e&&&:&&&t.sec&& } cout&&d.month&&&/&&&d.da y&&&/&&&d.year&& void display(const Date };Compare&numtype&::max() {return (x&y)?x:y;} template &class numtype& numtype&d,const Time &t) { Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){ }Compare&numtype&::min() {return (x&y)?x:y;}int main() void Date &d) { Time::display(const {Compare&int& cmp1(3,7); cout&&cmp1.max()&&& the Maximum of is twointeger numbers.&&& cout&&d.month&&&/&&&d.da y&&&/&&&d.year&& cout&&cmp1.min()&&& isthe Minimum of two integer numbers.&&&endl&&cout&&hour&&&:&&&minute& &&:&&&sec&& }Compare&float& cmp2(45.78,93.6); cout&&cmp2.max()&&& isthe Maximum of two float numbers.&&& int main() { Time t1(10,13,56); Date d1(12,25,2004); t1.display(d1); return 0; cout&&cmp2.min()&&& isthe Minimum of two float numbers.&&&endl&& Compare&char& cmp3('a','A'); cout&&cmp3.max()&&& the Maximum of is twocharacters.&&& cout&&cmp3.min()&&& 9.12 题 #include &iostream& template&class numtype& class Compare {public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: 10.1 题 #include &iostream& class Complex {public: } the Minimum of is twocharacters.&&& return 0;template &class numtype& Compare&numtype&::Comp are(numtype a,numtype b) {x=a;y=b;} template &class numtype& numtype
};void display(); private:void Complex::display() {cout&&&(&&&real&&&,&&&ima g&&&i)&&&}x &c) {return Complex(real+c.real,imag+c. imag);}double Complex::get_real() {} }; int mai}

我要回帖

更多关于 递归下降子程序伪代码 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信