3、JSON查找(必做)(树)

#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <math.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0


#define MAXSIZE 65536
#define N 8

using namespace std;

typedef string DataType;
typedef char ElemType;
typedef int Status;
typedef int status;

const int maxn=105;

struct JNode {
	DataType key;
	DataType value;
	JNode(string a="",string b=""):key(a),value(b){}
}JSON[MAXSIZE];



Status CountLines(string filename){//count lines
	ifstream ReadFile;
	int n = 0;
	string tmp;
	ReadFile.open(filename.c_str());
	if (ReadFile.fail())
	{
		return FALSE;
	}
	else
	{
		while (getline(ReadFile, tmp, '\n'))
		{
			n++;
		}
		ReadFile.close();
		return n;
	}
}

string ReadLine(string filename, int line){
	int lines, i = 0;
	string temp;
	fstream file;
	file.open(filename.c_str());
	lines = CountLines(filename);

	if (line <= 0)
	{
		return "Error!";
	}
	if (file.fail())
	{
		return "Error!!";
	}
	if (line > lines)
	{
		return "Error!!!";
	}
	while (getline(file, temp) && i < line - 1)
	{
		i++;
	}
	file.close();
	return temp;
}

DataType str[MAXSIZE];
DataType b[MAXSIZE];
DataType s;
DataType t ="" ;

int main() {
	int n,m;

	string filename = "text3.txt";
/*	int line = 1;
	string tmp = ReadLine(filename, line);
	cout << tmp << endl;
*/    //test
	for(int i = 1; i <= 10 ; i++){
		s = ReadLine(filename, i);
		str[i-1] = s;
	}
//	cin>>n>>m;//test
	n = 10;
	m = 5;
/*	getchar();//	{	
	
	for(int i=0; i<n ;i++){

	   	getline(cin,str[i]);
	}
*/    //test
	int cnt = 0;		//count 
    int flag = 0;		//type
    int d = 0;		//storey
    
  	for(int i = 0; i < n; i++) {
     	 for(int j = 0;j < str[i].length(); j++) {
       	 	if(str[i][j] == '{'){
				flag=0;
				d++;
		 	} 
       		 else if(str[i][j] == ':'){		//value
				flag=1;
			 }
       		 else if(str[i][j] == ','){		//object
				flag=0;
		 	}
       		 else if(str[i][j] == ' ' || str[i][j] == '\n'){}	//skip '\0' & '\n'
       		 else if(str[i][j] == '"'){
       	   	 	string t="";
       	 	    j++;
        		while(str[i][j] != '"'){
         	 		if(str[i][j] == '\\'){          
        	        	if(str[i][j+1] == '"')
         	   	        	t+="\"";		//
         	        	if(str[i][j+1] == '\\')
          	   	    		t+="\\";		//
           	 	    	j+=2;
            		}
            		else{
						t+=str[i][j];j++;
					}
           		}
          		b[d]="";
           	  	if(!flag) {
           	         cnt++;
            	     if(d>1) {
             	         b[d]+=".";
              	     }
            		 b[d]+=t;
            		 for(int i=1;i<=d;i++){
					     JSON[cnt].key+=b[i];
					 }
                }
                else if(flag){
                JSON[cnt].value += t;
                }
             }//
             else if(str[i][j ]== '}'){
                d--;
             }
             else if(str[i][j] == ' ' || str[i][j] == '\n')
		    	continue;
      	}//end for for
   }//end for
   for(int i=0;i<m;i++) {
	   t = ReadLine(filename, i+12);
       int j;
       for( j=1;j<=cnt;j++) {
          if(t==JSON[j].key) {
        	if(JSON[j].value == "")
				cout <<"OBJECT" <<endl; 
            else cout <<"STRING" <<" " <<JSON[j].value <<endl;
               	break;
          }
       }
       if(j==cnt+1)
       cout <<"NOTEXIST" <<endl;
   }
   return 0;
}

3、JSON查找(必做)(树)

 [问题描述]

  JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,可以用来描述半结构化的数据。JSON 格式中的基本单元是值 (value),出于简化的目的本题只涉及 2 种类型的值:

  * 字符串 (string):字符串是由双引号 " 括起来的一组字符(可以为空)。如果字符串的内容中出现双引号 ",在双引号前面加反斜杠,也就是用 \" 表示;如果出现反斜杠 \,则用两个反斜杠 \\ 表示。反斜杠后面不能出现 " 和 \ 以外的字符。例如:""、"hello"、"\"\\"。

  * 对象 (object):对象是一组键值对的无序集合(可以为空)。键值对表示对象的属性,键是属性名,值是属性的内容。对象以左花括号 { 开始,右花括号 } 结束,键值对之间以逗号 , 分隔。一个键值对的键和值之间以冒号 : 分隔。键必须是字符串,同一个对象所有键值对的键必须两两都不相同;值可以是字符串,也可以是另一个对象。例如:{}、{"foo": "bar"}、{"Mon": "weekday", "Tue": "weekday", "Sun": "weekend"}。

  除了字符串内部的位置,其他位置都可以插入一个或多个空格使得 JSON 的呈现更加美观,也可以在一些地方换行,不会影响所表示的数据内容。例如,上面举例的最后一个 JSON 数据也可以写成如下形式。

  {

  "Mon": "weekday",

  "Tue": "weekday",

  "Sun": "weekend"

  }

  给出一个 JSON 格式描述的数据,以及若干查询,编程返回这些查询的结果。

输入格式

  第一行是两个正整数 n 和 m,分别表示 JSON 数据的行数和查询的个数。

  接下来 n 行,描述一个 JSON 数据,保证输入是一个合法的 JSON 对象。

  接下来 m 行,每行描述一个查询。给出要查询的属性名,要求返回对应属性的内容。需要支持多层查询,各层的属性名之间用小数点 . 连接。保证查询的格式都是合法的。

[基本要求]

输出格式

  对于输入的每一个查询,按顺序输出查询结果,每个结果占一行。

  如果查询结果是一个字符串,则输出 STRING <string>,其中 <string> 是字符串的值,中间用一个空格分隔。

  如果查询结果是一个对象,则输出 OBJECT,不需要输出对象的内容。

  如果查询结果不存在,则输出 NOTEXIST。

样例输入

10 5

{

"firstName": "John",

"lastName": "Smith",

"address": {

"streetAddress": "2ndStreet",

"city": "NewYork",

"state": "NY"

},

"esc\\aped": "\"hello\""

}

firstName

address

address.city

address.postal

esc\aped

样例输出

STRING John

OBJECT

STRING NewYork

NOTEXIST

STRING "hello"

[基本要求]

(1)要求从文本文件中输入;

(2)本题目其实就是一棵普通的树(即每个结点的孩子数不固定,不能单纯采用n叉树来解决),可以考虑使用孩子兄弟表示法等进行表示和存储;

(3)严格按照要求的输入输出格式进行数据的输入、输出(训练CSP考试中的格式化输入输出的正确性);

猜你喜欢

转载自blog.csdn.net/m0_54070377/article/details/127337643