#include <iostream> #include <string> using namespace std; int main() { cout<<"input your name:"; string name; cin>>name; cout<<endl<<endl; const string greet="Hello, "+name+", I am sorry!"; const int pad=1; const int row=pad*2+3; const string::size_type col=greet.size()+pad*2+2; for(int r=0;r!=row;++r) { string::size_type c=0; while(c!=col) { if(r==pad+1 && c==pad+1) { cout<<greet; c+=greet.size(); } else { if(r==0||r==row-1||c==0||c==col-1) { cout<<"*"; } else { cout<<" "; } ++c; } } cout<<endl; } return 0; }lyj@qt:~/Desktop$ g++ 2-0.cpp -o 2-0
2-1. Change the framing program so that it writes its greeting with no separation from the frame.
Ans:要求输出与框架没有间隔的问候语,我们将2-0程序中pad设置为0,即可。
2-2. Change the framing program so that it uses a different amount of space to separate the sides from the greeting than it uses to separate the top and bottom borders from the greeting.
Ans:修改2-0的pad即可。
2-3. Rewrite the framing program to ask the user to supply the amount of spacing to leave between the frame and the greeting.
Ans:同2-0,仅将const int pad=1,修改为int pad; cin>>pad;即可。
2-4. The framing program writes the mostly blank lines that separate the borders from the greeting one character at a time. Change the program so that it writes all the spaces needed in a single output expression.
#include <iostream> #include <string> using namespace std; int main() { cout<<"input your name:"; string name; cin>>name; cout<<endl<<endl; const int pad=3; const string space(pad,' '); const string greet=space+"Hello, "+name+", I am sorry!"+space; const string ps(greet.size(),' '); const int row=pad*2+3; const string::size_type col=greet.size()+2; for(int r=0;r!=row;++r) { string::size_type c=0; while(c!=col) { if(r==pad+1 && c==1) { cout<<greet; c+=greet.size(); } else { if(r==0||r==row-1||c==0||c==col-1) { cout<<"*"; ++c; } else if(c==1) { cout<<ps; c+=ps.size(); } } } cout<<endl; } return 0; }lyj@qt:~/Desktop$ g++ 2-0.cpp -o 2-0
2-5. Write a set of "*" characters so that they form a square, a rectangle, and a triangle.正方形和长方形较为简单,仅仅给出三角形示例。
#include <iostream> #include <string> using namespace std; int main() { int r,c; cout<<"the r*c you want:"; cin>>r>>c; for(int i=0;i!=r;++i) { if(i<=c) { const string sp(c-i,' '); const string star(i,'*'); cout<<sp<<star<<endl; } } return 0; }
}
Ans:输出1,2,3,4,5,6,7,8,9,10
2-7. Write a program to count down from 10 to -5 .
#include <iostream> using namespace std; int main() { signed int n=11; while(n>-5) { n-=1; cout<<n<<endl; } return 0; }
lyj@qt:~/Desktop$ g++ 2.cpp -o 2
lyj@qt:~/Desktop$ ./2
10
9
8
7
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
lyj@qt:~/Desktop$
2-8. Write a program to generate the product of the numbers in the range [1, 10).
#include <iostream> using namespace std; int main() { cout<<"9*9 Table"<<endl<<endl; for(int i=1;i<=9;++i) { for(int j=1;j<=i;++j) { cout<<i<<"*"<<j<<"="<<i*j<<'\t'; } cout<<endl; } return 0; }lyj@qt:~/Desktop$ g++ 3.cpp -o 3
#include <iostream> using namespace std; int main() { cout<<"input m,n:"; int m,n; cin>>m>>n; cout<<endl; if(m>n) cout<<"the max is: "<<m; else cout<<"the max is: "<<n; return 0; }也可以用:cout<<(m>n?m:n)<<endl;代替上述if语句。
}
Ans:cout,cin,endl,string等都来自名称空间std,限定名std::cout指明cout来自std名称空间,之后使用cout即表示std::cout,从而使得程序较为简洁;注意作用域,有效范围从{开始,到}结束。