貌似网上找这个答案的很多,但还未见到完全版的……
最近没事我也在看这本书,顺便贴点后面习题的答案,贴上的代码都是在VC6中调试通过的。
2-5:
#include "stdafx.h"
#include <iostream>using namespace std;
//用"*"字符构成长方形
int main(int argc, char* argv[])
{
cout<<"请输入长方形的长和宽(大于0的整数):";int chang,kuan;
cin>>chang>>kuan;if (chang <= 0 || kuan <= 0)
{
cout<<"长和宽必须为大于0的整数!"<<endl;
return -1;
}unsigned int row = 1;
while (row <= kuan)
{
for (int lie=1;lie <= chang;lie++)
{
if (row == 1 || row == kuan)
{
cout<<"* ";
}
else
{
if (lie == 1 || lie ==chang)
{
cout<<"* ";
}
else
{
cout<<" ";
}
}
}
row++;
cout<<endl;
}
return 0;
}
已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/310.html 欢迎订阅Eddy Blog。
Tags:Accelerated C 习题答案