1.完成课本每一个编程题。要求先画出流程算法图或N-S图,然后编程实现,有可能的话使用两种以上方法;
2.编程求“百钱百鸡”问题。(鸡翁一值钱五,鸡母 一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?)
3.编程输入一个整数,计算它是几位数字,分别输出每一位数字,并输出各个数位上数字之和。
4.在一个平面上,有四个圆柱形塔,塔底圆心坐标分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),塔半径为1,塔高为10米,塔外无建筑物。编程,输入任一个点平面坐标,求该点建筑物的高度。
5.编程计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。
例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"<<oct <<"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<<setiosflags(ios::uppercase); cout<<"Hexadecimal:"<<'\t' <<"x="<<x<<"\t\t" <<"y="<<y<<"\t\t" <<"z="<<z<<endl; cout<<resetiosflags(ios::uppercase); cout<<"Hexadecimal:"<<'\t' <<"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.6
#include<iostream> #include<iomanip> using namespace std; int main() { int a=10; int b=1000; cout<<setw(5)<<a<<endl <<setw(2)<<b<<endl; return 0; }
例3.7
例3.8# 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.9#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<<setiosflags<<(ios::fixed)<<setprecision(0)<<test<<endl; cout<<setprecision(4)<<test<<endl; cout<<resetiosflags(ios::fixed); cout<<setiosflags(ios::scientific)<<test<<endl; cout<<setprecision(6); return 0; }
例题3.10#include<iostream> #include<iomanip> using namespace std; int main() { cout<<right <<setw(5)<<-1 <<setw(5)<<2 <<setw(5)<<3<<endl; cout<<left <<setw(5)<<-1 <<setw(5)<<2 <<setw(5)<<3<<endl; cout<<internal <<setw(5)<<-1 <<setw(5)<<2 <<setw(5)<<3<<endl; return 0; }
#include<iostream> #include<iomanip> using namespace std; int main() { double x=66,y=-8.246; cout<<"x="<<x<<"\t\t" <<"y="<<y<<endl; cout<<setiosflags(ios::showpoint); cout<<"x="<<x<<'\t' <<"y="<<y<<endl; return 0; }