课本例题,选做例题3.5
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x=30,y=300,z=1024;
cout<<"Decimal:"<<'\t'
<<"x ="<<x<<"\t\t"
<<"y ="<<y<<"\t\t"
<<"z ="<<z<<endl;
cout<<"Octal:"<<"\t\t"
<<"x ="<<x<<"\t\t"
<<"y ="<<y<<"\t\t"
<<"z ="<<z<<endl;
cout<<"Hexadecimal:"<<'\t'<<hex
<<"x ="<<x<<"\t\t"
<<"y ="<<y<<"\t\t"
<<"z ="<<z<<endl;
cout<<uppercase;
cout<<"Hexadecimal:"<<'\t'<<hex
<<"x ="<<x<<"\t\t"
<<"y ="<<y<<"\t\t"
<<"z ="<<z<<endl;
cout<<nouppercase;
cout<<"Hexadecimal:"<<'\t'<<hex
<<"x ="<<x<<"\t\t"
<<"y ="<<y<<"\t\t"
<<"z ="<<z<<endl;
cout<<"Decimal:"<<'\t'<<dec
<<"x ="<<x<<"\t\t"
<<"y ="<<y<<"\t\t"
<<"z ="<<z<<endl;
return 0;
}
例3.7
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setfill('*')
<<setw(2)<<"OK"<<endl
<<setw(3)<<"OK"<<endl
<<setw(4)<<"OK"<<endl;
cout<<setfill(' ');
cout<<setw(5)<<"OK"<<endl;
return 0;
}
例3.8
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double test =22.0/7;
cout<<test<<endl;
cout<<setprecision(0)<<test<<endl
<<setprecision(1)<<test<<endl
<<setprecision(2)<<test<<endl
<<setprecision(3)<<test<<endl
<<setprecision(4)<<test<<endl;
cout<<"--------------"<<endl;
cout<<fixed<<setprecision(0)<<test<<endl;
cout<<setprecision(4)<<test<<endl;
cout<<resetiosflags(ios::fixed);
cout<<scientific<<test<<endl;
cout<<setprecision(6);
return 0;
}
老师,这里我能不能不用到控制符resetiosflags来取消定点数fixed,如果能,应该怎么做,,还有最后重新设置的默认输出值还是科学计数法啊,是不是应该再设一次fixed?
例题3.10.11
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double x=66,y=-8.246;
cout<<"x="<<x<<"\t\t"
<<"y="<<y<<endl;
cout<<showpoint<<showpos;
cout<<"x="<<x<<'\t'
<<"y="<<y<<endl;
return 0;
}
例3.20
#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"请输入X的值:";
cin>>x;
if(x!=0)
{
if(x>0)
y=1;
else
y=-1;
}
else
y=0;
cout<<"x="<<x<<"\t"<<"y="<<y<<endl;
return 0;
}
例3.21
#include<iostream>
using namespace std;
int main()
{
int weekday;
cout<<"输入0到6中的一个数(0表示星期日,1表示星期一,以此类推):";
cin>>weekday;
switch(weekday)
{
case 0:cout<<"Sunday"<<endl;break;
case 1:cout<<"Monday"<<endl;break;
case 2:cout<<"Tuesday"<<endl;break;
case 3:cout<<"Wednesday"<<endl;break;
case 4:cout<<"Thursday"<<endl;break;
case 5:cout<<"Friday"<<endl;break;
case 6:cout<<"Saturday"<<endl;break;
default:cout<<"error"<<endl;
}
return 0;
}
习题5
#include<iostream>
using namespace std;
int main()
{
long double e=1;
double a=1,b;
int n=1,i;
while(!(b<1e-6));
{
for(i=1;i<=n;i++)
a*=i;
b=1.0/a;
e+=b;
n++;
}
cout<<"e="<<e<<endl;
return 0;
}
为什么输出的结果会是2啊,晕晕.......
习题6
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double a=1,b=1;
double y=1,c,d,pi;
do
{
a+=2;
b*=-1;
c=1.0/a;
d=b*c;
y+=d;
}while(c>1e-6);
pi=4*y;
cout<<"圆周率的近似值pi="<<pi<<endl;
return 0;
}
习题7
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"请输入一个数=";
cin>>a;
if(a<10)
cout<<a<<"小于10"<<endl;
else if(10<=a&&a<100)
cout<<a<<"is10to100"<<endl;
else if(100<=a&&a<1000)
cout<<a<<"is100to1000"<<endl;
else if(a>=1000)
cout<<a<<"大于1000"<<endl;
return 0;
}
为什么不能直接写10<=a<100呢
习题8
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(4)<<"*"<<endl
<<setw(5)<<"***"<<endl
<<setw(6)<<"*****"<<endl
<<setw(7)<<"*******"<<endl
<<setw(6)<<"*****"<<endl
<<setw(5)<<"***"<<endl
<<setw(4)<<"*"<<endl;
return 0;
}
习题9
#include<iostream>
using namespace std;
int main()
{
int n,b,s=0;
for(n=1;s<=1000;n++)
{
b=n*n;
s+=b;
}
cout<<"n的最大值="<<n-2<<endl;
return 0;
}
习题10
#include<iostream>
using namespace std;
int main()
{
int s1,day=1;
double i,s2=0;
s1=30*10000;
for(i=0.01;day<=30;day++)
{
s2+=i;
i*=2;
}
cout<<"富翁给陌生人的钱s1="<<s1<<endl;
cout<<fixed<<"陌生人给富翁的钱s2="<<s2<<endl;
return 0;
}
习题11
#include<iostream>
using namespace std;
int main()
{
int i,j,s;
for(j=1;j<=9;j++)
{
for(i=1;i<=j;i++)
{
s=i*j;
cout<<i<<"*"<<j<<"="<<s<<"\t";
}
cout<<endl;
}
return 0;
}
2.编程求“百钱百鸡”问题。(鸡翁一值钱五,鸡母 一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?)
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
for(a=0;a*5<=100;a++)
for(b=0;(a*5+b*3)<=100;b++)
{
c=100-a-b;
if((a*5+b*3+c/3==100)&&(c%3==0))
cout<<"鸡翁有"<<a<<"只"
<<"鸡母有"<<b<<"只"
<<"鸡雏有"<<c<<"只"<<endl;
}
return 0;
}
未完待续...