char c[15] =
{'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'};
for(i = 0 ; i < 15 ; i++) cout<< up(c[i])<<\cout<< endl; }
3.编写主程序条用带实数r和整数n两个参数的函数并输出r的n次幂。 #include < iostream.h > #include < math.h > double power(double a, int b) {
int i;
double result = 1.0; for(i=0;i< b;i++) result = result * a; return result; }
void main() { double r; int n; cout<<\cin>>r; cout<<\cin>>n;
cout<< r<<\的\次幂是:\
}
4.编写有字符型参数C和整形参数N的函数,让他们显示出由字符C组成的三角形。其方
式为第1行有1个字符C,第2行有2个字符C ,等等。
#include < iostream > using namespace std; void print_triangle(char c, int n)
{ int i, j;
for(i=0; i< n; i++) {
for(j=0; j<=i; j++) { cout<< c; }
cout<< endl; } }
void main() {
print_triangle('a',10); }
5.编写一个ieqiu字符串长度的函数,strlen(),再用strlen()函数编写一个函数revers(s)
的倒序递归程序,使字符串s逆序。
#include < iostream > #include < string > using namespace std; int strlen(char *str) {
int len = 0;
while(str[len] != '\\0') { len++; }
return len; }
void revers(char *b) { char c; int j, len; len=strlen(b); j=len/2-1; while(j>=0) { c=*(b+j);
*(b+j)=*(b+len-j-1); *(b+len-j-1)=c; j--; }
b[len]='\\0'; }
void main()
{
char str[]={\
cout<< str<<\的长度:\cout<< str<< endl;//倒序前 revers(str);//
cout<< str<< endl;//倒序后 }
6.用函数模板实现3个数值中按最小值到最大值排序的程序。 #include < iostream > using namespace std; template
void sort(T a, T b, T c) { T array[3],temp; int i,j; array[0] = a; array[1] = b; array[2] = c; for(i=0;i<3;i++) {
for(j=0;j<2;j++) if(array[j]>array[j+1]) {
temp = array[j]; array[j] = array[j+1]; array[j+1] = temp;
} }
cout<< array[0]<< array[1]<< array[2]<< endl; }
void main() {
sort(5,1,9); }
7.利用函数模板设计一个求数组元素中和的函数,并检验之。 #include < iostream > using namespace std; template T sum (T a[],int n) { int i; T s=0;
for(i=0;i< n;i++) s = s + a[i]; return s; }
void main () {
int a[5]={1,2,3,4,5}; int s = sum(a,5); cout<< s<< endl; }
8.重载上题中的函数模板,使他能够进行两个数组的求和。 #include < iostream > using namespace std; template
自考c++程序设计 参考



