C++程序设计 上机实践与学习辅导(清华大学出版社 第四版)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第一到十一章
实验1.1
#include
using namespace std;
void main ()
{
int a,b,sum;
cout<<"请输入加数与被加数\n";
cin>>a>>b;
sum=a+b;
cout<<"sum="<
1.2
#include
using namespace std;
void main ()
{
double f,c; //定义存放华氏温度及摄氏温度的两个变量空间
cout<<"请输入华氏温度"; //输出提示信息
cin>>f; //从键盘输入华氏温度
c=(f-32)*5/9; //计算摄氏温度
cout<<"摄氏温度="<
1.3
#include
using namespace std;
void main ()
{
float s,r;
cout<<"输入半径r=";
cin>>r;
const float PI=3.14159;
s=PI*r*r;
cout<}
1.4
#include
#include
using namespace std;
void main ()
{
float a,b,c,s,area;
cout<<"分别输入三角形三边长";
cin>>a>>b>>c;
s=a+b+c;
area=sqrt(s/2*(s/2-a)*(s/2-b)*(s/2-c));
cout<}
实验2.1
#include
using namespace std;
void main ()
{
float a,b,c;
cout<<"input a ,b,c\n";
cin>>a>>b>>c;
if((a+b
else
cout<<"Trianglr\n";
}
2.2
#include
using namespace std;
void main ()
{
int year;
cout<<"Input year:";
cin>>year;
if(year%100==0 && year%400==0)
cout<
cout<<"isn't a leap year"<
2.3
#include
using namespace std;
void main ()
{
float a1,a2;
char oper;
float result;
int flag=0;
cout<<"请输入简单的运算符:\n";
cin>>a1>>oper>>a2;
switch (oper)
{ case '+':
result=a1+a2;
break;
case '-':
result=a1-a2;
break;
case '*':
result=a1*a2;
break;
default:
cout<<"输入错误的运算符\n";
flag=1;
}
if(flag==0)
cout<
2.4
#include
#include
using namespace std;
void main ()
{
float a,b,c,d,x1,x2;
cout<<"请分别输入一元二次方程的三个系数:\n";
cin>>a>>b>>c;
d=b*b-4*a*c;
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
if (d<0)
cout<<"此方程无实根";
else
cout<<"此方程两根分别为:"<
2.5
#include
using namespace std;
void main ()
{
float a1,a2;
char oper;
float result;
int flag=0;
cout<<"请输入简单的运算符:\n";
cin>>a1>>oper>>a2;
switch (oper)
{ case '+':
result=a1+a2;
break;
case '-':
result=a1-a2;
break;
case '*':
result=a1*a2;
break;
case '/':
result=a1/a2;
if (a2==0)
cout<<"输入错误,被除数不可为0";
break;
default:
cout<<"输入错误的运算符\n";
flag=1;
}
if(flag==0)
cout<
<
实验3.1
#include
using namespace std;
void main ()
{ int x,y; //定义变量,X代表鸡的数目,Y代表兔的数目
for(x=1;x<30;x++) //鸡的数目从1~30皆有可能,所以程序将
//所有可能的解释循环一遍
for (y=1;y<30;y++) /*同样兔子的数目从1~30皆有可能,所以程序
将所有可能的解释循环一遍*/
if (2*x+4*y==90&&x+y==30) //在所有可能的解中将满足给定条件的解输出412
{ cout<<"鸡有:"<
3.2
#include
using namespace std;
void main ()
{ int a0,a1,a2;
int b;
for (a2=1;a2<=9;a2++)
for(a1=0;a1<=9;a1++)
{ b=a0*a0*a0+a1*a1*a1+a2*a2*a2;
if (b==100*a2+10*a1+a0)
cout<}
}
实验4.1
#include
void main()
{ int a,b;
int t,x;
cout<<"请输入两个整数:";
cin>>a>>b;
x=a*b;
if (a{ t=a; a=b; b=t;}
while (t=a%b)
{ a=b;
b=t;
}
cout<<"最大公约数:"<cout<<"最小公倍数:"<
4.2
#include
void main()
{ int a,b,c;
cout<<"Input three number:";
cin>>a>>b>>c;
for (int i=a;i>=1;i--)
if (a%i==0&&b%i==0&&c%i==0)
break;
cout<}
4.3
#include
void main()
{ int a;
cout<<"请输入一个正数";
cin>>a;
for(int i=2;i
break;
if (a%i!=0)
cout<else
cout<}
4.4
#include
void main(void){
int a,b,c,x;
cout<<"input three number:";
cin>>a>>b>>c;
x=a*b*c;
for(int i=a;i>=1;i--)
if(a%i==0&&b%i==0&&c%i==0)
break;
cout<cout<<"最小公倍数:"<
4.5
#include
void main()
{ int a;
for(int i=100;i<200;i++)
for(a=2;a
if (i%a!=0)
{ cout<break;
}
if(i%a==0)
cout<<"";
}
4.6
#include
void main()
{ int x,i;
long sum(0);
for (x=1;x<=1000;x++)
{ for(i=1;i
if(x==sum)
cout<
sum=0;}
}
实验5.1
#include
void main ()
{ int t=2;
int s=0;
for (int i=1;i<=7;i++)
{ s=s+t;
t=t*10+2;
}
cout<<"2+22+222+...+2222222="<}
5.2
#include
void main ()
{ float y=1,t=1;
int n;
cout<<"Input n:";
cin>>n;
for (int i=2;i<=n;i++)
{ t=t*i;
y=y+t;
}
cout<<"1!+2!+3!+...+"<
5.3
#include
#include
void main ()
{ double x=0.75,y,y0;
y=x;
do
{ y0=y;
y=2*y/3+x/(3*y*y);
}while (fabs(y0-y)>=0.0001);
cout<
5.4
#include
void main ()
{ float d=1,x,y,n,a,t,i;
cin>>n>>x;
a=x;
y=x;
for (i=2;i<=n;i++)
{ d=d+2;
t=x*x/(d*(d-1));
a=(-1)*t*a;
y=y+a;
}
cout<
实验6.1
#include
float fun(float x){
float y;
y=2*x*x+3*x+5;
return y;}
void main ()
{
float y1,y2;
y1=fun(1.0);
y2=fun(1.2);
cout<<"y1="<
6.2
#include
#include
float f(float x){
float y;
y=x*x+3*x-4;
return y;}
float df(float x)
{ float y;
y=2*x+3;
return y;}
void main ()
{ float x1,x0;
x1=0;
do{
x0=x1;
x1=x0-f(x0)/df(x0);
}while (fabs(f(x0))>=0.00001);
cout<<"x="<
6.3
#include
#include
float f(float x)
{ return x*x+3*x-4;}
void main ()
{ float x1,x2,x0;
do
{ cout<<"请输入初值x1和x2:";
cin>>x1>>x2;
}while (f(x1)*f(x2)>0);
do
{ x0=(x1+x2)/2;
if (f(x1)*f(x0)>0)
x1=x0;
else
x2=x0;
}while (fabs(f(x0))>1e-5);
cout<<"x0="<
6.4
#include
#include
float f(float x)
{ return x*x+3*x-4;}
void main ()
{ float x1,x2,x0;
do
{ cout<<"请输入初值x1和x2:";
cin>>x1>>x2;
}while (f(x1)*f(x2)>0);
do
{
x0=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
if (f(x1)*f(x0)<0)
x2=x0;
else
x1=x0;
}while (fabs(f(x0))>=0.00001);
cout<<"x0="<
6.5
#include
#include
float f(float x){
float y;
y=x*x+3*x-4;
return y;}
float df(float x)
{ float y;
y=2*x+3;
return y;}
void main (){
float x1,x0;
x1=0;
do{
x0=x1;
x1=x0-f(x0)/df(x0);
}while (fabs(f(x0))>=0.00001);
cout<<"x="<
实验7.1
#include
int prime(int x)
{
for (int i=2;i
return 0;
return 1;
}
void main(void)
{ for (int i=100;i<=200;i++)
{ if (prime(i)==1)
cout<}
}
7.2
#include
int prime(int num)
{
for (int i=2;i
if (num%i==0)
return 0;
return 1;
}
void main(void)
{ int n;
cout<<"请输入一个整数n:";
cin>>n;
for (int i=2;i<=n/2;i++)
{ if (prime(i)==1&&n%i==0)
cout<}
cout<
}
7.3
#include
int prime(int num)
{ for(int i=2;i
return 0;
return 1;
}
void main(void)
{ int n;
cout<<"请输入一个不小于6的偶数n: ";
cin>>n;
for(int i1=0,i2=n;i1<=n&&i2>=0;i1++,i2--)
{
if(prime(i1)==1&&prime(i2)==1&&i1+i2==n&&i1!=i2&&i1
cout<
}
cout<
7.4
#include
int fun(int num)
{
int a0,a1,a2,sum0;
a2=num/100;
a1=num/10%10;
a0=num%10;
sum0=a0*a1*a2;
return sum0;
}
void main()
{ int n,sum;
cout<<"输入一个正整数,输入0时停止。";
cin>>n;
do
{ sum=fun(n);
cout<
}while(n==0);
}
实验8.1
#include
void main ()
{ int a[6]={9,8,7,6,5,4};
int i,j,t;
int n=6;
for (i=0;i
{ t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
for (i=0;i
8.
2
#include
void main ()
{ int a[6]={9,8,7,6,5,4};
int i,j,min,t;
int n=6;
for (j=0;j
for (i=j ;i
min=i;
t=a[min];
a[min]=a[j];
a[j]=t;
}
for (i=0;i
8.3
#include
void main ()
{ int a[10]={2,5,78,4,6,3,89,12,15,34},i,max,maxindex;
max=a[0];
for (i=1;i<9;i++)
{ if (a[i]>max)
{ max=a[i];
maxindex=i;
}
}
cout<<"max="<
实验9.1
#include
void main()
{ int i,j,row=0,colum=0,max;
int a[4][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8},{4,6,34,1}};
max=a[0][0];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if (a[i][j]>max)
{ max=a[i][j];
row=i;
colum=j;
}
cout<<"max="<
9.2
#include
void main()
{ int i,j,k;
int colum,saddle,max,flag=0;
int a[4][4]={{5,12,23,56},{19,28,37,46},{-12,-34,-6,8},{4,6,34,10}};
for (i=0;i<4;i++)
{ max=a[i][0];
for(j=0;j<4;j++)
if(a[i][j]>max)
{ max=a[i][j];
colum=j;
}
for(k=0;k<4;k++)
if(a[k][colum]!=max)
max=a[k][colum];
if(i==k)
{ flag=1;
saddle=max;
break;
}
}
if (flag==1)
cout<<"saddle="<
cout<<"No saddle!\n";
}
9.3
#include
void main()
{ int i,j,num;
int a[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
for (i=0;i<4;i++)
for (j=0;j<4;j++)
if(i
a[i][j]=a[j][i];
a[j][i]=num;}
for (i=0;i<4;i++)
for(j=0;j<4;j++)
cout<
}
9.4
#include
void main()
{ int i,j;
for (i=1;i<10;i++)
{ for(j=1;j<=i;j++)
{ cout<
}
9.5
#include
void main()
{ int a[10][10],i,j;
for(i=0;i<10;i++)
for(j=0;j<=i;j++)
if(j==0||i==j)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=0;i<10;i++)
{for(j=0;j<=i;j++)
{cout<cout<
实验10.1
#include
int gys(int m,int n)
{ int r;
if(m
return n;
}
void main (void)
{ int a[8]={26,1007,956,705,574,371,416,517};
int b[8]={994,631,772,201,262,763,1000,781};
int c[8];
for (int i=0;i<8;i++)
c[i]=gys(a[i],b[i]);
for (i=0;i<8;i++)
cout<
10.2
#include
int fun(int x)
{ int sum=0;
while (x)
{ sum+=x%10;
x=x/10;
}
return sum;
}
void main ()
{ int a[10],b[10];
int sum=0;
cout<<"请输入10个整数";
for (int i=0;i<10;i++)
cin>>a[i];
for (i=0;i<10;i++)
{ b[i]=fun(a[i]);
sum+=a[i];
}
cout<<"sum="<
cout<}
10.3
#include
int fun(int x)
{ int a0,a1,a2;
a2=x/100;
a1=x/10%10;
a0=x%10;
if ((a0+a1+a2)==10)
r
eturn 1;
else
return 0;
}
void main ()
{
int a[1000],i,n,c,sum;
cout<<"输入100至1000之内的任意整数:";
cin>>n;
for (i=100;i<=n;i++)
{ a[i]=i;
sum=fun(a[i]);
if (sum==1)
{ cout<c=c+1;
}}
cout<<"个数为:"<
10.4
#include
int fun(int x)
{ int i;
for (i=2;i
return 0;
if (x==i)
return 1;
else
return 0;
}
void main ()
{ int a[15]={55,154,103,106,151,58,199,10,247,218,39,170,87,122,135},n,i;
cout<<"素数分别为:";
for (i=0;i<15;i++)
if (fun(a[i])==1)
{
cout<n=n+1;
}
cout<<"素数的个数为:"<
}
实验11.1
#include
/* 用折半法从一个一维数组中寻找指定的一个数 *
* 返回该指定的数的位置 *
* */
int binary (int array[],int n,int number)
{ int low=0,high=n-1,mid;
while (low<=high)
{ mid=(low+high)/2;
if (number
else if (number>array[mid])
low=mid+1;
else
return mid;
}
return -1;
}
void main()
{ int a[]={2,4,6,8,10,12,14,16,18,20,22,24};
int num;
cout<<"请输入要寻找的数:";
cin>>num;
int index=binary(a,sizeof(a)/sizeof(a[0]),num);
if(index!=-1)
cout<<""<
cout<<"";
}
11.2
#include
/* 在被调函数中删除一位数组中所有重复的数 *
* 数组中的数据已按由大到小排列 */
int del(int array[],int n)
{
for (int i=0;i
{ for (int j=i+2;j
n=n-1;
i=1;
}
return n;
}
void main()
{
int a[]={2,2,3,4,4,5,6,6,6,6,7,7,8,9,9,10,10,10};
int n=del(a,sizeof(a)/sizeof(a[0]));
for (int i=0;i
11.3
#include
/*对数据进行插入排序 *
*关键数据是第一个和最后一个数据 *
*方法很多 *
*效率是最重要的 *
*/
int fun(int array[],int n,int num)
{
for(int i=n-1;i>=0;i--)
{
if(array[i]==0)
continue;
else if(array[i]>=num)
{ array[i+1]=array[i];}
else
{ array[i+1]=num;
break;}
}
if(num
for(i=0;i
cout<
cout<
}
void main()
{
int a[100],num;
for(int i=0;i<100;i++)
{ a[i]=0;}
cout<<"输入数组数据(输入0结束):";
cin>>num;
a[0]=num;
while(num!=0)
{
cout<<"输入数组数据(输入0结束):";
cin>>num;
fun(a,100,num);
}
}
11.4
#include
/*统计数组全部元素中数字0,1,2,3,。。。,9 *
*各出现的次数(数组b[10]初始值均为零) */
void main()
{ int a[]={5,26,98,77,1,27,30};
int b[10],i;
for(i=0;i<10;i++)
{ b[i]=0;}
for(i=0;i<7;i++)
{ if (a[i]>9)
b[a[i]/10]+=1;
b[a[i]%10]+=1;
}
cout<<"0~9依次出现:";
for (i=0;i<10;i++)
{ cout<cout<<"次"<