2.Delimiter Matching

Prerequisites, Goals, and Outcomes

Prerequisites: Students should have mastered the following prerequisite skills. 

· Stack - Understanding of the implementation of stack. 

· Templates - Creation and use of template classes

· File IO - Basic file IO skills 

Goals: This assignment is designed to reinforce the student's understanding of the structure and use of stack as containers. 

Outcomes: Students successfully completing this assignment would master the following outcomes. 

· Familiarize how to implement a template class 

· Familiarize the structure and the use of the stack 

Background

Compilers check your programs for syntax errors, but frequently a lack of one symbol (such as a missing brace or comment starter) will cause the compiler to spill out a hundred lines of diagnostics without identifying the real error. 

A useful tool in this situation is a program that checks whether everything is balanced. Thus, every right brace, bracket, and parenthesis must correspond to their left counterparts. The sequence [()] is legal, but [(]) is wrong. Obviously, it is not worthwhile writing a huge program for this, but it turns out that it is easy to check these things. 

Description

Make an empty stack. Read characters until end of file. If the character is an open anything, push it onto the stack. If it is a close anything, then if the stack is empty report an error. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error. At end of file, if the stack is not empty report an error. 

Besides the balancing of the parentheses, brackets, and bracesthe Annotation (/**/) should be balanced. When a /* is found, all the letters behind will be ignored until a */ is found. If it is not found at the end of the file, an error will be reported.

Some of the contents of the stack are shown in the figure below.

Tasks

To complete this assessment, you need to define the implementation of class Stack and complete the main.cpp file in which the delimiter matching is defined. 

Following is an ordered list of steps that serves as a guide to completing this assessment. Work and test incrementally. Save often.

1. Begin by the creation of the class StackYou can implement a stack through either an array or a list. But this class should be defined as a template class. And it should contain the following basic operation. 

void Pop():  Pop an element from the top of the stack 

void Push(const T & e):  Push this element into the stack.

T & Top(): Return the top element of the stack.

bool Isempty() const: If the stack is empty, return true; otherwise return false.

int Size() const:  Return the number of the elements in the stack.

2. Nextcreate the file main.cpp and complete the implementation of delimiter matching. The detail steps are as follows.

Get a file name from the command line parameter, which specifies the file name needing delimiter matching. Invoke the delimiterMatching function using the file name.

Complete the function delimiterMatching, which accepts a string type parameterThis parameter is the file name described as above. This function repeats reading one character from the file and taking it into process until an error appears or at the end of the file. 

Submission

Submit only the following.

1. Stack.h - your completed class Stack definition 

2. Stack.cpp - if created 

3. main.cpp - your completed delimiter matching file

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

template <class T>
class Stack
{
private:
	T *data;//指向栈顶指针
	int size;//栈长度
	int top;
public:
	Stack(int size=0)//构造函数
	{
		this->size=size;
		this->top=0;
		this->data=new T[size];
	}
	~Stack()//析构函数
	{
		size=0;
		top=0;
		delete []data;	
	}
	void Pop()//栈顶元素出栈
	{
		if(!isEmpty())
		{
			top--;
		}
	}
	void Push(const T & item)//入栈
	{
		if(top<size)
		{
			data[top]=item;
			top++;
		}
	}
	T & Top()//获取栈顶元素
	{
		if(!isEmpty())
		{	
			return data[top-1];
		}
	}
	bool isEmpty()
	{
		if(top==0)
		{
			cout<<"Stack is empty"<<endl;
			return true;
		}
		else return false;
	}
	int Size()
	{
		return top;
	}
};
bool delimiterMatching()
{
	cout<<"请向文件输入字符串:"<<endl;
	string s;
	cin>>s;
	ofstream out("F:\\test.txt");
	out<<s<<endl;
	out.close();//关闭文件
	ifstream get("F:\\test.txt");
	char string[50];
	get>>string;//获取文件
	int i=0;
	Stack<char> stack(100);
	while(string[i]!='\0')
	{
		//1、判断是否为左括号,是则入栈
		if(string[i]=='('||string[i]=='['||string[i]=='{')
		{
			stack.Push(string[i]);
			i++;
		}
		//2、右括号的情况
		else if(string[i]==')'||string[i]==']'||string[i]=='}')
		{
			if(stack.isEmpty())//如果栈空说明没有左匹配符
			{
				return false;
			}
			else//栈未空
			{
				char temp;
				temp=stack.Top();//获取栈顶元素,匹配的话栈顶元素出栈
				if((string[i]==')'&&temp=='(')||(string[i]=='}'&&temp=='{')||(string[i]==']'&&temp=='['))
				{
					stack.Pop();
					i++;
				}
				else return false;	
			}
		}
		//3、当读到 / 时有可能是注释符的情况
		else if(string[i]=='/')
		{
			if(string[i+1]=='/'&&stack.isEmpty())
				return true;
			else if(string[i+1]=='/'&&!stack.isEmpty())
				return false;
			else if(string[i+1]=='*')
			{
				int flag=0;
				stack.Push(string[i]);
				stack.Push(string[i+1]);
				i=i+2;        //i加2,读取/*后面的字符
				while(string[i]!='\0')
				{
					if(string[i]=='*'&&string[i+1]=='/')
					{
							stack.Pop();
							stack.Pop();
							i=i+2;
							flag=1;
							break;//中断循环
					}
					else
						i++;//未读到*,往后走
				}
				if(flag==0)
					return false;
			}
		}
		//4、读到*/说明而之前没有与之匹配的/*
		else if(string[i]=='*'&&string[i+1]=='/')
			return false;
		//5、往后遍历
		else
		{
			i++;
		}
		if(string[i]=='\0'&&stack.isEmpty())//最终栈空返回true
			return true;
		if(string[i]=='\0'&&!stack.isEmpty())//最终栈非空返回false
			return false;
	}
}

int main()
{
	Stack<int> s(10);
	/*s.Push(10);
	s.Push(20);
	cout<<s.Size()<<endl;
	s.Pop();
	cout<<s.Size()<<endl;
	cout<<s.isEmpty()<<endl;*///Test Stack

	char c='y';
	while(c=='Y'||c=='y')
	{
		if(delimiterMatching())
			cout<<"分隔符匹配成功!"<<endl;
		else
			cout<<"分隔符匹配不成功!"<<endl;
		cout<<"是否继续 (Y/N)"<<endl;
		cin>>c;
	}
	cout<<"退出程序"<<endl;
	return 0;
}


 

已知地面数据为2024年10月24日下午(具体时刻见数据文件内GPS时间),在江苏海洋大学西运动场使用SVC HR-1024i观测的无遮挡、直射遮挡标准板反射的太阳辐射辐射亮度数据(10-10 W cm-2 nm-1 sr-1),分别绘出无遮挡标准板、直射遮挡标准板反射的辐射亮度光谱曲线图。 完成过程: 3)使用上步结果,求算该时刻到达地面的太阳直射辐射被标准板反射的辐射亮度(10-10 W cm-2 nm-1 sr-1)光谱,绘出光谱曲线图。 完成过程: 4)假设标准板在所有观测波长上反射率均为1.0,求算到达地面的相对于水平面(即垂直向下)的太阳直射辐射通量密度(W m-2 nm-1)。 完成过程: 5)已知日地平均距离处太阳辐射通量密度光谱数据(fun_F0.m),数据单位是μW cm-2 nm-1,将该数据读入,将其单位转为W m-2 nm-1,绘出光谱曲线图。 完成过程: 6)使用上步结果及经验公式(日地距离校正因子可用下式计算),求算地面观测时刻,大气层外太阳辐射通量密度数据,绘出光谱曲线图。1 + 0.033 × cos( (360° × td) / 365 ) (1) 其中,td为日序数。 完成过程: 7)什么是太阳天顶角(θz),使用经验公式求算观测时刻太阳天顶角及其余弦。 cos(θz)= sin(φ) sin(δ) +cos(φ) cos(δ) cos(h)(2) 式中,φ是纬度,δ是太阳赤纬角(sin(δ)由公式3计算),h是太阳时角(由公式4计算)。 δ=-23.45摄氏度cos(360度/365(d+10)) (3) h=15(tsolar​−12) (4) 其中,d为日序数,tsolar为太阳时(由公式5近似计算)。 tsolar=tclock +4/60(λstd-λloc) (5) 其中,tclock为时区时间,λstd​是时区标准经度,λloc​是观测点经度。 完成过程: 8)使用前两步结果,求算大气层外相对于水平面(即垂直向下)的太阳辐射通量密度(W m-2 nm-1)光谱。 完成过程: 9)什么是大气质量数,使用经验公式(6)求算观测时刻大气质量数。 m = 1 / [cos(z) + 0.50572 × (96.07995° - z)^(-1.6364)](6) 完成过程: 10)使用前面步骤结果,求算大气总光学厚度光谱,绘制大气总光学厚度光谱曲线,给出550 nm处大气总光学厚度数据。 完成过程: 11)什么是瑞利散射光学厚度,已知观测时刻地面气压数据(1021.2 hPa),使用经验公式计算观测时刻大气瑞利散射光学厚度。 τ_R(λ) = (P/P₀) × (84.35/λ⁴ - 1.255/λ⁵ + 1.40/λ⁶) × 10⁻⁴(7) 其中,P为观测时刻地面气压,P0为海平面标准大气压1013.25 hPa,λ为波长(单位为微米)。 完成过程: 12)使用前面步骤结果,求算大气气溶胶光学厚度光谱,给出光谱曲线,给出550 nm AOD数据。 (可用400 ~ 680、740 ~ 753、870 ~ 883、1020 ~ 1060 nm作为大气吸收作用影响较小的波段。) 数据为gr102424_001无遮挡标准版.sig和gr102424_002遮挡标准板1.sig(位置F:\dingliang bigtest),文件结构如下: gr102424_001无遮挡标准版.sig:/** Spectra Vista SIG Data */ name= gr102424_001.sig instrument= HI: 1142031 (HR-1024i) integration= 40.0, 40.0, 10.0, 30.0, 40.0, 10.0 scan method= Time-based, Time-based scan coadds= 125, 116, 392, 166, 116, 392 scan time= 5, 5 scan settings= AI, AI external data set1= 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 external data set2= 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 external data dark= 0,0,0,0,0,0,0,0 external data mask= 0 optic= FIBER1(2), FIBER1(2) temp= 22.3, -5.0, -9.8, 22.8, -5.0, -9.8 battery= 7.51, 7.45 error= 1, 1 units= Radiance, Radiance time= 2024/10/23 9:47:11, 2024/10/23 9:48:03 longitude= 11912.7438E , 11912.7438E latitude= 3436.6458N , 3436.6453N gpstime= 061740.000 , 061832.000 comm= memory slot= 1, 3 factors= 1.200, 1.170, 1.000 [Overlap: Remove @ 990,1900, Matching Type: None] data= 336.1 19816.09 18797.35 94.86 337.7 22528.28 21318.55 94.63 339.2 24719.48 23327.15 94.37 340.7 27921.59 26424.78 94.64 342.3 30279.36 28805.83 95.13 343.8 31832.10 30374.40 95.42 345.3 31917.38 30307.43 94.96 346.8 34518.50 32761.83 94.91 … 2501.2 969.59 943.38 97.30 2503.4 1059.60 1033.11 97.50 2505.5 1079.60 1025.62 95.00 2507.6 910.80 910.80 100.00 2509.7 844.80 816.64 96.67 2511.8 772.88 744.25 96.30 2513.9 723.88 608.06 84.00 gr102424_002遮挡标准板1.sig: / Spectra Vista SIG Data ***/ name= gr102424_002.sig instrument= HI: 1142031 (HR-1024i) integration= 40.0, 40.0, 10.0, 70.0, 40.0, 10.0 scan method= Time-based, Time-based scan coadds= 125, 116, 392, 71, 116, 392 scan time= 5, 5 scan settings= AI, AI external data set1= 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 external data set2= 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 external data dark= 0,0,0,0,0,0,0,0 external data mask= 0 optic= FIBER1(2), FIBER1(2) temp= 22.3, -5.0, -9.8, 23.1, -5.0, -9.8 battery= 7.51, 7.41 error= 1, 1 units= Radiance, Radiance time= 2024/10/23 9:47:11, 2024/10/23 9:48:42 longitude= 11912.7438E , 11912.7437E latitude= 3436.6458N , 3436.6452N gpstime= 061740.000 , 061911.000 comm= memory slot= 1, 4 factors= 1.200, 1.200, 1.000 [Overlap: Remove @ 990,1900, Matching Type: None] data= 336.1 19816.09 13633.24 68.80 337.7 22528.28 15573.17 69.13 339.2 24719.48 17118.75 69.25 340.7 27921.59 19389.32 69.44 342.3 30279.36 21196.03 70.00 343.8 31832.10 22354.63 70.23 345.3 31917.38 22175.70 69.48 … 2497.0 790.97 102.06 12.90 2499.1 852.72 51.68 6.06 2501.2 969.59 52.41 5.41 2503.4 1059.60 79.47 7.50 2505.5 1079.60 134.95 12.50 2507.6 910.80 138.00 15.15 2509.7 844.80 112.64 13.33 2511.8 772.88 57.25 7.41 2513.9 723.88 202.69 28.00 (读取数据时跳过第二列) 给出每个步骤所需的Matlab函数代码以及每个函数所需的调用代码 ,fun_F0.m的结构大致如下:%https://oceancolor.gsfc.nasa.gov/docs/rsr/f0.txt %fields=wavelength,irradiance; %units=nm,uW/cm^2/nm %missing=-999; % Thuillier, G., M. Hers?, P. C. Simon, D. Labs, H. Mandel, D. Gillotay, % ! and T. Foujols, 2003, "The solar spectral irradiance from 200 % ! to 2400 nm as measured by the SOLSPEC spectrometer from the % ! ATLAS 1-2-3 and EURECA missions, Solar Physics, 214(1): 1-22 %delimiter=space; %wavelength range 200 - 2397nm at 1nm increments; function F0=fun_F0() F0=[200 0.7729 201 0.8143 202 0.8756 203 0.9263 204 1.0335 205 1.1149 206 1.1211 207 1.2612 208 1.4234 209 1.8649 210 2.6086 211 3.1691 212 3.6546 213 3.2686 214 3.9173 … 2381 6.2889 2382 6.2090 2383 6.0941 2384 5.9994 2385 5.9797 2386 5.9948 2387 6.0355 2388 6.0769 2389 6.1063 2390 6.1071 2391 6.0972 2392 6.0766 2393 6.0643 2394 6.0564 2395 6.0574 2396 6.0511 2397 6.0476]; F0(F0==-999)=NaN;
最新发布
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值