⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 练习.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 TXT
字号:
3.1
#include <iostream.h>
#include <math.h>

void main()
{
	const float pi = 3.1415926;
	double a,c,x,tmp;
	
	cout<<"please input the parameter x of 表达式1:"<<endl;
	cin>>x;
	tmp = sqrt(pow(sin(x),2.5));
	cout<<"the value of 表达式1 is "<<tmp<<endl;

	cout<<"please input the parameter a of 表达式2:"<<endl;
	cin>>a;
	tmp=0.5*(a*x+((a+x)/4/a));
	cout<<"the value of 表达式2 is "<<tmp<<endl;

	cout<<"please input the parameter c of 表达式3:"<<endl;
	cin>>c;
	tmp=pow(c,pow(x,2))/sqrt(2*pi);
	cout<<"the value of 表达式3 is "<<tmp<<endl;
}

3.2
expression1: //cmp the execute result with the real one  ( 13.7 vs 14.2)
#include <iostream.h>
#include <math.h>

void main()
{
	int e=1,f=4,g=2;
	float m=10.5,n=4.0,k;
	k=(e+f)/g+sqrt((double)n)*1.2/g+m;
	cout<<"k = "<<k<<endl;
}

expression2:  //cmp the execute result with the real one  ( 2.5 vs 2.75 )
#include <iostream.h>
#include <math.h>

void main()
{
	float x=2.5,y=4.7,r;
	int a=7;
	r = x+(a%3)*(int)(x+y)%2/4;
	cout<<"r = "<<r<<endl;
}

expression3:  //note the difference between a++ and ++a
#include <iostream.h>
#include <math.h>

void main()
{
	int a=2,b=5,a1,b1,c;
	a1=a++;
	b1=b++;
	c=a+b;
	cout<<"a1 = "<<a1<<endl
		<<"b1 = "<<b1<<endl
		<<"c = "<<c<<endl;
}

3.3
(1)
#include <iostream.h>

void main()
{
	int a1,a2;
	int i=5,j=7,k=0;
	a1=!k;
	a2=i!=j;  //注解:此处的“!=”是一个比较运算符,原式可表达为:a2=(i!=j); 而i!=j为真,故a2=1;
	cout<<"a1 = "<<a1<<endl
	    <<"a2 = "<<a2<<endl;
}

(2)
#include <iostream.h>

void main()
{
	int x,y,z;
	x=1;
	y=0;
	z=0;
	x=x||y&&z;          //注意:&&优先级比||高,表达式为:x=x||(y&&z); 
	cout<<x<<","<<(x&&!y||z)<<endl;   //!优先级又比&&高,表达式为:(x&&(!y))||z;
}

(3)
#include <iostream.h>

void main()
{
	int a,b,c;
	int s,w,t;
	s=w=t=0;
	a=-1;
	b=3;
	c=3;
	if(c>0)
		s=a+b;
	if(a<=0)
	{
		if(b>0)
			if(c<=0)
				w=a-b;
	}
	else
		if(c>0)
			w=a-b;
		else
			t=c;
	cout<<s<<','
		<<w<<','
		<<t<<endl;
}

(4)
#include <iostream.h>

void main()
{
	int a,b,c,d,x;
	a=c=0;
	b=1;
	d=20;
	if(a)
		d=d-10;
	else
		if(!b)
			if(!c)
				x=15;
			else
				x=25;
	cout<<d<<endl;
}

3.4
#include <iostream.h>

void main()
{
	int x,y;
	cout<<"input the value of x:"<<endl;
	cin>>x;
	if(x>10)
		cout<<"the value of x is overflowed"<<endl;
	else
		if(x>2&&x<=10)
			y=x*(x+2);
		if(x>-1&&x<=2)
			y=2*x;
		if(x<=-1)
			y=x-1;
	cout<<"y = "<<y<<endl;
}

3.5
#include <iostream.h>
#include <conio.h>

void main()
{
	int x,f3=0,f5=0,f7=0,count=0;
	cout<<"please input the integer: "<<endl;
	cin>>x;
	if(!(x%3)||!(x%5)||!(x%7))
	{
		if(x%3==0)
		{f3=1;count++;}
		if(x%5==0)
		{f5=1;count++;}
		if(x%7==0)
		{f7=1;count++;}
	}
	else
		cout<<"the integer couldn't be divided exactly by 3 or 5 or 7!"<<endl;

	if(count>0 && count<=3)
	{
		if(count==1)
		{
			if(f3)
				cout<<"the integer could be divided by 3."<<endl;
			if(f5)
				cout<<"the integer could be divided by 5."<<endl;
			if(f7)
				cout<<"the integer could be divided by 7."<<endl;
		}
		if(count==2)
		{
			if(!f3)
				cout<<"the integer could be divided by 5 and 7."<<endl;
			if(!f5)
				cout<<"the integer could be divided by 3 and 7."<<endl;
			if(!f7)
				cout<<"the integer could be divided by 3 and 5."<<endl;
		}
		if(count==3)
			cout<<"the integer could be divided by 3,5,7 at the same time."<<endl;
	}

	cout<<"Exiting..."<<endl;
}

3.5
#include <iostream.h>

void main()
{
	int x;
	char scores;
	cout<<"please input your scores: "<<endl;
	cin>>x;
	if(x>100||x<0)
		cout<<"The input scores are wrong !"<<endl;
	else
	{
		if(x>=90)
			scores='A';
		if(x>=80 && x<90)
			scores='B';
		if(x>=70 && x<80)
			scores='C';
		if(x>=60 && x<70)	
			scores='D';
		if(x<60)
			scores='E';
	}
	if(x>=0 && x<=100)
	cout<<"Your grade is "<<scores<<endl
		<<"bye!"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -