Demo entry 6733279

aa

   

Submitted by anonymous on Apr 17, 2018 at 13:37
Language: C. Code size: 5.5 kB.

#include "postfix.h"
#include "reverse.h"
#include "cmp.h"
#include "complex.h"
int main(){
	while(1){
		int n;
		printf("########MENU##########\n"); 
		printf("#####1:汉诺塔#########\n"); 
		printf("#####2:逆波兰表达式####\n"); 
		printf("#####3:k阶斐波那契###\n"); 
		printf("#####4:线性表比较###\n"); 
		printf("#####5:反转链表###\n");
		printf("#####6:复数加减乘除###\n");
		printf("#####0:EXIT###########\n"); 
		scanf("%d",&n) ;
		if(!n) break;
		switch(n){
			case 1:{
				int t;
				printf("请输入盘子的个数\n"); 
				scanf("%d",&t);
				hanoi(1,t,'A','B','C');
				break;
			}
			case 2:{
				char exp[1800];
				printf("请输入表达式\n"); 
				scanf("%s",exp);
				int i;
				int count=0;
				struct Stack * pStack=(struct Stack *)malloc(sizeof(struct Stack));//分配栈空间 
				initStack(pStack);//初始化栈 
				Push(pStack,'#');//压入一个栈底元素 
				show(exp,pStack); 
				break;
			}
			case 3:{
				int i;
				SqQueue myqueue;
				int k;
				printf("请输入阶数:\n");
				scanf("%d",&k);
				int max;
				printf("请输入最大值:\n");
				scanf("%d",&max); 
				InitQueue(&myqueue,k);
				int temp=0;
				while(temp<=max){
					int i;
					int sum=0; 
					for(i=0;i<=k-1;i++){
						sum+=myqueue.base[i];
					}
					temp=sum;
					if(sum>max){
						break; 
					}
					else{
						EnQueue(&myqueue,sum,k);	
					}
						
				}
				printf("%d\n",myqueue.base[(myqueue.rear-1)%k]);
				break;
			}
			case 4:{
				int t;
				printf("请输入循环次数:\n");
				scanf("%d",&t);
				int sum=t;
				while(t--){
					printf("________________________\n");
					printf("第%d次循环:\n",sum-t); 
					SqList L1,L2;
					L1.len=0;//默认长度为0 
					L2.len=0;
					int lenL1;
					int lenL2;
					printf("请输入L1的长度:\n");
					scanf("%d",&lenL1);
					L1.len=lenL1;
					CreateSqList(&L1,lenL1);
					printf("您输入的L1为:");
					printList(L1,lenL1);
					printf("\n请输入L2的长度:\n");
					scanf("%d",&lenL2);
					L2.len=lenL2; 
					CreateSqList(&L2,lenL2); 
					printf("您输入的L2为:");
					printList(L2,lenL2);
					if(lenL1==0&&lenL2==0){
						printf("L1=L2\n");
					} 
					else if(lenL1==0&&lenL2!=0){
						printf("L1<L2\n");
					}
					else if(lenL1!=0&&lenL2==0){
						printf("L1>L2\n");
					}
					else{	
						compare(&L1,&L2);
					} 
				} 
				break;
			} 
			case 5:{
				List *head;
				head=(pList)malloc(sizeof(List));
				if(head==NULL){
					return ERROR;
				}
				head->next=NULL;
				CreateList(head);
				printf("原链表:\n") ;
				outputList(head);
				printf("倒转之后输出:\n");
				reverse(head);
				outputList(head);
				break;
			}
			case 6:{
				int t;
				printf("请输入循环次数:"); 
				scanf("%d",&t);
				fflush(stdin);
				while(t--){
					complex cpx1,cpx2;
					pcomplex pc = NULL;
					pc = CreateComplex();
					if(pc==NULL) return 1;
					float fReal,fImage;
					char buffer[100];
					char ch;
					printf("____________________\n");
					printf("HAVE FUN WITH COMPLEX\n");
					printf("INPUT THE FIRST COMPLEX LIKE A B\n");
					fgets(buffer,100,stdin);
					sscanf(buffer,"%f %f",&fReal,&fImage);
					if(AssignComplex(&cpx1,fReal,fImage)==1) return 1;
					if(fImage>=0){
						printf("THE FIRST ONE YOU INPUT:%.2f + %.2fi\n",cpx1.fReal,cpx1.fImage);
					}
					else{
						printf("THE FIRST ONE YOU INPUT:%.2f - %.2fi\n",cpx1.fReal,cpx1.fImage);
					}
					printf("\n");
					
					printf("INPUT THE SECOND COMPLEX LIKE A B\n");
					fgets(buffer,100,stdin);
					sscanf(buffer,"%f %f",&fReal,&fImage);
					fflush(stdin);
					printf("输入操作符号(+-*/):");
					char opra;
					scanf("%c",&opra);
					getchar();
					if(AssignComplex(&cpx2,fReal,fImage)==1) return 1;
					if(fImage>=0){
						printf("THE SECOND ONE YOU INPUT:%.2f + %.2fi\n",cpx2.fReal,cpx2.fImage);
					}
					else{
						printf("THE SECOND ONE YOU INPUT:%.2f - %.2fi\n",cpx2.fReal,cpx2.fImage);
					}
					printf("\n");
					switch(opra){
						case '+':{//加法运算 
							AddComplex(cpx1,cpx2,pc);
							if(pc->fImage>=0){
								printf("SUM:%.2f + %.2fi\n",pc->fReal,pc->fImage);	
				//		printf("SUM:%.2f + %.2fi\n",GetReal(*pc),pc->fImage);	
							}
							else {
								printf("SUM:%.2f - %.2fi\n",pc->fReal,fabs(pc->fImage));	
				//		printf("SUM:%.2f - %.2fi\n",GetReal(*pc),fabs(pc->fImage));	
							}
							break;
						}
						case '-':{//减法运算 
							SubComplex(cpx1,cpx2,pc);
							if(pc->fImage>=0){
								printf("SUB:%.2f + %.2fi\n",pc->fReal,pc->fImage);		
							}
							else {
								printf("SUB:%.2f - %.2fi\n",pc->fReal,fabs(pc->fImage));	
							}
							break;
						}
						case '*':{//乘法运算 
							MultiComplex(cpx1,cpx2,pc);
							if(pc->fImage>=0){
								printf("MULTI:%.2f + %.2fi\n",pc->fReal,pc->fImage);		
							}
							else {
								printf("MULTI:%.2f - %.2fi\n",pc->fReal,fabs(pc->fImage));	
							}
							break;
						}
						case '/':{//除法运算 
							int a=DivideComplex(cpx1,cpx2,pc);
							if(a==2){
								printf("除数为0\n"); 
							}
							else{
								if(pc->fImage>=0){
								printf("DIVIDE:%.2f + %.2fi\n",pc->fReal,pc->fImage);		
							}
							else {
								printf("DIVIDE:%.2f - %.2fi\n",pc->fReal,fabs(pc->fImage));	
							}
								
							} 
							break;
						}
						default:{
							printf("WRONG OPERATOR!!!!\n");
							break;
						} 
					}
					DestroyComplex(&pc);//销毁操作 
				} 
				break;
			}
		}
	} 
	
	return 0;
}
// 思考:程序的执行时间很长,递归的效率越到后面越低

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).