数据结构练习

1.点名问题

题目内容:
老师想知道考勤情况,让三个班的学生依次到讲台上录入自己的学号,再对三个班学生登录的学号进行整合排序,以便登记。

输入要求:
依次输入若干学号,每个学号一行,输入的学号为0代表当前这个班级的学生学号输入完毕,进入下一个班的学生录入。

输出要求:
各个班学生录入的学号,整合排序好的列表,以及人数。格式见下方测试数据输出样例。

测试数据:
输入示例:
1002
1087
1037
1012
1076
0
1027
1088
1009
1022
0
1004
1067
1082
1043
1029
1033
0

输出实例:
Class 1:5
1002
1087
1037
1012
1076
Class 2:4
1027
1088
1009
1022
Class 3:6
1004
1067
1082
1043
1029
1033
All:15
1002
1004
1009
1012
1022
1027
1029
1033
1037
1043
1067
1076
1082
1087
1088

注意:每个班人数不多于30人且只有三个班。

#include <stdio.h>
int main(int argc, char const *argv[])
{
	int stus[3][30]int nums[3];
	int all[90];
	int cnt = 0;
	int sum = 0;

	for(int i = 0 ; i < 3; i++)
	{
		for(int j = 0;; j++)
		{
			scanf("%d",&stus[i][j]);
			if(stus[i][j] == 0)
			{
				nums[i] = j;
				break;
			}
		}
	}

	for(int i = 0; i < 3; i++)
	{
		printf("Class %d:%d\n",i+1,nums[i]);
		for(int j = 0; j < nums[i]; j++)
		{
			all[cnt++] = stus[i][j];
			printf("%d\n",stus[i][j]);
			if(stus[i][j] == 0)
				break;
		}
		sum += nums[i];
	}


	for(int i = 0; i < sum; i++)
	{
		for(int j = i + 1; j < sum; j++)
		{
			if(all[i] > all[j])
			{
				int temp = all[i];
				all[i] = all[j];
				all[j] = temp;
			}
		}
	}

	printf("All:%d\n",sum);
	for(int i = 0; i < sum; i++)
		printf("%d\n",all[i]);

	return 0;
}

2.行编辑器

题目内容:
编写一个程序,模拟一个行编辑器。
表示退格符,即删除前面一个字符。@表示退行符,即删除这一行中所有字符。

输入要求:
一行英文字符串,可能含有空格。

输出要求:
编辑后的字符串。

测试数据:
输入示例:
#ab@@ab cd##ab

输出示例:
ab ab

1.倒序处理每个字符:

#include <iostream>  
#include <algorithm>  
#include <string>  
using namespace std;  

int main()  
{  
	string str,s;  
	int cnt = 0;  
 	getline(cin, str);

	for(int i = str.length() - 1; i >= 0; i--)  
	{  
		if(str[i] == '@') 
			break;  
		else if(str[i] == '#')
		{
			int siCount = 1;
			int x = i - 1;
			while(str[x--] == '#')
			{
				siCount++;
			}
			i -= (2 * siCount - 1);
		}
		else 
			s[cnt++] = str[i];   
	}  
	for(int i = cnt - 1; i >= 0; i--) cout << s[i];
	cout << endl;  
} 

2.栈

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

typedef int Status;

#define OK 1
#define ERROR 0
#define OVERFLOW -2

#define  MAXSIZE  100	
typedef int SElemType;
typedef struct
{
	SElemType   *base;	
	SElemType   *top;	
}SqStack;

Status init(SqStack &s);	 
Status push(SqStack &s,SElemType e); 
Status pop(SqStack &s);				
void clear(SqStack &s);					
void printSqStack(SqStack s);	

int main()
{
	SqStack s;
	init(s); 
	char a[MAXSIZE];
	while( gets(a) != '\n' )
	{
		for(int i = 0; i < strlen(a); i++)
		{
			switch (a[i])
			{
				case '#':
					pop(s);
					break;
				case '@':
					clear(s);
					break;
				default:
					push(s,a[i]);
			}
		}
		printSqStack(s);
	}
}

Status init(SqStack &s)
{
	s.base=new SElemType[MAXSIZE];
	if(!s.base)  exit(OVERFLOW);
	s.top=s.base;
	return OK;
}

Status push(SqStack &s, SElemType e)
{
	if(s.top-s.base == MAXSIZE) return ERROR;
	*s.top++ = e;
	return OK;
}

Status pop(SqStack &s)
{
	if(s.top == s.base)	return ERROR;
	*--s.top;
	return OK;
}

void clear(SqStack &s)
{
	while(s.top != s.base)
		*--s.top;
}

void printSqStack(SqStack s)
{
	while(s.top != s.base){
		printf("%c",*s.base++);
	}
	printf("\n");
}

3.括号匹配

题目内容:
判断一个表达式中的圆括号()和方括号[]是否匹配。

输入要求:
多组输入,每组输入为一个字符串(无空格),占一行。

输出要求:
括号匹配输出match,否则输出not match。

测试数据:
输入示例:
()
[([][])]
[(])
([())

输出示例:
match
match
not match
not match

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

typedef int SElemType;

typedef struct{  
	char data[MAXSIZE];  
	SElemType top;   
}SqStack;   
  
void init(SqStack &s)
{  
	s.top = 0;
}   

void push(SqStack &s,char data)
{  
	if(s.top < MAXSIZE)
	{  
		s.data[s.top++] = data;  
	}  
}  

SElemType pop(SqStack &s)
{  
	if(s.top != 0)
		return s.data[s.top--];  
} 

SElemType isEmpty(SqStack s)
{  
	if(s.top == 0)
		return true;    
	else 
		return false;  
}

SElemType getTop(SqStack s)
{  
	if(s.top != 0)
		return s.data[s.top-1];  
}  
  
int main()
{  
	char str[MAXSIZE];  
	while(gets(str) != NULL)
	{
		SqStack s;  
	    init(s);
	    int flag;
	    for(int i = 0; i < strlen(str); i++)
		{  
			flag = 1;
	        if(str[i] =='(' || str[i]=='[')  
	            push(s,str[i]);
	        else
			{    
	            if(str[i] ==')' && getTop(s) == '(')
				{           
	                pop(s);  
	            }  
	            else if(str[i]==']' && getTop(s) == '[' )
				{       
	                pop(s);  
	            } 
				else if(str[i]==')' || str[i]==']')
	        		flag = 0;         
	        }
	    }   
	    if(isEmpty(s) && flag)
	        printf("match\n");  
	    else
	    	printf("not match\n");
	}
}

4.表达式求值

题目内容:
给出符合下述条件的表达式的计算结果:
1.表达式只包含操作数和操作符
2. 操作数为一位正整数(0-9)
3. 操作符为+(加)、-(减)、*(乘)、/(整数相除),计算的规则为先乘除后加减,并且加和减的优先级相同,乘和除的优先级相同
4. 计算的顺序为从左向右
5. 表达式的长度不超过20个字符

输入要求:
多组输入。每行为一组输入。假设输入的表达式不会出现语法错误。见示例输入。

输出要求:
计算结果。详见示例输出

测试数据:
输入示例:
1+2
2+4-3*6

输出示例:
3
-12

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cmath>

using namespace std;

char s[20];
int  g_pos;  // 字符数组的下标

/* 字符转数字 */
int Translation(int & pos)
{
	int num = 0;    

	while (s[pos] >= '0' && s[pos] <= '9')
	{
		num *= 10;
		num += (s[pos] - '0');
		pos++;
	}
	return num;
}

/* 返回运算符级别 */
int GetLevel(char ch)
{
	switch (ch)
	{
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	case '#':
		return -1;
	}
}

/* 对两个数进行运算 */
int Operate(int a1, char op, int a2)
{
	switch (op)
	{
	case '+':
		return a1 + a2;
	case '-':
		return a1 - a2;
	case '*':
		return a1 * a2;
	case '/':
		return a1 / a2;
	}
}

/* 利用两个栈进行模拟计算 */
double Compute()
{
	stack<char> optr;    // 操作符栈
	stack<int> opnd;  // 操作数栈

	optr.push('#');
	int len = strlen(s);

	for (g_pos = 0; g_pos < len;)
	{
		//1. 数字  
		if (s[g_pos] >= '0' && s[g_pos] <= '9')
		{
			opnd.push(Translation(g_pos));
		}
		//2. + - * / 四种  
		else
		{
			while (GetLevel(s[g_pos]) <= GetLevel(optr.top()))
			{
				int a2 = opnd.top();
				opnd.pop();
				int a1 = opnd.top();
				opnd.pop();
				char op = optr.top();
				optr.pop();

				int result = Operate(a1, op, a2);
				opnd.push(result);
			}

			optr.push(s[g_pos]);
			g_pos++;
		}
	}

	while (optr.top() != '#')
	{
		int a2 = opnd.top();
		opnd.pop();
		int a1 = opnd.top();
		opnd.pop();
		char op = optr.top();
		optr.pop();

		int result = Operate(a1, op, a2);
		opnd.push(result);
	}

	return opnd.top();
}

int main()
{
	while (cin >> s)
		cout << Compute() << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值