nefu数据结构课设

1:停车场

//1.3停车场管理 
#include<iostream>
#include<string>
using namespace std;
class Stack{
	private:
		int *data;
		int size;
		int top;
		int Maxsize = 10;		
	public:
		Stack();
		~Stack();
		void Printf();
		bool IsEmpty();
		bool IsFull();
		void Push(int num);
		int Pop();
		int Top();
		int GetLength();
		bool Find(int number);
};
//初始化 
Stack::Stack(){
	data = new int [Maxsize];
	size = 0;
	top = -1;
}
Stack::~Stack(){
	delete []data;
}
void Stack::Printf(){
	int num = top;
	while(num>=0){
		cout<<data[num--]<<" ";
	}
	if(top!=-1) cout<<"号"<<endl;
}
bool Stack::IsEmpty(){
	return top == -1; 
}
bool Stack::IsFull(){
	return top ==Maxsize-1;
}
int Stack::Pop(){
	if(!IsEmpty()){
		return data[top--];
	}
}
void Stack::Push(int num){
	if(IsFull()){
		return ;
	}else{
		data[++top] = num;
	}
}
int Stack::Top(){
	return data[top];
}
int Stack::GetLength(){
	return top+1;
}
bool Stack::Find(int number){
	int num = top;
	while(num>=0){
		if(number==data[num--]){
			return true;
		}
	}
	return false;
}

class Node{
	public: 
		int data;
		Node *next;
		Node() {};
		Node(int _data) { data = _data; next = nullptr; }
};


class Queue{
	private:
		int count;
		Node *head;
		Node *last;
	public:
		Queue();
		~Queue();
		bool IsEmpty();
		void push(int num);
		bool Pop();
		int GetFront();
		int GetBack();
		void Printf();
		int GetLenth();		
};
Queue::Queue(){
	head = new Node();
	last = head;
	count = 0;
}
Queue::~Queue(){
	while(head->next!=nullptr){
		Pop();
	}
	delete head;
}
bool Queue::IsEmpty(){
	return count==0;
}
void Queue::push(int  num){
	count++;
	Node *newNode = new Node(num);
	last->next = newNode;
	last = newNode;
}
bool Queue::Pop(){
	if(IsEmpty()){
		return false;
	}
	count--;
	Node *n = head->next;
	head->next = n->next;
	delete n;
	if(head->next==nullptr){
		last=head;
	}
	return true;
}
int Queue::GetFront(){
	if(IsEmpty()){
		return -1;
	}
	return head->next->data;
}
int Queue::GetBack(){
	if(IsEmpty()){
		return -1; 
	}
	return last->data;
}
int Queue::GetLenth(){
	return count;
}
void Queue::Printf(){
	if(IsEmpty()){
		return ; 
	}
	Node *p=head->next;
	while(p!=nullptr){
		cout<<p->data<<" ";
		p=p->next;
	}
	
	if(!IsEmpty()) cout<<"号"<<endl;
	delete p;
}
int main(){
	cout<<"输入:(进入/退出(1/0)) (车号)"<<endl;
	int ins,num;
	Stack st;
	Stack st_temp;
	Queue q;
	while(cin>>ins>>num){
		if(num<=0){
			cout<<"车号为"<<num<<",结束 "<<endl;
			break;
		}
		if(ins==1){
			if(st.IsFull()){
				cout<<"停车场位满,"<<num<<"入便道"<<endl;
				q.push(num);
			}else{
				st.Push(num);	
			}
		}else if(ins==0){
			if(st.GetLength()==0){
				cout<<"停车场为空,无需退出"<<endl;
				continue; 
			}
			if(st.Find(num)==false){
				cout<<"车号输入有误"<<endl;
				continue; 
			}
			while(st.Top()!=num){
				st_temp.Push(st.Pop());	
			}
			st.Pop();
			while(!st_temp.IsEmpty()){
				st.Push(st_temp.Pop());
			}
			cout<<num<<"号已退出停车场"<<endl; 
			if(!q.IsEmpty()){
				cout<<"便道有车等待,便道第一个"<<q.GetFront()<<"号入停车场"<<endl;
				st.Push(q.GetFront());
				q.Pop();
			}
		}else{
			cout<<"非法输入"<<endl;
			continue; 
		}
		cout<<"此时,停车场有"<<st.GetLength()<<"辆车"<<endl;
		st.Printf();
		cout<<"此时,便道有"<<q.GetLenth()<<"辆车"<<endl;
		q.Printf();	
	}
} 

2:真值表

#include <iostream>
#include <stack>
#include <cstring>

using namespace std;

// 定义二叉树节点的结构体
typedef struct Node
{
    char data[100];           // 数据域
    struct Node *leftChild;    // 左子树指针
    struct Node *rightChild;   // 右子树指针
    struct Node *father;       // 根结点指针
} BiTreeNode;

// 定义堆栈节点的结构体
typedef struct
{
    BiTreeNode *dizhi[100];
    int top;
} SeqStack1;

// 函数:判断字符是否为运算符
bool isOperator(char c)
{
    return (c == '&' || c == '|' || c == '!');
}

// 获取运算符的优先级
int getPrecedence(char op)
{
    if (op == '!')
        return 3;
    else if (op == '&')
        return 2;
    else if (op == '|')
        return 1;
    else
        return 0;
}

// 将中缀表达式转换为后缀表达式
void infixToPostfix(char infix[], char postfix[])
{
    stack<char> s;
    int j = 0;

    for (int i = 0; i < strlen(infix); i++)
    {
        char c = infix[i];

        if (isalnum(c))
        {
            postfix[j++] = c;
        }
        else if (c == '(')
        {
            s.push(c);
        }
        else if (c == ')')
        {
            while (!s.empty() && s.top() != '(')
            {
                postfix[j++] = s.top();
                s.pop();
            }
            s.pop(); // 弹出 '('
        }
        else
        {
            while (!s.empty() && getPrecedence(s.top()) >= getPrecedence(c))
            {
                postfix[j++] = s.top();
                s.pop();
            }
            s.push(c);
        }
    }

    while (!s.empty())
    {
        postfix[j++] = s.top();
        s.pop();
    }

    postfix[j] = '\0';
}

// 函数:根据后缀表达式构建二叉表达树
BiTreeNode *buildExpressionTree(char postfix[])
{
    stack<BiTreeNode *> s;

    for (int i = 0; i < strlen(postfix); i++)
    {
        char c = postfix[i];

        BiTreeNode *newNode = new BiTreeNode;
        strcpy(newNode->data, "");
        newNode->leftChild = nullptr;
        newNode->rightChild = nullptr;
        newNode->father = nullptr;

        if (isalnum(c))
        {
            strcpy(newNode->data, &c);
            s.push(newNode);
        }
        else
        {
            newNode->rightChild = s.top();
            s.pop();
            newNode->leftChild = s.top();
            s.pop();
            strcpy(newNode->data, &c);
            s.push(newNode);
        }
    }

    return s.top();
}


// 函数:递归地评估表达树
bool evaluateExpressionTree(BiTreeNode *root, bool variableValues[])
{
    if (root == nullptr)
        return false;

    if (isalpha(root->data[0]))
    {
        // 如果是变量,返回其值
        int index = root->data[0] - 'a'; // 假设变量是单个字母
        return variableValues[index];
    }
    else
    {
        // 如果是运算符,递归评估左右子树
        bool leftValue = evaluateExpressionTree(root->leftChild, variableValues);
        bool rightValue = evaluateExpressionTree(root->rightChild, variableValues);

        // 根据运算符执行操作
        if (root->data[0] == '&')
            return leftValue && rightValue;
        else if (root->data[0] == '|')
            return leftValue || rightValue;
        else if (root->data[0] == '!')
            return !rightValue;
        else
            return false; // 无效的运算符
    }
}

// 函数:显示真值表
void displayTruthTable(char variables[], char postfix[])
{
    int numVariables = strlen(variables);
    int numCombinations = 1 << numVariables; // 2^n 种组合

    cout << "真值表:\n";

    // 打印表头
    for (int i = 0; i < numVariables; i++)
    {
        cout << variables[i] << "\t";
    }
    cout << "表达式\n";

    // 打印分隔符
    for (int i = 0; i < numVariables; i++)
    {
        cout << "--------";
    }
    cout << "---------\n";

    // 评估并打印每种组合
    for (int i = 0; i < numCombinations; i++)
    {
        bool variableValues[26]; // 假设变量是单个字母

        // 根据组合给变量赋值
        for (int j = 0; j < numVariables; j++)
        {
            variableValues[j] = (i & (1 << (numVariables - 1 - j))) != 0;
            cout << variableValues[j] << "\t";
        }

        // 评估当前组合下的表达式
        BiTreeNode *root = buildExpressionTree(postfix);
        bool result = evaluateExpressionTree(root, variableValues);
        cout << result << "\n";
    }
}

int main()
{
    char infix[100], postfix[100], variables[26];

    cout << "输入中缀表达式:";
    cin >> infix;

    // 从中缀表达式提取唯一的变量
    int varIndex = 0;
    for (int i = 0; i < strlen(infix); i++)
    {
        char c = infix[i];
        if (isalpha(c) && strchr(variables, c) == nullptr)
        {
            variables[varIndex++] = c;
        }
    }
    variables[varIndex] = '\0';

    infixToPostfix(infix, postfix);

    cout << "后缀表达式:" << postfix << endl;

    displayTruthTable(variables, postfix);

    return 0;
}
//a&b|c
//(a&b)|c&d

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值