学习日志2
姓名:龙村村 什么时候:2018.9.4
今天学习任务 结合前一天学习的指针的知识,学习C语言当中栈的相关知 识
学习如何定义一个结构体变量。
学习栈的相关函数的编写
今日任务完成情况:代码写完了
今日开发中出现的问题汇总:编码不够细心,跟不上老师进度 打字慢
今日未解决问题:没有
今日开发收获:我会栈了
自我评价:代码写完,上课认真,挺好的
课堂代码:
源文件main.c
#include "stack.h"
#include <stdio.h>
int main(void)
{
int ret,i;
Stack s;
//初始化栈(顺序栈)
ret = InitStack(&s); //自定义函数
if(ret == SUCCESS)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
//***********************************************************
//判断栈是否为空
ret = EmptyStack(s);
if (ret == SUCCESS)
{
printf("stack is empty!\n");
}
else
{
printf("stack is not empty!\n");
}
//***********************************************************
for (i = 0;i < 11;i++)
{
ret = push( &s,i + 1);//进栈操作
if (ret == SUCCESS)
{
printf("push %d success!\n",i+1);
}
else
{
printf("push %d failure!\n",i+1);
}
}
ret = GetTop(s);//获取栈顶元素
if (ret == FAILURE)
{
printf("Get Top Failure!\n");
}
else
{
printf("Top is %d\n",ret);
}
//***********************************************************
for (i = 0;i < 5;i++)
{
ret = pop(&s);//出栈操作
if (ret == FAILURE)
{
printf("pop failure!\n");
}
else
{
printf("pop %d success!\n",ret);
}
}
//printf("Top is %d\n",GetTop(s));
ret = GetTop(s);//获取栈顶元素
if (ret == FAILURE)
{
printf("Get Top Failure!\n");
}
else
{
printf("Top is %d\n",ret);
}
//***********************************************************
ret = ClearStack(&s);//清空栈元素
if (ret == FAILURE)
{
printf("clear Failure!\n");
}
else
{
printf("clear Success!\n");
}
ret = GetTop(s);//获取栈顶元素
if (ret == FAILURE)
{
printf("Get Top Failure!\n");
}
else
{
printf("Top is %d\n",ret);
}
return 0;
}
源文件stack.c
#include "stack.h"
#include <stdlib.h>
int InitStack(Stack *s)
{
if (NULL == s)
{
return FAILURE;
}
s->top = -1;
return SUCCESS;
}
int EmptyStack(Stack s)
{
return (s.top == -1) ? SUCCESS : FAILURE;
}
int push(Stack *s,int e)
{
if (NULL == s)
{
return FAILURE;
}
if (s->top == 9)//栈已满
//if (s.top == 9)//栈已满
{
return FAILURE;
}
s->data[s->top +1 ] = e;
s->top++;
return SUCCESS;
}
int GetTop(Stack s)
{
if (s.top == -1)//如果是空栈,失败
{
return FAILURE;
}
return s.data[s.top];//返回栈顶元素
}
int pop(Stack *s)
{
int e;
if (NULL == s)
{
return FAILURE;
}
if (s->top == -1)//如果是空栈,失败
{
return FAILURE;
}
e = s->data[s->top];//保存栈顶元素
s->top --;
return e;//返回出栈的元素
}
int ClearStack(Stack *s)
{
if (NULL == s)//入参判断
{
return FAILURE;
}
s->top = -1;
return SUCCESS;
}
源文件stack.h
#ifndef STACK_H
#define STACK_H
#define MAXSIZE 10
#define SUCCESS 10000
#define FAILURE 10001
struct stack
{
int data[MAXSIZE];
int top; //栈顶指针(数组下标)
};
typedef struct stack Stack;
#endif
源文件sort.c
#include <stdio.h>
#include "stack.h"
#include <stdlib.h>
int main(void)
{
int i,num;
Stack s1,s2;
if (InitStack(&s1) == FAILURE || InitStack(&s2) == FAILURE)
{
printf("Init Failure!\n");
exit(1);
}
for (i = 0;i < 5;i++)
{
scanf("%d",&num);
if (i == 0)
{
push(&s1,num);
}
else
{
while(num < GetTop(s1) && EmptyStack(s1) != SUCCESS)
{
push(&s2,pop(&s1));
}
push(&s1,num);
while(EmptyStack(s2) != SUCCESS)
{
push(&s1,pop(&s2));
}
}
}
for (i = 0;i < 5;i++)
{
printf("%d ",pop(&s1));
}
printf("\n");
return 0;
}
今日作业
一、输入一个字符串,计算字符串中子串出现的次数
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
printf("Please input your string.\n");
scanf("%s",a);
char * c = a;
char b[10];
printf("Please input the subsidiary string. \n");
scanf("%s",b);
char * d = b;
int n;
int num = 0;
n = strlen(d);
while(strlen(c) > 0)
{
if(strncmp(c,d,n) == 0)
{
num++;
c += n;
}
else
{
c++;
}
}
printf("%d\n",num);
return 0;
}
2.编写一个C函数,将”I am from shanghai “倒置为”shanghai from an I”,即将句子中的单词位置倒置,而不改变单词内容结构
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="I am from shanghai";
printf("%s\n",str);
//puts (str);
char tmp;
char *p1 = str;
char *p2 = str + strlen(str) -1;
while (p1<p2)
{
tmp = *p1;
*(p1++) = *p2;
*(p2--) = tmp;
}
char *p3 = NULL;
p1 = str;
p2 = str;
while (*p2)
{
if (*p2 == ' ')
{
p3 = p2 - 1;
while (p1<p3)
{
tmp = *p1;
*(p1++) = *p3;
*(p3--) = tmp;
}
p1 = p2 + 1;
}
p2++;
}
p3 = p2 - 1;
while (p1<p3)
{
tmp = *p1;
*(p1++) = *p3;
*(p3--) = tmp;
}
//puts (str);
printf("%s",str);
return 0;
}
3.输入一个字符串,同时输入帧头和帧尾,将该字符串中合法的帧识别出来
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
char str1[100];
char str2[100];
printf("Please input the whole string.\n");
gets(s);
printf("Please input the head string.\n");
gets(str1);
printf("Please input the tail string.\n");
gets(str2);
char *p = s;
char *p1 = str1;
char *p2 = str2;
char *p3 = NULL;
char *p4 = NULL;
printf("The legal frame in the string is :\n");
while(*p)
{
if(strncmp(p,p1,strlen(str1))==0)
{
p3=p;
while(*p3)
{
if(strncmp(p3,p2,strlen(str2))==0)
{
int j=p3-p+strlen(str2),i;
p4 = p;
for(i=0;i<j;i++)
{
printf("%c",*p4++);
}
puts("\0");
}
*p3++;
}
}
*p++;
}
return 0;
}