数制转换
#include<stdio.h> #include<stdlib.h> #define MAXSIZE 1024 typedef int elemtype; typedef struct SequenStack { elemtype data[MAXSIZE]; int top; //设置顺序栈的栈顶指针 }SequenStack; //顺序栈的结构类型 SequenStack * Init_SequenStack(); SequenStack * Push_SequenStack(SequenStack *S,elemtype x); SequenStack * Pop_SequenStack(SequenStack *S,elemtype *x); void Conversion(SequenStack *S,int N,int r); int main() { SequenStack *S; elemtype x,y; system("cls"); S=Init_SequenStack(); printf("请输入要转换的字符和数制: "); scanf("%d,%d",&x,&y); Conversion(S,x,y); return 0; } SequenStack * Init_SequenStack() //初始化 { SequenStack *S; //定义顺序栈指针变量 S=(SequenStack *)malloc(sizeof(SequenStack)); //申请内存空间 S->top=-1; return S; } //入栈 SequenStack * Push_SequenStack(SequenStack *S,elemtype x) { if(S->top>=MAXSIZE-1) { return 0; //栈满,插入失败,返回0; } else { S->top++; S->data[S->top]=x; return S; } } //出栈 SequenStack * Pop_SequenStack(SequenStack *S,elemtype *x) { if(S->top==-1) { return 0; //栈空 } else { *x=S->data[S->top]; S->top--; return S; } } void Conversion(SequenStack *S,int N,int r) { int x,n,i; char c; x=N; if(r<=10) { while(N>0) { S=Push_SequenStack(S,N%r); N=N/r; } } else if(r>10) { if(N%r<10) { while(N>0) { S=Push_SequenStack(S,N%r); N=N/r; } } else if(N%r>=10) { while(N>0) { n=N%r-10; c='a'+n; S=Push_SequenStack(S,c); N=N/r; } } } printf("%d经过%d进值转换后为:",x,r); while(S->top>=0) { if(S->data[S->top]>=97) { printf("%c",S->data[S->top]); S->top--; } else { printf("%d",S->data[S->top]); S->top--; } } printf(" "); }
行编辑器
#include<stdio.h> #include<stdlib.h> #define MAXSIZE 1024 typedef char elemtype; typedef struct SequenStack { elemtype data[MAXSIZE]; int top; //设置顺序栈的栈顶指针 }SequenStack; //顺序栈的结构类型 SequenStack * Init_SequenStack(); SequenStack * Push_SequenStack(SequenStack *S,elemtype c); SequenStack * Pop_SequenStack(SequenStack *S,elemtype *x); SequenStack * EDIT(SequenStack *S,elemtype c); int main() { char cmd; SequenStack *S; elemtype c,x; int isdo,i,len; system("cls"); S=Init_SequenStack(); printf("请输入要编辑的字符串: "); c=getchar(); S=EDIT(S,c); while(S->top>=0) { printf("%c",S->data[S->top]); S->top--; } return 0; } SequenStack * Init_SequenStack() //初始化 { SequenStack *S; //定义顺序栈指针变量 S=(SequenStack *)malloc(sizeof(SequenStack)); //申请内存空间 S->top=-1; return S; } //入栈 SequenStack * Push_SequenStack(SequenStack *S,elemtype x) { if(S->top>=MAXSIZE-1) { return 0; //栈满,插入失败,返回0; } else { S->top++; S->data[S->top]=x; return S; } } //出栈 SequenStack * Pop_SequenStack(SequenStack *S,elemtype *x) { if(S->top==-1) { return 0; //栈空 } else { *x=S->data[S->top]; S->top--; return S; } } SequenStack * EDIT(SequenStack *S,elemtype c) { elemtype x; while(c!='*') { if(c=='#') { Pop_SequenStack(S,&x); } else if(c=='@') { S->top=-1; } else { Push_SequenStack(S,c); } c=getchar(); } return S; }
杨辉三角
#include<stdio.h> #include<stdlib.h> #define MAXSIZE 1024 typedef int elemtype; typedef struct SequenQueue { elemtype data[MAXSIZE]; int front; int rear; }SequenQueue; SequenQueue *Init_SequenQueue(); int SequenQueue_Empty(SequenQueue * Q); int SequenQueue_Full(SequenQueue * Q); int SequenQueue_Length(SequenQueue *Q); int Enter_SequenQueue(SequenQueue * Q,elemtype x); int Delete_SequenQueue(SequenQueue * Q,elemtype *x); int GetFront_SequenQueue(SequenQueue * Q,elemtype *x); int find(SequenQueue * Q,elemtype key); void YHTriangle(int n); void menu(); //初始化 SequenQueue *Init_SequenQueue() { SequenQueue * Q; Q=(SequenQueue *)malloc(sizeof(SequenQueue)); if(Q!=NULL) { Q->front=0; Q->rear=0; } return Q; } //判队列空 int SequenQueue_Empty(SequenQueue * Q) { if(Q->front==Q->rear) { return 1; } else { return 0; } } //判断队列满 int SequenQueue_Full(SequenQueue * Q) { if((Q->rear+1)%MAXSIZE==Q->front) { return 1; } else { return 0; } } //循环队列的长度 int SequenQueue_Length(SequenQueue * Q) { return((Q->rear-Q->front+MAXSIZE)%MAXSIZE); } //入队 int Enter_SequenQueue(SequenQueue * Q,elemtype x) { if(SequenQueue_Full(Q)) { return 0; } Q->data[Q->rear]=x; Q->rear=(Q->rear+1)%MAXSIZE; return 1; } //出队 int Delete_SequenQueue(SequenQueue * Q,elemtype *x) { if(Q->front==Q->rear) { return 0; } else { *x=Q->data[Q->front]; Q->front=(Q->front+1)%MAXSIZE; return 1; } } //取队头数据元素 int GetFront_SequenQueue(SequenQueue * Q,elemtype *x) { if(Q->front==Q->rear) { return 0; } else { *x=Q->data[Q->front]; return 1; } } int find(SequenQueue * Q,elemtype key) { int j=0,k; k=Q->front; if(Q->front==Q->rear) { return 0; } else { while(k!=Q->rear) { if(Q->data[k]!=key) { j++; k=(k+1)%MAXSIZE; } else if(Q->data[k]==key) { return j; } } return 0; } } void menu() { system("cls"); printf(" 1-创建队列 "); printf(" 2-input data "); printf(" 3-get length "); printf(" 4-判断队空 "); printf(" 5-修改队头元素 "); printf(" 6-取出队头元素 "); printf(" 7-查找 "); printf(" 8-输出 "); printf(" 9-判断队满 "); printf(" a-打印杨辉三角 "); printf(" #-quit "); printf("Please select: "); } void YHTriangle(int n) { SequenQueue *T; int a1,a2; //分别存放第i-1行上的相邻两个数据 int i,k,y; T=Init_SequenQueue(); printf(" %d ",1); Enter_SequenQueue(T,1); Enter_SequenQueue(T,1); for(i=2;i<=n;i++) { Enter_SequenQueue(T,1); for(k=0;k<i-1;k++) { y=Delete_SequenQueue(T,&a1); y=GetFront_SequenQueue(T,&a2); printf("%d ",a1); y=Enter_SequenQueue(T,a1+a2); } y=Delete_SequenQueue(T,&a2); printf(" %d ",a2); y=Enter_SequenQueue(T,1); } } int main() { SequenQueue * Q=NULL; char cmd; int i,len,isdo,m,n; elemtype x,key; system("cls"); menu(); while((cmd=getchar())!='#') { switch(cmd) { case '1': Q=Init_SequenQueue(); printf(" Created the Queue! "); printf(" "); break; case '2': printf(" Inputing data .... "); scanf("%d",&x); while(x!=0) { isdo=Enter_SequenQueue(Q,x); if(isdo==0) { printf("入队失败 "); } scanf("%d",&x); } printf(" "); break; case '3': printf(" Caculating the Length of the Queue... "); len=SequenQueue_Length(Q); printf("队列的长度为:%d ",len); printf(" "); break; case '4': isdo=SequenQueue_Empty(Q); if(isdo==0) { printf("队列不为空 "); } else if(isdo==1) { printf("队列为空 "); } printf(" "); break; case '5': isdo=Delete_SequenQueue(Q,&x); if(isdo==0) { printf("修改队头元素失败 "); } else if(isdo==1) { printf("删除的数据元素:%d,新的队头元素为%d ",x,Q->data[Q->front]); } printf(" "); break; case '6': isdo=GetFront_SequenQueue(Q,&x); if(isdo==0) { printf("取出失败 "); } else { printf("取出的数据元素:%d ",x); } printf(" Geting the data of the position... "); printf(" "); break; case '7': printf("请输入要查找的数值:"); scanf("%d",&key); isdo=find(Q,key); if(isdo==0) { printf("查找失败 "); } else { printf("该数值在队列中的位置:%d ",isdo+1); } printf(" "); break; case '8': printf(" Displaying the all data of the Queue!..."); m=Q->front; while(m!=Q->rear) { printf("%d ",Q->data[m]); m=(m+1)%MAXSIZE; } printf(" "); break; case '9': isdo=SequenQueue_Full(Q); if(isdo==1) { printf("队列已满 "); } else { printf("队列不满"); } break; case 'a': printf("请输入要打印的行数 "); scanf("%d",&n); YHTriangle(n); break; } fflush(stdin); system("pause"); menu(); } return 0; }