当前位置:文档之家› C++上机实验报告(指针)

C++上机实验报告(指针)

C++上机实验报告实验名称:指针专业班级:姓名:学号:实验日期:目录1.实验目的2.实验内容3.程序代码4.调试结果5.实验心得1.实验目的(1)通过实验进一步掌握指针的概念,会定义和使用指针变量;(2)能正确使用数组的指针和指向数组的指针变量;(3)能正确使用字符串的指针和指向字符串的指针变量;(4)能正确使用引用型变量。

2.实验内容编程序并上机调试运行程序(要求用指针或引用处理)。

(1)输入3个整数,按由小到大的顺序输出。

编译一个程序,用指针变量作为参数。

(2)在上题的基础上将程序改为:输入3个字符串,按由小到大的顺序输出。

(3)用引用指针变量作为形参,实现3个整数由小到大输出。

(4)有n个人围成一圈,顺序排号。

从第1个人开始报数(从1~3报数),凡是到3的人退出圈子,问最后留下的人原来排在第几号。

(5)在主函数中输入10个字符串。

用另一个函数最它们排序。

然后在主函数输出这10个已排好的字符串。

要求用以下方法编程:Ⅰ.指向一维数组的指针座函数参数;Ⅱ.用string数组方法。

3.程序代码(1)#include<iostream>using namespace std;int main(){void swap(int *p1,int *p2);int n1,n2,n3;int *p1,*p2,*p3;cout<<"input three integers n1,n2,n3:";cin>>n1>>n2>>n3;p1=&n1;p2=&n2;p3=&n3;if(n1>n2) swap(p1,p2);if(n1>n3) swap(p1,p3);if(n2>n3) swap(p2,p3);cout<<"Now,the order is:"<<n1<<" "<<n2<<" "<<n3<<endl; return 0;}void swap(int *p1,int *p2){int p;p=*p1;*p1=*p2;*p2=p;}(2)Ⅰ.用字符数组方法的源程序#include<iostream>using namespace std;int main(){void swap(char *,char *);char str1[20],str2[20],str3[30];cout<<"input three line:"<<endl;gets(str1);gets(str2);gets(str3);if(strcmp(str1,str2)>0) swap(str1,str2); if(strcmp(str1,str3)>0) swap(str1,str3); if(strcmp(str2,str3)>0) swap(str2,str3); cout<<endl<<"Now,the order is:"<<endl; cout<<str1<<endl<<str2<<endl<<str3<<endl; return 0;}void swap(char *p1,char *p2){char p[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);}Ⅱ.用string方法的源程序(程序中使用了指针和引用)#include<iostream>#include<string>using namespace std;int main(){void change(string &,string &);string str1=" ",str2=" ",str3=" ";char *p1=&str1[0],*p2=&str2[0],*p3=&str3[0];cout<<"input three line:"<<endl;gets(p1);gets(p2);gets(p3);if(str1>str2) change(str1,str2);if(str1>str3) change(str1,str3);if(str2>str3) change(str2,str3);cout<<endl<<"Now,the order is:"<<endl;cout<<str1<<endl<<str2<<endl<<str3<<endl; return 0;}void change(string &st1,string &st2) {string st;st=st1;st1=st2;st2=st;}(3)#include<iostream>using namespace std;int main(){void exchange(int *,int *,int *);int a,b,c,*p1,*p2,*p3;cin>>a>>b>>c;p1=&a;p2=&b;p3=&c;exchange(p1,p2,p3);cout<<a<<" "<<b<<" "<<c<<endl;return 0;}void exchange(int *q1,int *q2,int *q3){void swap(int *,int *);if( *q1> *q2) swap(q1,q2);if( *q1> *q3) swap(q1,q3);if( *q2> *q3) swap(q2,q3);}void swap(int *pt1,int *pt2){int temp;temp=*pt1;*pt1=*pt2;*pt2=temp;}(4)#include<iostream>using namespace std;int main(){int i,k,m,n,num[50],*p;cout<<"input number of person:n="; cin>>n;p=num;for(i=0;i<n;i++)*(p+i)=i+1;i=0;k=0;m=0;while(m<n-1){if(*(p+i)!=0) k++;if(k==3){*(p+i)=0;k=0;m++;}i++;if(i==n) i=0;}while(*p==0)p++;cout<<"The last one is NO."<<*p<<endl; return 0;}(5)Ⅰ.指向一维数组的指针最函数参数的源程序#include<iostream>using namespace std;int main(){void sort(char (*p)[6]);int i;char str[10][6];char (*p)[6];cout<<"input 10 strings:"<<endl;for(i=0;i<10;i++)cin>>str[i];p=str;sort(p);cout<<"Now,the sequence is:"<<endl; for(i=0;i<10;i++)cout<<str[i]<<endl;return 0;}void sort(char (*s)[6]){int i,j;char temp[6],*t=temp;for(i=0;i<9;i++)for(j=0;j<9-i;j++)if(strcmp(s[j],s[j+1])>0){strcpy(t,s[j]);strcpy(s[j],s[+j+1]);strcpy(s[j+1],t);}}Ⅱ.用string数组方法的源程序#include<iostream>#include<string>using namespace std;int main(){void sort(string*);int i;string str[10],*p=str;cout<<"input 10 strings:"<<endl;for(i=0;i<10;i++)cin>>str[i];sort(p);cout<<"Now,the sequence is:"<<endl; for(i=0;i<10;i++)cout<<str[i]<<endl;return 0;}void sort(string *s){int i,j;string temp;for(i=0;i<9;i++)for(j=0;j<9-i;j++)if(s[j]>s[j+1]){temp=s[j];s[j]=s[+j+1];s[j+1]=temp;}}4.调试结果(1)input three integers n1,n2,n3:54 -12 2 Now,the order is:-12 2 54(2)input three lines:I study very hard.C language is very interesting.He is a professor.Now,the order is:C language is very interesting.He is a professor.I study very hard.(3)12 6 876 12 87(4)input number of person:n=8The last one is No.7(5)input 10 strings: Now,the sequence is:China BurmaJapan ChinaKorea EgyptEgypt GhanaNepal ItalyBurma JapanGhana KoreaSudan LibyaItaly NepalLibya Sudan5.实验心得指针同数组一样,是C++中一个重要的概念,一个重要的知识点。

相关主题