Beispiel #1
0
 void test_read_cell_formulae()
 {
     xlnt::workbook wb;
     auto ws = wb.get_active_sheet();
     auto path = PathHelper::GetDataDirectory("/reader/worksheet_formula.xml");
     std::ifstream ws_stream(path);
     xlnt::read_worksheet(ws, ws_stream, { "string", "string2" }, {}, {});
     
     auto b1 = ws.get_cell("B1");
     TS_ASSERT(b1.has_formula());
     TS_ASSERT_EQUALS(b1.get_formula(), "CONCATENATE(A1,A2)");
     
     auto a6 = ws.get_cell("A6");
     TS_ASSERT(a6.has_formula());
     TS_ASSERT_EQUALS(a6.get_formula(), "SUM(A4:A5)");
 }
/* load_egg_cmd:
 *  Reads a list of commands.
 */
static EGG_COMMAND *load_egg_cmd(EGG *egg, int root, char *error)
{
   EGG_COMMAND *cmd = NULL;
   EGG_COMMAND *tail = NULL;
   EGG_TYPE *type;
   char buf[1024];
   char buf2[256];


   /* helper macro for inserting new commands into the list */
   #define ADD_COMMAND(_type_)                                 \
   {                                                           \
      if (tail) {                                              \
	 tail->next = malloc(sizeof(EGG_COMMAND));             \
	 tail = tail->next;                                    \
      }                                                        \
      else                                                     \
	 tail = cmd = malloc(sizeof(EGG_COMMAND));             \
							       \
      tail->type = _type_;                                     \
      tail->line = egg_line;                                   \
      tail->var = NULL;                                        \
      tail->exp = NULL;                                        \
      tail->cmd = NULL;                                        \
      tail->cmd2 = NULL;                                       \
      tail->next = NULL;                                       \
   }


   while ((!egg_eof()) && (!error[0])) {
      get_word(buf);

      if (strcmp(buf, "}") == 0) {
	 /* block end marker */
	 if (root)
	    egg_error(error, "Unexpected '}'");
	 else
	    return cmd;
      }
      else if (strcmp(buf, "if") == 0) {
	 /* parse an if statement */
	 get_word(buf);

	 if (strcmp(buf, "(") != 0) {
	    egg_error(error, "Missing '('");
	 }
	 else {
	    get_formula(buf, ')', error);

	    if (!error[0]) {
	       get_brace(error);

	       if (!error[0]) {
		  ADD_COMMAND(EGG_COMMAND_IF);

		  tail->exp = malloc(strlen(buf)+1);
		  strcpy(tail->exp, buf);

		  tail->cmd = load_egg_cmd(egg, FALSE, error);
	       }
	    }
	 }
      }
      else if (strcmp(buf, "else") == 0) {
	 /* parse an else statement */
	 if ((!tail) || (tail->type != EGG_COMMAND_IF) || (tail->cmd2)) {
	    egg_error(error, "Invalid context for 'else'");
	 }
	 else {
	    get_brace(error);

	    if (!error[0])
	       tail->cmd2 = load_egg_cmd(egg, FALSE, error);
	 }
      }
      else if (strcmp(buf, "lay") == 0) {
	 /* parse a lay statement */
	 ADD_COMMAND(EGG_COMMAND_LAY);

	 get_word(buf);

	 if (strcmp(buf, "(") == 0) {
	    get_formula(buf, ')', error);

	    if (!error[0]) {
	       tail->exp = malloc(strlen(buf)+1);
	       strcpy(tail->exp, buf);

	       get_word(buf);
	    }
	 }

	 if (!error[0]) {
	    check_ascii_word(buf, error);

	    if (!error[0]) {
	       tail->var = malloc(strlen(buf)+1);
	       strcpy(tail->var, buf);

	       get_word(buf);

	       if (strcmp(buf, "{") == 0)
		  tail->cmd = load_egg_cmd(egg, FALSE, error);
	       else if (strcmp(buf, ";") != 0)
		  egg_error(error, "Expecting '{' or ';'");
	    }
	 }
      }
      else if (strcmp(buf, "die") == 0) {
	 /* parse a die statement */
	 ADD_COMMAND(EGG_COMMAND_DIE);

	 get_word(buf);

	 if (strcmp(buf, ";") != 0)
	    egg_error(error, "Missing ';'");
      }
      else if (strcmp(buf, "type") == 0) {
	 /* parse a type definition */
	 if (!root) {
	    egg_error(error, "Nested type definition");
	 }
	 else {
	    get_ascii_word(buf, error);

	    if (!error[0]) {
	       type = malloc(sizeof(EGG_TYPE));

	       type->name = malloc(strlen(buf)+1);
	       strcpy(type->name, buf);

	       type->cmd = NULL;
	       type->next = NULL;

	       get_brace(error);

	       if (error[0]) {
		  free(type->name);
		  free(type);
	       }
	       else {
		  type->cmd = load_egg_cmd(egg, FALSE, error);

		  if (error[0]) {
		     free(type->name);
		     free(type);
		  }
		  else {
		     type->next = egg->type;
		     egg->type = type;
		  }
	       }
	    }
	 }
      }
      else if (buf[0]) {
	 /* this must be a variable assignment */
	 check_ascii_word(buf, error);

	 if (!error[0]) {
	    get_word(buf2);

	    if (strcmp(buf2, "=") == 0) {
	       ADD_COMMAND(EGG_COMMAND_SET);
	    }
	    else if (strcmp(buf2, ":") == 0) {
	       get_word(buf2);

	       if (strcmp(buf2, "=") != 0) {
		  egg_error(error, "Missing '='");
	       }
	       else {
		  ADD_COMMAND(EGG_COMMAND_INIT);
	       }
	    }
	    else {
	       egg_error(error, "F****d up syntax ");
	    }

	    if (!error[0]) {
	       tail->var = malloc(strlen(buf)+1);
	       strcpy(tail->var, buf);

	       get_formula(buf, ';', error);

	       if (!error[0]) {
		  tail->exp = malloc(strlen(buf)+1);
		  strcpy(tail->exp, buf);
	       }
	    }
	 }
      }
   }

   if ((!error[0]) && (egg_eof()) && (!root))
      egg_error(error, "Unexpected EOF");

   if (error[0]) {
      destroy_egg_cmd(cmd);
      return NULL;
   }

   return cmd;
}
Beispiel #3
0
double calc_RPN(void)
{
	int ret;
	double i, j;
	double data;
	double stack2[100];
	int index = 0;

	f_error = 0;
	for(;;) {
		ret = get_formula(&data);
		if(ret == 1) {
			stack2[index++] = data;
		} else if(ret == 0) {
			switch((int)data) {
			case '+':
				j = stack2[--index];
				i = stack2[--index];
				stack2[index++] = i + j;
				break;
			case '-':
				j = stack2[--index];
				i = stack2[--index];
				stack2[index++] = i - j;
				break;
			case '*':
				j = stack2[--index];
				i = stack2[--index];
				stack2[index++] = i * j;
				break;
			case '/':
				j = stack2[--index];
				i = stack2[--index];
				if(j == 0) {
					fprintf(stderr, "Error: Division error\nDivision by zero\n");
					f_error = 1;
					return -1;
				}
				stack2[index++] = i / j;
				break;
			case '^':
				j = stack2[--index];
				i = stack2[--index];
				stack2[index++] = pow(i, j);
				break;
			case SIN:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = sin(i);
				break;
			case COS:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = cos(i);
				break;
			case TAN:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				if(i == (M_PI / 2)) {
					fprintf(stderr, "Error: tan90(deg) not calculate!!\n");
					f_was_calc = 0;
					break;
				}
				stack2[index++] = tan(i);
				break;
			case ASIN:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = asin(i);
				break;
			case ACOS:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = acos(i);
				break;
			case ATAN:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = atan(i);
				break;
			case SINH:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = sinh(i);
				break;
			case COSH:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = cosh(i);
				break;
			case TANH:
				i = stack2[--index];
				if(f_use_degree)  i = radian_to_degree(i);
				stack2[index++] = tanh(i);
				break;
			case SQRT:
				i = stack2[--index];
				if(i < 0) {
					fprintf(stderr, "Error: minus number is not calculate square root\n");
					f_was_calc = 0;
					break;
				}
				stack2[index++] = sqrt(i);
				break;
			case EXP:
				i = stack2[--index];
				stack2[index++] = exp(i);
				break;
			case LOG:
				i = stack2[--index];
				if(i < 0) {
					fprintf(stderr, "Error: minus number is not calculate common logarithm\n");
					f_was_calc = 0;
					break;
				}
				stack2[index++] = log10(i);
				break;
			case LN:
				i = stack2[--index];
				if(i < 0) {
					fprintf(stderr, "Error: minus number is not calculate natural logarithm\n");
					f_was_calc = 0;
					break;
				}
				stack2[index++] = log(i);
				break;
			case ABS:
				i = stack2[--index];
				stack2[index++] = abs(i);
				break;
			case NO_FUNC:
				break;
			}
		} else if(ret == -1) {
			break;
		}
	}
	i = stack2[--index];
	return i;
}
int main(int argc, char** argv){
	// Get arguments
	if (argc==1) {
		print_usage(argv[0]);
		return -1;
	}
	init_args();
	int result;
	while((result=getopt(argc,argv,"hc:i:o:"))!=-1){
		switch(result){
			/* INPUTS */
			case 'c':
				strcpy(m_config_file,optarg);
				printf("config file: %s\n",m_config_file);
				break;
			case 'i':
				strcpy(m_input_file,optarg);
				printf("input file: %s\n",m_input_file);
				break;
			case 'o':
				strcpy(m_output_file,optarg);
				printf("output file: %s\n",m_output_file);
				break;
			case '?':
				printf("Wrong option! optopt=%c, optarg=%s\n", optopt, optarg);
				break;
			case 'h':
			default:
				print_usage(argv[0]);
				return 1;
		}
	}

	// wire info
	std::vector<std::string> m_layer_name;
	std::vector<double> m_layer_radius;
	std::vector<std::string> m_layer_nWire;
	std::vector<std::string> m_layer_nWire_string;
	std::vector<std::vector<std::string> > m_layer_nWire_paras;
	std::vector<std::string> m_layer_phi;
	std::vector<std::string> m_layer_phi_string;
	std::vector<std::vector<std::string> > m_layer_phi_paras;

	std::stringstream buf;
	std::string temp;
	// Read from configure file
	std::ifstream fin_config(m_config_file);
	if(!fin_config){
		std::cout<<"Cannot open "<<m_config_file<<" in ifstream format"<<std::endl;
		return -1;
	}
	while(getline(fin_config,temp)){
		buf.str("");
		buf.clear();
		buf<<temp;
		double value;
		std::string identifier;
		std::string name;
		std::string trash;
		buf>>identifier>>name>>trash>>value;
		m_para_value.push_back(value);
		m_para_identifier.push_back(identifier);
		m_para_name.push_back(name);
	}

	// Read from input file
	std::ifstream fin_input(m_input_file);
	if(!fin_input){
		std::cout<<"Cannot open "<<m_input_file<<" in ifstream format"<<std::endl;
		return -1;
	}
	bool inWireLines = false;
	while(getline(fin_input,temp)){
		//std::cout<<temp<<std::endl;
		buf.str("");
		buf.clear();
		buf<<temp;
		double double1,double2;
		std::string seg1,seg2,seg3,seg4;
		std::string trash;
		buf>>seg1>>seg2;
		bool isParameter = false;
		int i_para = -1;
		for ( int i = 0; i < m_para_value.size(); i++ ){
			//std::cout<<"m_para_identifier["<<i<<"] = "<<m_para_identifier[i]<<", m_para_name["<<i<<"] = "<<m_para_name[i]<<std::endl;
			if ( seg1 == m_para_identifier[i] && seg2 == m_para_name[i] ){
				isParameter = true;
				i_para = i;
				break;
			}
		}
		if (isParameter){
			buf>>trash>>m_para_value[i_para];
			continue;
		}
		else if ( seg1 == "ROWS" && seg2 == "Polar" ) {
			inWireLines = true;
			continue;
		}
		if ( inWireLines ){
			if (seg1.size()==0){
				inWireLines = false;
				break;
			}
			else{
				buf>>trash>>double1>>seg3;
				m_layer_name.push_back(seg1);
				m_layer_radius.push_back(double1);
				std::vector<std::string> vec;
				vec.clear();
				m_layer_nWire_string.push_back(seg2);
				m_layer_nWire.push_back("");
				m_layer_nWire_paras.push_back(vec);
				get_formula(seg2,m_layer_nWire[m_layer_nWire.size()-1],m_layer_nWire_paras[m_layer_nWire_paras.size()-1]);
				m_layer_phi_string.push_back(seg3);
				m_layer_phi.push_back("");
				m_layer_phi_paras.push_back(vec);
				get_formula(seg3,m_layer_phi[m_layer_phi.size()-1],m_layer_phi_paras[m_layer_phi_paras.size()-1]);
			}
		}
	}