您的位置:首页 > 其它

第三章 程序设计初步

2013-04-27 15:57 435 查看
//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;
}
运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: