递归和栈编程训练

1.   题目: 利用递归方法实现一个函数,该函数能够实现n的阶乘,即 n! = n*(n-1)*…*3*2*1;

#include<stdio.h>
long jieceng(int n)
{
long s=1;
if(n>1)
return n*jieceng(n-1);
else 
return 1;
// peintf("%ld",s);
}


int main()
{
int n;
long s;
scanf("%d",&n);
printf("%ld",jieceng(n));
return 0;
}

1.   题目:利用字符数组实现一个先入后出的栈结构,并提供栈操作的push和pop的接口


#include <stdio.h>  
int i=10;  
int push(int a[],int n)  
{  
     if(i<0)  
     {  
         printf("栈满!\n");  
           return -1;  
     }  
     a[--i]=n;  
     return 1;  
}  
int pop(int a[])  
{      


if(i<0)  




     {  
          printf("栈空!\n");  
          return -1;  
     }    
    printf("%d\t",a[i++]);  
    return 1;  
}  
int main()  
{  
     int a[10]={0};  
     int j=0;  
     for(j=0;j<10;j++)  
     {  
          push(a,j);  
     }  
     for(j=0;j<10;j++)  
     {  
          pop(a);  
     }  
     printf("\n");  
     return 0;  

1.   题目:输入一个表达式字符串,如1+3*4-6,输出这个表达式的值.

提示:需要建立两个栈结构,一个为整形存放操作数,另一个为字符型,存放运算符,运算符的进栈要和在站顶的元素比较优选级如果低于栈顶元素则进行一次运算,要求至少实现正整数的加减乘除四则运算,如100- 5*4 -50/10 =75 


//输入一个表达式字符串,如1+3*4-6,输出这个表达式的值.
#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
struct count  
{  
     int num;  
     char mark;  
     struct count *next;  
};  
struct count *top1;  
struct count *top2;  
void creat_list()  
{  
     top1=(struct count*)malloc(sizeof(struct count));  
     top2=(struct count*)malloc(sizeof(struct count));  
     top1=NULL;  
     top2=NULL;  
}  
void insert_list1(int num)  
{  
     struct count *p=(struct count *)malloc(sizeof(struct count));  
     struct count *q=top2;  
    if(q!=NULL)  
    {  
     if((q->mark)=='*')  
     {  
        p=top1;  
        num=num*(top1->num);  
    top1=top1->next;  
    top2=top2->next;  
     }  
     else if((q->mark)=='/')  
     {  
        num=(top1->num)/num;  
    top2=top2->next;  
        top1=top1->next;  
     }  
    }  
     p->num=num;  
     p->next=top1;  
     top1=p;  
}  
void display_list1()  
{  
     struct count *p=top1;  
     printf("num:");  
     while(p)  
     {  
        printf("%d\t",p->num);  
    p=p->next;  
     }  
     printf("\n");  
}  
void display_list2()  
{  
     struct count *p=top2;  
     printf("mark:");  
     while(p)  
     {  
        printf("%c\t",p->mark);  
    p=p->next;  
     }  
     printf("\n");  
}  
void insert_list2(char ch)  
{  
     if(ch=='\0');  
     else  
     {  
      struct count *p=(struct count*)malloc(sizeof(struct count));  
      p->mark=ch;  
      p->next=top2;  
      top2=p;  
     }  
}  
void show(char a[])  
{  
     int n=0;  
     int i=0;  
     int num=0;  
     n=strlen(a);  
     for(i=0;i<=n;i++)  
     {  
          if(a[i]>='0'&&a[i]<='9')  
      {  
         num=num*10+a[i]-48;  
      }  
      else   
      {  
         insert_list1(num);  
         if(i!=n)  
         {  
           insert_list2(a[i]);  
         }  
        
         num=0;  
      }  
     }  
}  
void math_list()  
{  
     int num=0;  
     if((top1->next)==NULL)  
     {  
           ;  
     }  
     else  
     {  
       while(top1!=NULL)  
       {  
         if(top2==NULL)  
     {  
        num=num+top1->num;  
        top1=top1->next;  
     }  
     else  
     {  
          switch(top2->mark)  
      {  
         case '+':  
            num=num+top1->num;  
        top1=top1->next;  
        top2=top2->next;  
        break;  
         case '-':  
            num=num-top1->num;  
        top1=top1->next;  
        top2=top2->next;  
        break;  
        default:  
            printf("input error!");  
      }  
         }  
       }  
     }  
        
       
        
      printf("the result is :%d\n",num);  
}  
int main()  
{  
     char a[20];  
     creat_list;  
     printf("please input\n");  
     scanf("%s",a);  
     show(a);  
    // display_list1();  
    // display_list2();  
     math_list();  
     return 0;  


1.   题目:利用递归函数调用方式,将所输入的n个字符以相反顺序打印出来

#include<stdio.h>
void reverse (char a[]);
void main()
{
char a[50];
gets(a);
reverse(a);
}
void reverse (char a[])
{int i=0;
if(a[i]!='\0')
{
reverse(&a[i+1]);
printf("%c",a[i]);
}
else return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值