问题:根据main函数中对printchs函数的调用,以及printchs的功能要求,编写printchs函数。
图案:
#include <iostream>
using namespace std;
//在下面写printchs函数的定义,功能是输出一行若干个指定字符
int main()
{
int n=6; //n代表要输出的行数
int i;
//通过在下面的循环里调用printchs,输出上面的星号图
for(i=1; i<=n; ++i)
{
printchs(n-i,' ');
printchs(2*i-1,'*');
cout<<endl;
}
return 0;
}
答案:
/*
*Copyright (c) 2014,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:曾建强
*完成时间:2014年11月6日
*版本号:v1.0
*/
#include <iostream>
using namespace std;
void printchs(int a,char b)
{
for (int j=1;j<=a;++j)
cout<<b;
}
int main()
{
int n=6;
int i;
//通过在下面的循环里调用printchs,输出上面的星号图
for(i=1; i<=n; ++i)
{
printchs(n-i,' ');
printchs(2*i-1,'*');
cout<<endl;
}
return 0;
}
运行结果: