C9_结构体指针
来源:互联网 发布:java 数字分割 编辑:程序博客网 时间:2024/05/16 07:56
//
// MyFunction.h
// C9_结构体指针
//
// Created by dllo on 15/7/10.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
// 声明结构体
// 学生:姓名,年龄,性别,成绩,最后用typedef
struct student{
int stuAge; // int只能放在他的整数个位置
char stuSex;
float stuScore;
char stuName[20];// 成员变量
};
typedefstruct student Student;
// 用指针来进行冒泡排序
void bubbleSort(int *p,int count);
///练习1******************************************************************
//定义⼀一个点坐标的结构体CPoint,包含两个float成员x,y。定义两个CPoint结构体变量m,n使⽤用两个结构体指针分别指向m,n然后通过指针计算两点距离(提⽰示:1、勾股定理,2、开⽅方函数sqrt)
struct CPoint{
float x;
float y;
};
typedefstruct CPoint CPoint;
void Gogu(CPoint *p1, CPoint *p2);
///练习2****************************************************************
// student stu = {1,”lan ou”,’m’,95.6}; student * p = &stu; 请通过p将name成员中的第⼀一个字⺟母改成⼤大写,所有’ ‘(空格)改成下划线’_’
//struct student1{
// int stuNum; // int只能放在他的整数个位置
// char stuName[20];
// char stuSex;
// float stuScore;// 成员变量
//};
//typedef struct student1 Student1;
//
//void changeName(Student *stu);
///********************************************************************
void changeScore(Student *stu,int count);
// ATM取款机:姓名,账号,密码,余额
struct person{
char name[30];
int cardNum;
int passWord;
float moneyCount;
};
typedefstruct person Person;
int checkCardAndPassWord(int enterCard,int enterPassWord, Person *p, int count); // 在这里的 *p和p[]的意义完全一样,都可以用
// 如果钱正好被取走,余额为0,在姓名后面拼接一个"00",并且把取走之后的钱数修改并打印,判断钱数够不够
void changeMoney(int money, Person *p,int index);
void getMoney(int result,float getMoney, Person *p);
//
// MyFunction.m
// C9_结构体指针
//
// Created by dllo on 15/7/10.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "MyFunction.h"
void bubbleSort(int *p,int count){
//指针无法计算数组的长度,但是如果是字符串因为字符串独有的'\0'结束标志,所以能
for (int i =0; i < count - 1; i++) {
for (int j =0; j < count - i; j++) {
if (p[j] >= p[j + 1]) {
int temp = p[j];
p[j] = p[j +1];
p[j +1] = temp;
}
}
}
}
///********************************************************************
void Gogu(CPoint *p1, CPoint *p2){
// 取地址
float result = sqrtf((p1 -> x- p2 -> x) * (p1 -> x- p2 -> x) + (p1 -> y- p2 -> y) * (p1 -> y- p2 -> y));
printf("%g\n", result);
};
///********************************************************************
//
//void changeName(Student *stu){
// // 判断首字母是不是小写
// if (stu -> stuName[0] >= 'a'&& stu ->stuName[0] <= 'z') {
// stu->stuName[0] -= 32;
// }
// for (int i = 0; i < strlen(stu->stuName); i++) {
// if (stu ->stuName[i] == ' ') {
// stu ->stuName[i] = '_';
// }
// }
// printf("%s\n",stu->stuName);
//}
///*********************************************************************
void changeScore(Student *stu,int count){
for (int i =0; i < count; i++){
if (stu[i].stuSex == 'm') {
stu[i].stuScore +=10;
// if (stu[i].stuScore > 100) {
// stu[i].stuScore = 100;
// }
if (stu[i].stuScore >= 90) {
stu[i].stuScore =100;
}
}
}
for (int j =0; j < count ; j++) {
printf("%s",stu[j].stuName);
}
}
///ATM*************************************************************
int checkCardAndPassWord(int enterCard,int enterPassWord, Person *p, int count){
int result = 0;
for (int i =0; i < count ; i++) {
if (p[i].cardNum == enterCard && p[i].passWord == enterPassWord) {
result = i;
printf("姓名:%s\n余额:%g\n",p[i].name,p[i].moneyCount);
return i;
}
}
return 4;
}
void changeMoney(int money, Person *p,int index){
//判断钱够不够
if (p[index].moneyCount < money) {
printf("哥们钱不够\n");
}else {
p[index].moneyCount -= money;
if (p[index].moneyCount == 0) {
// 钱取完了对姓名进行拼接
strcat(p[index].name,"00");
}
// printf("余额为:%g\n",p[index].moneyCount);
printf("姓名:%s\n余额:%g\n",p[index].name,p[index].moneyCount);
}
}
//void getMoney(int result, float getMoney, Person *p){
// if (p[result].moneyCount >= getMoney) {
// printf("请稍等");
// } else {
// printf("对不起,余额不足");
// }
//}
//
//
// main.m
// C9_结构体指针
//
// Created by dllo on 15/7/10.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyFunction.h"
// 宏是一个替换的过程,把后面的内容进行替换
#define PI 3.14
// 带参数的宏
// 计算两个数的积
#define MUL(A, B) A * B
#define MUL(A, B) (A) * (B)
#define MAXVALUE(A,B) A > B ? A : B
#define MAXVALUE(A,B) (A) > (B) ? (A) : (B)
//// 条件编译的第一种形式
//#ifdef PI //ifdef后面必须跟定义的宏
//#define TEST 20 // 根据宏是否被定义决定走那个条件
//#else
//#define TEST 50
//#endif
//// 第二种
//#ifndef PI // 如果没有宏定义PI 走下面的条件
//#define TEST 20
//#else // 如果定义了走这个条件
//#define TEST 50
//#endif
// 第三种
// 条件有两种,一种是0,一种是1
#if 1
int a =100;
#define TEST 20
#else
int a =50
#define TEST 50
#endif
int main(int argc,const char * argv[]) {
// // 定义一个结构体类型的变量
// Student stu = {18, 70.5, 'w', "zhangsan"};
// // 用'.'来访问成员变量
// printf("%s\n",stu.stuName);
// // 结构体可以直接进行赋值
// Student temp = stu;
// // 数组名本身是地址,称为常量地址,不可以变,所以数组不能直接赋值
///***************************************************************
//指针变量(认为指针就是变量,里面存储的是地址)
// int *p = NULL; // 定义一个空指针
// int a = 10;
// char str[20] = ""; // str本身为第一个字符的地址
// // scanf("%s",str);
// printf("%p\n",&a);
// p = &a;
// // *取值符
// printf("%d\n", *p);
// int arr[5] = { 3, 2, 5, 4, 6};
// int *p = arr;
// bubbleSort(p, 5);
// // 调用函数
// for (int i = 0; i < 5; i++) {
// printf("%d\n",arr[i]);
// }
// // int *每次跳4个字节
///******************************************************************
// // 自定义的数据类型
// Student stu = {20, 70.5, 'w', "yanglin"};
// Student *p = &stu;
// printf("%p\n", p);
// printf("%p\n", &stu);
//
// // p保存的是结构体的地址,*p取到的结果相当于地址所对应的结构体的变量,之后的用法和结构体变量一样
// // 相当于(*p).stuName;==stu.stuName;
// printf("%s\n", (*p).stuName);
//
// // 对stuName进行重新赋值
// strcpy((*p).stuName, "wangwu");
// printf("%s\n", (*p).stuName);
//
// //
// printf("%p\n", &stu.stuAge);
// printf("%p\n", &stu.stuScore);
// // p ->stuName;
// printf("%s\n",p ->stuName);
//
///****************************************************************
// CPoint m = {5, 4};
// CPoint n = {5, 19};
// CPoint *p1 = &m;
// CPoint *p2 = &n;
// Gogu(p1, p2);
//
// ///*******************************************************************
// Student stu = {28, 70, 'w', "y a n g l i n"};
// Student *p = &stu;
// printf("%c\n", p ->stuName[0]);
// changeName(p);
//
///*****************************************************************
// 结构体数组
// Student stu1 = {20, 75.5, 'w', "zhangsan"};
// Student stu2 = {30, 80.5, 'm', "lisi"};
// Student stu3 = {46, 66.0, 'w', "wangwu"};
// Student stu4 = {17, 55, 'w', "yangliu"};
// Student stu[4] = {stu1, stu2, stu3, stu4};
// printf("%p\n", stu);
// printf("%p\n", &stu[0]);
//
// Student *p = stu;
// printf("%s\n",p ->stuName);
// // stu[i]和p[i]一样,取出来的是结构体,所以用'.'来找成员变量
// // (p + 1)是根据指针来操作的,用->来访问成员变量
// printf("%s\n",p[1].stuName);
// printf("%s\n",(p + 1) ->stuName);
// // 用指针的方式来遍历所有学生姓名
// for (int i = 0; i < 4; i++) {
// printf("%s\n", (p + i) ->stuName);
// }
//
// // 根据学生的成绩,用指针对他进行排序,从小到大
// for (int i = 0; i < 4 - 1; i++) {
// for (int j = 0; j < 4 - 1 - i; j++){
// if (p[j].stuScore > p[j+1].stuScore) {
// Student temp = {};
// temp = p[j];
// p[j] =p[j+1];
// p[j+1] = temp;
// }
// }
// }
//
// for (int i = 0; i<4; i++) {
// printf("%s\n", stu);
// }
// // 平均成绩和平均年龄
// int ageCount = 0;
// float scoreCount = 0;
// for (int i = 0; i < 4; i++) {
// ageCount += (p+i)->stuAge;
// scoreCount += (p+i)->stuScore;
// }
// printf("%d %g\n", ageCount/4, scoreCount/4);
//
///******************************************************************
//// 结构体数组练习
// Student stu1 = {25, 78.5, 'w', "yangliu"};
// Student stu2 = {18, 93, 'm', "wangwu"};
// Student stu3 = {23, 100, 'w', "lisi"};
// Student stu4 = {21, 69, 'w', "zhangsan"};
// Student stu[4] = {stu1, stu2, stu3, stu4};
// // 如果是男生,加10分,超过100,则仅记100
// //使⽤用指针查找学员数组中性别为男的学员,成绩增加10分,超过100分的记为100分)
// int i = 0;
// while (1) {
// i++;
// printf("%d\n",i);
// if (i==3) {
// break; // continue 只是i= 3的时候不执行
// }
//
// }
///ATM**********************************************************
// Person per1 = {"yanglin", 111, 123, 100};
// Person per2 = {"liushanshan", 222, 234, 200};
// Person per3 = {"shangshuai", 333, 345, 300};
// Person per4 = {"zhoushengmin", 444, 456, 400};
// Person per[4] = {per1, per2, per3, per4};
//
//// 输入账号密码正确返回下标不正确返回4
//
// //while (1) {
// int enterCard = 0;
// printf("请输入账号:");
// scanf("%d", &enterCard);
// printf("请输入密码:");
// int enterPassWord = 0;
// scanf("%d", &enterPassWord);
// int result = checkCardAndPassWord(enterCard, enterPassWord, per, 4);
// while(1){
// if (result == 4) {
// printf("账号或者密码错误\n");
// continue;
// } else {
// printf("登陆成功\n");
// break;
// }
// }
// printf("请输入取款金额:");
// float getMoney = 0;
// scanf("%g", &getMoney);
// // int result = checkCardAndPassWord(enterCard, enterPassWord, per, 4);
// // getMoney(result,getMoney,per);
// changeMoney(getMoney, per, result);
///*****************************************************************
// 宏定义
//定义一个宏,命名要么全大写,要么 k+驼峰的方式
//// printf("%g\n", PI + 3);
// int a = 10, b = 20;
// printf("%d\n",MAXVALUE(a ,b));
// printf("%d\n",MUL(a ,b));
// printf("%d\n",MUL((3 + 4), 5));
//在运用宏的时候,要加上括号,防止因为算数符优先级问题的影响
printf("%d\n",TEST);
printf("test = %d\n",a);
return 0;
}
- C9_结构体指针
- C9_指针
- C9_指针高级
- C9_高级指针
- C9_作业
- 函数指针,结构体指针
- 结构体、结构体指针
- 结构体和指针
- 结构体指针
- 结构体与指针
- 结构体指针
- 结构体数组指针
- 结构体指针连接
- 结构体指针问题
- 结构体指针小记
- 结构体指针
- 结构体指针函数
- 结构体指针说
- NSString NSArray的用法
- iOS中 @synthesize 和 @dynamic 区别
- 1.矩形的渲染与绘制
- NYOJ - 914 Yougth的最大化
- ssh连接远程主机执行脚本的环境变量问题
- C9_结构体指针
- 不少程序员都会碰到的三个面试题
- cf dp B. Maximum Submatrix 2
- 宁波出台房产新政 购房可获50%契税补贴
- HTTP POST GET 本质区别详解
- [SHELL]监控LINUX目录文件变化
- C语言--程序设计基础——4、5章
- 字典与model
- [两岸同心]台湾学子听武大教授讲传统文化