C和指针#2.8编程练习

1.题目1:
C和指针#2.8编程练习_第1张图片

increment.c-->int increment(int)    
negate.c-->int negate(int)
main.c-->void main()
编辑好后,在linux用命令gcc main.c increment.c negate.c -o main即可,主要考察编译和链接C程序的命令。
//increment.c
#include
int increment(int a)
{
    return a+1;
}
//negant.c
#include
int negate(int b)
{
    return -b;
}
//main.c
#include
int main()
{
    printf("%d\n",increment(10));
    printf("%d\n",increment(0));
    printf("%d\n",increment(-10));
    printf("%d\n",negate(10));
    printf("%d\n",negate(0));
    printf("%d\n",negate(-10));
}

2.题目二:
这里写图片描述

括号匹配问题,本人用栈将其实现,此时已经是回头看这本书了,决定用C++里的模板实现

顺序栈的实现参见本人github源码

匹配思想:从左至右扫描一个字符串(或表达式),则每个右括号将与最近遇到的那个左括号相匹配。
则可以在从左至右扫描过程中把所遇到的左括号存放到堆栈中。每当遇到一个右括号时,就将它与栈顶的左括号(如果存在)相匹配,同时从栈顶删除该左括号。 
算法思想:设置一个栈,当读到左括号时,左括号进栈。当读到右括号时,则从栈中弹出一个元素,
与读到的左括号进行匹配,若匹配成功,继续读入;否则匹配失败,返回FLASE。
另外,在算法的开始和结束时,栈都应该是空的.所以匹配到最后还要判断栈是否为空,若非空,则说明匹配失败.
#ifndef _BRACEMATCH_
#define _BRACEMATCH_

#include"SeqStack.h"
BOOL BraceMatch(char *str)
{
    SeqStack<char> st(0, 20);
    while(*str != '\0')
    {
        switch(*str)
        {
        case '[':
        case '(':
        case '{':
            st.Push(*str);
            break;
        case ']':
            if((st.GetTop()) != '[')
                return FALSE;
            else
                st.Pop();
            break;
        case ')':
            if((st.GetTop()) != '(')
                return FALSE;
            else
                st.Pop();
            break;
        case '}':
            if((st.GetTop()) != '{')
                return FALSE;
            else
                st.Pop();
            break;
        default:
            break;
        }
        str++;
    }
    if((st.IsEmpty()) && (*str=='\0'))
        return TRUE;
}
#endif

你可能感兴趣的:(C,//【编程语言】)