c/c++练习–18

c/c++练习–18


  • 习题来源:C语言经典编程282例

171.用栈实现行编辑程序

编写一个简单的行编辑程序,主要功能是将用户输入的信息存入用户的数据区,当用户发现输入错误时,可补进一个“#”号表示前一个字符无效,当发现错误较多时,可补进一个“@”,表示前面写过的字符均无效,回车表示该行输入完毕。

#include <stdio.h>  
#include <stdlib.h>
#include <ctype.h>

#define LENGTH      100

typedef struct stcak{
    char        *top, *base;
}sStack,*psStack;

psStack create_list(void){
    psStack head;

    head = (psStack)calloc(1,sizeof(sStack));
    if(!head) exit(0);
    head->base =  (char*)calloc(1, LENGTH);
    head->top = head->base; 
    return(head);
}  

int is_empty(psStack s){
    if(s->base == s->top )return(1);
    return 0;
}

int is_full(psStack s){
    if(s->top - s->base >= LENGTH) return(1);
    return 0;
}

void    push(psStack s, char num){
    if(!is_full(s))
        *s->top++ = num; 
    else printf("wrong\n");
}

char        pop(psStack s){
    if(is_empty(s)) printf("void\n");
    else    return(*s->top--);
}

void    clear_s(psStack s){
    s->top = s->base;
}


void    conv(psStack s){
    char        ch;
    while((ch = getchar()) != EOF && ch != '\n' ){
        switch(ch){
        case  '#':pop(s);break;
        case '@':clear_s(s);break;
        default:push(s, ch);
        }
    }

    while(s->top != s->base )
        printf("%c", *--s->top);  
}

int main(void){
    psStack s;
    int num, b;
    s = create_list(); 
    printf("please input a string:");
    scanf("%d %d", &num,&b);
    conv(s);
    return(EXIT_SUCCESS);
}                                                   

172.用栈设置密码

使用栈设置一个密码,当输入错误密码时,系统提示密码错误,输入错误3次退出,输入正确的密码后,显示密码正确,程序密码为13579

#include <stdio.h>  
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define LENGTH      100

typedef struct stcak{
    char        *top, *base;
}sStack,*psStack;

psStack create_list(void){
    psStack head;

    head = (psStack)calloc(1,sizeof(sStack));
    if(!head) exit(0);
    head->base =  (char*)calloc(1, LENGTH);
    head->top = head->base; 
    return(head);
}  

int is_empty(psStack s){
    if(s->base == s->top )return(1);
    return 0;
}

int is_full(psStack s){
    if(s->top - s->base >= LENGTH) return(1);
    return 0;
}

void    push(psStack s, char num){
    if(!is_full(s))
        *s->top++ = num; 
    else printf("wrong\n");
}

char        pop(psStack s){
    if(is_empty(s)) printf("void\n");
    else    return(*s->top--);
}

void    clear_s(psStack s){
    s->top = s->base;
}


void    conv(psStack s){
    char        pwd[] = "13579";
    char        ch;
    int i=0,flag=0;
    int t_flag = 0;
    while(i<3 ){
        printf("please input the password:");
        if(flag)clear_s(s);
        while((ch=getchar())!=EOF && ch != '\n')
            push(s,ch);
        for(int j=0;j<strlen(pwd);j++){
            if(*--s->top == pwd[strlen(pwd)-1-j]){
                if(j==strlen(pwd)-1) t_flag=1;
                continue;             
            }
            else{
                flag = 1;
                break;    
            }
        }
        if(t_flag) break;
        i++;
    }
    if(i>=3)printf("3次错误\n") ;
    else printf("welcome\n");
}

int main(void){
    psStack s;
    int num, b;
    s = create_list(); 
    conv(s);
    return(EXIT_SUCCESS);
}                                                       

173.括号匹配检测

编写检测括号是否匹配的程序,其主要功能是对输入的一组字符串进行检测,当输入的字符串中括号(包括{},[],())匹配时输出Yes,否则输出No

#include <stdio.h>  
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define LENGTH      100

typedef struct stcak{
    char        *top, *base;
}sStack,*psStack;

psStack create_list(void){
    psStack head;

    head = (psStack)calloc(1,sizeof(sStack));
    if(!head) exit(0);
    head->base =  (char*)calloc(1, LENGTH);
    head->top = head->base; 
    return(head);
}  

int is_empty(psStack s){
    if(s->base == s->top )return(1);
    return 0;
}

int is_full(psStack s){
    if(s->top - s->base >= LENGTH) return(1);
    return 0;
}

void    push(psStack s, char num){
    if(!is_full(s))
        *s->top++ = num; 
    else printf("wrong\n");
}

char        pop(psStack s){
    if(is_empty(s)) printf("void\n");
    else    return(*s->top--);
}

void    clear_s(psStack s){
    s->top = s->base;
}


void    conv(psStack s){
    char        ch;

    while((ch=getchar()) != EOF && ch != '\n'){
        if('[' == ch || '(' ==ch || '{' == ch)push(s,ch);
        if(']' == ch || ')' ==ch || '}' == ch)pop(s);     
    }
    if(s->top <= s->base)printf("Yes\n");
    else printf("No\n");
}

int main(void){
    psStack s;
    int num, b;
    s = create_list(); 
    conv(s);
    return(EXIT_SUCCESS);
}                                                   

174. 用栈计算多项式

#include <stdio.h>  
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define LENGTH      100

typedef struct stcak{
    int     *top, *base;
}sStack,*psStack;

psStack create_list(void){
    psStack head;

    head = (psStack)calloc(1,sizeof(sStack));
    if(!head) exit(0);
    head->base =  (int*)calloc(1, LENGTH);
    head->top = head->base; 
    return(head);
}  

int is_empty(psStack s){
    if(s->base == s->top )return(1);
    return 0;
}

int is_full(psStack s){
    if(s->top - s->base >= LENGTH) return(1);
    return 0;
}

void    push(psStack s, int num){
    if(!is_full(s))
        *s->top++ = num; 
    else printf("wrong\n");
}

int     pop(psStack s){
    if(is_empty(s)) printf("void\n");
    else    return(*--s->top);
}

void    clear_s(psStack s){
    s->top = s->base;
}


void    conv(psStack s, int x, int n){
    int num, nn,m;

    push(s,1);
    push(s,2*x);
    for(int i=2;i<=n;i++){
        nn = pop(s);
        m = pop(s);
        num = 2*x*nn - 2*(i-1)*m;
        push(s, m);
        push(s, nn);
        push(s, num);
    }  
    printf("%d\n",num);
}

int main(void){
    psStack s;
    int num=4, b=3;
    s = create_list(); 
    conv(s,3,4);
    return(EXIT_SUCCESS);
}                                                   

175.用递归计算多项式

#include <stdio.h>  
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int f(int x,int n){
    if(n==0)    return 1;
    else{
        if(n==1) return 2*x;
        else    return 2*x*f(x, n-1) - 2* (n-1)*f(x, n-2);
    }
}

int main(void){

    printf("%d",f(3,4));

    return(EXIT_SUCCESS);
}                                                   

176.链队列

采用链式存储法编程实现元素入队、出队以及将队列中的元素显示出来,要求整个过程以菜单形式显示。

#include <stdio.h>  
#include <stdlib.h>

struct node{
    int data;
    struct node *n;
};

struct que{
    struct node *h, *t;
};

void    create_que(struct que **q){
    struct node *h;
    h = (struct node *)calloc(1,sizeof(struct node));
    h->n = NULL;
    (*q)->h = h;
    (*q)->t = h;
}

void    push_que(struct que *q){
    struct node *h;
    int num=0,x;

    printf("input the number of number you want to insert:");
    scanf("%d",&num);
    for(int i=0;i<num;i++){
        scanf("%d", &x);
        h = (struct node *)calloc(1,sizeof(struct node));
        h->n = NULL;
        h->data = x;  
        q->t->n=h;
        q->t = h;     
    }
}

void    delete_que(struct que *q){
    struct node *n=q->h;
    while(n->n->n) n = n->n;
    free(n->n);
    q->t = n;
    n->n = NULL;
}

void    display(struct que *q){
    struct node *n=q->h;

    while(n){
        printf("%d\t",n->data);
        n = n->n;
    }     
}

int main(void){
    struct que  *q;
    struct node *n;
    int num=0;
    int flag = 1;

    q = (struct que *)calloc(1,sizeof(struct que));

    do{
        printf("****************************************\n");
        printf("1. create a new queue\n");
        printf("2. insert a new number\n");
        printf("3. delete a new number\n");
        printf("4. display a new queue\n");
        printf("5. quit\n");
        printf("****************************************\n");
        printf("please input your number:");
        scanf("%d",&num);

        switch(num){
            case 1:create_que(&q);display(q);break;
            case 2:push_que(q);display(q);break;
            case 3:delete_que(q);display(q);break;
            case 4:display(q);display(q);break;
            case 5:flag = 0;continue;
        }

    }while(flag);
    n = q->h;
    while (n){
        q->h = n->n;
        free(n);
        n=q->h;
    }

    free(q);
    return(EXIT_SUCCESS);
}                                                       

177.简单文本编辑器

要求实现3个功能:第一,要求对指定行输入字符串,第二,删除指定行的字符串,第三,显示输入的字符串的内容

#include <stdio.h>  
#include <stdlib.h>
#include <string.h>

typedef struct nn{
    int     len;
    int     num;
    char        data[100];
    struct nn   *n;
}node; 

void init(node *h) {
    for(int i=0;i<100;i++){
        (h+i)->num = i+1;
        (h+i)->len = 0;
        *((h+i)->data)='\0';
        if(i<99)    (h+i)->n = h+i+1;
        else    (h+i)->n = NULL;
    }
}

void    input(node *h){
    int num;
    node *p=h;
    int     id=1;

    printf("please input the line you want to input:");
    scanf("%d",&num);
    scanf("%s", h[num-1].data);
    h[num-1].len = strlen (h[num-1].data);
}

void    delete_num(node *h){
    int num;
    printf("please input the line you want to del:");
    scanf("%d",&num);
    h[num-1].len=0;
    *(h[num-1].data) = '\0';
}

void    display(node *q){
    while(q){
        if(q->data == NULL) printf("%d\t%d\n",q->num, q->len);
        else    printf("%d\t%d\t%s\n",q->num, q->len, q->data);
        q = q->n;
    }     
}

int main(void){
    node    h[100]; 
    int     num=0;
    int     flag = 1;    

    init(h);

    do{
        printf("****************************************\n");
        printf("1. Input\n");
        printf("2. Delete\n");
        printf("3. display\n");
        printf("4. quit\n");
        printf("****************************************\n");
        printf("please input your number:");
        scanf("%d",&num);

        switch(num){
        case 1:input(h);display(h);break;
        case 2:delete_num(h);display(h);break;      
        case 3:display(h);break;
        case 4:flag = 0;continue;
        }

    }while(flag);



    return(EXIT_SUCCESS);
}                                               

178.使二进制数特定位翻转

实现使其低4位翻转

#include <stdio.h>  
#include <stdlib.h>

int main(void){
    unsigned int a,b;

    printf("please input 2 number:");
    scanf("%u",&a);
    b = 15  ;
    printf("%u",a^b);

    return(EXIT_SUCCESS);
}

179.将输入的数左移两位并输出

#include <stdio.h>  
#include <stdlib.h>

int main(void){
    unsigned int a;

    printf("please input 1 number:");
    scanf("%u",&a);    
    printf("%u",a<<2);
    return(EXIT_SUCCESS);
}                                                   

180.编写循环移位函数

#include <stdio.h>  
#include <stdlib.h>

unsigned    bit_cir(unsigned a, int n){
    unsigned b;
    if(n>0){
        while(n--){
            b = a&(1<<31);
            b >>= 31;
            a  <<= 1;
            a |=b;
        }    
    }
    else{
        while(n++){
             b = a&1;
             a  >>= 1;
             a |=b<<31;
        }
    }
    return(a);
}

int main(void){
    unsigned int a;

    printf("please input 1 number:");
    scanf("%u",&a);    
    printf("%u",bit_cir(a,-2));
    return(EXIT_SUCCESS);
}                                                       
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值