1、首先我们打开3名学生(Amy,Jacqualine,Rory)的考试成绩数据。
2、将上述3名学生的考试成绩创建为成结构数组。启动MATLAB,新建脚本(Ctrl+N),输入图示代码,其中Student就是创建的结构数组,该结构数组中包含了3名学生的姓名,考试科目和考试成绩。
3、保存和运行上述脚本,在工作区(Workspace)就会得到结构数组Student,双击该结构数组,可以发现结构数组Student中包含了3名学生的姓名,考试科目和考试成绩。
4、通过结构数组中的数据绘制柱银枣状图。例如将第3名的同学Rory的三科成绩('Mathematics','Physics','Chemistry',85,65,75)绘制成柱状图,只需接着输入以下枝败脚本。
5、保存和运行上述脚本,得到如下柱状图,该柱状图即代表了第3名的同学Rory的三科成绩('Mathematics','Physics','Chemistry',猛搏颤85,65,75)。
程序运行前,SS=1000H,SP=100Hpush axax压栈后SS=1000H,SP=FEH
push bxbx压栈后SS=1000H,SP=FCH
push cxcx压栈后SS=1000H,SP=FAH
push dxdx压栈后SS=1000H,SP=F8H
对于堆栈图弊缺销,根据上面你扮碰自己可以画出来了~
对于堆栈内的数据采用"高高低低"的方法写出来,即高地址放数据的高位,租游低地址放地址的低位~~
个人认为楼上的不懂C语言堆栈到底是怎么回事,按楼含御毁上说法,只是大概讲了下栈,没有讲堆.要讲C语言的堆栈,要从计拆基算机的数据内存分配谈备讲起.
____________________
| Stack区(数组,指针,结构体,局部变量)
____________________
| Static变量(静态变量,全局变量)
____________________
| Heep区(堆区)
____________________
| 代码段
____________________
从上面示意图中可看出整个内存分配,堆分配是在内存中按块划分,也就是相对与函数malloc,realloc,calloc.这3个函数为内存分配函数.而且需要手动调用free函数释放资源,否则会造成大量的内存碎片.
如果楼主不相信可以自己写一个死循环,内部调用malloc函数,创建N个内存块,运行一段时间后,绝对会造成系统瘫痪,资源被耗尽.
栈区划分为计算机自身划分,即在函数或局部变量被调用时,系统自动为其分配栈,以后进先出为原则实现变量的保存,在函数调用完毕时,系统会自动释放栈内资源,所以,栈可以说是短命的(生存周期只在调用过程中).
这里只是粗略说了下堆和栈,另外再说下static-->静态区,全局变量或静态变量存放于静态区,只要代码中存在静态变量或全局变量,自动放于静态区,静态区存放的变量生存周期是整个程序结束时才释放.
代码段区,顾名思义存放的是程序代码(暂时先这么理解).
PS:本人原创,最近发现一些人盗用本人回答的问题.特此声明.嘿嘿.
____________________ _________
补充:
我对于C#不是很熟悉,而且我也是从事C开发的,对于面向对象语言应用不是很熟.在这只能给出C++的代码.代码有点长,不知道你能不能看的懂,才写的.
#include <iostream.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>
/*
//基于数组的栈的实现
#define N 50
typedef struct Stack{
int top
int A[N]
}*pStack
//Pop出栈
int Pop(pStack pst)
{
int e
if(pst->top == -1)
{
cout<<"Stack is empty!"<<endl
return -1
}
else
{
e = pst->A[pst->top]
pst->top--
// cout<<"The element "<<e<<" is pop"<<endl
return e
}
}
//Push入栈
void Push(pStack pst)
{
int e
if(pst->top == N-1)
{
cout<<"Stack is full!"<<endl
}
else
{
cout<<"Input the push number:"
cin>>e
pst->top++
pst->A[pst->top] = e
}
}
//清空栈
void empty(pStack pst)
{
pst->top = -1
}
//判断栈是否为空
int IsEmpty(pStack pst)
{
if(pst->top == -1)
{
return 0
// cout<<"The Stack is empty!"<<endl
}
else
{
return 1
// cout<<"The Stack is not empty!"<<endl
}
}
//判断栈是否为满
int IsFull(pStack pst)
{
if(pst->top == N-1)
{
return 0
}
else
{
return 1
}
}
//初始化栈
void InitStack(pStack pst)
{
pst->top = -1
}
void main()
{
Stack S
InitStack(&S)
int n
cout<<"How many times do you want to Push:"
cin>>n
for(int i=0i<ni++)
{
Push(&S)
}
cout<<"How many times do you want to Pop:"
cin>>n
for(i=0i<ni++)
{
cout<<"The element "<<Pop(&S)<<" is pop"<<endl
}
cout<<"The Stack's stutor:"<<endl
if(IsEmpty(&S) == 0)
{
cout<<"The Stack is empty!"<<endl
}
else
{
cout<<"The Stack is not empty!"<<endl
}
if(IsFull(&S) == 0)
{
cout<<"The Stack is full!"<<endl
}
else
{
cout<<"The Stack is not full!"<<endl
}
empty(&S)
cout<<"The Stack's stutor:"<<endl
if(IsEmpty(&S) == 0)
{
cout<<"The Stack is empty!"<<endl
}
else
{
cout<<"The Stack is not empty!"<<endl
}
}
*/
typedef struct Stack{
Stack *prior
Stack *next
int element
}*pStack
//压栈
void Push(pStack *pst)
{
if((*pst) == NULL)
{
pStack S = (pStack)malloc(sizeof(Stack))
(*pst) = S
(*pst)->next = NULL
(*pst)->prior = NULL
cout<<"Input the PUSH data:"
cin>>(*pst)->element
}
else
{
pStack S = (pStack)malloc(sizeof(Stack))
(*pst)->next = S
S->prior = (*pst)
S->next = NULL
(*pst) = S
cout<<"Input the PUSH data:"
cin>>(*pst)->element
}
}
//判断是否为空
int IsEmpty(pStack pst)
{
if(pst == NULL)
{
cout<<"The Stack is empty!"<<endl
return 1
}
return 0
}
//出栈
pStack Pop(pStack *pst)
{
if(IsEmpty((*pst)) == 1)
return (*pst)
pStack S = (*pst)
if((*pst)->prior == NULL)
{
cout<<"Out:"<<(*pst)->element<<endl
(*pst) = NULL
free(S)
return (*pst)
}
else
{
cout<<"Out:"<<(*pst)->element<<endl
(*pst) = (*pst)->prior
(*pst)->next = NULL
free(S)
return (*pst)
}
}
//初始化栈
void InitStack(pStack pst)
{
pst = NULL
}
void main()
{
pStack pS = NULL
// InitStack(pS)
int n
cout<<"How many times do you want to Push:"
cin>>n
for(int i=0i<ni++)
{
Push(&pS)
}
pStack S
S = Pop(&pS)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)