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

📄 程序1.txt

📁 C语言高级编程的三个实验DOC和PPT文件。包括源码。详细参见文档说明。
💻 TXT
字号:
********************1.cpp********************
#include <iostream.h>
#include <iomanip.h>

void main()
{
	int i;
	//cout<<setiosflags(ios::right);
	cout<<"\tN\t"<<"10*N\t"<<"100*N\t"<<"2000*N"<<endl;
	for(i=1;i<=5;i++)
		cout<<"\t"<<i<<"\t"<<10*i<<"\t"<<100*i<<"\t"<<2000*i<<endl;
}
结果:

        N       10*N    100*N   2000*N
        1       10      100     2000
        2       20      200     4000
        3       30      300     6000
        4       40      400     8000
        5       50      500     10000
Press any key to continue
********************2.cpp********************
#include <iostream.h>
#include <iomanip.h>
#include <math.h>

double calculateCharges(double time);

void main()
{
	double time[3],ttime;
	double cost[3],ccost;
	cout<<"please input the first time:";
	cin>>time[0];
	cout<<"please input the second time:";
	cin>>time[1];
	cout<<"please input the third time:";
	cin>>time[2];
	ttime=time[0]+time[1]+time[2];
	cost[0]=calculateCharges(time[0]);
	cost[1]=calculateCharges(time[1]);
	cost[2]=calculateCharges(time[2]);
	ccost=cost[0]+cost[1]+cost[2];
	cout<<setw(8)<<"Car"<<setw(8)<<"Hours"<<setw(8)<<"Charge"<<endl;
	for(int i=0;i<=2;i++)
	{
		cout<<setw(8)<<i<<setiosflags(ios::fixed)
			<<setw(8)<<setprecision(1)<<time[i]
			<<setw(8)<<setprecision(2)<<cost[i]<<endl;
	}
	cout<<setw(8)<<"TOTAL"
		<<setw(8)<<setprecision(1)<<ttime
		<<setw(8)<<setprecision(2)<<ccost<<endl;	
}

double calculateCharges(double time)
{
	double cost=0;
	if(time<=3)
		cost=2;
	else
		if(time>3)
			cost=(2+(time-3)*0.5<10?2+(time-3)*0.5:10);
	return(cost);
}
结果:
please input the first time:1.5
please input the second time:4.0
please input the third time:24.0
     Car   Hours  Charge
       0     1.5    2.00
       1     4.0    2.50
       2    24.0   10.00
   TOTAL    29.5   14.50
Press any key to continue
********************3.cpp********************
#include <iostream.h>
#include <iomanip.h>

int gcd(int,int);

void main()
{
	int x,y;
	cout<<"please input two integers:"<<endl;
	cin>>x>>y;

	cout<<gcd(x,y)<<endl;
}

int gcd(int x,int y)
{
	if (x==0)
		return(y);
	else 
		if(y==0)
			return(x);
		else
			return(gcd(y,x%y));
}
结果:
please input two integers:
56 96
8
Press any key to continue
********************4.cpp********************
#include <iostream.h>
#define PI 3.1415926

double radius;

inline void circleArea()
{
	cout<<"please input the radius of the circle:"<<endl;
	cin>>radius;
}

void main()
{
	double area;
	circleArea();
	area=PI*radius*radius;
	cout<<"the area of the circle is: "<<area<<endl;
}
结果:
please input the radius of the circle:
35
the area of the circle is: 3848.45
Press any key to continue
********************5.cpp********************
#include <iostream.h>

double tripleByReference1(double);
double tripleByReference2(double&);

void main()
{
	double count;
	double& rcount=count;
	cout<<"please input the value of count:"<<endl;
	cin>>count;
	
	cout<<tripleByReference1(count)<<endl;
	cout<<tripleByReference2(rcount)<<endl;
}

double tripleByReference1(double x)
{
	return(3*x);
}

double tripleByReference2(double& x)
{
	return(3*x);
}//不能重载!
结果:
please input the value of count:
246
738
738
Press any key to continue
********************7.cpp********************
#include <iostream.h>

template<class T>
T min(T a,T b)
{
	return a<b?a:b;
}

void main()
{
	cout<<"min(3,5) is "<<min(3,5)<<endl;
	cout<<"min(3.0,5.0) is "<<min(3.0,5.0)<<endl;
	cout<<"min('3','5') is "<<min('3','5')<<endl;
}
结果:
min(3,5) is 3
min(3.0,5.0) is 3
min('3','5') is 3
Press any key to continue

⌨️ 快捷键说明

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