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

📄 练习.txt

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

void main()
{
	int a = 42486;
	cout<<"Octal: "<<oct<<a<<endl
	    <<"Hexadecimal: "<<hex<<a<<endl;
	unsigned int b=42486;
	cout<<"Signed value: "<<b<<endl; //printf("%d",&b);
}
--------------------------------------------------------------
2.2
#include <iostream.h>
#include <iomanip.h>

void main()
{
	const double pi = 3.1415926;
	double radius,area;
	for(int i=0;i<2;i++)
	{
		cout<<"Please input the radius of circle: ";
		scanf("%lf",&radius);
		area = pi*radius*radius;
		cout<<"pi = "<<setw(13)<<pi<<endl
		    <<"radius = "<<setw(13)<<radius<<endl
		    <<"area = "<<setw(13)<<area<<endl;
	}
}

2.3
#include <iostream.h>
#include <iomanip.h>

void main()
{
	const double e = 2.718281828;
	cout<<setprecision(10)<<e<<endl;

	cout<<setiosflags(ios::fixed);
	cout<<setprecision(8)<<e<<endl;

	cout<<setiosflags(ios::scientific)
	    <<e<<endl;
}


2.4
#include <iostream.h>

void main()
{
	cout<<"\""<<"How many students here?"<<"\""<<endl;  //note the displaying of "
	cout<<"\""<<"500"<<"\""<<endl;
}


2.5
#include <iostream.h>

void main()
{
	cout<<setiosflags(ios::left)          //warning: here the format is left, not right
	    <<setw(18)<<"size of char"
	    <<sizeof(char)<<endl;
	cout<<setw(18)<<"size of int"
	    <<sizeof(int)<<endl;
	cout<<setw(18)<<"size of float"
	    <<sizeof(float)<<endl:

	cout<<setw(18)<<"size of double"
	    <<sizeof(double)<<endl;
	cout<<setw(18)<<"size of long double"
	    <<sizeof(long double)<<endl;
}

2.6
origin:  //6,6,8->17.88854
#include <stdio.h>
#include <math.h>

void main()
{
	float a,b,c,s,area;
	printf("please input 3 sides of one triangle:\n");
	scanf("%f,%f,%f",&a,&b,&c);

	s = (a+b+c)/2;
	area = sqrt(s*(s-a)*(s-b)*(s-c));

	printf("a = %7.2f,b = %7.2f,c = %7.2f\n",a,b,c);
	printf("area of triangle is S%10.5f\n",area);
}

changed:
#include <iostream.h>
#include <iomanip.h>
#include <math.h>

float tri(float,float,float);

void main()
{
	float a,b,c,area;
	cout<<"please input 3 sides of one triangle:"<<endl;
	cin>>a>>b>>c;

	area = tri(a,b,c);

	cout<<setiosflags(ios::fixed)
	    <<setprecision(2);
	cout<<"a = "<<setw(7)<<a<<","
	    <<"b = :"<<setw(7)<<b<<","
	    <<"c = :"<<setw(7)<<c<<endl;

	cout<<setiosflags(ios::fixed)<<setprecision(5);
	cout<<"area of triangle is "
	    <<setw(10)<<area<<endl;
}

float tri(float a, float b, float c)
{
	float s;
	s = (a+b+c)/2;
	return sqrt(s*(s-a)*(s-b)*(s-c));
}

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

int add(int x,int y);

void main()
{
	cout<<"In main():\n";
	int a,b,c;
	cout<<"Enter two numbers:\n";
	cin>>a>>b;

	cout<<"\nCalling add():\n";
	c = add(a,b);

	cout<<"\nBack in main():\n";
	cout<<"c was set to "<<c<<endl;

	cout<<"\nExiting...\n";
	getche();    //note the strange execute order of cout and getche()
}

int add(int x,int y)
{
	cout<<"In add(),received"<<x<<" and "<<y<<endl;
	cout<<"and return "<<x+y<<endl;
	return x+y;
}

2.8
#include <iostream.h>

float volume(float r,float h);

void main()
{
	float r,h,v;
	cout<<"please input the radius of column: ";
	cin>>r;
	cout<<"please input the height of column: ";
	cin>>h;

	v = volume(r,h);

	cout<<"the volume of column is "<<v<<endl;
}

float volume(float r,float h)
{
	const float pi = 3.1415926;
	return pi*r*r*h;
}

⌨️ 快捷键说明

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