- 参考链接:https://www.cnblogs.com/blogxjc/p/11287961.html,https://www.cnblogs.com/Roni-i/p/8586332.html,https://www.cnblogs.com/lanhaicode/p/10776166.html
#include <stdlib.h> #include <stdio.h> // 自己定义需要的栈结构,及栈基本操作函数,假设操作数都是整数 #define maxn 100005 typedef int DataType ; struct stack { DataType data[maxn]; }; /*在此定义并完成第一关函数,参考main要求*/ int result(int n,int m,int a,int x)//分别表示n站,最后一站下车的人数m,始发站上车人数a,求第x站时车上的人数 { struct stack pstack; pstack.data[1]=1; pstack.data[2]=1; for (int i = 3; i <=n ; ++i) { pstack.data[i]=pstack.data[i-1]+pstack.data[i-2]; } DataType ans; ans=(m-(pstack.data[n-3]+1)*a)/(pstack.data[n-2]-1); ans=(pstack.data[x-2]+1)*a+(pstack.data[x-1]-1)*ans; return ans; }
头文件
#include <stdio.h> #include <stdlib.h> #include "stack.h" int main() { int n,m,a,x ;//分别表示n站,最后一站下车的人数m,始发站上车人数a,求第x站时车上的人数 scanf("%d%d%d%d",&n,&m,&a,&x); printf("%d",result(n,m,a,x)); }
主函数
第2关:中缀表达式转换为后缀表达式
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include <string.h> 5 6 #define STACK_INIT_SIZE 20 7 #define STACK_INCREMENT 10 8 #define EXPRESS_MAX 10274 9 typedef char element; 10 typedef int status; 11 typedef struct stack 12 { 13 element *top; 14 element *base; 15 int stackSize; 16 char ch[100]; 17 }Stack; 18 status initStack(Stack *stack) 19 { 20 stack->base=stack->top=(element*)malloc(sizeof(Stack)*STACK_INIT_SIZE); 21 if(!stack->base) 22 { 23 exit(0); 24 } 25 stack->stackSize=STACK_INIT_SIZE; 26 return 1; 27 } 28 status push(Stack *stack,element e) 29 { 30 if(stack==NULL) 31 { 32 return 0; 33 } 34 if(stack->top - stack->base == stack->stackSize) 35 { 36 stack->base=(element*)realloc(stack->base,stack->stackSize+STACK_INCREMENT); 37 if(!stack->base) 38 { 39 exit(0); 40 41 } 42 stack->top=stack->base+stack->stackSize; 43 stack->stackSize+=STACK_INCREMENT; 44 } 45 *stack->top=e; 46 stack->top++; 47 return 1; 48 } 49 status pop(Stack* stack,element *e) 50 { 51 if(stack==NULL||e==NULL) 52 { 53 return 0; 54 } 55 if(stack->top==stack->base) 56 { 57 return 0; 58 } 59 *stack->top--; 60 *e=*stack->top; 61 return 1; 62 } 63 64 status getTop(Stack *stack,element *e)//get the stack element to e 65 { 66 if(NULL==stack) 67 { 68 return 0; 69 } 70 *e = *(stack->top-1); 71 return 1; 72 } 73 74 status isEmptyStack(Stack *stack)//negative for emp,posative for emp,0 for exi 75 { 76 if(NULL==stack) 77 { 78 return -1; 79 } 80 if(stack->top==stack->base) 81 { 82 return 1; 83 } 84 return 0; 85 } 86 status DestroyStack(Stack *stack) 87 { 88 if(NULL==stack) 89 { 90 return 0; 91 } 92 if(!stack->base) 93 { 94 free(stack->base); 95 } 96 stack->top=stack->base=NULL; 97 stack->stackSize=0; 98 return 1; 99 100 } 101 102 Stack inToPost(char *expression) 103 { 104 //在此处填写代码,完成中缀表达式转换为后缀表达式并输出 105 /********** Begin **********/ 106 107 char *suffixExp=(char*)malloc(sizeof(char)*EXPRESS_MAX); 108 memset(suffixExp,0,EXPRESS_MAX); 109 110 111 int indexSuf=0; 112 int indexPre=0; 113 Stack symbolStack; 114 initStack(&symbolStack); 115 116 char numLen[10]={0}; 117 char ch = 0; 118 int numLenIndex=0;//index of numLen arrray 119 while(expression[indexPre]) 120 { 121 ch=expression[indexPre++]; 122 if(ch==' ') 123 { 124 continue; 125 } 126 if ('