C++程序设计上机实践与学习辅导 (清华大学出版社)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 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="<}

(2)
#include
using namespace std;
void main ()
{
double f,c; //定义存放华氏温度及摄氏温度的两个变量空间
cout<<"请输入华氏温度"; //输出提示信息
cin>>f; //从键盘输入华氏温度
c=(f-32)*5/9; //计算摄氏温度
cout<<"摄氏温度="<}
(3)

#include
using namespace std;
void main ()
{
float s,r;
cout<<"输入半径r=";
cin>>r;
const float PI=3.14159;
s=PI*r*r;
cout<}

(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+bcout<<"Not Triangle\n";
else
cout<<"Trianglr\n";
}

(2)
#include
using namespace std;
void main ()
{
int year;
cout<<"Input year:";
cin>>year;
if(year%100==0 && year%400==0)
cout<else
cout<<"isn't a leap year"<}

(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<}


(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<<"此方程两根分别为:"<}


(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<<"鸡有:"<}

(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<<"最小公倍数:"<}


(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<}


(3)

#include
void main()
{ int a;
cout<<"请输入一个正数";
cin>>a;
for(int i=2;iif (a%i==0)
break;
if (a%i!=0)
cout<else
cout<}


(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<<"最小公倍数:"<}

(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<<"";



}

(6)

#include
void main()
{ int x,i;
long sum(0);
for (x=1;x<=1000;x++)
{ for(i=1;i{ sum=sum+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="<}

(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!+...+"<}


(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<}


(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="<cout<<"y2="<}

(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="<}

(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="<}


(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="<}

(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="<}

相关文档
最新文档