0
点赞
收藏
分享

微信扫一扫

第四周实验(一)

生活记录馆 2022-03-17 阅读 57

目录

1.双向循环链表排序

冒泡排序 

插入排序

 2.字符串镜像

3.表达式求值

 4.学生信息管理


1.双向循环链表排序

冒泡排序 

        插入排序还没写好,加上老师讲了。有空写了再把代码贴上。 

        用个end指针表示排序的范围,从1-n,到1-n-1,……直到1-1。

        然后用两个指针指向相邻的结点进行操作。注意结点更改链接时的操作顺序。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node* pre;
	node* nxt;
};
int len = 0;
node* head = NULL;
node* tail = NULL;
void create(){
	node *p;
	int x;
	while(cin >> x , x != 0 ){
		len++;
		p = new node;
		p->data = x;
		if(head == NULL){
			head = p;
			tail = p;
		}
		else{
			tail->nxt = p;
			p->pre = tail;
			tail = p;
		}
	}
	tail->nxt = head;
	head->pre = tail;

}
void print();
void Bubble_Sort(){
	node *cur,*next,*end;
	end = head;
	do{
		for(cur = head, next = cur->nxt; next != end ; cur = cur->nxt , next =  next->nxt){
			if(cur->data > next ->data){
		 	    cur->pre->nxt = next;
		 	    if(cur == head) head = next,end = next;
				next->nxt->pre  = cur;
				next->pre = cur->pre;
				cur->nxt = next->nxt;
				cur->pre = next;
				next->nxt = cur;
				node* tmp = cur;
				cur = next;
				next = tmp;
			}
//			print();
		}
		end = cur;
	}while(head != end);
}
void print(){
	node* p = head;
	do{
		cout << p->data << " ";
		p = p->nxt;
	}while(p != head);
	cout << endl;
}
int main(){
	create();
	Bubble_Sort();
	print();
} 
/*
  test: 1 7 8 9 10 2 3 5 4 6 0
  		2 1 1 1 1 3 3 3 4 4 1 0
  		9 8 7 6 5 4 3 2 1 0
  		3213 231 411 242 8934 3218 32153 132 0
  		1 2 3 4 5 6 7 8 9 10 0
*/ 

插入排序

        没写。 

 2.字符串镜像

         我把函数放结构体里面了。等我有空写个链表栈。

#include<bits/stdc++.h>
using namespace std;
char s[1001];
struct stk{
	int size = 0;
	char data[1001];
	void pop(){
		size--;
	}
	char top(){
		return data[size];
	}
	void push(char x){
		size++;
		data[size] = x;
	}
};
int main(){
	cin >> s;
	stk a;
	int len = strlen(s) , i ; 
	for(i = 0 ; i < len ; i++ ){
		if(s[i] == '&') break;
		a.push(s[i]);
	}
	i++;
	int ans = a.size ;
	for(; i < len-1 ; i++){
		if(s[i] == a.top()) a.pop();
		else {
			cout << "no" ;
			return 0 ;
		}
	}
	cout << ans <<endl;
}

3.表达式求值

        百度一下运算符的优先级,然后用个数组映射。减和除的时候注意一下顺序。

        小数点前后分别用整形和浮点型的变量来保存,因为两者的运算逻辑不同,然后相加即可。

        利用c++泛型编程的特性,设计一个栈模板就可以存字符和数字了。

#include<cstdio>
template <typename T>
struct stack{
	int size = 0;
	T data[1001];
	void pop(){
		size--;
	}
	T top(){
		return data[size];
	}
	void push(T x){
		size++;
		data[size] = x;
	}
};
int priority[128][128];
void init_priority(){
	priority['+']['+'] = 1;
	priority['-']['+'] = 1;
	priority['*']['+'] = 1;
	priority['/']['+'] = 1;
	priority['(']['+'] = 0;
	priority[')']['+'] = 1;
	priority['#']['+'] = 0;
	priority['+']['-'] = 1;
	priority['-']['-'] = 1;
	priority['*']['-'] = 1;
	priority['/']['-'] = 1;
	priority['(']['-'] = 0;
	priority[')']['-'] = 1;
	priority['#']['-'] = 0;
	priority['+']['*'] = 0;
	priority['-']['*'] = 0;
	priority['*']['*'] = 1;
	priority['/']['*'] = 1;
	priority['(']['*'] = 0;
	priority[')']['*'] = 1;
	priority['#']['*'] = 0;
	priority['+']['/'] = 0;
	priority['-']['/'] = 0;
	priority['*']['/'] = 1;
	priority['/']['/'] = 1;
	priority['(']['/'] = 0;
	priority[')']['/'] = 1;
	priority['#']['/'] = 0;
	priority['+']['('] = 0;
	priority['-']['('] = 0;
	priority['*']['('] = 0;
	priority['/']['('] = 0;
	priority['(']['('] = 0;
//	priority[')']['('] = 1;
	priority['#']['('] = 0;
	priority['+'][')'] = 1;
	priority['-'][')'] = 1;
	priority['*'][')'] = 1;
	priority['/'][')'] = 1;
	priority['('][')'] = -1;
	priority[')'][')'] = 1;
//	priority['#'][')'] = 1;
	priority['+']['#'] = 1;
	priority['-']['#'] = 1;
	priority['*']['#'] = 1;
	priority['/']['#'] = 1;
//	priority['(']['#'] = 0;
	priority[')']['#'] = 1;
	priority['#']['#'] = -1;
	
}
double calc(double x, char op ,double y){
	if(op == '+')
	return x + y;
	if(op == '-')
	return y - x;
	if(op == '*')
	return x * y;
	if(op == '/')
	return y / x;
}
int main(){
	stack<double> num;
	stack<char> op;
	init_priority();
	char Input[100];
	op.push('#');
	scanf("%s",Input);
	char c = Input[0]; int pos = 0;
	while(c != '#' || op.top() != '#'){
//		cout << c<< endl<< num.top()<<endl;
		int Int = 0; double Double = 0;
		if(c >= '0' && c <= '9') {
			while(c >= '0' && c <= '9'){
			Int = Int*10 + (c - '0');
//			cout << Int << endl;
			c = Input[++pos];
			}
			if( c == '.'){
				int y = 10;
				c = Input[++pos];
				while(c >= '0' && c <= '9'){
				Double += (c - '0')/(double)y;
				y *= 10;
				c = Input[++pos];
			  }
			}
//			cout << Int + Double<< " " << endl;
			num.push(Int + Double);
		}
		else {
//			cout << op.top() << " " << c << " "; 
//			cout << priority[op.top()][c] <<endl;
			if(priority[op.top()][c] == -1){
				op.pop();
				c = Input[++pos];
			}
			else if(priority[op.top()][c]){
//				cout << "op" <<endl; 
				char opr = op.top();op.pop();
				double x = num.top();num.pop();
				double y = num.top();num.pop();
				num.push(calc(x,opr,y));
//				cout << num.top()<<endl;
			}
			else {
				op.push(c);
				c = Input[++pos];
			}
		}
	}
	printf("%.2f", num.top());
	return 0;
}

 4.学生信息管理

        没写。

举报

相关推荐

第四周总结(一)

第四周

DVWA(第四周)

第四周作业

第四周 实验 bmp转YUV文件

Work·第四周

第四周总结

第四周练习总结

0 条评论