- 学习要求:
理解指针、地址、指针类型等相关概念。
掌握指针变量的定义、初始化、访问、指针运算和表达式。
掌握指针和数组、字符串的联系,灵活使用指针来处理它们。
掌握函数的传址调用。
掌握指针和数组作为函数参数。
- 实验作业
1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
2.编程实现课本每一个编程习题。
3. 有n个人围成一个圆圈,从第一个人开始报数,从1到3循环,凡是报3的退出圈子,问最后退出圈子的人是原来的几号。
4.一个班有5个学生,每一个学生有几门成绩(到底是几门自已定,或3或4或5或6),定义函数来完成下面功能:
计算第2门课成的平均分;
找出有2门以上成绩不及格的学生,并输出其学号和全部课程成绩;
找出平均成绩在90以上或者全部课程在85分以上的学生。
- 作业要求
网上提交各个程序和相关要求的文字。作业不能拷贝。
/****************************
*** 例6.1 ***
****************************/
#include<iostream>
using namespace std;
int main()
{
int a =10;
int *p;
p=&a;
cout<<"a = "<<a<<endl;
cout<<"p = "<<p<<endl;
cout<<"&a ="<<&a<<endl;
cout<<"*p ="<<*p<<endl;
cout<<"&p ="<<&p<<endl;
cout<<endl;
*p =15;
cout<<"a = "<<a<<endl;
cout<<"p = "<<p<<endl;
cout<<"&a = "<<&a<<endl;
cout<<"*p ="<<*p<<endl;
cout<<"&p ="<<&p<<endl;
return 0;
}
/*************************
*** 例题6.2 ***
*************************/
#include<iostream>
using namespace std;
int main()
{
int a,b;
int *p=&a,*q =&b, *tp;
cout<<"请输入两个数:";
cin>>a>>b;
if(a<b)
{
tp =p; p= q; q= tp;
}
cout<<"初始的两个数为:"<<a<<" "<<b<<endl;
cout<<"从大到小排序后的数为:"<<*p<<" "<<*q<<endl;
return 0;
}
/****************************
** 例题6.3 **
*****************************/
#include<iostream>
using namespace std;
int main()
{
int a,b,t;
int *p =&a,*q=&b;
cout<<"请输入a和b的值:";
cin>>a>>b;
cout<<"初始的两个数分别为:"<<a<<" "<<b<<endl;
if(a<b)
{
t =*p;*p=*q; *q=t;
}
cout<<"从大到小排序后的数为:"<<*p<<" "<<*q<<endl;
return 0;
}
/*********************************
*** 例题6.4 ***
*********************************/
#include<iostream>
using namespace std;
int main()
{
int a = 10, b=19,*p1,*p2;
p1 =&a;
p2 =&b;
cout<<boolalpha<<(*p1 == *p2)<<endl;
cout<<boolalpha<<(p1 == p2)<<endl;
return 0;
}
/*******************************
** 例题6.5 **
*******************************/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[10];
for(int i =0;i<10;i++)
a[i]=2*(i+1);
for(i = 0;i<10;i++)
cout<<setw(4)<<a[i];
cout<<endl;
return 0;
}
/*****************************
*** 例题6.6 ***
*****************************/
#include<iostream>
using namespace std;
int main()
{
char a[]="Hello world", b[20], *p, *q;
p= a, q = b;
for( ; *p!='\0';p++,q++)
*q = *p;
*q ='\0';
cout<<"string1 is:";
cout<<a<<endl;
cout<<"string2 is:";
cout<<b<<endl;
return 0;
}
/************************
** 例题6.9 **
************************/
#include<iostream>
using namespace std;
int main()
{
char s[50];
char *p;
cout<<"Please input a string:";
cin.get(s,50);
p = s;
for( ;*p != '\0';)
p++;
cout<<"the length is : "<<p-s<<endl;
return 0;
}