当前位置:首页 >> 计算机软件及应用 >> 《c++程序设计》谭浩强

《c++程序设计》谭浩强


CCCCDFDSFFVGCCC

C++程序设计课后答案

袁俊南

袁俊南

第一章
1.5 题 #include <iostream> using namespace std; int main() { cout<<"This"<<

;"is"; cout<<"a"<<"C++"; cout<<"program."; return 0;} 1.6 题 #include <iostream> using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout<<c; cout<<endl; return 0; } 1.7 七题 #include <iostream> using namespace std;

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<<endl; return 0; } int f(int x,int y,int z) { int m; if (x<y) m=x; else m=y; if (z<m) m=z; return(m); } 1.8 题 #include <iostream> using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="<<a+b<<endl; return 0; } 1.9 题 #include <iostream>

using namespace std; int main() { int a,b,c; int add(int x,int y); cin>>a>>b; c=add(a,b); cout<<"a+b="<<c<<endl; return 0; } int add(int x,int y) {int z; z=x+y; return(z); }

第二章
2.3 题 #include <iostream> using namespace std; int main() {char c1='a',c2='b',c3='c',c4='\101',c5='\116'; cout<<c1<<c2<<c3<<'\n'; cout<<"\t\b"<<c4<<'\t'<<c5<<'\n'; return 0; } 2.4 题



袁俊南
#include <iostream> using namespace std; int main() {char c1='C',c2='+',c3='+'; cout<<"I say: \""<<c1<<c2<<c3<<'\"'; cout<<"\t\t"<<"He says: \"C++ is very interesting!\""<< '\n'; return 0; } 2.7 题 #include <iostream> using namespace std; int main() {int i=8; j=10; m=++i+j++; n=(++i)+(++j)+m; cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl; return 0; } 2.8 题 #include <iostream> using namespace std; int main() {char c1='C', c2='h', c3='i', c4='n', c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; } 3.3 题 #include <iostream> i,j,m,n; 3.2 题 #include <iostream> #include <iomanip> using namespace std; int main ( ) {float h,r,l,s,sq,vq,vz; const float pi=3.1415926; cout<<"please enter r,h:"; cin>>r>>h; l=2*pi*r; s=r*r*pi; sq=4*pi*r*r; vq=3.0/4.0*pi*r*r*r; vz=pi*r*r*h; cout<<setiosflags(ios::fixed)<<setiosflags(ios::right) <<setprecision(2); cout<<"l= "<<setw(10)<<l<<endl; cout<<"s= "<<setw(10)<<s<<endl; cout<<"sq="<<setw(10)<<sq<<endl; cout<<"vq="<<setw(10)<<vq<<endl; cout<<"vz="<<setw(10)<<vz<<endl; return 0; } 3.4 题另一解 #include <iostream> using namespace std; 3.4 题 #include <iostream> using namespace std; int main ( ) {char c1,c2; cout<<"请输入两个字符 c1,c2:"; c1=getchar(); c2=getchar(); putchar(c1); putchar(c2); cout<<endl; cout<<"用 cout 语句输出结果为:"; cout<<c1<<c2<<endl; return 0; //将输入的第一个字符赋给 c1 //将输入的第二个字符赋给 c2 } cout<<"password is:"<<c1<<c2<<c3<<c4<<c5<<endl; return 0; using namespace std; int main () {float c,f; cout<<"请输入一个华氏温度:"; cin>>f; c=(5.0/9.0)*(f-32); 为0 cout<<"摄氏温度为:"<<c<<endl; return 0; }; //注意 5 和 9 要用实型表示,否则 5/9 值

第三章

cout<<"用 putchar 函数输出结果为:";



袁俊南
int main ( ) {char c1,c2; cout<<"请输入两个字符 c1,c2:"; c1=getchar(); c2=getchar(); putchar(c1); putchar(44); putchar(c2); cout<<endl; cout<<"用 cout 语句输出结果为:"; cout<<c1<<","<<c2<<endl; return 0; } 3.5 题 #include <iostream> using namespace std; int main ( ) {char c1,c2; int i1,i2; cin>>i1>>i2; c1=i1; c2=i2; cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl; return 0; } 3.8 题 #include <iostream> using namespace std; 3.9 题另一解 } } else // x≥10 else cout<<"max="<<a; cout<<endl; return 0; } else if (x<10) {y=2*x-1; cout<<"x="<<x<<", y=2*x-1="<<y; // 1≤x<10 //定义为整型? cout<<"请输入两个整数 i1,i2:"; } 3.9 题 include <iostream> using namespace std; int main ( ) {int a,b,c; cout<<"please enter 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; 3.10 题 #include <iostream> using namespace std; int main ( ) {int x,y; cout<<"enter x:"; cin>>x; if (x<1) {y=x; cout<<"x="<<x<<", y=x="<<y; } //将输入的第一个字符赋给 c1 //将输入的第二个字符赋给 c2 int main ( ) { int a=3,b=4,c=5,x,y; cout<<(a+b>c && b==c)<<endl; cout<<(a||b+c && b-c)<<endl; cout<<(!(a>b) && !c||1)<<endl; cout<<(!(x=a) && (y=b) && 0)<<endl; cout<<(!(a+b)+c-1 && b+c/2)<<endl; return 0; #include <iostream> using namespace std; int main ( ) {int a,b,c,temp,max ; cout<<"please enter three integer numbers:"; cin>>a>>b>>c; temp=(a>b)?a:b; 中 */ max=(temp>c)?temp:c; c 比较,最大者存入 max */ cout<<"max="<<max<<endl; return 0; /* 将 a 和 b 中的大者与 /* 将 a 和 b 中的大者存入 temp

cout<<"用 putchar 函数输出结果为:";



袁俊南
{y=3*x-11; cout<<"x="<<x<<", y=3*x-11="<<y; } cout<<endl; return 0; } 3.11 题 #include <iostream> using namespace std; int main () {float score; char grade; cout<<"please enter score of student:"; cin>>score; while (score>100||score<0) {cout<<"data error,enter data again."; cin>>score; } switch(int(score/10)) {case 10: case 9: grade='A';break; case 8: grade='B';break; case 7: grade='C';break; case 6: grade='D';break; default:grade='E'; } cout<<"score is "<<score<<", grade is "<<grade<<endl; return 0; } 3.12 题 indiv=(int)(num-ten_thousand*10000-thousand*1000-hundre d*100-ten*10); } ten=(int)(num-ten_thousand*10000-thousand*1000-hundred* 100)/10; hundred=(int)(num-ten_thousand*10000-thousand*1000)/100 ; else else else #include <iostream> using namespace std; int main () {long int num; int indiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位,十位,百位,千位,万位和位数*/ cout<<"enter an integer(0~99999):"; cin>>num; if (num>9999) place=5; if (num>999) place=4; if (num>99) place=3; if (num>9) place=2; else place=1; cout<<"place="<<place<<endl; //计算各位数字 ten_thousand=num/10000; thousand=(int)(num-ten_thousand*10000)/1000; dl; cout<<"reverse order:"; cout<<indiv<<ten<<hundred<<thousand<<endl; break; case 3:cout<<hundred<<","<<ten<<","<<indiv<<endl; cout<<"reverse order:"; cout<<indiv<<ten<<hundred<<endl; break; case 2:cout<<ten<<","<<indiv<<endl; cout<<"reverse order:"; cout<<indiv<<ten<<endl; break; case 1:cout<<indiv<<endl; cout<<"reverse order:"; cout<<indiv<<endl; break; case 4:cout<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<en cout<<indiv<<ten<<hundred<<thousand<<ten_thousand<<end l; break; cout<<"original order:"; switch(place) {case 5:cout<<ten_thousand<<","<<thousand<<","<<hundred<<","<< ten<<","<<indiv<<en dl; cout<<"reverse order:";



袁俊南
return 0; } 3.13 题 #include <iostream> using namespace std; int main () { long i; bon1=100000*0.1; 金 bon2=bon1+100000*0.075; 金 bon4=bon2+100000*0.05; 金 bon6=bon4+100000*0.03; 金 bon10=bon6+400000*0.015; 奖金 cout<<"enter i:"; cin>>i; if (i<=100000) bonus=i*0.1; 金 else if (i<=200000) bonus=bon1+(i-100000)*0.075; 至 20 万时的奖金 else if (i<=400000) bonus=bon2+(i-200000)*0.05; 至 40 万时的奖金 else if (i<=600000) //利润在 20 万元 //利润在 10 万元 //利润在 10 万元以内按 10%提成奖 //利润为 100 万元时的 //利润为 60 万元时的奖 //利润为 40 万元时的奖 //利润为 20 万元时的奖 } 3.13 题另一解 #include <iostream> using namespace std; int main () {long i; float bonus,bon1,bon2,bon4,bon6,bon10; int c; bon1=100000*0.1; bon2=bon1+100000*0.075; bon4=bon2+200000*0.05; bon6=bon4+200000*0.03; bon10=bon6+400000*0.015; cout<<"enter i:"; cin>>i; c=i/100000; if (c>10) switch(c) {case 0: bonus=i*0.1; break; case 1: bonus=bon1+(i-100000)*0.075; break; c=10; } 3.14 题 #include <iostream> using namespace std; int main () {int t,a,b,c,d; cout<<"enter four numbers:"; cin>>a>>b>>c>>d; cout<<"a="<<a<<", b="<<b<<", c="<<c<<",d="<<d<<endl; 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) //i 为利润 //利润为 10 万元时的奖 float bonus,bon1,bon2,bon4,bon6,bon10; bonus=bon4+(i-400000)*0.03; 60 万时的奖金 else if (i<=1000000) bonus=bon6+(i-600000)*0.015; 100 万时的奖金 else bonus=bon10+(i-1000000)*0.01; 以上时的奖金 cout<<"bonus="<<bonus<<endl; return 0; //利润在 100 万元 //利润在 60 万元至 //利润在 40 万元至 case 2: case 3: bonus=bon2+(i-200000)*0.05;break; case 4: case 5: bonus=bon4+(i-400000)*0.03;break; case 6: case 7: case 8: case 9: bonus=bon6+(i-600000)*0.015; break; case 10: bonus=bon10+(i-1000000)*0.01; } cout<<"bonus="<<bonus<<endl; return 0;



袁俊南
{t=b; b=d; d=t;} if (c>d) {t=c; c=d; d=t;} cout<<"the sorted sequence:"<<endl; cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl; return 0; } 3.15 题 #include <iostream> using namespace std; int main () {int p,r,n,m,temp; cout<<"please enter two positive integer numbers n,m:"; cin>>n>>m; if (n<m) {temp=n; n=m; m=temp; m中 } p=n*m; 中, 以便求最小公倍数时用 while (m!=0) {r=n%m; n=m; m=r; } cout<<"HCF="<<n<<endl; cout<<"LCD="<<p/n<<endl; 乘积 // p 是原来两个整数的 3.17 题 #include <iostream> using namespace std; 3.19 题 #include <iostream> //求 n 和 m 的最大公约数 //先将 n 和 m 的乘积保存在 p } cout<<"letter:"<<letters<<", digit:"<<digit<<", other:"<<other<<endl; return 0; } } cout<<"1!+2!+...+20!="<<s<<endl; return 0; } space:"<<space<<", //把大数放在 n 中, 小数放在 else other++; 3.16 题 #include <iostream> using namespace std; int main () {char c; int letters=0,space=0,digit=0,other=0; cout<<"enter one line::"<<endl; while((c=getchar())!='\n') {if (c>='a' && c<='z'||c>='A' && c<='Z') letters++; else if (c==' ') space++; else if (c>='0' && c<='9') digit++; } cout<<"a+aa+aaa+...="<<sn<<endl; return 0; } 3.18 题 #include <iostream> using namespace std; int main () {float s=0,t=1; int n; for (n=1;n<=20;n++) { t=t*n; s=s+t; // 求 n! // 将各项累加 return 0; } int main () {int a,n,i=1,sn=0,tn=0; cout<<"a,n=:"; cin>>a>>n; while (i<=n) {tn=tn+a; sn=sn+tn; a=a*10; ++i; //赋值后的 tn 为 i 个 a 组成数的值 //赋值后的 sn 为多项式前 i 项之和



袁俊南
using namespace std; int main () {int i,j,k,n; cout<<"narcissus numbers are:"<<endl; 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<<endl; return 0; } 3.20 题 #include <iostream> using namespace std; int main() {const int m=1000; int i,a,n,s; for (a=2;a<=m;a++) 它是否为完数 {n=0; s=a; 和,开始时等于 a for (i=1;i<a;i++) if (a%i==0) {n++; s=s-i; // 检查 i 是否为 a 的因子 // 如果 i 是 a 的因子 // n 加 1,表示新找到一个因子 // s 减去已找到的因子,s 的新 了 {cout<<a<<" is a 完数"<<endl; cout<<"its factors are:"; if (n>1) cout<<k1<<","<<k2; // n>1 表示 a 至少有 // n 用来累计 a 的因子的个数 // s 用来存放尚未求出的因子之 } if (s==0) // s=0 表示全部因子都已找到 // a 是 2~1000 之间的整数, 检查 k10 } // 定义寻找范围 int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10; 值是尚未求出的因子之和 switch(n) {case 1: k1=i; case 2: k2=i; case 3: k3=i; case 4: k4=i; case 5: k5=i; case 6: k6=i; case 7: k7=i; case 8: k8=i; case 9: k9=i; case 10: k10=i; break; // 找出的笫 10 个因子赋给 break; // 找出的笫 9 个因子赋给 k9 break; // 找出的笫 8 个因子赋给 k8 break; // 找出的笫 7 个因子赋给 k7 } 3.20 题另一解 #include <iostream> using namespace std; 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 完数"<<endl; cout<<"its factors are:"; for (i=1;i<m;i++) if (m%i==0) cout<<i<<" "; break; // 找出的笫 6 个因子赋给 k6 } return 0; break; // 找出的笫 5 个因子赋给 k5 } break; // 找出的笫 4 个因子赋给 k4 break; // 找出的笫 3 个因子赋给 k3 break; // 找出的笫 2 个因子赋给 k2 break; // 找出的笫 1 个因子赋给 k1 // 将找到的因子赋给 k1,...,k10 2 个因子 if (n>2) if (n>3) if (n>4) if (n>5) if (n>6) if (n>7) if (n>8) if (n>9) cout<<","<<k3; // n>2 表示至少有 3 个因子, cout<<","<<k4; // n>3 表示至少有 4 个因子, cout<<","<<k5; cout<<","<<k6; cout<<","<<k7; cout<<","<<k8; cout<<","<<k9; cout<<","<<k10; // 以下类似 故应再输出一个因子 故应再输出一个因子

cout<<endl<<endl;



袁俊南
cout<<endl; } } return 0; } 3.20 题另一解 #include <iostream> using namespace std; int main() {int k[11]; int i,a,n,s; for (a=2;a<=1000;a++) {n=0; s=a; for (i=1;i<a;i++) if ((a%i)==0) {n++; s=s-i; k[n]=i; k[10] } if (s==0) {cout<<a<<" is a 完数"<<endl; cout<<"its factors are:"; for (i=1;i<n;i++) cout<<k[i]<<" "; cout<<k[n]<<endl; } } return 0; // 将找到的因子赋给 k[1]┅ 3.22 题 #include <iostream> using namespace std; int main() {int day,x1,x2; day=9; x2=1; while(day>0) {x1=(x2+1)*2; 数加 1 后的 2 倍 x2=x1; // 第 1 天的桃子数是第 2 天桃子 3.24 题 #include <iostream> using namespace std; int main() {int i,k; } } cout<<"sum="<<s<<endl; return 0; } 分子 b=t; // 将前一项的分子作为下一项的分母 3.21 题 #include <iostream> using namespace std; 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; // 将前一项分子与分母之和作为下一项的 3.23 题 #include <iostream> #include <cmath> using namespace std; int main() {float a,x0,x1; cout<<"enter a positive number:"; cin>>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<<endl; return 0; // 输入 a 的值 } } cout<<"total="<<x1<<endl; return 0; } day--;



袁俊南
for (i=0;i<=3;i++) {for (k=0;k<=2*i;k++) cout<<"*"; cout<<endl; } for (i=0;i<=2;i++) {for (k=0;k<=4-2*i;k++) cout<<"*"; cout<<endl; } return 0; } 3.25 题 #include <iostream> using namespace std; int main() {char i,j,k; 手*/ for (i='X';i<='Z';i++) for (j='X';j<='Z';j++) if (i!=j) for (k='X';k<='Z';k++) if (i!=k && j!=k) if (i!='X' && k!='X' && k!='Z') cout<<"A--"<<i<<" C--"<<k<<endl; return 0; } int lcd(int u,int v,int h) {return(u*v/h); } return 0; B--"<<j<<" } /* i 是 a 的对手;j 是 b 的对手;k 是 c 的对 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); } else {smaller_than_zero(a,b); cout<<"x1="<<p<<"+"<<q<<"i"<<endl; cout<<"x2="<<p<<"-"<<q<<"i"<<endl; } else if (disc==0) {equal_to_zero(a,b); cout<<"x1="<<x1<<",x2="<<x2<<endl; } // 输出*号 // 输出完一行*号后换行 // 输出下面 3 行*号 // 输出*号 //输出完一行*号后换行 4.1 题 #include <iostream> using namespace std; int main() {int hcf(int,int); int lcd(int,int,int); int u,v,h,l; cin>>u>>v; h=hcf(u,v); cout<<"H.C.F="<<h<<endl; l=lcd(u,v,h); cout<<"L.C.D="<<l<<endl; return 0; // 输出上面 4 行*号

第四章

} 4.2 题 #include <iostream> #include <math.h> using namespace std; float x1,x2,disc,p,q; int main() {void greater_than_zero(float,float); void equal_to_zero(float,float); void smaller_than_zero(float,float); float a,b,c; cout<<"input a,b,c:"; cin>>a>>b>>c; disc=b*b-4*a*c; cout<<"root:"<<endl; if (disc>0) { greater_than_zero(a,b); cout<<"x1="<<x1<<",x2="<<x2<<endl;



袁俊南
} void greater_than_zero(float a,float b) 用来求 disc>0 时方程的根 */ {x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); } void equal_to_zero(float a,float b) 来求 disc=0 时方程的根 */ { x1=x2=(-b)/(2*a); } void smaller_than_zero(float a,float b) 来求 disc<0 时方程的根 */ { p=-b/(2*a); q=sqrt(-disc)/(2*a); } 4.3 题 #include <iostream> using namespace std; int main() {int prime(int); int n; cout<<"input an integer:"; cin>>n; if (prime(n)) int fac(int n) {int f=1; /* 函数原型声明 */ } /* 定义一个函数, 用 4.4 题 #include <iostream> using namespace std; int main() {int fac(int); int a,b,c,sum=0; cout<<"enter a,b,c:"; cin>>a>>b>>c; sum=sum+fac(a)+fac(b)+fac(c); cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<endl; return 0; 4.6 题 #include <iostream> #include <cmath> using namespace std; int main() {double solut(double ,double ,double ,double ); double a,b,c,d; double e(double x) {return exp(x);} } } /* 定义一个函数,用 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); } /* 定义一个函数, cout<<n<<" is a prime."<<endl; else cout<<n<<" is not a prime."<<endl; return 0; } 4.5 题 #include <iostream> #include <cmath> using namespace std; int main() {double e(double); double x,sinh; cout<<"enter x:"; cin>>x; sinh=(e(x)+e(-x))/2; cout<<"sinh("<<x<<")="<<sinh<<endl; return 0; for (int i=1;i<=n;i++) f=f*i; return f;

10

袁俊南
cout<<"input a,b,c,d:"; cin>>a>>b>>c>>d; cout<<"x="<<solut(a,b,c,d)<<endl; return 0; } double solut(double a,double b,double c,double d) {double x=1,x0,f,f1; do {x0=x; 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); } 4.7 题 #include <iostream> #include <cmath> using namespace std; int main() {void godbaha(int); int n; cout<<"input n:"; cin>>n; godbaha(n); return 0; } void godbaha(int n) 4.8 题 #include <iostream> using namespace std; int main() {int x,n; float p(int,int); cout<<"input n & x:"; cin>>n>>x; cout<<"n="<<n<<",x="<<x<<endl;; cout<<"P"<<n<<"(x)="<<p(n,x)<<endl; return 0; void hanoi(int n,char one,char two,char three) //将 n 个盘从 one 座借助 two 座,移到 three 座 {void move(char x,char y); if(n==1) move(one,three); else {hanoi(n-1,one,three,two); } } else int prime(int m) {int i,k=sqrt(m); for(i=2;i<=k;i++) if(m%i==0) break; if (i>k) return 1; return 0; 4.9 题 #include <iostream> using namespace std; int main() {void hanoi(int n,char one,char two,char three); int m; cout<<"input the number of diskes:"; cin>>m; cout<<"The steps of moving "<<m<<" disks:"<<endl; hanoi(m,'A','B','C'); return 0; } } } {int prime(int); int a,b; for(a=3;a<=n/2;a=a+2) {if(prime(a)) {b=n-a; if (prime(b)) cout<<n<<"="<<a<<"+"<<b<<endl;} float p(int n,int x) {if (n==0) return(1); else if (n==1) return(x); else return(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/n); }

11

袁俊南
move(one,three); hanoi(n-1,two,one,three); } } void move(char x,char y) {cout<<x<<"-->"<<y<<endl;} 4.10 题 #include <iostream> using namespace std; int main() {void convert(int n); int number; cout<<"input an integer:"; cin>>number; cout<<"output:"<<endl; if (number<0) {cout<<"-"; number=-number; } convert(number); cout<<endl; return 0; } void convert(int n) {int i; char c; if ((i=n/10)!=0) convert(i); c=n%10+'0'; } 4.12 题 #include <iostream> #include <cmath> using namespace std; #define S(a,b,c) #define sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)) (a+b+c)/2 AREA(a,b,c) } 4.15 题 #include <iostream> #endif cout<<c<<endl; return 0; int f(int n) {; if (n==1) return 1; else return (n*n+f(n-1)); #else if(c>='A' && c<='Z') c=c+32; } 4.11 题 #include <iostream> using namespace std; int main() {int f(int); int n,s; cout<<"input the number n:"; cin>>n; s=f(n); cout<<"The result is "<<s<<endl; return 0; 4.14 题 #include <iostream> using namespace std; //#define LETTER 1 int main() {char c; cin>>c; #if LETTER if(c>='a' && c<='z') c=c-32; } cout<<" "<<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) cout<<"area="<<AREA(a,b,c)<<endl; else cout<<"It is not a triangle!"<<endl; return 0;

12

袁俊南
using namespace std; #define CHANGE 1 int main() {char ch[40]; cout<<"input text:"<<endl;; gets(ch); #if (CHANGE) {for (int i=0;i<40;i++) {if (ch[i]!='\0') if (ch[i]>='a'&& ch[i]<'z'||ch[i]>'A'&& ch[i]<'Z') ch[i]+=1; else if (ch[i]=='z'||ch[i]=='Z') ch[i]-=25; } } #endif cout<<"output:"<<endl<<ch<<endl; return 0; } 4.16 题 file #include <iostream> using namespace std; int a; int main() {extern int power(int); int b=3,c,d,m; cout<<"enter an integer a and its power m:"<<endl; cin>>a>>m; c=a*b; cout<<a<<"*"<<b<<"="<<c<<endl; 5.1 题 #include <iostream> #include <iomanip> using namespace std; #include <math.h> int main() {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) if (a[j]%a[i]==0) a[j]=0; } } cout<<endl<<"The original numbers:"<<endl;; for (i=1;i<=10;i++) cout<<a[i]<<" "; cout<<endl;; for (i=1;i<=9;i++) {min=i; for (j=i+1;j<=10;j++) //以下 8 行是对 10 个数排序 // 输出这 10 个数 } } 4.16 题 file extern int a; int power(int n) {int i,y=1; for(i=1;i<=n;i++) y*=a; return y; } } cout<<endl; return 0; 5.2 题 #include <iostream> using namespace std; //#include <math.h> int main() {int i,j,min,temp,a[11]; cout<<"enter data:"<<endl; for (i=1;i<=10;i++) {cout<<"a["<<i<<"]="; cin>>a[i]; //输入 10 个数 d=power(m); cout<<a<<"**"<<m<<"="<<d<<endl; return 0; cout<<endl; for (i=1,n=0;i<=100;i++) {if (a[i]!=0) {cout<<setw(5)<<a[i]<<" "; n++;} if(n==10) {cout<<endl; n=0;}

第五章

13

袁俊南
if (a[min]>a[j]) min=j; temp=a[i]; //以下 3 行将 a[i+1]~ a[10]中最小者与 a[i] 对换 a[i]=a[min]; a[min]=temp; } cout<<endl<<"The sorted numbers:"<<endl; for (i=1;i<=10;i++) cout<<a[i]<<" "; cout<<endl; return 0; } 5.3 题 #include <iostream> using namespace std; int main() {int a[3][3],sum=0; int i,j; cout<<"enter data:"<<endl;; 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<<endl; return 0; } 5.4 题 #include <iostream> } 5.5 题 #include <iostream> } cout<<"Now, array a:"<<endl; for (i=0;i<11;i++) cout<<a[i]<<" "; cout<<endl; return 0; } } 5.6 题 #include <iostream> #include <iomanip> using namespace std; int main() {const int n=11; int i,j,a[n][n]; for (i=1;i<n;i++) } // 输出已排好序的 10 个数 using namespace std; int main() {int a[11]={1,4,6,9,13,16,19,28,40,100}; int num,i,j; cout<<"array a:"<<endl; for (i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl;; cout<<"insert data:"; cin>>num; if (num>a[9]) a[10]=num; else {for (i=0;i<10;i++) {if (a[i]>num) {for (j=9;j>=i;j--) a[j+1]=a[j]; a[i]=num; break; } cout<<endl<<"Now,array a:"<<endl; for (i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; return 0; using namespace std; int main() { const int n=5; int a[n],i,temp; cout<<"enter array a:"<<endl; for (i=0;i<n;i++) cin>>a[i]; cout<<"array a:"<<endl; for (i=0;i<n;i++) cout<<a[i]<<" "; for (i=0;i<n/2;i++) 互换 { temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp; //循环的作用是将对称的元素的值

14

袁俊南
{a[i][i]=1; a[i][1]=1; } for (i=3;i<n;i++) for (j=2;j<=i-1;j++) a[i][j]=a[i-1][j-1]+a[i-1][j]; for (i=1;i<n;i++) {for (j=1;j<=i;j++) cout<<setw(5)<<a[i][j]<<" "; cout<<endl; } cout<<endl; return 0; } 5.7 题 #include <iostream> using namespace std; int main() { const int n=4,m=5; int i,j,a[n][m],max,maxj; bool flag; for (i=0;i<n;i++) for (j=0;j<m;j++) cin>>a[i][j]; for (i=0;i<n;i++) {max=a[i][0]; maxj=0; for (j=0;j<m;j++) if (a[i][j]>max) {max=a[i][j]; 中 //将本行的最大数存放在 max //找出第 i 行中的最大数 //输入数组 5.8 题 #include <iostream> using namespace std; int main() { const int n=7; int i,number,top,bott,mid,loca,a[n]; bool flag=true,sign; char c; cout<<"enter data:"<<endl;; } //假设数组为 4 行 5 列 } } if(!flag) return 0; //如果 flag 为假表示鞍点不存在 cout<<"It does not exist!"<<endl; } break; if(flag) 表 for (int k=0;k<n;k++) if (max>a[k][maxj]) {flag=false; 鞍点令 flag1 为假 continue;} //如果 flag1 为真表示是鞍点 //输出鞍点的值和所在行列号 {cout<<"a["<<i<<"]["<<"["<<maxj<<"]="<<max<<endl; //将最大数和其同列元素相比 } cout<<endl; for (i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; while(flag) {cout<<"input number to look for:"; cin>>number; sign=false; top=0; bott=n-1; 查找区间内 loca=-1; // 表示找不到 //top 是查找区间的起始位置 //bott 是查找区间的最末位置 //要查的数不在 //如果 max 不是同列最小, 表示不是 在 maxj 中 } flag=true; //先假设是鞍点,以 flag 为真代 maxj=j; //将最大数所在的列号存放 cin>>a[0]; i=1; while(i<n) {cin>>a[i]; if (a[i]>=a[i-1]) i++; else cout<<"enter this data again:";

if ((number<a[0])||(number>a[n-1]))

while ((!sign) && (top<=bott)) {mid=(bott+top)/2; if (number==a[mid]) {loca=mid; cout<<"Find "<<loca+1<<endl; sign=true; "<<number<<", its position is

15

袁俊南
else if (number<a[mid]) bott=mid-1; else top=mid+1; } if(!sign||loca==-1) cout<<number<<" has not found."<<endl;; cout<<"continu or not(Y/N)?"; cin>>c; if (c=='N'||c=='n') flag=false; } return 0; } 5.9 题 #include <iostream> using namespace std; int main() {int sum_day(int,int); int leap(int year); int year,month,day,days=0; cout<<"input date(year,month,day):"; cin>>year>>month>>day; cout<<year<<"/"<<month<<"/"<<day; days=sum_day(month,day); /* 调用函数一 */ if(leap(year) /* 调用函数二 */ days=days+1; && month>=3) 5.10 题 #include <iostream> using namespace std; int main() {int i,j,upper,lower,digit,space,other; char text[3][80]; upper=lower=digit=space=other=0; for (i=0;i<3;i++) {cout<<"please input line "<<i+1<<endl; gets(text[i]); for (j=0;j<80 && text[i][j]!='\0';j++) 格 } } 5.11 题 #include <iostream> using namespace std; int main() { char a[5]={'*','*','*','*','*'}; int i,j,k; char space=' '; for (i=0;i<5;i++) { cout<<endl; cout<<" "; // 输出 5 行 // 输出每行前先换行 // 每行前面留 4 个空 int leap(int year) {int leap; leap=year%4==0&&year%100!=0||year%400==0; return(leap); //判断是否为闰年 } int sum_day(int month,int day) {int i; int day_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for (i=0;i<month-1;i++) day+=day_tab[i]; return(day); } cout<<"upper case:"<<upper<<endl; cout<<"lower case:"<<lower<<endl; cout<<"digit cout<<"space cout<<"other return 0; :"<<digit<<endl; :"<<space<<endl; :"<<other<<endl; } //计算日期 cout<<" is the "<<days<<"th day in this year."<<endl; return 0; } {if (text[i][j]>='A'&& text[i][j]<='Z') upper++; else if (text[i][j]>='a' && text[i][j]<='z') lower++; else if (text[i][j]>='0' && text[i][j]<='9') digit++; else if (text[i][j]==' ') space++; else other++;

16

袁俊南
for (j=1;j<=i;j++) cout<<space; for (k=0;k<5;k++) cout<<a[k]; } cout<<endl; return 0; } 5.11 题另一解 #include <iostream> #include <string> using namespace std; int main() { string stars="*****"; int i,j; for (i=0;i<5;i++) { cout<<" "; for (j=1;j<=i;j++) cout<<" "; cout<<stars<<endl; } return 0; } 5.12 题 #include <iostream> using namespace std; int main() {int j,n; char ch[80],tran[80]; cout<<"input cipher code:"; // 每行再插入 i 个空格 // 输出 5 个*号 5.12 题另一解 #include <iostream> using namespace std; int main() {int j,n; char ch[80]; cout<<"input cipher code:"; gets(ch); cout<<"cipher code:"<<ch<<endl; j=0; } // 输出 5 行 // 每行前面留 4 个空格 } n=j; cout<<"original text:"; for (j=0;j<n;j++) putchar(tran[j]); cout<<endl; return 0; 5.12 另一解 #include <iostream> #include <string> using namespace std; int main() {int j; string ch="I will visit China next week.",tran; tran=ch; cout<<"cipher code:"<<ch<<endl; j=0; while (j<=ch.size()) { if ((ch[j]>='A') && (ch[j]<='Z')) } // 每行输出 5 个*号 // 每行再留一个空格 gets(ch); cout<<"cipher code:"<<ch<<endl; j=0; while (ch[j]!='\0') { 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(ch[j]); cout<<endl; return 0; while (ch[j]!='\0') { 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]; else ch[j]=ch[j]; j++;

17

袁俊南
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<<endl; return 0; } #include <iostream> #include <string> using namespace std; int main() {int j; string ch="I will visit China next week."; cout<<"cipher code:"<<ch<<endl; j=0; 5.12 另一解 #include <iostream> #include <string> using namespace std; int main() {int j; string ch="I will visit China next week."; cout<<"cipher code:"<<ch<<endl; 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++; } cout<<"original text:"; 5.13 题 #include <iostream> #include <string> using namespace std; } 5.13 另一解 } } cout<<"original text:"; cout<<ch<<endl; return 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.13 另一解 #include <iostream> using namespace std; int main() {char s1[80],s2[40]; cout<<"input string1:"; cin>>s1; cout<<"input string2:"; cin>>s2; strcat(s1,s2); cout<<"The new string is:"<<s1<<endl; return 0; } } cout<<ch<<endl; return 0; 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<<endl; return 0;

18

袁俊南
#include <iostream> #include <string> using namespace std; int main() { string s1="week",s2="end"; cout<<"s1="<<s1<<endl; cout<<"s2="<<s2<<endl; s1=s1+s2; cout<<"The new string is:"<<s1<<endl; return 0; } 5.15 题 #include <iostream> #include <string> using namespace std; int main() { const int n=5; string str; for(int i=0;i<n;i++) 5.14 题 #include <iostream> #include <string> using namespace std; int main() { const int n=5; int i,j; string str[n],temp; cout<<"please input strings:"<<endl; 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]) {temp=str[j];str[j]=str[j+1];str[j+1]=temp;} cout<<endl<<"sorted strings:"<<endl; for(i=0;i<n;i++) 5.16 题 #include <iostream> using namespace std; int main() { const n=10; int i; char a[n],temp; cout<<"please input a string:"; } 5.17 题 #include <iostream> #include <string> using namespace std; const int n=10; } return 0; {cout<<"please input string:"; cin>>str; if(str[0]=='A') cout<<str<<endl;} } 5.16 题另一解 #include <iostream> #include <string> using namespace std; int main() { string a; int i,n; char temp; cout<<"please input a string:"; cin>>a; n=a.size(); for(i=0;i<n/2;i++) {temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;} cout<<a<<endl; return 0; } cout<<str[i]<<endl; return 0; for(i=0;i<n;i++) cin>>a[i]; for(i=0;i<n/2;i++) {temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;} for(i=0;i<n;i++) cout<<a[i]; cout<<endl; return 0;

19

袁俊南
string name[n]; int num[n],score[n]; int main() {int i; void input_data(); input_data(); cout<<endl<<"The list of failed:"<<endl; for(i=0;i<n;i++) if(score[i]<60) cout<<name[i]<<" "<<num[i]<<" return 0; } void input_data() {int i; for (i=0;i<n;i++) {cout<<"input "<<i+1<<":"; cin>>name[i]>>num[i]>>score[i];} } 6.2 题 #include <iostream> #include <cstring> using namespace std; int main() 6.1 题 #include <iostream> #include <string> using namespace std; const int n=10; string name[n]; int num[n],score[n]; {void swap(char *,char *); char str1[20],str2[20],str3[20]; cout<<"input three line:"<<endl; gets(str1); gets(str2); gets(str3); if(strcmp(str1,str2)>0) swap(str1,str2); name,number and score of student } "<<score[i]<<endl; } void input_data() {int i; for (i=0;i<n;i++) {cout<<"input "<<i+1<<":"; cin>>name[i]>>num[i]>>score[i];} name,number and score of student } 6.2 题另一解 #include <iostream> #include <string> using namespace std; int main() {void change(string &,string &); string str1=" str2=" str3=" cout<<"input three line:"<<endl; gets(p1); gets(p2); gets(p3); if(str1>str2)change(str1,str2); if(str1>str3)change(str1,str3); if(str2>str3)change(str2,str3); cout<<endl<<"Now,the order is:"<<endl; cout<<str1<<endl<<str2<<endl<<str3<<endl; ", ", "; int main() {int i; void input_data(); input_data(); cout<<endl<<"The list of failed:"<<endl; for(i=0;i<n;i++) if(score[i]<60) cout<<name[i]<<" "<<num[i]<<" return 0; "<<score[i]<<endl; } void swap(char *p1,char *p2) */ {char p[20]; strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); /* 交换两个字符串 if(strcmp(str1,str3)>0) if(strcmp(str2,str3)>0) swap(str1,str3); swap(str2,str3);

cout<<endl<<"Now,the order is:"<<endl; cout<<str1<<endl<<str2<<endl<<str3<<endl; return 0;

第六章

char *p1=&str1[0],*p2=&str2[0],*p3=&str3[0];

20

袁俊南
return 0; } void change(string &st1,string &st2) 字符串 */ {string st; st=st1;st1=st2;st2=st; } 6.3 题 #include <iostream> using namespace std; int main() { void input(int *number); void max_min_value(int *number); void output(int *number); int number[10]; input(number); 数 max_min_value(number); output(number); return 0; } void input(int *number) {int i; cout<<"input 10 numbers:"; for (i=0;i<10;i++) cin>>number[i]; } void max_min_value(int *number) 数 // 交换函 // 输入 10 个数的函数 // 调用交换函数 // 调用输出函数 // 调用输入 10 个数的函 } void output(int *number) {int *p; cout<<"now,they are: cout<<*p<<" "; cout<<endl; return; } 6.4 题 #include <iostream> using namespace std; int main() {void move(int *array,int n,int m); int number[20],n,m,i; cout<<"how many numbers?"; 有多少个数 cin>>n; cout<<"input "<<n<<" numbers:"<<endl; // 要求输入 n // 询问共 6.5 题 #include <iostream> using namespace std; int main() {int i,k,m,n,num[50],*p; } "; for (p=number;p<number+10;p++) // 输出函数 } void move(int *array,int n,int m) 数 {int *p,array_end; array_end=*(array+n-1); for (p=array+n-1;p>array;p--) *p=*(p-1); *array=array_end; m--; if (m>0) move(array,n,m); //递归调用,当循环次数 m 减至为 0 时,停止调用 //使循环后移一次的函 min temp=number[0];number[0]=*min;*min=temp; 最小数与第一数交换 temp=number[9];number[9]=*max;*max=temp; 最小数与第一数交换 // 将 // 将 max else if (*p<*min) min=p; // 将小数地址赋给 /* 交换两个 { int *max,*min,*p,temp; max=min=number; for (p=number+1;p<number+10;p++) if (*p>*max) max=p; // 将大数地址赋给 个数 for (i=0;i<n;i++) cin>>number[i]; cout<<"how many places do you want move?"; 移多少个位置 cin>>m; move(number,n,m); cout<<"Now,they are:"<<endl; for (i=0;i<n;i++) cout<<number[i]<<" "; cout<<endl; return 0; //调用 move 函数 // 询问后

21

袁俊南
cout<<"input number of person: n="; cin>>n; p=num; for (i=0;i<n;i++) *(p+i)=i+1; i=0; k=0; 量 m=0; while (m<n-1) 退出人数大于 1 时)执行循 环体 {if (*(p+i)!=0) if (k==3) {*(p+i)=0; k=0; m++; } i++; if (i==n) i=0; } while(*p==0) p++; cout<<"The last one is NO."<<*p<<endl; return 0; } 6.6 题 #include <iostream> using namespace std; int main() {int length(char *p); // 报数到尾后,i 恢复为 0 6.7 题 #include <iostream> using namespace std; int main() {void copystr(char *,char *,int); int m; char str1[20],str2[20]; cout<<"input string:"; gets(str1); cout<<"which character do you want begin to copy?"; cin>>m; if (strlen(str1)<m) } 6.8 题 #include <iostream> using namespace std; int main() {int upper=0,lower=0,digit=0,space=0,other=0,i=0; char *p,s[20]; cout<<"input string:"; } *p2='\0'; } k++; // 将退出的人的编号置为 0 } return(n); } while (*p1!='\0') {*p2=*p1; p1++; p2++; // m 为退出人数 // 当退出人数比 n-1 少时(即未 // 以 1 至 n 为序给每个人编号 // i 为每次循环时计数变量 // k 为按 1,2,3 报数时的计数变 } int length(char *p) {int n; n=0; while (*p!='\0') {n++; p++; //求字符串长度的函数 void copystr(char *p1,char *p2,int m) 数*/ {int n; n=0; while (n<m-1) {n++; p1++; //字符串部分复制函 int len; char str[20]; cout<<"input string:"; cin>>str; len=length(str); cout<<"The length of string is "<<len<<endl; return 0; } } return 0; cout<<"input error!"<<endl; else {copystr(str1,str2,m); cout<<"result:"<<str2<<endl;

22

袁俊南
while ((s[i]=getchar())!='\n') i++; p=&s[0]; while (*p!='\n') {if (('A'<=*p) && (*p<='Z')) ++upper; else if (('a'<=*p) && (*p<='z')) ++lower; else if (*p==' ') ++space; else if ((*p<='9') && (*p>='0')) ++digit; else ++other; p++; } cout<<"upper case:"<<lower<<endl; cout<<"space:"<<space<<endl<<"digit:"<<digit<<endl<<"other: "<<other<<endl; return 0; } 6.9 题 #include <iostream> using namespace std; int main() {void move(int *); int a[3][3],*p,i; cout<<"input matrix:"<<endl; for (i=0;i<3;i++) cin>>a[i][0]>>a[i][1]>>a[i][2]; 素 } 6.10 题 #include <iostream> using namespace std; int main() {void change(int *p); int a[5][5],*p,i,j; cout<<"input matrix:"<<endl; for (i=0;i<5;i++) for (j=0;j<5;j++) cin>>a[i][j]; p=&a[0][0]; //使 p 指向 0 行 0 列元 //输入矩阵 换 *(p+12)=*pmax; *pmax=temp; temp=*p; 互换 *p=*pmin; *pmin=temp; //将最小值与左上角元素 } temp=*(p+12); //将最大值与中心元素互 case:"<<upper<<endl<<"lower } 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); *(pointer+3*i+j)=*(pointer+3*j+i); *(pointer+3*j+i)=t; void change(int *p) {int i,j,temp; int *pmax,*pmin; 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; //找最大值和最小值的地址,并赋给 //交换函数 } } p=&a[0][0]; move(p); cout<<"Now,matrix:"<<endl; for (i=0;i<3;i++) cout<<a[i][0]<<" "<<a[i][1]<<" "<<a[i][2]<<endl; cout<<endl; return 0; } return 0; change(p); cout<<"Now,matrix:"<<endl; for (i=0;i<5;i++) {for (j=0;j<5;j++) cout<<a[i][j]<<" "; cout<<endl; //输出已交换的矩阵 //调用函数,实现交换

23

袁俊南
pmin=p+1; //将 a[0][1]的地址赋给 pmin, 从该位置开始找最小的元 素 for (i=0;i<5;i++) pmin for (j=0;j<5;j++) if (((p+5*i+j)!=p) && (*pmin > *(p+5*i+j))) pmin=p+5*i+j; temp=*pmin; 素互换 *pmin=*(p+4); *(p+4)=temp; pmin=p+1; for (i=0;i<5;i++) for (j=0;j<5;j++) if(((p+5*i+j)!=(p+4)) &&(*pmin>*(p+5*i+j))) pmin=p +5*i+j; temp=*pmin; 互换 *pmin=*(p+20); *(p+20)=temp; pmin=p+1; for (i=0;i<5;i++) */ for (j=0;j<5;j++) if (((p+5*i+j)!=p) &&((p+5*i+j)!=(p+4)) && } return 0; ((p+5*i+j)!=(p+20)) && // 找第四最小值的地址赋给 pmin // 将第三最小值与左下角元素 素 change(p); 换 cout<<"Now,matrix:"<<endl; for (i=0;i<5;i++) 阵 {for (j=0;j<5;j++) cout<<a[i][j]<<" "; cout<<endl; 素 for (i=0;i<5;i++) pmin for (j=0;j<5;j++) {if(i==0 && j==0) continue; // 找第二最小值的地址赋给 //输出已交换的矩 pmin=p+1; //将 a[0][1]的地址赋给 pmin, 从该位置开始找最小的元 //调用函数, 实现交 && ((p+5*i+j)!=p) //找第三最小值的地址赋给 pmin //将第二最小值与右上角元 6.10 题另一解 #include <iostream> using namespace std; int main() {void change(int *p); int a[5][5],*p,i,j; cout<<"input matrix:"<<endl; for (i=0;i<5;i++) for (j=0;j<5;j++) cin>>a[i][j]; p=&a[0][0]; //使 p 指向 0 行 0 列元 temp=*p; 互换 *p=*pmin; *pmin=temp; //将最小值与左上角元素 //输入矩阵 换 *(p+12)=*pmax; *pmax=temp; } temp=*(p+12); //将最大值与中心元素互 // 找第二最小值的地址赋给 换 *pmin=*(p+24); *(p+24)=temp; } (*pmin>*(p+5*i+j))) pmin=p+5*i+j; temp=*pmin; //将第四最小值与右下角元素互 void change(int *p) {int i,j,temp; int *pmax,*pmin; 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; //找最大值和最小值的地址,并赋给 //交换函数 }

24

袁俊南
if (*pmin > *(p+5*i+j)) pmin=p+5*i+j; } temp=*pmin; 换 *pmin=*(p+4); *(p+4)=temp; pmin=p+1; for (i=0;i<5;i++) for (j=0;j<5;j++) {if((i==0 } temp=*pmin; 换 *pmin=*(p+20); *(p+20)=temp; void sort(char s[][6]) pmin=p+1; for (i=0;i<5;i++) for (j=0;j<5;j++) {if ((i==0 j==0)) continue; if (*pmin>*(p+5*i+j)) pmin=p+5*i+j; } temp=*pmin; 换 *pmin=*(p+24); *(p+24)=temp; } } 6.11 题另一解 } } 6.11 题另一解 //将第四最小值与右下角元素互 && j==0) ||(i==0 && j==4)||(i==4 && // 找第四最小值的地址赋给 pmin {int i,j; char *p,temp[10]; p=temp; 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); } void sort(char (*s)[6]) {int i,j; char temp[6],*t=temp; 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); } } // 将第三最小值与左下角元素互 && j==0) ||(i==0 && j==4)) continue; if(*pmin>*(p+5*i+j)) pmin=p+5*i+j; //找第三最小值的地址赋给 pmin //将第二最小值与右上角元素互 6.11 题 #include <iostream> using namespace std; int main() {void sort(char s[][6]); int i; char str[10][6]; cout<<"input 10 strings:"<<endl; for (i=0;i<10;i++) cin>>str[i]; sort(str); cout<<"Now,the sequence is:"<<endl; for (i=0;i<10;i++) cout<<str[i]<<endl; return 0; #include <iostream> using namespace std; int main() {void sort(char (*p)[6]); int i; char str[10][6]; char (*p)[6]; cout<<"input 10 strings:"<<endl; for (i=0;i<10;i++) cin>>str[i]; p=str; sort(p); cout<<"Now,the sequence is:"<<endl; for (i=0;i<10;i++) cout<<str[i]<<endl; return 0;

25

袁俊南
#include <iostream> #include <string> using namespace std; int main() {void sort(string *); int i; string str[10],*p=str; cout<<"input 10 strings:"<<endl; for (i=0;i<10;i++) cin>>str[i]; sort(p); cout<<"Now,the sequence is:"<<endl; for (i=0;i<10;i++) cout<<str[i]<<endl; return 0; } } void sort(string *s) {int i,j; string temp; 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]=temp; } } 6.12 题 #include <iostream> } 6.13 题 #include <iostream> } } void sort(char *s[]) {int i,j; char *temp; 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)=temp; using namespace std; int main() {void sort(char *[]); int i; char *p[10],str[10][20]; for (i=0;i<10;i++) p[i]=str[i]; 数组 p 的第 i 个元素 cout<<"input 10 strings:"<<endl; for (i=0;i<10;i++) cin>>p[i]; sort(p); cout<<"Now,the sequence is:"<<endl; for (i=0;i<10;i++) cout<<p[i]<<endl; return 0; //将第 i 个字符串的首地址赋予指针 #include <cmath> using namespace std; int main() {float integral(float (*p)(float),float a,float b,int n); float a1,b1,a2,b2,a3,b3,c,(*p)(float); float fsin(float); float fcos(float); float fexp(float); int n=20; cout<<"input a1,b1:"; 下限和上限 cin>>a1>>b1; cout<<"input a2,b2:"; 下限和上限 cin>>a2>>b2; cout<<"input a3,b3:"; <iostream> cin>>a3>>b3; p=fsin; c=integral(p,a1,b1,n); p=fcos; c=integral(p,a2,b2,n); p=fexp; c=integral(p,a3,b3,n); return 0; // 求出 的定积分 cout<<"The integral of exp(x) is :"<<c<<endl; // 求出 cos(x)的 定积分 cout<<"The integral of cos(x) is :"<<c<<endl;; // 求出 sin(x)的定积分 cout<<"The integral of sin(x) is :"<<c<<endl; // 输 入 求 #include // 输入求 cos(x) 定积分的 //输入求 sin(x) 定积分的 // 对 fsin 函数作声明 // 对 fcos 函数作声明 // 对 fexp 函数作声明

26

袁俊南
float integral(float (*p)(float),float a,float b,int n) //用矩形法求定积分的通用函数 {int i; 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); } float fsin(float x) {return sin(x);} float fcos(float x) {return cos(x);} float fexp(float x) 数 {return exp(x);} 6.13 题 #include <iostream> #include <cmath> using namespace std; int main() {float integral(float (*p)(float),float a,float b,int n); float a1,b1,a2,b2,a3,b3,c,(*p)(float); float fsin(float); float fcos(float); float fexp(float); // 对 fsin 函数作声明 // 对 fcos 函数作声明 // 对 fexp 函数作声明 {int i; float x,h,s; h=(b-a)/n; x=a; s=0; for (i=1;i<=n;i++) {x=x+h; float integral(float (*p)(float),float a,float b,int n) //用矩形法求定积分的通用函数 } // 计算 exp(x)的函 // 计算 cos(x) 的函数 // 计算 sin(x) 的函数 int n=20; cout<<"input a1,b1:"; 上限 cin>>a1>>b1; cout<<"input a2,b2:"; 上限 cin>>a2>>b2; cout<<"input a3,b3:"; cin>>a3>>b3; p=fsin; c=integral(p,a1,b1,n); p=fcos; c=integral(p,a2,b2,n); p=fexp; c=integral(p,a3,b3,n); return 0; // 求出 的定积分 6.14 题 #include <iostream> using namespace std; int main() { void sort (char *p,int m); int i,n; char *p,num[20]; cout<<"input n:"; cin>>n; cout<<"please input these numbers:"<<endl; for (i=0;i<n;i++) cin>>num[i]; p=&num[0]; cout<<"The integral of exp(x) is :"<<c<<endl; // 求出 cos(x)的 定积分 float fexp(float x) 数 {return exp(x);} // 计算 exp(x)的函 cout<<"The integral of cos(x) is :"<<c<<endl;; // 求出 sin(x)的定积分 cout<<"The integral of sin(x) is :"<<c<<endl; float fcos(float x) 数 {return cos(x);} // 计算 cos(x) 的函 // 输入求#include <iostream> // 输入求 cos(x) 定积分的下限和 float fsin(float x) 数 {return sin(x);} // 计算 sin(x) 的函 //输入求 sin(x) 定积分的下限和 } } return(s); s=s+(*p)(x)*h;

27

袁俊南
sort(p,n); cout<<"Now,the sequence is:"<<endl; for (i=0;i<n;i++) cout<<num[i]<<" "; cout<<endl; return 0; } void sort (char *p,int m) 函数 {int i; char temp, *p1,*p2; for (i=0;i<m/2;i++) {p1=p+i; p2=p+(m-1-i); temp=*p1; *p1=*p2; *p2=temp; } } 6.15 题 #include <iostream> using namespace std; int main() {void avsco(float *,float *); void avcour1(char (*)[10],float *); void fali2(char course[5][10],int num[],float *pscore,float aver[4]); void good(char course[5][10],int num[4],float *pscore,float } paver=&aver[0]; cout<<endl<<endl; avsco(pscore,paver); avcour1(pcourse,pscore); cout<<endl<<endl; fali2(pcourse,pnum,pscore,paver); // 找出两门课不及格的 学生 cout<<endl<<endl; good(pcourse,pnum,pscore,paver); 生 // 找出成绩好的学 } void fail2(char course[5][10],int num[],float *pscore,float aver[4]) // 找两门以上课程不及格的学生的函数 // 求出每个学生的平均成绩 // 求出第一门课的平均成绩 分 average1=sum/4; cout<<"course score:"<<average1<<endl; 1: //计算平均成绩 "<<*pcourse<<",average // 将 n 个数逆序排列 aver[4]); int i,j,*pnum,num[4]; float score[4][5],aver[4],*pscore,*paver; char course[5][10],(*pcourse)[10]; cout<<"input course:"<<endl; pcourse=course; for (i=0;i<5;i++) cin>>course[i]; cout<<"input NO. and scores:"<<endl; cout<<"NO."; for (i=0;i<5;i++) cout<<","<<course[i]; cout<<endl; pscore=&score[0][0]; pnum=&num[0]; for (i=0;i<4;i++) {cin>>*(pnum+i); for (j=0;j<5;j++) cin>>*(pscore+5*i+j); } void avcour1(char (*pcourse)[10],float *pscore) 一课程的平均成绩的函数 {int i; float sum,average1; sum=0.0; for (i=0;i<4;i++) sum=sum+(*(pscore+5*i)); //累计每个学生的得 // 求第 } 成绩 average=sum/5; *(paver+i)=average; //计算平均成绩 } void avsco(float *pscore,float *paver) 成绩的函数 {int i,j; float sum,average; for (i=0;i<4;i++) {sum=0.0; for (j=0;j<5;j++) sum=sum+(*(pscore+5*i+j)); //累计每个学生的各科 // 求每个学生的平均 return 0;

28

袁俊南
{int i,j,k,labe1; cout<<" ======= ==========Student who failed in two courses "<<endl; "; "; 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++) cout<<*(pscore+5*i+k)<<" cout<<" } } } "; "; 6.16 题 #include <iostream> using namespace std; int main() {char str[50],*pstr; int i,j,k,m,e10,digit,ndigit,a[10],*pa; cout<<"input a string:"<<endl; gets(str); cout<<endl; pstr=&str[0]; ======Students "; "; whose score is pa=&a[0]; ndigit=0; i=0; j=0; //字符指针 pstr 指向数组 str 首元素 //指针 pa 指向 a 数组首元素 //ndigit 代表有多少个整数 //i 代表字符串中的第几个字符/ //j 代表连续数字的位数 } if (j>0) 据 {digit=*(pstr+i-1)-48; k=1; while (k<j) 值累加于 digit // 将含有两位以上数的其它位的数 //将个数位赋予 digit //以数字结尾字符串的最后一个数 } i++; } 素 j=0; 因子 digit=digit+(*(pstr+i-1-k)-48)*e10; 值累加于 digit k++; } *pa=digit; ndigit++; pa++; //指针 pa 指向 a 数组下一元 //将数值放在数组 a 中 //位数 k 自增 //将该位数的数 "<<aver[i]<<endl; "; "; j++; else {if (j>0) {digit=*(pstr+i-1)-48; k=1; while (k<j) 累计于 digit {e10=1; for (m=1;m<=k;m++) e10=e10*10; //e10 代表该位数所应乘的 //将含有两位以上数的其它位的数值 //将个数位赋予 digit

cout<<"NO. for (i=0;i<5;i++)

cout<<course[i]<<" cout<<" {labe1=0; for (j=0;j<5;j++) for (i=0;i<4;i++)

average"<<endl;

if (*(pscore+5*i+j)<60.0) labe1++; if (labe1>=2) {cout<<num[i]<<" for (k=0;k<5;k++) cout<<*(pscore+5*i+k)<<" cout<<" } } } void good(char course[5][10],int num[4],float *pscore,float aver[4]) // 找成绩优秀学生(全部课程成绩在 85 分以上或平均成 绩在 90 分以上)的函数 {int i,j,k,n; cout<<" good======"<<endl; cout<<"NO. for (i=0;i<5;i++) cout<<course[i]<<" cout<<" average"<<endl; "<<aver[i]<<endl;

while(*(pstr+i)!='\0') {if((*(pstr+i)>='0') && (*(pstr+i)<='9'))

29

袁俊南
{e10=1; for (m=1;m<=k;m++) e10=e10*10; 因子 digit=digit+(*(pstr+i-1-k)-48)*e10; 累加于 digit k++; /*位数 K 自增*/ } *pa=digit; ndigit++; j=0; } printf("There are:\n",ndigit); j=0; pa=&a[0]; for (j=0;j<ndigit;j++) cout<<*(pa+j)<<endl; cout<<endl; return 0; } 6.17 题 #include <iostream> using namespace std; int main() {int strcmp(char *p1,char *p2); int m; char str1[20],str2[20],*p1,*p2; cout<<"input two strings:"<<endl; cin>>str1; 6.18 题 #include <iostream> using namespace std; int main() {char *month_name[13]={"illegal month","January","February","March","April", "May","June","July","August","September","October", "November","December"}; int n; cout<<"input month:"<<endl; cin>>n; } //打印数据 } are %d numbers in this line. They //将数值放到数组 a 中 int strcmp(char *p1,char *p2) 数 {int i; i=0; while(*(p1+i)==*(p2+i)) if (*(p1+i++)=='\0') return(0); 果0 return(*(p1+i)-*(p2+i)); 不相同字符的 ASCII 码的差值 //不相同时返回结果为第一对 //全部字符相同时返回结 int main() {void sort(char **p); const int m=20; int i; char **p,*pstr[5],str[5][m]; for (i=0;i<5;i++) pstr[i]=str[i]; /*将第 i 个字符串的首地址赋予指针数组 pstr 的第 i 个元素*/ cout<<"input 5 strings:"<<endl; for (i=0;i<5;i++) cin>>pstr[i]; p=pstr; sort(p); cout<<"strings sorted:"<<endl; for (i=0;i<5;i++) cout<<pstr[i]<<endl; return 0; //定义字符串的最大长度 //自已定义字符串比较函 } 6.19 题 #include <iostream> using namespace std; //将该位数的数值 //e10 代表位数所应乘的 cin>>str2; p1=&str1[0]; p2=&str2[0]; m=strcmp(p1,p2); cout<<"result:"<<m<<endl; return 0; } if ((n<=12) && (n>=1)) cout<<"It is "<<*(month_name+n)<<endl; else cout<<"It is wrong"<<endl; return 0;

30

袁俊南
void sort(char **p) {int i,j; char *temp; for (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)=temp; } } } } 6.20 题 #include <iostream> using namespace std; int main() {void sort(int **p,int n); int i,n,data[10],**p,*pstr[10]; cout<<"input n:"; cin>>n; for (i=0;i<n;i++) pstr[i]=&data[i]; pstr 的第 i 个元素*/ cout<<"input "<<n<<" integer numbers:"<<endl; for (i=0;i<n;i++) cin>>*pstr[i]; p=pstr; sort(p,n); /*将第 i 个整数的地址赋予指针数组 7.1 题 #include <iostream> using namespace std; struct { int year; int month; int day; }date; int main() {int days; cout<<"input year,month,day:"; 7.1 题另一解 #include <iostream> using namespace std; struct {int year; int month; int day; } } void sort(int **p,int n) {int i,j,*temp; for (i=0;i<n-1;i++) {for (j=i+1;j<n;j++) {if (**(p+i)>**(p+j)) {temp=*(p+i); *(p+i)=*(p+j); *(p+j)=temp; } if ((date.year %4== 0 && date.year % 100 != 0 ||date.year % 400 == 0) && date.month >=3) days+=1; cout<<date.month<<"/"<<date.day<<" is the "<<days <<"th day in "<<date.year<<"."<<endl; return 0; //比较后交换整数地址 //比较后交换字符串 } //冒泡法对 5 个字符串排序函数 cout<<"Now,the sequence is:"<<endl; for (i=0;i<n;i++) cout<<*pstr[i]<<" cout<<endl; return 0; "; cin>>date.year>>date.month>>date.day; switch(date.month) { case 1: days=date.day; case 2: days=date.day+31; case 3: days=date.day+59; case 4: days=date.day+90; case 5: days=date.day+120; case 6: days=date.day+151; case 7: days=date.day+181; case 8: days=date.day+212; case 9: days=date.day+243; break; break; break; break; break; break; break; break; break;

case 10: days=date.day+273; break; case 11: days=date.day+304; break; case 12: days=date.day+334; break;

第七章

31

袁俊南
}date; int main() {int i,days; int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; cout<<"input year,month,day:"; cin>>date.year>>date.month>>date.day; days=0; for (i=1;i<date.month;i++) days+=day_tab[i]; days+=date.day; if ((date.year%4==0 && date.year%100!=0 || int days(int year,int month,int day) */ {int day_sum,i; int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; day_sum=0; for (i=1;i<month;i++) day_sum+=day_tab[i]; day_sum+=day; if ((year%4==0 && year%100!=0 || year%4==0) && 7.2 题 #include <iostream> using namespace std; struct y_m_d {int year; int month; int day; }; int main() {y_m_d date; 7.3 题 #include <iostream> #include <iomanip> using namespace std; const int n=5; } void print(student stu[6]) {int i,j; cout<<" NO. score3"<<endl; for (i=0;i<n;i++) {cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" name score1 score2 month>=3) day_sum+=1; return(day_sum); } print(stu); return 0; } } cout<<endl; /* 定义 days 函数 date.year%400==0) && date.month>=3) days+=1; cout<<date.month<<"/"<<date.day<<" is the "<<days <<"th day in "<<date.year<<"."<<endl; return 0; } } int days(int,int,int); 数的声明 */ int day_sum; cout<<"input year,month,day:"; cin>>date.year>>date.month>>date.day; day_sum=days(date.year,date.month,date.day); cout<<date.month<<"/"<<date.day<<" is the "<<day_sum <<"th day in "<<date.year<<endl; return 0; /* 对 days 函 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 student "<<i+1<<":"<<endl; cout<<"NO.: "; cin>>stu[i].num; cout<<"name: "; cin>>stu[i].name; for (j=0;j<3;j++) {cout<<"score "<<j+1<<":"; cin>>stu[i].score[j];

32

袁俊南
"; for (j=0;j<3;j++) cout<<setw(3)<<stu[i].score[j]<<" cout<<endl; } } } 7.4 题 #include <iostream> #include <iomanip> using namespace std; 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; } void input(student stu[]) {int i,j; for (i=0;i<n;i++) {cout<<"input scores of student "<<i+1<<":"<<endl; cout<<"NO.: "; 7.5 题 #include <iostream> #include <iomanip> using namespace std; const int n=10; struct student { char num[6]; char name[8]; } } "; for (j=0;j<3;j++) cout<<setw(3)<<stu[i].score[j]<<" cout<<endl; "; } average=0; max=0; maxi=0; for (i=0;i<n;i++) {sum=0; for (j=0;j<3;j++) sum+=stu[i].score[j]; stu[i].avr=sum/3.0; average+=stu[i].avr; if (sum>max) {max=sum; } cout<<endl; } void print(student stu[]) {int i,j; cout<<" NO. score3"<<endl; for (i=0;i<n;i++) {cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" name score1 score2 } "; cin>>stu[i].num; cout<<"name: "; cin>>stu[i].name; for (j=0;j<3;j++) {cout<<"score "<<j+1<<":"; cin>>stu[i].score[j]; int main() { int i,j,max,maxi,sum; float average; for (i=0;i<n;i++) {cout<<"input scores of student "<<i+1<<endl;; cout<<"NO.:"; cin>>stu[i].num; cout<<"name:"; cin>>stu[i].name; for (j=0;j<3;j++) {cout<<"score "<<j+1<<":"; cin>>stu[i].score[j]; int score[4]; float avr; } stu[n];

33

袁俊南
maxi=i; } } average/=n; cout<<" score3 average"<<endl; for (i=0;i<n;i++) {cout<<setw(8)<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" for (j=0;j<3;j++) cout<<setw(3)<<stu[i].score[j]<<" cout<<stu[i].avr<<endl; } cout<<"average="<<average<<endl; cout<<"The highest score is :"<<stu[maxi].name<<", score total:"<<max<<endl; return 0; } 7.6 题 #include <iostream> using namespace std; #define NULL 0 struct student {long num; float score; struct student *next; }; int main() {student a,b,c,*head,*p; } 7.6 题另一解 #include <iostream> using namespace std; #define NULL 0 struct student {long num; float score; student *next; 7.7 题 #include <iostream> using namespace std; #define NULL 0 struct student 点 }while(p!=NULL); NULL return 0; //输出完 c 结点后 p 的值为 } p2->next=NULL; return(head); } "; 址 p=head; do {cout<<p->num<<" 结点的数据 p=p->next; //使 p 指向下一结 "<<p->score<<endl; //输出 p 指向的 //使 p 指针指向 a 结点 "; NO. name score1 score2 a.num=10001; a.score=89.5; b.num=10003; b.score=90; c.num=10007; c.score=85; 赋值 head=&a; head a.next=&b; 成员 b.next=&c; 成员 c.next=NULL; //c 结点的 next 成员不存放其他结点地 //将结点 c 的起始地址赋给 b 结点的 next //将结点 b 的起始地址赋给 a 结点的 next //将结点 a 的起始地址赋给头指针 //为结点的 num 和 score 成员 各函数均可使用它 student *creat(void) 链表头的指针 {student *head; student *p1,*p2; int n=0; p1=p2=new student; 向它 cin>>p1->num>>p1->score; head=NULL; while(p1->num!=0) {n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=new student; cin>>p1->num>>p1->score; //开辟一个新单元,并使 p1,p2 指 //定义函数。此函数带回一个指向 }; //定义 n 为全局变量,本文件模块中

34

袁俊南
{long num; float score; student *next; }; int n; void print(student *head) {student *p; cout<<"Now,These "<<n<<" records are:"<<endl; p=head; if(head!=NULL) do {cout<<p->num<<" p=p->next; }while(p!=NULL); } 7.7 题另一解 #include <iostream> using namespace std; #define NULL 0 struct student {long num; float score; student *next; }; int n; student *del(student *head,long num) {student *p1,*p2; if (head==NULL) {cout<<"list null!"<<endl; return(head);} p1=head; //使 p1 指向第一个结 //是空表 } 7.8 题 #include <iostream> using namespace std; #define NULL 0 struct student {long num; float score; student *next; }; int n; student *del(student *head,long num) {student *p1,*p2; }; int n; } else cout<<"cannot find "<<num; return(head); //找不到该结点 "<<p->score<<endl; 点 while(num!=p1->num && p1->next!=NULL) //p1 指向的不是 所要找的结点且后面还有 结点 {p2=p1; p1=p1->next;} 点 if(num==p1->num) {if(p1==head) head=p1->next; 把第二个结点地址赋予 head else p2->next=p1->next; 一结点地址 cout<<"delete:"<<num<<endl; n=n+1; } else cout<<"cannot find "<<num; return(head); } 7.9 题 #include <iostream> using namespace std; #define NULL 0 struct student {long num; float score; student*next; //找不到该结点 //否则将下一结点地址赋给前 //找到了 //若 p1 指向的是首结点, //p1 后移一个结 结点 while(num!=p1->num && p1->next!=NULL) //p1 指向的 不是所要找的结点且后面还有结点 {p2=p1; p1=p1->next;} if(num==p1->num) {if(p1==head) head=p1->next; 第二个结点地址赋予 head else p2->next=p1->next; 一结点地址 cout<<"delete:"<<num<<endl; n=n-1; //否则将下一结点地址赋给前 //p1 后移一个结点 //找到了 //若 p1 指向的是首结点, 把 if (head==NULL) {cout<<"list null!"<<endl; return(head);} p1=head; //使 p1 指向第一个 //是空表

35

袁俊南
student *insert(student *head,student *stud) {student *p0,*p1,*p2; p1=head; 点 p0=stud; 点 if(head==NULL) {head=p0;p0->next=NULL;} 结点 else {while((p0->num>p1->num) && (p1->next!=NULL)) {p2=p1; 结点 p1=p1->next;} if(p0->num<=p1->num) {if(head==p1) head=p0; 前 else p2->next=p0; 后 p0->next=p1;} else {p1->next=p0; p0->next=NULL;}} n=n+1; return (head); } 7.10 题 #include <iostream> using namespace std; #define NULL 0 //插到最后的结点之后 //结点数加1 址 print(head); 结点 cin>>stu.num>>stu.score; head=insert(head,&stu); print(head); 结点 //返回地址 //输出全部结点 student *del(student *head,long num) {student *p1,*p2; if (head==NULL) {cout<<"list null!"<<endl; return(head);} //是空表 //删除结的函数 //输出全部结点 号 head=del(head,del_num); //删除后链表的头地 } p2->next=NULL; return(head); } cout<<endl<<"input the inserted record:"; //输入要插入的 //插到 p2 指向的结点之 //插到原来第一个结点之 //p1 后移一个结点 //使 p2 指向刚才 p1 指向的 //原来的链表是空表 //使 p0 指向的结点作为头 int main() { student *creat(void); student *del(student *,long); student *insert(student *,student *); void print(student *); student *head,stu; long del_num; cout<<"input records:"<<endl; head=creat(); print(head); cout<<endl<<"input the deleted number:"; cin>>del_num; //输入要删除的学 //返回头指针 //输出全部结点 //指向要插入的结 //使 p1 指向第一个结 struct student {long num; float score; student *next; }; int n; student *creat(void) {student *head; student *p1,*p2; n=0; p1=p2=new student; 向它 cin>>p1->num>>p1->score; head=NULL; while(p1->num!=0) {n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=new student; cin>>p1->num>>p1->score; //开辟一个新单元,并使 p1,p2 指 //建立链表的函数 } cin>>stu.num>>stu.score; head=insert(head,&stu); print(head); return 0; //返回地址

cout<<endl<<"input the inserted record:"; //输入要插入的

36

袁俊南
p1=head; 点 while(num!=p1->num && p1->next!=NULL) //p1 指向的不是 所要找的结点且后面还有结点 {p2=p1; p1=p1->next;} if(num==p1->num) {if(p1==head) head=p1->next; 把第二个结点地址赋予 head else p2->next=p1->next; 一结点地址 cout<<"delete:"<<num<<endl; n=n-1; } else cout<<"cannot find "<<num; return(head); } student *insert(student *head,student *stud) //插入结点的函 数 {student *p0,*p1,*p2; p1=head; 结点 p0=stud; 点 if(head==NULL) {head=p0;p0->next=NULL;} 作为头结点 else {while((p0->num>p1->num) && (p1->next!=NULL)) //原来的链表是空表 //使p0指向的结点 7.10 题另一解 #include <iostream> using namespace std; #define NULL 0 } cout<<endl<<"input the inserted record:"; //输入要插入的 结点 stu=new student; //开辟一个新 //指向要插入的结 } //使p1指向第一个 //找不到该结点 void print(student *head) {student *p; cout<<"Now,These "<<n<<" records are:"<<endl; p=head; if(head!=NULL) do {cout<<p->num<<" p=p->next; }while(p!=NULL); "<<p->score<<endl; //输出链表的函数 } //否则将下一结点地址赋给前 //p1 后移一个结点 //找到了 //若 p1 指向的是首结点, 前 else p2->next=p0; 后*/ p0->next=p1;} else {p1->next=p0; p0->next=NULL;}} n=n+1; return (head); //插到最后的结点之后 //结点数加1 //插到 p2 指向的结点之 int main() { student *creat(void); student *del(student *,long); student *insert(student *,student *); void print(student *); student *head,*stu; long del_num; cout<<"input records:"<<endl; head=creat(); print(head); cout<<endl<<"input the deleted number:"; cin>>del_num; 学号 while(del_num!=0) {head=del(head,del_num); 地址 print(head); cout<<"input the deleted number:"; cin>>del_num; //输出全部结点 //删除后链表的头 //输入要删除的 //返回头指针 //输出全部结点 //使 p1 指向第一个结 {p2=p1; 向的结点 p1=p1->next;} if(p0->num<=p1->num) {if(head==p1) head=p0; //插到原来第一个结点之 //p1 后移一个结点 //使p2指向刚才 p1指 struct student {long num; float score; student *next; }; int n;

37

袁俊南
结点 cin>>stu->num>>stu->score; while(stu->num!=0) {head=insert(head,stu); print(head); 的结点 stu=new student; cin>>stu->num>>stu->score; } return 0; } student *creat(void) {student *head; student *p1,*p2; n=0; p1=p2=new student; 向它 cin>>p1->num>>p1->score; head=NULL; while(p1->num!=0) {n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=new student; cin>>p1->num>>p1->score; } p2->next=NULL; student *insert(student *head,student *stud) //插入结点的函 数 {student *p0,*p1,*p2; } } else cout<<"cannot find "<<num; return(head); //找不到该结点 void print(student *head) {student *p; cout<<"Now,These "<<n<<" records are:"<<endl; p=head; if(head!=NULL) do //输出链表的函数 //开辟一个新单元,并使 p1,p2 指 //建立链表的函数 //返回地址 //输出全部结点 //输入要插入 student *del(student *head,long num) {student *p1,*p2; if (head==NULL) {cout<<"list null!"<<endl; return(head);} p1=head; 点 while(num!=p1->num && p1->next!=NULL) //p1 指向的不是 所要找的结点且后面还有 结点 {p2=p1; p1=p1->next;} 点 if(num==p1->num) {if(p1==head) head=p1->next; 把第二个结点地址赋予 head else p2->next=p1->next; 一结点地址 cout<<"delete:"<<num<<endl; n=n-1; } //否则将下一结点地址赋给前 //找到了 //若 p1 指向的是首结点, 后 p0->next=p1;} else {p1->next=p0; p0->next=NULL;}} n=n+1; return (head); //插到最后的结点之后 //结点数加1 前 else p2->next=p0; //插到 p2 指向的结点之 //p1 后移一个结 //使 p1 指向第一个结 //是空表 //删除结点的函数 return(head); } p1=head; 点 p0=stud; 点 if(head==NULL) {head=p0;p0->next=NULL;} 结点 else {while((p0->num>p1->num) && (p1->next!=NULL)) {p2=p1; 结点 p1=p1->next;} if(p0->num<=p1->num) {if(head==p1) head=p0; //插到原来第一个结点之 //p1 后移一个结点 //使 p2 指向刚才 p1 指向的 //原来的链表是空表 //使 p0 指向的结点作为头 //指向要插入的结 //使 p1 指向第一个结

cout<<endl<<"input the inserted record:";

38

袁俊南
{cout<<p->num<<" p=p->next; }while(p!=NULL); } } "<<p->score<<endl; {set_time(); show_time(); return 0; 8.2 题 #include <iostream> using namespace std; 8.1 题 #include <iostream> using namespace std; class Time {public: int hour; int minute; int sec; }; Time t; void set_time(void) { cin>>t.hour; cin>>t.minute; cin>>t.sec; } void show_time(void) { cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } } int main() //在 main 函数之前定义 Time t; int main() { t.set_time(); t.show_time(); return 0; 8.4 题 //xt8-4.h(student.h) } }; Time t; int main() { t.set_time(); t.show_time(); return 0; //在 main 函数之前定义 private: int hour; int minute; int sec; void Time::show_time(void) {cout<<hour<<":"<<minute<<":"<<sec<<endl;} } void show_time(void) {cout<<hour<<":"<<minute<<":"<<sec<<endl;} } //成员改为公用的 class Time {public: void set_time(void) {cin>>hour; cin>>minute; cin>>sec; }; void Time::set_time(void) {cin>>hour; cin>>minute; cin>>sec; 8.3 题 #include <iostream> using namespace std; class Time {public: void set_time(void); void show_time(void); private: int hour; int minute; int sec;

第八章

39

袁俊南
class Student { public: void display( ); void set_value(); private: int num; char name[20]; char sex ; }; 8.5 题 8.4 题 //xt8-4-1.cpp(main.cpp) #include <iostream> using namespace std; #include "xt8-4.h" int main() {Student stud; stud.set_value(); stud.display(); return 0; } 8.4 题另一解 //xt8-4-2.cpp(即 student.cpp) #include "xt8-4.h" 定义 #include <iostream> using namespace std; void Student::display( ) { cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; //不要漏写此行 //在此文件中进行函数的 8.5 题 //xt8-5-1.cpp(file1.cpp) #include <iostream> #include "xt8-5.h" int main() {Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); 8.6 题 #include <iostream> using namespace std; class Box {public: void get_value(); }; void Array_max::show_value() {cout<<"max="<<max<<endl; } //xt8-5.h(arraymax.h) class Array_max {public: void set_value(); void max_value(); void show_value(); private: int array[10]; int max; } void Array_max::max_value() {int i; max=array[0]; for (i=1;i<10;i++) if(array[i]>max) max=array[i]; } } void Student::set_value() { cin>>num; cin>>name; cin>>sex; } cout<<"sex:"<<sex<<endl; } 8.5 题 //xt8-5-2.cpp(arraymax.cpp) #include <iostream> using namespace std; #include "xt8-5.h" void Array_max::set_value() { int i; for (i=0;i<10;i++) cin>>array[i]; return 0;

40

袁俊南
float volume(); void display(); public: float lengh; float width; float height; }; void Box::get_value() { cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height; } float Box::volume() { return(lengh*width*height);} void Box::display() { cout<<volume()<<endl;} void Box::get_value() int main() {Box box1,box2,box3; box1.get_value(); cout<<"volmue of bax1 is "; box1.display(); box2.get_value(); cout<<"volmue of bax2 is "; box2.display(); box3.get_value(); void Box::volume() { vol=lengh*width*height;} } #include <iostream> using namespace std; class Date {public: { cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height; 9.2 题 8.6 题另一解 #include <iostream> using namespace std; class Box {public: void get_value(); void volume(); void display(); public: float lengh; float width; float height; float vol; }; } } cout<<"volmue of bax3 is "; box3.display(); return 0; int main() {Box box1,box2,box3; box1.get_value(); box1.volume(); cout<<"volmue of bax1 is "; box1.display(); box2.get_value(); box2.volume(); cout<<"volmue of bax2 is "; box2.display(); box3.get_value(); box3.volume(); cout<<"volmue of bax3 is "; box3.display(); return 0; void Box::display() { cout<<vol<<endl;}

第九章

41

袁俊南
Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y) {} Date::Date(int m,int d):month(m),day(d) {year=2005;} Date::Date(int m):month(m) {day=1; year=2005; } Date::Date() {month=1; day=1; year=2005; } Date::Date(int m,int d,int y):month(m),day(d),year(y) void Date::display() {cout<<month<<"/"<<day<<"/"<<year<<endl;} {} void Student::display() }; }; 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> using namespace std; class Date {public: Date(int=1,int=1,int=2005); void display(); private: int month; int day; int year; 9.4 题 #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void display(); private: int num; float score; } 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; void Date::display() {cout<<month<<"/"<<day<<"/"<<year<<endl;}

42

袁俊南
{cout<<num<<" "<<score<<endl;} int main() {Student stud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; Student *p=stud; for(int i=0;i<=2;p=p+2,i++) p->display(); return 0; } 9.6 题 #include <iostream> using namespace std; 9.5 题 #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} int num; float score; }; void main() {Student stud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; void max(Student* ); Student *p=&stud[0]; max(p); } }; int main() {Student stud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return 0; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display(){cout<<num<<" "<<score<<endl;} private: int num; float score; } 9.7 题另一解 #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) const {num=n;score=s;} }; int main() {const Student stud(101,78.5); stud.display(); //stud.change(101,80.5); stud.display(); return 0; } } 9.7 题 void max(Student *arr) {float max_score=arr[0].score; int k=0; for(int i=1;i<5;i++) if(arr[i].score>max_score) {max_score=arr[i].score;k=i;} cout<<arr[k].num<<" "<<max_score<<endl; #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} // 可 改 为 :void "<<score<<endl;} private: int num; float score; display() const {cout<<num<<"

43

袁俊南
void display() const {cout<<num<<" "<<score<<endl;} private: mutable int num; mutable float score; }; int main() {const Student stud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return 0; } 9.7 题另一解 #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: int num; float score; }; int main() {Student stud(101,78.5); Student *p=&stud; p->display(); p->change(101,80.5); } 9.7 题另一解 B #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} int main() {Student stud(101,78.5); Student * const p=&stud; p->display(); }; }; int main() {Student stud(101,78.5); const Student *p=&stud; p->display(); p->change(101,80.5); p->display(); return 0; } 9.7 题另一解 A #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: mutable int num; mutable float score; } 9.7 题另一解 #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: int num; float score; }; int main() {Student stud(101,78.5); const Student *p=&stud; p->display(); stud.change(101,80.5); p->display(); return 0; p->display(); return 0; void display() const{cout<<num<<" "<<score<<endl;} private: int num; float score;

44

袁俊南
p->change(101,80.5); p->display(); return 0; } 9.8 题 #include <iostream> using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: int num; float score; }; int main() {Student stud(101,78.5); void fun(Student&); fun(stud); return 0; } void fun(Student &stu) {stu.display(); stu.change(101,80.5); stu.display(); } 9.9 题 #include <iostream> using namespace std; float Product::discount=0.05; float Product::sum=0; } float Product::average() {return(sum/n);} }; Time::Time(int h,int m,int s) {hour=h; minute=m; } void Product::display() {cout<<sum<<endl; cout<<average()<<endl; }; void Product::total() {float rate=1.0; if(quantity>10) rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity; class Product {public: Product(int n,int q,float p):num(n),quantity(q),price(p){}; void total(); static float average(); static void display(); private: int num; int quantity; float price; static float discount; static float sum; static int n; } 9.10 题 #include <iostream> using namespace std; class Date; class Time {public: Time(int,int,int); friend void display(const Date &,const Time &); private: int hour; int minute; int sec; Product(101,5,23.5),Product(102,12,24.56),Product(103,100,2 1.5) }; for(int i=0;i<3;i++) Prod[i].total(); Product::display(); return 0; int main() { Product Prod[3]={ int Product::n=0;

45

袁俊南
sec=s; } class Date {public: Date(int,int,int); friend void display(const Date &,const Time &); private: int month; int day; int year; }; Date::Date(int m,int d,int y) {month=m; day=d; year=y; } void display(const Date &d,const Time &t) { cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } int main() { Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; } } int main() }; Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){ } void Time::display(const Date &d) { cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl; }; Date::Date(int m,int d,int y):month(m),day(d),year(y){ } class Time {public: Time(int,int,int); void display(const Date &); private: int hour; int minute; int sec; }; template <class numtype> Compare<numtype>::Compare(numtype a,numtype b) {x=a;y=b;} template <class numtype> numtype Compare<numtype>::max() {return (x>y)?x:y;} template <class numtype> numtype Compare<numtype>::min() {return (x<y)?x:y;} int main() {Compare<int> cmp1(3,7); cout<<cmp1.max()<<" is the Maximum of two integer 9.11 题 #include <iostream> using namespace std; class Time; class Date {public: Date(int,int,int); friend Time; private: int month; int day; int year; } 9.12 题 #include <iostream> using namespace std; template<class numtype> class Compare {public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; { Time t1(10,13,56); Date d1(12,25,2004); t1.display(d1); return 0;

46

袁俊南
numbers."<<endl; cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl; Compare<float> cmp2(45.78,93.6); cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl; cout<<cmp2.min()<<" numbers."<<endl<<endl; Compare<char> cmp3('a','A'); cout<<cmp3.max()<<" characters."<<endl; cout<<cmp3.min()<<" characters."<<endl; return 0; } is the Minimum of two is the Maximum of two is the Minimum of two float }; double Complex::get_real() {return real;} double Complex::get_imag() {return imag;} void Complex::display() {cout<<"("<<real<<","<<imag<<"i)"<<endl;} Complex operator + (Complex &c1,Complex &c2) { return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_ima g()); } int main() {Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c3="; 10.1 题 #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; } 10.2 题 #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2); Complex operator-(Complex &c2); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c 2.imag); c3.display(); return 0; Complex Complex::operator+(Complex &c2) {Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c;} Complex Complex::operator-(Complex &c2) {Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c;} Complex Complex::operator*(Complex &c2) {Complex c; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; return c;} Complex Complex::operator/(Complex &c2) {Complex c; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c 2.imag); }; double imag; Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display(); private: double real; double imag;

第十章

47

袁俊南
return c;} void Complex::display() {cout<<"("<<real<<","<<imag<<"i)"<<endl;} int main() {Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1+c2="; c3.display(); c3=c1-c2; cout<<"c1-c2="; c3.display(); c3=c1*c2; cout<<"c1*c2="; c3.display(); c3=c1/c2; cout<<"c1/c2="; c3.display(); return 0; } 10.3 题 #include <iostream> <iostream.h> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2); Complex operator+(int &i); friend Complex operator+(int&,Complex &); } 10.4 题 #include <iostream> using namespace std; //用 VC++时为取消此行 //用 VC++时改为∶ #include }; Complex Complex::operator+(Complex &c) {return Complex(real+c.real,imag+c.imag);} Complex Complex::operator+(int &i) {return Complex(real+i,imag);} void Complex::display() {cout<<"("<<real<<","<<imag<<"i)"<<endl;} Complex operator+(int &i,Complex &c) {return Complex(i+c.real,c.imag);} int main() {Complex c1(3,4),c2(5,-10),c3; int i=5; c3=c1+c2; cout<<"c1+c2="; c3.display(); c3=i+c1; cout<<"i+c1="; c3.display(); c3=c1+i; cout<<"c1+i="; c3.display(); return 0; } void Matrix::input() 数 {cout<<"input value of matrix:"<<endl; //定义输入数据函 }; Matrix::Matrix() 数 {for(int i=0;i<2;i++) for(int j=0;j<3;j++) mat[i][j]=0; }Matrix operator+(Matrix &a,Matrix &b) 算符“+”函数 {Matrix c; for(int i=0;i<2;i++) for(int j=0;j<3;j++) {c.mat[i][j]=a.mat[i][j]+b.mat[i][j];} return c; //定义重载运 //定义构造函 数 private: int mat[2][3]; void display(); private: double real; double imag; class Matrix 类 {public: Matrix(); 函数 friend Matrix operator+(Matrix &,Matrix &); //重载运算符 “+” void input(); 函数 void display(); //输出数据函 //输入数据 //默认构造 //定义 Matrix

48

袁俊南
for(int i=0;i<2;i++) for(int j=0;j<3;j++) cin>>mat[i][j]; } void Matrix::display() 数 {for (int i=0;i<2;i++) {for(int j=0;j<3;j++) {cout<<mat[i][j]<<" ";} cout<<endl;} } int main() {Matrix a,b,c; a.input(); b.input(); cout<<endl<<"Matrix a:"<<endl; a.display(); cout<<endl<<"Matrix b:"<<endl; b.display(); c=a+b; 相加 cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl; c.display(); return 0; } 10.5 题 #include <iostream.h> //using namespace std; class Matrix {public: } ostream& operator<<(ostream &out,Matrix &m) {for (int i=0;i<2;i++) {for(int j=0;j<3;j++) }; //用重载运算符“+”实现两个矩阵 } istream& operator>>(istream &in,Matrix &m) {cout<<"input value of matrix:"<<endl; for(int i=0;i<2;i++) for(int j=0;j<3;j++) in>>m.mat[i][j]; return in; } Matrix operator+(Matrix &a,Matrix &b) {Matrix c; for(int i=0;i<2;i++) for(int j=0;j<3;j++) {c.mat[i][j]=a.mat[i][j]+b.mat[i][j]; } return c; } 10.6 题 #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r){real=r;imag=0;} Complex(double r,double i){real=r;imag=i;} operator double(){return real;} void display(); private: double real; double imag; }; Matrix::Matrix() {for(int i=0;i<2;i++) for(int j=0;j<3;j++) mat[i][j]=0; //定义输出数据函 Matrix(); friend Matrix operator+(Matrix &,Matrix &); friend ostream& operator<<(ostream&,Matrix&); friend istream& operator>>(istream&,Matrix&); private: int mat[2][3]; } int main() { Matrix a,b,c; cin>>a; cin>>b; cout<<endl<<"Matrix a:"<<endl<<a<<endl; cout<<endl<<"Matrix b:"<<endl<<b<<endl; c=a+b; cout<<endl<<"Matrix b :"<<endl<<c<<endl; return 0; c = Matrix a + Matrix {out<<m.mat[i][j]<<" ";} out<<endl;} return out;

49

袁俊南
void Complex::display() {cout<<"("<<real<<", "<<imag<<")"<<endl;} int main() {Complex c1(3,4),c2; double d1; d1=2.5+c1; cout<<"d1="<<d1<<endl; c2=Complex(d1); cout<<"c2="; c2.display(); return 0; } 10.7 题 #include <iostream> using namespace std; class Student {public: Student(int,char[],char,float); int get_num(){return num;} char * get_name(){return name;} char get_sex(){return sex;} void display() {cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<" \nscore:"<<score<<"\n \n";} private: int num; char name[20]; char sex; float score; } Teacher::Teacher(Student& stud) {num=stud.get_num(); strcpy(name,stud.get_name()); sex=stud.get_sex(); pay=1500;} }; Teacher::Teacher(int n,char nam[],char s,float p) {num=n; strcpy(name,nam); sex=s; pay=p; } class Teacher {public: Teacher(){} Teacher(Student&); Teacher(int n,char nam[],char sex,float pay); void display(); private: int num; char name[20]; char sex; float pay; 11.1 题 #include <iostream> using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;} private : int num; } }; Student::Student(int n,char nam[],char s,float so) {num=n; strcpy(name,nam); sex=s; score=so; void Teacher::display() {cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<" \npay:"<<pay<<"\n\n";} int main() {Teacher teacher1(10001,"Li",'f',1234.5),teacher2; Student student1(20010,"Wang",'m',89.5); cout<<"student1:"<<endl; student1.display(); teacher2=Teacher(student1); cout<<"teacher2:"<<endl; teacher2.display(); return 0;

第十一章

50

袁俊南
char name[10]; char sex; }; class Student1: public Student {public: void get_value_1() {get_value(); cin>>age>>addr;} void display_1() { cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl;} 的私有成员,正确。 private: int age; char addr[30]; }; int main() //引用派生类 }; //引用派生类 class Student1: private Student {public: void get_value_1() {get_value(); cin>>age>>addr;} void display_1() {display(); cout<<"age: "<<age<<endl; 私有成员,正确。 cout<<"address: "<<addr<<endl;} 的私有成员,正确。 private: int age; char addr[30]; }; int main() {Student1 stud1; } class Student1: protected Student 保护派生类 {public: void get_value_1(); void display1( ); //声明一个 //引用派生类 //引用派生类的 void Student::display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; {Student1 stud1; stud1.get_value_1(); stud1.display(); stud1.display_1(); return 0; } 11.2 题 #include <iostream> using namespace std; class Student }; void Student::get_value() {cin>>num>>name>>sex;} 的私有成员,正确。 {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;} private : int num; char name[10]; char sex; 11.3 题 #include <iostream> using namespace std; class Student {public: void get_value(); void display( ); protected : int num; char name[10]; char sex; //基类保护成员 //声明基类 //基类公用成员 } stud1.get_value_1(); stud1.display_1(); return 0;

51

袁俊南
private: int age; char addr[30]; }; void Student1::get_value_1() {get_value(); cin>>age>>addr; } void Student1::display1( ) {cout<<"num: "<<num<<endl; 成员 cout<<"name: "<<name<<endl; 成员 cout<<"sex: "<<sex<<endl; 员 cout<<"age: "<<age<<endl; 有成员 cout<<"address: "<<addr<<endl; 成员 } int main( ) {Student1 stud1; student1 类的对象 stud1.get_value_1(); stud1 的公用成员函数 stud1.display1( ); stud1 的公用成员函数 return 0; } 11.4 题 }; //调用派生类对象 //调用派生类对象 //stud1 是派生类 class Student1: public Student 用派生类 {public: void get_value_1(); void display1( ); private: int age; char addr[30]; } 11.4 另一题#include <iostream> using namespace std; class Student //声明基类 //声明一个公 } //引用派生类的私有 //引用派生类的私 void Student::display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; //引用基类的保护成 //引用基类的保护 void Student::get_value() {cin>>num>>name>>sex;} } int main( ) {Student1 stud1; student1 类的对象 stud1.get_value_1(); stud1 的公用成员函数 get_value_1 stud1.display1( ); stud1 的公用成员函数 display1 return 0; //调用派生类对象 //调用派生类对象 //stud1 是派生类 //引用基类的保护 }; #include <iostream> using namespace std; class Student {public: void get_value(); void display( ); protected : int num; char name[10]; char sex; //基类保护成员 //声明基类 //基类公用成员 } void Student1::display1( ) {cout<<"num: "<<num<<endl; 成员,合法 cout<<"name: "<<name<<endl; 成员,合法 cout<<"sex: "<<sex<<endl; 员,合法 cout<<"age: "<<age<<endl; 有成员,合法 cout<<"address: "<<addr<<endl; 成员,合法 //引用派生类的私有 //引用派生类的私 //引用基类的保护成 //引用基类的保护 //引用基类的保护 void Student1::get_value_1() {get_value(); cin>>age>>addr;

52

袁俊南
{public: void get_value(); void display( ); protected : int num; char name[10]; char sex; }; void Student::get_value() {cin>>num>>name>>sex;} void Student::display( ) {cout<<"num: "<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } class Student1: protected Student 公用派生类 {public: void get_value_1(); void display1( ); private: int age; char addr[30]; }; void Student1::get_value_1() {cin>>age>>addr;} void Student1::display1( ) {cout<<"age:"<<age<<endl; }; class B: public A {public: void f3( ); protected: int m; private: //B 为 A 的公用派生类 }; class B: public A {public: void f3( ); int k; private: //声明一个 } 11.5 题 class A {public: void f1( ); int i; protected: void f2(); int j; private: int k; //A 为基类 } 11.6 题 #include <iostream> using namespace std; class A {public: void f1( ); protected: void f2(); private: int i; 数 return 0; //基类保护成员 //基类公用成员 } int main( ) {Student1 stud1; student1 类的对象 stud1.get_value(); stud1.get_value_1(); stud1.display( ); stud1.display1(); 生类中的公用成员函 //合法。 display1 是派 }; int main() {A a1; B b1; C c1; return 0; //a1 是基类 A 的对象 //b1 是派生类 B 的对象 //c1 是派生类 C 的对象 //stud1 是派生类 cout<<"address:"<<addr<<endl; }; class C: public B {public: void f4(); private: int p; //C 为 B 的公用派生类 int n;

53

袁俊南
int m; }; class C: protected B {public: void f4(); protected: int n; private: int p; }; class D: private C {public: void f5(); protected: int q; private: int r; }; int main() {A a1; B b1; C c1; D d1; return 0; } 11.7 题 #include <iostream> using namespace std; class A { }; int main() { B b1; B b2(1); B b3(1,3); B b4(1,3,5); b1.display1(); b2.display1(); } }; int main() { C c1; return 0; } private: int c; class C { public: C(){cout<<"constructing C "<<endl;} ~C(){cout<<"destructing C "<<endl;} : public B }; class B { public: B(){c=0;} B(int i):A(i){c=0;} B(int i,int j):A(i,j){c=0;} B(int i,int j,int k):A(i,j){c=k;} void display1() {display(); cout<<" c="<<c<<endl; }; }; class B { public: B(){cout<<"constructing B "<<endl;} ~B(){cout<<"destructing B "<<endl;} : public A : public A public: A(){a=0;b=0;} A(int i){a=i;b=0;} A(int i,int j){a=i;b=j;} void display(){cout<<"a="<<a<<" b="<<b;} private: int a; int b; } 11.8 题 #include <iostream> using namespace std; class A { public: A(){cout<<"constructing A "<<endl;} ~A(){cout<<"destructing A "<<endl;} b3.display1(); b4.display1(); return 0;

54

袁俊南
11.9 题 #include<string> #include <iostream> using namespace std; class Teacher {public: Teacher(string nam,int a,char s,string tit,string ad,string t); void display(); protected: string name; int age; char sex; string title; string addr; string tel; }; Teacher::Teacher(string ad,string t): name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){ } void Teacher::display() {cout<<"name:"<<name<<endl; cout<<"age"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"title:"<<title<<endl; cout<<"address:"<<addr<<endl; cout<<"tel:"<<tel<<endl; } class Cadre {public: Cadre(string nam,int a,char s,string p,string ad,string t); }; Teacher_Cadre::Teacher_Cadre(string nam,int a,char s,string t,string } class Teacher_Cadre:public Teacher,public Cadre {public: Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w); void show( ); private: float wage; 函数 private: int num; char name[20]; char sex; }; Teacher::Teacher(int n,char nam[],char s) //定义构造函数 数 void display(); //声明输出 nam,int a,char s,string tit,string }; Cadre::Cadre(string nam,int a,char s,string p,string ad,string t): name(nam),age(a),sex(s),post(p),addr(ad),tel(t){} void Cadre::display() {cout<<"name:"<<name<<endl; cout<<"age:"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"post:"<<post<<endl; cout<<"address:"<<addr<<endl; cout<<"tel:"<<tel<<endl; } 11.10 题 #include <iostream> #include <cstring> using namespace std; class Teacher {public: Teacher(int,char [],char); //声明构造函 //教师类 void display(); protected: string name; int age; char sex; string post; string addr; string tel; p,string ad,string tel,float w): Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) {} void Teacher_Cadre::show( ) {Teacher::display(); cout<<"post:"<<Cadre::post<<endl; cout<<"wages:"<<wage<<endl; } int main( ) {Teacher_Cadre te_ca("Wang-li",50,'f',"prof.","president","135 Beijing Road,Shanghai","(021)61234567",1534.5); te_ca.show( ); return 0;

55

袁俊南
{num=n; strcpy(name,nam); sex=s; } void Teacher::display() 函数 {cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } class BirthDate {public: BirthDate(int,int,int); 函数 void display(); 数 void change(int,int,int); 函数 private: int year; int month; int day; }; BirthDate::BirthDate(int y,int m,int d) {year=y; month=m; day=d; } void BirthDate::display() 函数 //定义输出 //定义构造函数 对 象作为数据成员 }; Professor::Professor(int n,char nam[20],char s,int y,int m,int d,float a): Teacher(n,nam,s),birthday(y,m,d),area(a){ } 函数 void Professor::display() 数 {Teacher::display(); //定义输出函 //定义构造 //习题 12.1 中的 circle.cpp 文件 //CIRCLE.CPP //#include <iostream.h> Circle::Circle(float a,float b,float r):Point(a,b),radius(r){} void Circle::setRadius(float r) {radius=r;} //声明修改 函数 private: float area; BirthDate birthday; //定义 BirthDate 类的 } //声明输出函 函数 void change(int,int,int); //声明修改 //声明构造 函数 void display(); //声明输出 //生日类 class Professor:public Teacher 类 {public: Professor(int,char [],char,int,int,int,float); //声明构造 //教授 } //定义输出 void BirthDate::change(int y,int m,int d) {year=y; month=m; day=d; //定义修改函数 } void Professor::change(int y,int m,int d) 函数 {birthday.change(y,m,d); } int main() {Professor prof1(3012,"Zhang",'f',1949,10,1,125.4); Professor 对象 prof1 cout<<endl<<"original data:"<<endl; prof1.display(); 函数 cout<<endl<<"new data:"<<endl; prof1.change(1950,6,1); 函数 prof1.display(); 函数 return 0; //调用 prof1 对象的 display //调用 prof1 对象的 change //调用 prof1 对象的 display //定义 //定义修改 {cout<<"birthday:"<<month<<"/"<<day<<"/"<<year<<endl;} birthday.display(); cout<<"area:"<<area<<endl;

第十二章

56

袁俊南
float Circle::getRadius() const {return radius;} float Circle::area() const {return 3.14159*radius*radius;} float Cylinder::volume() const ostream &operator<<(ostream &output,const Circle &c) {output<<"Center=["<<c.x<<","<<c.y<<"], area="<<c.area() <<endl; return output; } //习题 12.1 中的 circle.h 文件 //CIRCLE.H #include "point.h" class Circle:public Point {public: Circle(float x=0,float y=0,float r=0); void setRadius(float); float getRadius() const; float area () const; friend ostream &operator<<(ostream &,const Circle &); protected: float radius; }; //习题 12.1 中的 cylinder.cpp 文件 //CYLINDER.CPP Cylinder::Cylinder(float a,float b,float r,float h) :Circle(a,b,r),height(h){} void Cylinder::setHeight(float h){height=h;} }; //习题 12.1 中的 point.cpp 文件 } //习题 12.1 中的 cylinder.h 头文件 //CYLINDER.H #include "circle.h" class Cylinder:public Circle {public: Cylinder (float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight() const; float area() const; float volume() const; friend ostream& operator<<(ostream&,const Cylinder&); protected: float height; }; 12.1 题 #include <iostream> <iosttram.h> using namespace std; #include "cylinder.h" #include "point.cpp" #include "circle.cpp" #include "cylinder.cpp" int main() //如用 VC++应取消此行 // 如 用 VC++ 应 改 为 ∶ #include r="<<c.radius<<", ostream &operator<<(ostream &output,const Cylinder& cy) {output<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height <<"\narea="<<cy.area()<<", volume="<<cy.volume()<<endl; return output; } //习题 12.1 中的 point.h 头文件 //POINT.H class Point {public: Point(float=0,float=0); void setPoint(float,float); float getX() const {return x;} float getY() const {return y;} friend ostream & operator<<(ostream &,const Point &); protected: float x,y; {return Circle::area()*height;} float Cylinder::getHeight() const {return height;} float Cylinder::area() const { return 2*Circle::area()+2*3.14159*radius*height;} //POINT.CPP Point::Point(float a,float b) {x=a;y=b;} void Point::setPoint(float a,float b) {x=a;y=b;} ostream & operator<<(ostream &output,const Point &p) {output<<"["<<p.x<<","<<p.y<<"]"<<endl; return output;

57

袁俊南
{Cylinder cy1(3.5,6.4,5.2,10); cout<<"\noriginal y="<<cy1.getY()<<", r=" <<cy1.getRadius()<<", h="<<cy1.getHeight()<<"\narea="<<cy1.area() <<", volume="<<cy1.volume()<<endl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); cout<<"\nnew cylinder:\n"<<cy1; Point &pRef=cy1; cout<<"\npRef as a point:"<<pRef; Circle &cRef=cy1; cout<<"\ncRef as a Circle:"<<cRef; return 0; } 12.3 题 #include <iostream> using namespace std; class Point {public: Point(float a,float b):x(a),y(b){} ~Point(){cout<<"executing Point destructor"<<endl;} private: float x; float y; }; class Circle:public Point {public: }; }; class Circle:public Point {public: Circle(int a,int b,int r):Point(a,b),radius(r){} ~Circle(){cout<<"executing Circle destructor"<<endl;} private: float radus; } }; void main() {Point *p=new Circle(2.5,1.8,4.5); delete p; 12.3 题另一解 #include <iostream> using namespace std; class Point {public: Point(float a,float b):x(a),y(b){} ~Point(){cout<<"executing Point destructor"<<endl;} private: float x; float y; }; class Circle:public Point {public: Circle(float a,float b,float r):Point(a,b),radius(r){} virtual ~Circle(){cout<<"executing Circle destructor"<<endl;} private: float radus; } int main() {Point *p=new Circle(2.5,1.8,4.5); delete p; return 0; 12.3 题另一解 #include <iostream> using namespace std; class Point {public: Point(float a,float b):x(a),y(b){} virtual ~Point(){cout<<"executing Point destructor"<<endl;} private: float x; float y; }; } cylinder:\nx="<<cy1.getX()<<", Circle(float a,float b,float r):Point(a,b),radius(r){} ~Circle(){cout<<"executing Circle destructor"<<endl;} private: float radus; int main() {Point *p=new Circle(2.5,1.8,4.5); Circle *pt=new Circle(2.5,1.8,4.5); delete pt; return 0;

58

袁俊南
12.4 题 #include <iostream> using namespace std; //定义抽象基类 Shape class Shape {public: virtual double area() const =0; }; //定义 Circle 类 class Circle:public Shape {public: Circle(double //结构函数 virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数 protected: double //半径 }; //定义 Rectangle 类 class Rectangle:public Shape {public: Rectangle(double //结构函数 virtual 数 protected: double //宽与高 width,height; double area() const {return width*height;} //定义虚函 w,double h):width(w),height(h){} radius; r):radius(r){} }; //输出面积的函数 void printArea(const Shape &s) {cout<<s.area()<<endl;} 积 int main() { Circle //建立 Circle 类对象 circle cout<<"area of circle printArea(circle); Rectangle //建立 Rectangle 类对象 rectangle cout<<"area of rectangle ="; printArea(rectangle); //输出 ectangle 的面积 Triangle //建立 Triangle 类对象 cout<<"area of triangle printArea(triangle); ="; //输出 triangle 的面积 triangle(4.5,8.4); ="; //输出 circle 的面积 rectangle(4.5,8.4); }; //定义 Square(正方形)类 class Square:public Shape {public: Square(double //结构函数 virtual protected: double area() const {return side*side;} //定义虚函数 s):side(s){} circle(12.6); //输出 s 的面 //纯虚函数 }; class Triangle:public Shape {public: Triangle(double //结构函数 virtual double area() const {return 0.5*width*height;} //定义虚函数 protected: double //宽与高 width,height; w,double h):width(w),height(h){} } 12.5 题 #include <iostream> using namespace std; //定义抽象基类 Shape class Shape {public: virtual //纯虚函数 }; //定义 Circle(圆形)类 class Circle:public Shape {public: Circle(double //结构函数 virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数 protected: double //半径 radius; r):radius(r){} double area() const =0; return 0;

59

袁俊南
double side; }; //定义 Rectangle(矩形)类 class Rectangle:public Shape {public: Rectangle(double //结构函数 virtual double area() const {return width*height;} //定义虚函数 protected: double //宽与高 }; //定义 Trapezoid(梯形)类 class Trapezoid:public Shape {public: Trapezoid(double t,double b,double h):top(t),bottom(t),height(h){} //结构函数 virtual double area() const {return 0.5*(top+bottom)*height;} //定义虚函数 protected: double //上底、下底与高 }; //定义 Triangle(三角形)类 class Triangle:public Shape {public: Triangle(double //结构函数 virtual double area() const {return 0.5*width*height;} w,double h):width(w),height(h){} 13.1 题 #include <iostream> } } top,bottom,height; width,height; w,double h):width(w),height(h){} }; int main() { Circle //建立 Circle 类对象 circle Square //建立 Square 类对象 square Rectangle //建立 Rectangle 类对象 rectangle Trapezoid //建立 Trapezoid 类对象 trapezoid Triangle //建立 Triangle 类对象 Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle}; double //areas 为总面积 for(int i=0;i<5;i++) {areas=areas+pt[i]->area();} cout<<"totol of all areas="<<areas<<endl; return 0; void input(double a,double b,double c) {cout<<"please input a,b,c:"; cin>>a>>b>>c; //输出总面积 // 定义基类指针数组 pt, 使它每一个元素指向一个派生类对象 areas=0.0; 13.1 题另一解 #include <iostream> #include <cmath> using namespace std; } triangle(4.5,8.4); trapezoid(2.0,4.5,3.2); rectangle(4.5,8.4); square(3.5); circle(12.6); //定义虚函数 protected: double //宽与高 width,height; #include <cmath> using namespace std; int main() {double a,b,c,s,area; cout<<"please input a,b,c:"; cin>>a>>b>>c; if (a+b<=c) cerr<<"a+b<=c,error!"<<endl; else if(b+c<=a) cerr<<"b+c<=a,error!"<<endl; else if (c+a<=b) cerr<<"c+a<=b,error!"<<endl; else {s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"area="<<area<<endl;} return 0;

第十三章

60

袁俊南
void area(double a,double b,double c) {double s,area; if (a+b<=c) cerr<<"a+b<=c,error!"<<endl; else if(b+c<=a) cerr<<"b+c<=a,error!"<<endl; else if (c+a<=b) cerr<<"c+a<=b,error!"<<endl; else {s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"area="<<area<<endl;} } int main() {double a=2,b=3,c=5; input(a,b,c); area(a,b,c); return 0; } 13.2 题 #include <iostream> #include <iomanip> using namespace std; int main() {float a[5]; cout<<"input data:"; for(int i=0;i<5;i++) cin>>a[i]; 13.3 题 #include <iostream> #include <iomanip> using namespace std; int main() { for(int n=1;n<8;n++) } cout<<"enter 10 integer numbers:"<<endl; for(int i=0;i<10;i++) 件中 {cin>>a[i]; outfile1<<a[i]<<" ";} //输入 10 个数存放到 f1.dat 文 } 13.2 题另一解 #include <iostream> using namespace std; int main() {float a[5]; int i; cout<<"input data:"; for(i=0;i<5;i++) cin>>a[i]; cout.setf(ios::fixed); cout.precision(2); for(i=0;i<5;i++) {cout.width(10); cout<<a[i]<<endl;} return 0; 功 {cerr<<"open f2.dat error!"<<endl; exit(1); } if(!outfile2) //检查打开 f2.dat 是否成 } 13.4 题 #include <iostream> #include <fstream> using namespace std; 件中 void fun1() {int a[10]; ofstream outfile1("f1.dat"),outfile2("f2.dat"); 个文件流对象 if(!outfile1) 功 {cerr<<"open f1.dat error!"<<endl; exit(1); //检查打开 f1.dat 是否成 //分别定义两 //VC++ 6.0 要此行 //fun1 函数从键盘输入 20 个整数,分别存放在两个磁盘文 cout<<setiosflags(ios::fixed)<<setprecision(2); for(i=0;i<5;i++) cout<<setw(10)<<a[i]<<endl; return 0; } cout<<setw(20-n)<<setfill(' ')<<" " return 0; //nm <<setw(2*n-1)<<setfill('B')<<"B"<<endl;

61

袁俊南
cout<<"enter 10 integer numbers:"<<endl; for(i=0;i<10;i++) 件中 {cin>>a[i]; outfile2<<a[i]<<" ";} outfile1.close(); outfile2.close(); } //从 f1,dat 读入 10 个数, 然后存放到 f2.dat 文件原有数据的 后面 void fun2() {ifstream infile("f1.dat"); if(!infile) {cerr<<"open f1.dat error!"<<endl; exit(1); } ofstream outfile("f2.dat",ios::app); //f2.dat 作为输出文件,文件指针指向文件尾,向它写入的 数据放在原来数据的后面 if(!outfile) {cerr<<"open f2.dat error!"<<endl; exit(1); } int a; for(int i=0;i<10;i++) {infile>>a; outfile<<a<<" "; } infile.close(); outfile.close(); 数 //磁盘文件 f2.dat 读入一个整数 //将该数存放到 f2.dat 中 中 for(i=0;i<19;i++) for(j=0;j<19-i;j++) if(a[j]>a[j+1]) {t=a[j];a[j]=a[j+1];a[j+1]=t;} infile.close(); //关闭输入文件 f2.dat ofstream outfile("f2.dat",ios::out); // f2.dat 作为输出文件,文件中原有内容删除 if(!outfile) {cerr<<"open f2.dat error!"<<endl; exit(1);} cout<<"data in f2.dat:"<<endl; for( i=0;i<20;i++) {outfile<<a[i]<<" "; //向 f2.dat 输出已排序的 20 个 3006,"Xue",45,476.5,5101,"Ling",39,656.6},staf1; fstream iofile("staff.dat",ios::in|ios::out|ios::binary); if(!iofile) //用起泡法对 20 个数排序 //f1.dat 作为输入文件 } int a[20]; int i,j,t; for(i=0;i<20;i++) infile>>a[i]; //从磁盘文件 f2.dat 读入 20 个数放在数组 a 13.5 题 #include <iostream> #include <fstream> using namespace std; struct staff {int num; char name[20]; int age; double }; int main() {staff staf[7]={2101,"Li",34,1203,2104,"Wang",23,674.5,2108,"Fun", 54,778, pay; //关闭 f1.dat 文件 //关闭 f2.dat 文件 //输入 10 个数存放到 f2.dat 文 //从 f2.dat 中读入 20 个整数,将它们按从小到大的顺序存 放到 f2.dat void fun3() {ifstream infile("f2.dat"); //定义输入文件流 infile,以输入方 式打开 f2.dat if(!infile) {cerr<<"open f2.dat error!"<<endl; exit(1); } int main() {fun1(); fun2(); fun3(); return 0; //分别调用 3 个函数 } } cout<<a[i]<<" ";} cout<<endl; outfile.close(); //同时输出到显示器

62

袁俊南
{cerr<<"open error!"<<endl; abort(); } int i,m,num; cout<<"Five staff :"<<endl; for(i=0;i<5;i++) {cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf [i].pay<<endl; iofile.write((char *)&staf[i],sizeof(staf[i]));} cout<<"please input data you want insert:"<<endl; for(i=0;i<2;i++) {cin>>staf1.num>>staf1.name>>staf1.age>>staf1.pay; iofile.seekp(0,ios::end); iofile.write((char *)&staf1,sizeof(staf1));} iofile.seekg(0,ios::beg); for(i=0;i<7;i++) {iofile.read((char *)&staf[i],sizeof(staf[i])); cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf [i].pay<<endl; } bool find; cout<<"enter number you want search,enter 0 to stop."; cin>>num; while(num) {find=false; iofile.seekg(0,ios::beg); for(i=0;i<7;i++) {iofile.read((char *)&staf[i],sizeof(staf[i])); struct student {int num; char name[20]; double score; }; 13.6 题 #include <iostream> #include <strstream> using namespace std; 13.6 题另一题 #include <iostream> #include <strstream> using namespace std; struct student {int num; char name[20]; } } } iofile.close(); return 0; } if(!find) cout<<"can't find "<<num<<endl; cout<<"enter number you want search,enter 0 to stop."; cin>>num; } if(num==staf[i].num) {m=iofile.tellg(); cout<<num<<" is No."<<m/sizeof(staf1)<<endl; cout<<staf[i].num<<" "<<staf[i].age<<" "<<staf [i].pay<<endl; find=true; break; "<<staf[i].name<<" int main() {student stud[3]={1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90},stud1[ 3]; char c[50]; int i; ostrstream strout(c,50); for(i=0;i<3;i++) strout<<stud[i].num<<" "<<stud[i].score<<" "; strout<<ends; cout<<"array c:"<<endl<<c<<endl<<endl; istrstream strin(c,50); for(i=0;i<3;i++) strin>>stud1[i].num>>stud1[i].name>>stud1[i].score; cout<<"data from array c to array stud1:"<<endl; for(i=0;i<3;i++) cout<<stud1[i].num<<" "<<stud1[i].score<<endl; cout<<endl; return 0; "<<stud1[i].name<<" "<<stud[i].name<<"

63

袁俊南
double score; }; int main() {int i; student stud[3]={1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90},stud1[ 3]; char c[50]; strstream strio(c,50,ios::in|ios::out); for(i=0;i<3;i++) strio<<stud[i].num<<" "<<stud[i].score<<" "; strio<<ends; cout<<"array c:"<<endl<<c<<endl<<endl; for(i=0;i<3;i++) strio>>stud1[i].num>>stud1[i].name>>stud1[i].score; cout<<"data from array c to array stud1:"<<endl; for(i=0;i<3;i++) cout<<stud1[i].num<<" "<<stud1[i].score<<endl; cout<<endl; return 0; } } 14.2 题 14.1 题 #include <iostream> #include <cmath> using namespace std; #include <iostream> #include <string> using namespace std; class Student } int main() {cout<<"main begin"<<endl; cout<<"call fun()"<<endl; fun(); "<<stud1[i].name<<" double q(double a,double b,double c) {double disc; disc=b*b-4*a*c; if (disc<0) throw disc; return sqrt(disc)/(2*a); } catch(int n) {cout<<"num="<<n<<",error!"<<endl;} } {cout<<"a="<<a<<",b="<<b<<",c="<<c<<",disc="<<d<<",error!" <<endl;} cout<<"end"<<endl; void fun() {Student stud1(1101,"tan"); stud1.get_data(); try {Student stud2(0,"Li"); stud2.get_data(); } "<<stud[i].name<<" } catch(double d) double q(double,double,double); void main() {double a,b,c,p,x1,x2; cout<<"please enter a,b,c:"; cin>>a>>b>>c; p=-b/(2*a); try {x1=p+q(a,b,c); x2=p-q(a,b,c); cout<<"x1="<<x1<<endl<<"x2="<<x2<<endl; }; void Student::get_data() {if(num==0) throw num; else cout<<num<<" "<<name<<endl; cout<<"in get_data()"<<endl; {public: Student(int n,string nam) {cout<<"constructor-"<<n<<endl; num=n;name=nam;} ~Student(){cout<<"destructor-"<<num<<endl;} void get_data(); private: int num; string name;

第十四章

64

袁俊南
cout<<"main end"<<endl; return 0; } void show_data(); private: int num; string name; int age; 14.3 题 //main file #include <iostream> using namespace std; #include "xt14-3-h1.h" #include "xt14-3-h2.h" using namespace std; using namespace student1; int main() {Student Road,Shanghua"); stud1.show_data(); student2::Student stud2(1102,"Li",'f',89.5); stud2.show_data(); return 0; } 14.3 题 H1 //header1.h #include <string> namespace student1 {class Student {public: Student(int n,string nam,int a,string addr) {num=n;name=nam;age=a;address=addr;} }; stud1(1001,"Wang",18,"123 Beijing } 14.3 题 H2 //header2.h #include <string> namespace student2 {class Student {public: Student(int n,string nam,char s,float sco) {num=n;name=nam;sex=s;score=sco;} void show_data(); private: int num; string name; char sex; float score; } }; void Student::show_data() {cout<<"num:"<<num<<" age:"<<age <<" address:"<<address<<endl; name:"<<name<<" string address; } void Student::show_data() {cout<<"num:"<<num<<" <<" name:"<<name<<" sex:"<<sex score:"<<score<<endl; }

65


更多相关文档:

《C++程序设计》谭浩强版实验指导书

《C++程序设计》谭浩强版实验指导书_理学_高等教育_教育专区。大一C++实验要求的内容 《C++程序设计》 实验教学指导书 课程编号:??? 撰写人:朱世华 审核人: 前言...

c++程序设计谭浩强课后习题答案(完整版)

c++程序设计谭浩强课后习题答案(完整版)_工学_高等教育_教育专区 暂无评价|0人阅读|0次下载|举报文档c++程序设计谭浩强课后习题答案(完整版)_工学_高等教育_教育...

c++程序设计谭浩强第5章2013修订

c++程序设计谭浩强第5章2013修订_理学_高等教育_教育专区。c++课件第五章 数 组 5.1 数组的概念(P128)在解决实际问题时,往往会需要 处理一批具有相同的数 据类...

c++程序设计_谭浩强_答案_修改版

c++程序设计_谭浩强_答案_修改版_电子/电路_工程科技_专业资料 暂无评价|0人阅读|0次下载c++程序设计_谭浩强_答案_修改版_电子/电路_工程科技_专业资料。第一章...

《c++程序设计》谭浩强_答案 第13章

41页 5财富值 《c++程序设计》谭浩强_答... 40页 10财富值如要投诉违规内容,请到百度文库投诉中心;如要提出功能问题或意见建议,请点击此处进行反馈。 ...

谭浩强C++程序设计复习提纲

清华大学出版社版谭浩强《C++程序设计》复习提纲复习目标: (1)掌握 C++的基本语法结构; (2)掌握基本的程序设计算法和技巧; (3)具备初步的高级语言程序设计能力; ...

C++程序设计(第2版 谭浩强)习题答案

C++程序设计(第2版 谭浩强)习题答案_工学_高等教育_教育专区 暂无评价|0人阅读|0次下载|举报文档C++程序设计(第2版 谭浩强)习题答案_工学_高等教育_教育专区。...

C++语言程序设计-谭浩强_图文

C++语言程序设计-谭浩强_计算机软件及应用_IT/计算机_专业资料。第 1 章 C++的...参考文献: 《C++编程思想》 Bruce Eckel 著 机械工业出版社 195 第十三讲 第...

《c++程序设计》谭浩强课后习题答案

第一章 1.5 题 #include <iostream> using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0; ...

《c++程序设计》谭浩强_答案

《c++程序设计》谭浩强_答案_电脑基础知识_IT/计算机_专业资料 暂无评价|0人阅读|0次下载|举报文档《c++程序设计》谭浩强_答案_电脑基础知识_IT/计算机_专业资料。...
更多相关标签:
网站地图

文档资料共享网 nexoncn.com copyright ©right 2010-2020。
文档资料共享网内容来自网络,如有侵犯请联系客服。email:zhit325@126.com