您的位置:首页 > 编程语言 > C语言/C++

c语言(结构体)

2015-01-22 08:56 148 查看
// main.m

// 1-21课堂笔记

//讲师: 小辉

//笔者: 王学文

// Created by lanouhn on 15/1/21.

// Copyright (c) 2015年 lanouhn. All rights reserved.

//structural(结构体)

#import <Foundation/Foundation.h>

#import "Dog.h"

#import "Phone.h"

void test(int n);

int main(int argc, const char * argv[]) {

//结构体

//结构体是一种由用户自己定义的数据类型

//作用: 用于存储多个不同类型的数据

//结构体的定义

/*

struct 结构体名字 {

成员变量的数据类型1 成员变量名1;

成员变量的数据类型2 成员变量名2;

成员变量的数据类型3 成员变量名3;

...

};

*/

struct student {

int number;

char name[20];

char gender;

float score;

};

//注意

//1. 结构体的命名用小驼峰法

//2. 每一个成员变量后要加分号(;)

//3. 结构体最外层的大括号也要加分号

//结构体变量的定义

// int a = 0;

//格式

//struct 结构体名字 变量名 = {值1, 值2, 值3, ...}

// struct student stu1 = {9527, "wangxuewen", 'M', 99.9};

// struct student stu2 = {1234, "wanhao", 'M', 88.8};

// struct student stu3 = {4567, "weizijain", 'M', 77.7};

// int max = stu1.number > stu2.number ? stu1.number : stu2.number;

// max = max > stu3.number ? max : stu3.number;

// printf("学号最大的是%d\n", max);

// int max1 = stu1.score > stu2.score ? stu1.score : stu2.score;

// max1 = max1 > stu3.score ? max1 : stu3.score;

//

// if (max1 == stu1.score) {

// printf("分数最高者是%s", stu1.name);

// } else if (max1 == stu2.score) {

// printf("分数最高者是%s", stu2.name);

// } else if (max1 == stu3.score) {

// printf("分数最高者是%s", stu3.name);

// }

//姓名最靠前

// struct student stu4 = {0};

// stu4 = strcmp(stu1.name, stu2.name) < 0 ? stu1 : stu2;

// stu4 = strcmp(stu4.name, stu3.name) < 0 ? stu4 : stu3;

// printf("名字最靠前的是%s\n", stu4.name);

//结构体变量的使用

//通过点语句访问成员变量

//格式

//变量名.成员变量名

// printf("%d\n", stu1.number);

// printf("%s\n", stu1.name);

// printf("%c\n", stu1.gender);

// printf("%.2f\n", stu1.score);

//数组不能够直接参与运算

// int array1[5] = {0};

// int array2[5] = {1, 2, 3, 4, 5};

// array1 = array2;

//结构体变量可以直接参与运算

// struct student stu2 = {0};

// stu2 = stu1;

//修改成员变量的值

// stu2.number = 12345;

// stu2.score = 100.0;

// strcpy(stu2.name, "weizijian");

// stu2.gender = 'F';

// printf("%d\n", stu2.number);

// printf("%s\n", stu2.name);

// printf("%c\n", stu2.gender);

// printf("%.2f\n", stu2.score);

//类型重命名

//typedef 原类型名 新类型名

// typedef struct student Student;

//

//新类型名:大驼峰法

// Student stu7 = {2345, "zhangsan", 'M', 9.99};

//Dog dog = {"Isabella", 3, 'M'};

//结构体的内存是最大成员变量数据类型所占的字节数的倍数

//结构体成员变量的顺序,会影响到结构体所占的字节数

//合理的排布成员变量的顺序,能够减少内存碎片,降低结构体所占的字节数

//结构体数组

//定义5个结构体变量,存到数据中

// phone p1 = {"red", 100};

// phone p2 = {"green", 200};

// phone p3 = {"blue", 150};

// phone p4 = {"white", 500};

// phone p5 = {"black", 350};

// phone array[5] = {p1, p2, p3, p4, p5};

// array[0].color

//array[0].price

//按照价格从高到低进行排序

// for (int i = 0; i < 5 - 1; i++) {

// for (int j = 0; j < 5 - 1 -i; j++) {

// if (array[j].price < array[j + 1].price) {

// phone p6 = {0};

// p6 = array[j];

// array[j] = array[j + 1];

// array[j + 1] = p6;

// }

// }

// }

// for (int i = 0; i < 5; i++) {

// printf("%s %.2f\n", array[i].color, array[i].price);

// }

//按照颜色字母从z到a排序

// for (int i = 0; i < 5 - 1; i++) {

// for (int j = 0; j < 5 - 1 - i; j++) {

// if (strcmp(array[j].color, array[j + 1].color) < 0) {

// phone p7 = {0};

// p7 = array[j];

// array[j] = array[j + 1];

// array[j + 1] = p7;

// }

// }

// }

// for (int i = 0; i < 5; i++) {

// printf("%s %.2f\n", array[i].color, array[i].price);

// }

// int array[5] = {10, 9, 8, 7, 6};

// BubbleSort(array, 5);

// for (int i = 0; i < 5; i++) {

// printf("%d\n", array[i]);

// }

//

//对12345,正序输出1, 2, 3, 4, 5

//倒叙输出5, 4, 3, 2, 1

//用递归解决:

test(12345);

//冒泡排序的优化

int arr[5] = {1, 2, 3, 4, 5};

BOOL flag = YES;//发生了交换

for (int i = 0; i < 5 - 1 && flag; i++) {

flag = NO; //假定没有发生过变化

for (int j = 0; j < 5 - 1 -i; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

flag = YES; //发生了交换

}

} printf("aaa");

}

for (int i = 0; i < 5; i++) {

printf("%d ", arr[i]);

}

return 0;

}

void test (int n) { //从最外层一层一层的进行执行,递归一定要有出口

if (n == 0) {

return;

}

test(n /10);

printf("%d\n", n % 10);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: