某农业大学数据结构A-第5周作业

1.双向循环链表排序

【问题描述】实现不带头结点的双向循环链表的创建,然后实现该双向循环链表上数据的排序。(方法自定)
【输入形式】随机的数据
【输出形式】排序后的数据
【样例输入】5 7 2 8 1 3 4 9 6 0
【样例输出】1 2 3 4 5 6 7 8 9

【提示】0代表数据输入的结束

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100

int main()
{
	int a[MAXSIZE];
	printf("");
	int m;
	for(int n=0;n<MAXSIZE ;n++)
	{
		scanf("%d",&a[n]);
		if(a[n]==0)
		{
			m=n;
			break;
		}
	}
	
	int i,j,k;
	for(i=1;i<=m;i++)
	{
		for(j=0;j<=m-i;j++)
		if(a[j]>a[j+1])
		{
			k=a[j];
			a[j]=a[j+1];
			a[j+1]=k; 
		}
	}
	int s;
	for(s=1;s<=m;s++)
	printf("%d ",a[s]);
	return 0;
 } 
2.字符串镜像

【问题描述】试写一个算法,识别依次读入的一个以“@”为结束符的字符序列是否为形如 “序列1&序列2”  模式的字符序列。其中序列1和序列2都不含字符 “&”,且序列2是序列1的逆序列。例如,“ a+b&b+a ”是属该模式的字符序列,而 “1+3&3-1”则不是。

【输入形式】
 以@为结尾的一串字符

【输出形式】
 若符合模式则输出字符串长度,否则输出no

【样例输入】
 a+b&b+a@

【样例输出】
 3

【注意】本题务必使用顺序栈或者链式栈的一种来实现,否则不给分。

#include <stdio.h>
#include <string.h>
typedef char ElemType;
#define MaxSize 100
typedef struct
{
	ElemType data[MaxSize];
	int top;
} STACK;

void InitStack(STACK *s)
{
	s->top=-1;
}

int Push(STACK *S, ElemType x)
{
	if (S->top==MaxSize -1){
		printf("\n Stack is full!");
		return 0;
	}
 
		S->top++;
		S->data[S->top]=x;
		return 1; 
}

int Empty(STACK *S)
{
		return(S->top==-1?1:0);
}
int Pop(STACK *S,ElemType *x)
{
	if (Empty(S))
		return 0;
 
		*x=S->data[S->top];
		S->top--;
		return 1;
 
}
int GetTop(STACK *S,ElemType *x)
{
	if (Empty(S))
		return 0;
 
		*x=S->data[S->top];
		return 1;
 
}
 
int main()
{
	int i,len;
	STACK st;
	ElemType e,a[MaxSize];
	printf("Input :");

	InitStack(&st);
	gets(a);
	len=strlen(a);
	for(i=0;;i++)
	{
		if(a[i]=='&'||a[i]=='@')
			break;
		else
			Push(&st,a[i]);
	}
	if((len-2)!=2*i)/*判断字符串在&左右的序列是否长度相同,若不同直接输出不是*/
    {
	    printf("no");
	    continue;
	}
	for(i++;;i++)
	{
		if(a[i]=='@')
			break;
		else
		{
		    GetTop(&st,&e);
		    if(e!=a[i])
			    break;
		    else
			    Pop(&st,&e);
		}
	}
	if(Empty(&st))
		printf("%d",(len-2)/2);
	else
		printf("no");
    
}
3.表达式求值

【问题描述】栈的应用,给定一个以“#”作为结束符的算式,求出算式的结果

【输入形式】以“#”结尾的表达式,运算数为正整数。每个表达式占一行。

【输出形式】输出表达式运算的结果。

【样例输入1】4+2.53*3-10/5#

【样例输出1】9.59

【样例输入2】3*(7.91-2)#

【样例输出2】17.73

【样例输入3】2.4*3.6/2#

【样例输出3】4.32

【注意】分别运用C和C++语言如何处理表达式中带小数的数据,输出数据请保留2位小数。

#include <bits/stdc++.h>
using namespace std;
template <typename T>
//构造栈
struct mstack
{
    T a[1000];
    int top;
    //压栈
    void push(T t)
    {
        top++;
        a[top]=t;
    }
    //出栈
    void pop()
    {
        top--;
    }
    //获得栈顶元素
    T gettop()
    {
        return a[top];
    }
};
//判断运算符优先级
/* 运算符优先级表
	'>','>','<','<','<','>','>',
	'>','>','<','<','<','>','>',
	'>','>','>','>','<','>','>',
	'>','>','>','>','<','>','>',
	'<','<','<','<','<','=',' ',
	'>','>','>','>',' ','>','>',
	'<','<','<','<','<',' ','='
*/
char Compare(char x,char ch)
{
	switch(x)
	{
	case '+':
		if(ch=='+'||ch=='-'||ch==')'||ch=='#')
			return '>';
		else if(ch=='*'||ch=='/'||ch=='(')
				return '<';
		break;
	case '-':
		if(ch=='+'||ch=='-'||ch==')'||ch=='#')
			return '>';
		else if(ch=='*'||ch=='/'||ch=='(')
			return '<';
		break;
	case '*':
		if(ch=='(')
			return '<';
		else
			return '>';
		break;
	case '/':
		if(ch=='(')
			return '<';
		else
			return '>';
		break;
	case '(':
		if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='(')
			return '<';
		else if(ch==')')
			return '=';
		else if(ch=='#')
			return '0';
		break;
	case ')':
		if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch==')'||ch=='#')
			return '>';
		else if(ch=='(')
			return '0';
		break;
	case '#':
		if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='(')
			return '<';
		else if(ch=='#')
			return '=';
		else if(ch==')')
			return '0';
		break;
	default:
		return '0';
		break;
	}
    return '0';
}
//判断输入的是操作符还是操作数
bool isoper(char c)
{
    if(c>='0'&&c<='9')
        return false;
    return true;
}
//进行计算
double fun(char c,double x,double y)
{
    switch(c)
    {
    case '+':return x+y;
    case '-':return x-y;
    case '*':return x*y;
    case '/':return x/y;
    }
    return -100;
}
int main()
{
    char ch;
    double x;
    while(cin>>ch)
    {
        mstack<double> A;
        mstack<char> B;
        A.top=B.top=-1;
        B.push('#');
        while(1)
        {
            if(!isoper(ch))
            {
                cin.putback(ch);
                cin>>x;
                A.push(x);
                cin>>ch;
            }
            else
                //判断运算符优先级
                switch(Compare(B.gettop(),ch))
                {
                case '<':
                    B.push(ch);
                    cin>>ch;
                    break;
                case '>':
                    double left,right;
                    right=A.gettop();
                    A.pop();
                    left=A.gettop();
                    A.pop();
                    A.push(fun(B.gettop(),left,right));
                    B.pop();
                    break;
                case '=':
                    B.pop();
                    if(ch==')')
					    cin>>ch;
                    break;
                }
            if(B.top==-1)
            {
                cout<<fixed<<setprecision(2)<<A.gettop()<<endl;
                break;
            }
        }
    }
    return 0;
}
4.学生信息管理(综合项目题)

本题为小组综合项目题,请组长协调为各位组员分配任务,然后周末小组碰头集体讨论共同完成,提交相同的一份代码,每人在完成的模块后面写上注释。

【问题描述】设有一个存储学生信息的结构体包含:学号(no),姓名(name  ),班级号(classno),大学入学成绩总分(score),学生号指针(pno),班级号指针(pclass),成绩数指针(pscore)。请采用链式结构将信息读取并记录,并且完成如下功能:

1.添加一个学生记录

2.按学号no递增输出

3.按班级号classno递增输出,当classno一致时,按no递增顺序输出

4.按总分score递增输出,当score一致时,按no递增顺序输出

5.按学号删除:输入一个学号,删除该学号的学生记录,若无,则不执行

6. 修改某一学号学生的成绩:输入一个学号和成绩总分,找到该学号学生,修改成绩总分

7. 修改某一学号学生的姓名:输入一个学号和姓名,找到该学号学生,修改其姓名

8. 查找:输入一个成绩,查找该成绩总分的所有学生信息,按3的规则输出

9. 查找:输入一个班级号,查找该班级号所有学生信息,按4的规则输出

10.退出:退出运行程序

【提示】请用switch菜单模式完成

【目的】链表综合应用

【输入形式】一个整数代表操作类型,若需要则在下一行输入相关信息

【输出形式】按要求输出执行结果

【样例输入】

1

06208 gaoya 2103 630  

1  

06209 lisi 2104 617 

1

06210 lisi 2103 643

6                  

06208 600

4

10

【样例输出】

06209 lisi 2104 617

06208 gaoya 2103 630  

06210 lisi 2103 643  

06208 gaoya 2103 600 

06209 lisi 2104 617

06210 lisi 2103 643 

【样例说明】添加各学生记录之后,建立链表,执行各种操作。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int no;
	string name;
	int classno;
	int score;
	node* pno;
	node* pclass;
	node* pscore;
	node* p;
};
node *head;
void add_head(int no, string name, int classno, int score){
	node* tmp = new node;
	tmp->p = head->p;
	head->p = tmp;
	tmp->pno = head->pno;
	head->pno = tmp;
	tmp->pclass = head->pclass;
	head->pclass = tmp;
	tmp->pscore = head->pscore;
	head->pscore = tmp;
	tmp->classno = classno;
	tmp->name = name;
	tmp->no = no;
	tmp->score = score;
	return;
}
void init(){
	head = new node;
	head->p = NULL;
	head->pno = NULL;
	head->pclass = NULL;
	head->pscore = NULL;
}
void Sort_no(node* head){
	node *pre,*cur,*next,*end;
	end = NULL;
	while(head->pno != end ){
		for(pre = head , cur = pre->pno, next = cur->pno; next != end; pre = pre->pno , cur = cur->pno , next =  next->pno){
			if(cur->no > next ->no ){
				pre->pno = next;
				cur->pno = next->pno;
				next->pno=cur;
				node* tmp = cur;
				cur = next;
				next = tmp;
			}
		}
		end = cur;
	}
}
void Sort_classno(node* head){
	node *pre,*cur,*next,*end;
	end = NULL;
	while(head->pclass != end ){
		for(pre = head , cur = pre->pclass, next = cur->pclass; next != end; pre = pre->pclass , cur = cur->pclass , next =  next->pclass){
			if(cur->classno > next ->classno || (cur->classno == next->classno && cur->score > next->score) ){
				pre->pclass = next;
				cur->pclass = next->pclass;
				next->pclass=cur;
				node* tmp = cur;
				cur = next;
				next = tmp;
			}
		}
		end = cur;
	}
}
void Sort_score(node* head){
	node *pre,*cur,*next,*end;
	end = NULL;
	while(head->pscore != end ){
		for(pre = head , cur = pre->pscore, next = cur->pscore; next != end; pre = pre->pscore , cur = cur->pscore , next =  next->pscore){
			if(cur->score > next ->score || (cur->score == next->score && cur->no > next->no) ){
				pre->pscore = next;
				cur->pscore = next->pscore;
				next->pscore=cur;
				node* tmp = cur;
				cur = next;
				next = tmp;
			}
		}
		end = cur;
	}
}
void print_all(node* head){
	node* tmp = head->p;
	while(tmp!=NULL){
		cout << "0" << tmp->no << " " << tmp->name << " " << tmp->classno << " " << tmp->score << endl;
		tmp = tmp->p;
	}
}
void print_no(node* head){
	node* tmp = head->pno;
	while(tmp!=NULL){
		cout << "0" << tmp->no << " " << tmp->name << " " << tmp->classno << " " << tmp->score << endl;
		tmp = tmp->pno;
	}
}
void print_classno(node* head){
	node* tmp = head->pclass;
	while(tmp!=NULL){
		cout << "0" << tmp->no << " " << tmp->name << " " << tmp->classno << " " << tmp->score << endl;
		tmp = tmp->pclass;
	}
}
void print_score(node* head){
	node* tmp = head->pscore;
	while(tmp!=NULL){
		cout << "0" << tmp->no << " " << tmp->name << " " << tmp->classno << " " << tmp->score << endl;
		tmp = tmp->pscore;
	}
}
 
void clear_head(node* head){
	node* tmp = head->p;
	while(tmp != NULL){
		node* tmp1 = tmp;
		tmp = tmp->p;
		delete tmp1;
	}
}
int main(){
	init();
	int Input = 0;
//	cout << "select:" << endl;
	while(cin >> Input){
		if(Input == 1 || Input == 10 ) cout<<"select:"<<endl;
		if(Input == 1){
			int no, classno, score;
			string name;
			cin >> no >> name >> classno >> score;
			add_head(no, name, classno, score);
		}
		else if(Input == 2){
			print_all(head);
		}
		else if(Input == 3){
			Sort_no(head);
		}
		else if(Input == 4){
			Sort_no(head);
			print_no(head);
		}
		else if(Input == 5){
			Sort_classno(head);
		}
		else if(Input == 6){
			Sort_classno(head);
			print_classno(head);
		}
		else if(Input == 7){
			Sort_score(head);
		}
		else if(Input == 8){
			Sort_score(head);
			print_score(head);
		}
		else if(Input == 9){
			clear_head(head);
		}
		else if(Input == 10){
			break;
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值