11C第七章函数与指针2.pptVIP

  • 9
  • 0
  • 约3.78千字
  • 约 41页
  • 2017-04-27 发布于四川
  • 举报
11C第七章函数与指针2

计算机程序设计;授 课 内 容 ;7.1 指针与函数;一. 指针作为函数的参数;二. 返回指针的函数;例 3 将月份数值(1-12)转换为相应的英文名称; 函数原型:char *strchr(char *str, char c); 算法: 从str的第一个字符查起,查到字符后就返回该字符的地址;查不到则返回空值NULL。 while(*str!=‘\0’) { if(*str==c)return str; str++; } return NULL; 完整程序如下:;char *strchr(char *str,char c) { while(*str!=c *str!=‘\0’) str++; if(*str==c) return str; else return NULL; } ;三. 指向函数的指针;;double integral(double a, double b, double (*fun)(double), int n) { double h = (b-a)/n; double sum =(fun(a)+fun(b))/2; for(int i=1; in; i++) sum +=fun(a+i*h); sum *= h; return sum; } 主函数中调用格式: y1=integral(0.0,1.0,f,1000); y2=integral(0.0,2.0,g,1000); y3=integral(0.0,3.0,h,1000);;;7.2 带参数的main()函数;例6 使用带参的main() 编程,实现如下功能: 若从命令行输入:cal 123 + 456 计算2个数的和 若从命令行输入:cal 56 – 34 计算2个数的差 若从命令行输入:cal 56 * 34 计算2个数的乘积 若从命令行输入:cal 456 / 34 计算2个数的商; 结构体类型的引入;一. 结构体类型的定义;二. 结构体变量的定义与使用;例7 结构体的应用-统计学生平均分的程序;;int i,j; for(i=0; i PERSON; i=i+1) { cin St[i].id; St[i].GPA=0; for(j=0; j COURSE; j++) { cin St[i]. score[j]; St[i]. GPA += St[i]. score[j]; } St[i]. GPA /= COURSE; };输出部分;三. 结构体与指针;struct StudentType { char id[10]; //学号 int score[COURSE]; //课程成绩 float GPA; //平均分 };;for(ptr=St; ptr St+PERSON; ptr++) { cinptr-id; ptr-GPA=0; for(int j=0; j COURSE; j++) { cin ptr- score[j]; ptr- GPA += ptr- score[j]; } ptr- GPA /= COURSE; };输出部分;;例9 定义一个结构体Rectangle(矩形),根据给定的矩形左上角顶点坐标和右下角顶点坐标,计算该矩形的面积。; 输入函数Input(),返回结构类型值;计算面积的函数GetArea();int main() { Rectangle rec; int tlx, tly, brx, bry; coutPlease input

文档评论(0)

1亿VIP精品文档

相关文档