CH6-1 结构赋值 做函数参数 做函数返回值
2009-12-22 11:35
127 查看
7.1 7.2 做函数参数 做函数返回值
#include <stdio.h>
#include <stdlib.h>
//结构变量初始化,赋值,做函数参数,做函数返回值
struct Point{
int x;
int y;
}; //声明,不会分配内容空间
struct Point createPoint(int x, int y){
struct Point pt;
pt.x = x;
pt.y = y;
return pt;
}
//矩形 包含了两个成员均为 Point变量
struct Rectangle{
struct Point pt1;
struct Point pt2;
};
//结构变量做形参
struct Rectangle createRectangle(struct Point pt1, struct Point pt2){
struct Rectangle screen;
screen.pt1 = pt1;
screen.pt2 = pt2;
return screen;
}
struct Point addPoint(struct Point pt1, struct Point pt2){
pt1.x += pt2.x;
pt1.y += pt2.y;
return pt1;
}
int ifInside(struct Point pt, struct Rectangle rt){
return pt.x >= rt.pt1.x && pt.x < rt.pt2.x &&
pt.y >= rt.pt1.y && pt.y < rt.pt2.y;
}
int getArea(struct Rectangle rt){
int length = (rt.pt2.x>rt.pt1.x)?(rt.pt2.x-rt.pt1.x) : (rt.pt1.x - rt.pt2.x);
int width = (rt.pt2.y>rt.pt1.y)?(rt.pt2.y-rt.pt1.y) : (rt.pt1.y - rt.pt1.y);
return length * width;
}
int main(int argc, char** argv) {
struct Point pt1 = {10, 10};//声明结构变量,分配空间,并赋初值
struct Point pt2 = {20, 20};
//==================
struct Point pt3 = addPoint(pt1, pt2);
struct Rectangle screen = createRectangle(pt1, pt3);
printf("area(pt1, pt3) = %d\n", getArea(screen));
printf("pt1.x = %d, pt1.y = %d\n", pt1.x, pt1.y);//传值,不修改实参的值
//==============
struct Point middlePT = createPoint((int)((pt1.x + pt3.x)/2),
(int)((pt1.y + pt3.y)/2));
if(ifInside(middlePT, screen))
printf("middlePT[%d, %d] is inside of screen(pt1[%d, %d], pt3[%d, %d])\n",
middlePT.x, middlePT.y, pt1.x, pt1.y, pt3.x, pt3.y);
else
printf("middle point is not inside of screen\n");
return (EXIT_SUCCESS);
}
7.3
#include <stdio.h>
#include <stdlib.h>
//结构变量初始化,赋值,做函数参数,做函数返回值
struct Point{
int x;
int y;
}; //声明,不会分配内容空间
struct Point createPoint(int x, int y){
struct Point pt;
pt.x = x;
pt.y = y;
return pt;
}
//矩形 包含了两个成员均为 Point变量
struct Rectangle{
struct Point pt1;
struct Point pt2;
};
//结构变量做形参
struct Rectangle createRectangle(struct Point pt1, struct Point pt2){
struct Rectangle screen;
screen.pt1 = pt1;
screen.pt2 = pt2;
return screen;
}
struct Point addPoint(struct Point pt1, struct Point pt2){
pt1.x += pt2.x;
pt1.y += pt2.y;
return pt1;
}
int ifInside(struct Point pt, struct Rectangle rt){
return pt.x >= rt.pt1.x && pt.x < rt.pt2.x &&
pt.y >= rt.pt1.y && pt.y < rt.pt2.y;
}
int getArea(struct Rectangle rt){
int length = (rt.pt2.x>rt.pt1.x)?(rt.pt2.x-rt.pt1.x) : (rt.pt1.x - rt.pt2.x);
int width = (rt.pt2.y>rt.pt1.y)?(rt.pt2.y-rt.pt1.y) : (rt.pt1.y - rt.pt1.y);
return length * width;
}
int main(int argc, char** argv) {
struct Point pt1 = {10, 10};//声明结构变量,分配空间,并赋初值
struct Point pt2 = {20, 20};
//==================
struct Point pt3 = addPoint(pt1, pt2);
struct Rectangle screen = createRectangle(pt1, pt3);
printf("area(pt1, pt3) = %d\n", getArea(screen));
printf("pt1.x = %d, pt1.y = %d\n", pt1.x, pt1.y);//传值,不修改实参的值
//==============
struct Point middlePT = createPoint((int)((pt1.x + pt3.x)/2),
(int)((pt1.y + pt3.y)/2));
if(ifInside(middlePT, screen))
printf("middlePT[%d, %d] is inside of screen(pt1[%d, %d], pt3[%d, %d])\n",
middlePT.x, middlePT.y, pt1.x, pt1.y, pt3.x, pt3.y);
else
printf("middle point is not inside of screen\n");
return (EXIT_SUCCESS);
}
7.3
相关文章推荐
- ES6-变量的结构赋值-函数参数
- 函数参数返回值 void 和 void *
- 函数多个返回值(返回多个参数/局部变量)通过双重指针来实现
- Perl专题之函数(二)函数参数与返回值
- 当函数需要传入较多的参数,可分装成结构体
- c++ const: const 变量, const 输入参数, const 返回值, const 成员函数
- 函数参数的赋值形式
- UNIX网络编程第3章套接字编程简介3.2套接字地址结构3.3值结果参数3.4字节排序函数
- JavaScript函数-带参数的函数、带返回值的参数
- 在C#中如何在函数参数中返回结构数组?
- 探讨c++函数中的参数传递与返回值(三) 函数返回值
- 二维数组做函数参数 及返回值
- 创建一个数组, 实现函数init()初始化数组、 实现reverse()函数完成数组元素的逆置,实现empty()清空数组。要求:自己设计函数的参数,返回值。
- 定义一个结构体变量(包含年月日),计算该日在本年中为第几天?要求写一个days函数.参数是此结构体类型的变量,返回值是整数
- C语言中数组作为函数的参数以及返回值的使用简单入门
- C函数与汇编函数之间参数及返回值传递方法
- x64 调用约定,参数传递以及函数返回值
- ES6——Day3(字符串的解构赋值+函数参数解构赋值)
- 函数的参数(形参实参)及返回值
- C函数参数传递与返回值传递