实验7指针的应用 【实验目的】 1掌握指针的使用方法 2掌握引用的使用方法 3学习 const修饰符在指针中的使用方法。 4学习指针在数组中的使用方法 5掌握指针数组的使用方法。 6学习指针在函数中的使用方法。 7学习使用指针处理字符串的方法 8学习使用new和 delete动态分配内存 【实验内容 1.编程输出二维数组a[2}={122,344,55}各数,并输出该数组的每行的首地址 及每个元数的地址 #include <iostream. h> void mainO {inta2}={11,22,33,44,55}; int i,j, "p3=a[0k a[lls a2 cou←<"数组a每行的首地址为:mn"; p< cout<<endl: cou←<"数组a每个元素的地址为:mn" for(i=0;<3;i++) forg=0;j<2; j+ cout<<*(p+i)+j<<""; cout<<endI; cou←<"数组a每个元素的值为:n"; for(i=0;i<3;i++) {for(=02j++)cou<<*(p+i)+j)<""; cout<<end; 2使用指向指针的指针输出计算机课程名(" Visual basic"," Visual c++"," Delph", Power Build", Visual Foxpro") #include <iostream. h> i char namely Visual Basic",Visual C++,Delphi Power Build",Visual Foxpro; for(int F0; i<5; i++) cout<<"(p+i<<endl; 3.从键盘输入一个整数,当该数为偶数时,用函数的指针调用函数sum1输出其不 大于该数的偶数和;当该数为奇数时,用函数的指针调用函数sum2输出其不大 于该数的奇数和
实验 7 指针的应用 【实验目的】 1 掌握指针的使用方法。 2 掌握引用的使用方法。 3 学习 const 修饰符在指针中的使用方法。 4 学习指针在数组中的使用方法。 5 掌握指针数组的使用方法。 6 学习指针在函数中的使用方法。 7 学习使用指针处理字符串的方法。 8 学习使用 new 和 delete 动态分配内存。 【实验内容】 ⒈编程输出二维数组 a[][2]={11,22,33,44,55 }各数,并输出该数组的每行的首地址 及每个元数的地址。 #include <iostream.h> void main() { int a[][2]={11,22,33,44,55}; int i,j,*p[3]={a[0],a[1],a[2]}; cout<<"数组 a 每行的首地址为:\n"; for(i=0;i<3;i++) cout<<p[i]<<" "; cout<<endl; cout<<"数组 a 每个元素的地址为:\n"; for(i=0;i<3;i++) {for(j=0;j<2;j++) cout<<*(p+i)+j<<" "; cout<<endl; } cout<<endl; cout<<"数组 a 每个元素的值为:\n"; for(i=0;i<3;i++) { for(j=0;j<2;j++) cout<<*(*(p+i)+j)<<" "; cout<<endl; } } ⒉使用指向指针的指针输出计算机课程名("Visual Basic","Visual C++", "Delphi", "Power Build","Visual Foxpro") #include <iostream.h> void main() { char *name[]={"Visual Basic","Visual C++","Delphi", "Power Build","Visual Foxpro"}; char **p =name; for(int i=0;i<5;i++) cout<<*(p+i)<<endl; } ⒊从键盘输入一个整数,当该数为偶数时,用函数的指针调用函数 sum1 输出其不 大于该数的偶数和;当该数为奇数时,用函数的指针调用函数 sum2 输出其不大 于该数的奇数和;
#include <iostream. h> int suml(int) int sum2(int) void mainO nt (f(int) cout<<Input a data: cin>>a if(a%2==0) df=suml; cout<<(“fa)<endl;} df=sum2; cout<<(f(a<<end; int suml(int m) fint sI=0; for(int F2; i<=m; i=i+2) =S return sl int sum2(int n) d int s2=0; for(int Fl; i<=n; ii+2) s2=s2+i; return s2 4编写一个程序判定一个字符在一个字符串中出现的次数,如果该字符不出现则返 回0 #include <iostream.h> int count(char*s, char letter i int count=0; while(s) if(s++=letter) count++; return(count);) char str[100l, cout<<"input a string: " cout<<" input a letter: cout<<"the count is: < count(str ck<<endl; 5自定义函数实现字符串的拷贝,并通过主函数调用验证 #includesiostream.h> char copy string(char, const char*); maIn
#include <iostream.h> int sum1(int); int sum2(int); void main() { int a; int (*f)(int); cout<<"Input a data:"; cin>>a; if(a%2==0) { f=sum1; cout<<(*f)(a)<<endl;} else { f=sum2; cout<<(*f)(a)<<endl;} } int sum1(int m) { int s1=0; for(int i=2;i<=m;i=i+2) s1=s1+i; return s1;} int sum2(int n) { int s2=0; for(int i=1;i<=n;i=i+2) s2=s2+i; return s2;} ⒋编写一个程序判定一个字符在一个字符串中出现的次数,如果该字符不出现则返 回 0。 #include <iostream.h> int count(char *s,char letter) { int count=0; while(*s) if(*s++==letter) count++; return (count); } void main() {char str[100],c; cout<<"input a string:"; cin>>str; cout<< "input a letter:"; cin>>c; cout<<"the count is:"<< count(str,c)<<endl;} ⒌自定义函数实现字符串的拷贝,,并通过主函数调用验证。 #include<iostream.h> char *copy_string(char*,const char*); void main()
char sll"i am a student " i 1); copy_ string(s2, sl cout<<sl="<<sl<<end: cout<<"s2=<<s2<<endl; har copy string(char to, const char*from) i char*temp= for( "from=\0;from++, to++) *to=*from return temp;
{char s1[]="I am a student "; char s2[20]; copy_string(s2,s1); cout<<"s1="<<s1<<endl; cout<<"s2="<<s2<<endl;} char *copy_string(char *to,const char *from) { char *temp=to; for(;*from!='\0';from++,to++) *to=*from; *to='\0'; return temp; }