Demo_张仕传_结构体考试-modify
/*
题目:
//声明一个结构体类型
struct _AdvTeacher
{
char *name;
char *tile;
int age;
char *addr;
char *p1; //系统预留成员域
char **p2;//系统预留成员域
};
要求定义一个结构体数组(6个元素),要求从键盘输入数据,并按照名称大小进行排序;打印输出。
1、 打印结构体数组,需要单独封装成函数;10
2、 排序结构体数组,需要单独封装成函数(按照名称进行排序);50
3、 main函数中编写业务测试模型;40 2014-04-22 19:59:31
wirting by zhangshichuan.
*/
#include <iostream> using namespace std; #define StructArrarySize 3 // 老师数量
#define StudentNum 1 // 每位老师的学生的数量 typedef struct _AdvTeacher
{
char *name;
char *tile;
int age;
char *addr;
char **student;
}AdvTeacher; int CreateStructArray(AdvTeacher **, int, int); //客户端初始化结构体数组
int FreeStructArray(AdvTeacher **, int, int); //客户端释放结构体数组内存
int PrintStructArray(AdvTeacher*, int, int); //客户端打印结构体数组元素
int SortStructArray(AdvTeacher*, int); //客户端对结构体数组元素排序 int main(void)
{
int rv = ;
AdvTeacher * t = NULL; rv = CreateStructArray(&t, StructArrarySize, StudentNum); //被调函数分配内存,甩出来
if (rv != )
{
printf("func: CreateStructArray() _%d_error_\n ", rv);
goto End;
} for (int i = ; i < StructArrarySize; ++i) // 客户端初始化赋值
{
printf("请输入第%d位老师的姓名: ", i+);
scanf("%s", t[i].name);
printf("请输入第%d位老师的年龄: ", i+);
scanf("%d", &(t[i].age));
printf("请输入第%d位老师的职务: ", i+);
scanf("%s", t[i].tile);
printf("请输入第%d位老师的地址: ", i+);
scanf("%s", t[i].addr);
for (int j = ; j < StudentNum; ++j)
{
printf("请输入第%d位老师的第%d位学生的姓名: ", i+, j+);
scanf("%s", t[i].student[j]);
}
} printf("排序前:\n");
rv = PrintStructArray(t, StructArrarySize, StudentNum); // 打印
if (rv != )
{
printf("func: PrintStructArray() _%d_error_\n ", rv);
goto End;
} rv = SortStructArray(t, StructArrarySize); // 排序
if (rv != )
{
printf("func: SortStructArray() _%d_error_\n ", rv);
goto End;
} printf("排序后:\n");
rv = PrintStructArray(t, StructArrarySize, StudentNum); // 打印
if (rv != )
{
printf("func: PrintStructArray() _%d_error_\n ", rv);
goto End;
} End:
rv = FreeStructArray(&t, StructArrarySize, StudentNum);
if (rv != )
{
printf("致命错误: FreeStructArray()执行失败!\n _%d_error_\n", rv);
} system("pause");
return rv;
} // 创建结构体数组
int CreateStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = ;
if (NULL == t)
{
rv = -;
return rv;
} AdvTeacher * temp = NULL; temp = (AdvTeacher *)malloc(structArrarySize * sizeof(AdvTeacher));
if (NULL == temp)
{
rv = -;
return rv;
} for (int i = ; i < structArrarySize; ++i)
{
temp[i].name = (char *)malloc( * sizeof(char));
temp[i].addr = (char *)malloc( * sizeof(char));
temp[i].tile = (char *)malloc( * sizeof(char)); if (NULL == temp[i].name || NULL ==temp[i].addr || NULL == temp[i].tile)
{
rv = -;
return rv;
} temp[i].student = (char **)malloc(studentNum * sizeof(char *));
if (NULL == temp[i].student)
{
rv = -;
return rv;
}
for (int j = ; j < studentNum; ++j) //创建学生内存块
{
(temp[i].student)[j] = (char *)malloc( * sizeof(char));
if (NULL == (temp->student)[j])
{
rv = -;
return rv;
}
}
} *t = temp; return rv;
} // 销毁结构体数组
int FreeStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = ;
AdvTeacher *temp = *t; for (int i = ; i < structArrarySize; ++i)
{
for (int j = ; j < studentNum; ++j) // 销毁学生内存块
{
if (NULL != temp[i].student[j])
{
free(temp[i].student[j]);
}
} if (NULL != temp[i].addr && NULL != temp[i].name && NULL != temp[i].tile && NULL != temp[i].student)
{
free(temp[i].addr);
free(temp[i].name);
free(temp[i].tile);
free(temp[i].student);
}
} if (NULL != temp)
{
free(temp);
*t = NULL; //间接赋值 通过*(实参的地址), 去间接修改实参的值 为null
} return rv;
} // 打印结构体数组
int PrintStructArray(AdvTeacher*t, int structArrarySize, int studentNum)
{
int rv = ;
if (NULL == t)
{
rv = -;
return rv;
} AdvTeacher *temp = t; for (int i = ; i < structArrarySize; ++i)
{
printf("第%d位老师的姓名为:%s \n", i + , temp[i].name);
printf("第%d位老师的年龄为:%d \n", i + , (temp[i].age));
printf("第%d位老师的职务为:%s \n", i + , temp[i].tile);
printf("第%d位老师的地址为:%s \n", i + , temp[i].addr);
for (int j = ; j < studentNum; ++j)
{
printf("第%d位老师的第%d位学生的姓名为:%s\n", i + , j + , temp[i].student[j]);
}
} return rv;
}
// 排序结构体数组
int SortStructArray(AdvTeacher*t, int structArrarySize)
{
int rv = ;
if (NULL == t)
{
rv = -;
return rv;
}
AdvTeacher *temp = t; for (int i = ; i < structArrarySize; ++i)
{
for (int j = i + ; j < structArrarySize; ++j)
{
if ( > strcmp(temp[i].name, temp[j].name))
{
AdvTeacher tmp = temp[i];
temp[i] = temp[j];
temp[j] = tmp;
}
}
} return rv;
}
Demo_张仕传_结构体考试-modify的更多相关文章
- Demo02_对结构体进行文件读写_张仕传_作业_
#include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...
- 全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型
函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...
- c语言中较常见的由内存分配引起的错误_内存越界_内存未初始化_内存太小_结构体隐含指针
1.指针没有指向一块合法的内存 定义了指针变量,但是没有为指针分配内存,即指针没有指向一块合法的内浅显的例子就不举了,这里举几个比较隐蔽的例子. 1.1结构体成员指针未初始化 struct stude ...
- 『Python CoolBook』C扩展库_其四_结构体操作与Capsule
点击进入项目 一.Python生成C语言结构体 C语言中的结构体传给Python时会被封装为胶囊(Capsule), 我们想要一个如下结构体进行运算,则需要Python传入x.y两个浮点数, type ...
- C++_系列自学课程_第_12_课_结构体
#include <iostream> #include <string> using namespace std; struct CDAccount { double bal ...
- C++_知识点_结构体/枚举/联合
//C++中结构体的不同之处 #include <iostream> #include <string> using namespace std; int main(void) ...
- C语言_结构体变量指针做函数参数的使用案例
# include <stdio.h> # include <stdlib.h> # include <string.h> # include <malloc ...
- C语言_结构体的4种定义初始化方式及案例
结构体是一种构造数据类型 (构造数据类型:数组类型.结构体类型(struct).共用体类型(union)).用途:把不同类型的数据组合成一个整体,通俗讲就像是打包封装,把一些有共同特征(比如同属于某一 ...
- 倍福TwinCAT(贝福Beckhoff)基础教程2.2 TwinCAT常见类型使用和转换_结构体
在DUTs文件夹上右击添加结构体,结构体中可以放基本变量类型,也可以嵌套其他结构体 使用的时候,需要声明结构体的实例,然后按照类.属性的格式来读写变量,会有代码的自动提示 你也可以声明数组,类 ...
随机推荐
- POJ2192 - Zipper(区间DP)
题目大意 给定三个字符串s1,s2,s3,判断由s1和s2的字符能否组成字符串s3,并且要求组合后的字符串必须是s1,s2中原来的顺序. 题解 用dp[i][j]表示s1的前i个字符和s2的前j个字符 ...
- [一]java环境变量的配置
1.JAVA_HOME(新建):D:\jdk1.6 2.classpath(新建): .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar; 3.path(新增):% ...
- .NET程序集(Assembly)
在.NET 中,新引入了一个程序集的概念,就是指经由编译器编译得到的,供CLR进一步编译执行的那个中间产物,在WINDOWS系统中,它一般表现为.dll,或者是.exe的格式,但是要注意,它们跟普通意 ...
- Lua代码解析-写给C和C++开发人员
lua语言作为一门轻量级脚本语言,能够非常好的被嵌入到应用程序,因此,在移动游戏开发中举足轻重 然后C/C++开发人员转lua并非非常习惯,我也是..所以,一起努力学习lua吧 lua没有类的概念,有 ...
- [GIF] GIF Loop Coder - Animating with Arrays
In this lesson, we discuss animating using arrays, and how different data types are interpolated whi ...
- Volley完全解析
从前在学校用的最多的网络请求框架就是AsyncHttpClient,用久了发现这样一个问题,就是代码复用太难,基本上做不到,所以有一段时间又回归了HttpURLConnection和HttpClien ...
- javascript中的内置对象
2015.12.1 javascript中的内置对象 复习: 1.js中的内置函数 alert prompt write parseInt parseFloat eval isNaN document ...
- Markdown 添加 Latex 数学公式
添加公式的方法 Latex 数学公式语法 添加公式的方法 行内公式 $行内公式$ 行间公式 $$行间公式$$ Latex 数学公式语法 角标(上下标) 上标命令^{} 下标命令_{} 上下标命令用来放 ...
- I2C Verilog的实现(一)
<span style="font-size:14px;">`timescale 1ns / 1ps module test( sda ); reg scl; inou ...
- xml中使用foreach遍历对象
如果是一个带数据的List对象 <select id="selectProductMSTList" resultType="java.util.Map" ...