运行语句第三章程序设计初步

发一下牢骚和主题无关:

//p55
//各行小数点对齐
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	double a=123.456,b=3.14159,c=-3214.67;
	cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)
		<<setprecision(2);
	cout<<setw(10)<<a<<endl;
	cout<<setw(10)<<b<<endl;
	cout<<setw(10)<<c<<endl;
	return 0;
}

    运行结果:

    

//p56
//用 putchar 函数停止符字的输出
#include <iostream>
using namespace std;
int main()
{
	char a,b,c;
	a='B';
	b='O';
	c='Y';
	putchar(a);
	putchar(b);
	putchar(c);
	putchar('\n');
	putchar(66);
	putchar(79);
	putchar(89);
	putchar('\n');
	return 0;
}

    运行结果:

    

//p57
//用 getchar 函数停止符字的输入
#include <iostream>
using namespace std;
int main()
{
	char c;
	c=getchar();
	putchar(c+32);
	putchar('\n');
	return 0;
}

    运行结果:

    

//p58
//求一元二次方程ax*x+b*x+c=0的根
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	float a,b,c,x1,x2;
	cin>>a>>b>>c;
	x1=(-b+sqrt(b*b-4*a*c))/(2*a);
	x2=(-b-sqrt(b*b-4*a*c))/(2*a);
	cout<<"x1="<<x1<<endl;
	cout<<"x2="<<x2<<endl;
	return 0;
}

    运行结果:

    

//p67
//求三角形的面积
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double a,b,c;
	cout<<"Please enter a,b,c:"<<endl;
	cin>>a>>b>>c;
	if(a+b>c&&b+c>a&&a+c>b)
	{
		double s,area;
		s=(a+b+c)/2;
		area=sqrt(s*(s-a)*(s-b)*(s-c));
		cout<<setiosflags(ios::fixed)<<setprecision(4);
		cout<<"area="<<area<<endl;
	}
	else		
		cout<<"It is not a trilateral!"<<endl;
	return 0;
}

    运行结果:

    

//p68
//断判输入的符字否是为大写字母,若是转换为小写,否则不转换
#include <iostream>
using namespace std;
int main()
{
	char ch;
	cin>>ch;
	ch=(ch>='A'&&ch<='Z')? (ch+32):ch;
	cout<<ch<<endl;
	return 0;
}

    运行结果:

    

//p71
//断判否是为闰年
#include <iostream>
using namespace std;
int main()
{
	int year;
	bool leap;
	cout<<"Please enter year:"<<endl;
	cin>>year;
	if(year%4==0)
	{
		if(year%100==0)
		{
			if(year%400==0)
				leap=true;
			else
				leap=false;
		}
		else
			leap=true;
	}
	else
		leap=false;
	if(leap)
		cout<<year<<" is ";
	else
		cout<<year<<" is not ";
	cout<<"a leap year"<<endl;
	return 0;
}

    运行结果:

    

    每日一道理
风,渐渐吹起,吹乱了我的发丝,也让我的长裙有些飘动。绿叶仿佛在风中起舞,离开了树,投向了大地,却不知这样会枯萎,我弯下腰,轻轻拾起一片树叶,那非常有序的茎脉,是一种美的点缀。我有些哀叹:绿叶啊,绿叶,你这般美丽地从树上轻轻飘下,随风起舞,却不知已被人称之为落叶!
//p71
//应用switch语句算计费运
#include <iostream>
using namespace std;
int main()
{
	int c,s;
	float p,w,d,f;
	cout<<"Please enter p,w,s:"<<endl;
	cin>>p>>w>>s;
	if(s>=3000)
		c=12;
	else
		c=s/250;
	switch(c)
	{
	case 0: d=0;break;
	case 1: d=2;break;
	case 2:
	case 3: d=5;break;
	case 4:
	case 5:
	case 6:
	case 7: d=8;break;
	case 8:
	case 9:
	case 10:
	case 11: d=10;break;
	case 12: d=15;break;
	}
	f=p*w*s*(1-d/100.0);
	cout<<"freight="<<f<<endl;
	return 0;
}

    运行结果:

    

//p74
//用while语句求1到100的和
#include <iostream>
using namespace std;
int main()
{
	int i=1,sum=0;
	while(i<=100)
	{
		sum=sum+i;
		i++;
	}
	cout<<"sum="<<sum<<endl;
	return 0;
}

    运行结果:

    

//p75
//用do while语句求1到100的和
#include <iostream>
using namespace std;
int main()
{
	int i=1,sum=0;
	do
	{
		sum=sum+i;
		i++;
	}while(i<=100);
	cout<<"sum="<<sum<<endl;
	return 0;
}

    运行结果:

    

//p81
//用公式PI/4=1-1/3+1/5-1/7…求PI,直到最后一项的绝对值小于10的-7次方为止
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
	int s=1;
	double n=1,t=1,pi=0;
	while(fabs(t)>1e-7)
	{
		pi=pi+t;
		n=n+2;
		s=-s;
		t=s/n;
	}
	pi=pi*4;
	cout<<"pi="<<setiosflags(ios::fixed)<<setprecision(6)<<pi<<endl;
	return 0;
}

    运行结果:

    

//p82
//求Fibonacci数列的前40个数,F1=1(n=1),F2=1(n=2),Fn=Fn-1+Fn-2(n>=3)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	long f1,f2;
	int i;
	f1=f2=1;
	for(i=1;i<=20;i++)
	{
		cout<<setw(12)<<f1<<setw(12)<<f2;
		if(i%2==0)
			cout<<endl;
		f1=f1+f2;
		f2=f2+f1;
	}
	return 0;
}

    运行结果:

    

//p83
//输出100到200之间的素数
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	int m,k,i,n=0;
	bool prime;
	for(m=101;m<=200;m=m+2)
	{
		prime=true;
		k=int(sqrt(m));
		for(i=2;i<=k;i++)
		{
			if(m%i==0)
			{
				prime=false;
				break;
			}
		}
		if(prime)
		{
			cout<<setw(5)<<m;
			n=n+1;
		}
		if(n%10==0)
			cout<<endl;
	}
	cout<<endl;
	return 0;
}

    运行结果:

    

//p84
//译密码
#include <iostream>
using namespace std;
int main()
{
	char c;
	while((c=getchar())!='\n')
	{
		if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
		{
			c=c+4;
			if(c>'Z'&&c<='Z'+4||c>'z')
				c=c-26;
		}
		cout<<c;
	}
	cout<<endl;
	return 0;
}

    运行结果:

    

文章结束给大家分享下程序员的一些笑话语录: IBM和波音777
  波音777是有史以来第一架完全在电脑虚拟现实中设计制造的飞机,所用的设备完全由IBM公司所提供。试飞前,波音公司的总裁非常热情的邀请IBM的技术主管去参加试飞,可那位主管却说道:“啊,非常荣幸,可惜那天是我妻子的生日,So..”..
  波音公司的总载一听就生气了:“胆小鬼,我还没告诉你试飞的日期呢!”

posted @ 2013-04-27 19:54  xinyuyuanm  阅读(203)  评论(0编辑  收藏  举报