If we want to excel in a school based on the table, quickly generate some automation of c ++ code, you can quickly look through a student's name id, height, etc., and scalability, try to do the following
1. Create a new list
Write a simple school class
#ifndef _SCHOOL_H_
#define _SCHOOL_H_
#include <string>
#include <list>
using namespace std;
struct student
{
int id;
string name;
int gender;
int height;
student(int _id,string _name,int _gender, int _height):id(_id),name(_name),gender(_gender),height(_height){};
};
class school
{
public:
school();
~school();
int getStudentNum(void);
string getStudentNamebyId(int id);
private:
list<student*> mStundentList;
};
#endif
Class implementation:
#include "StdAfx.h"
#include "school.h"
#define ADD_STUDENT(id,name,gender,height) mStundentList.push_back(new student(id,name,gender,height))
school::school()
{
/* list start */
/* list end */
}
school::~school()
{
list<student*>::iterator it = mStundentList.begin();
for(; it!= mStundentList.end();it++)
{
delete (*it);
}
}
int school::getStudentNum(void)
{
return mStundentList.size();
}
string school::getStudentNamebyId(int id)
{
list<student*>::iterator it = mStundentList.begin();
for(; it!= mStundentList.end();it++)
{
if((*it)->id == id )
{
return (*it)->name;
}
}
return "";
}
Documentation will be a file in the same directory school.cpp on excelTest.py folder, excelTest.py as follows:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'a test module'
_author_ = 'Dani'
import os
import sys
import xlrd
reload(sys)
sys.setdefaultencoding('utf8') ##just for py2
studentData = []
RESOURCE_LIST_FILE_PATH = './tt.xlsx'
DEFINE_OUT_FILE = './Documentation/school.cpp'
START_PORT = '/* list start */'
END_PORT = '/* list end */'
TAB = ' '
STUDENT_ID_INDEX = 3
STUDENT_NAME_INDEX = 4
STUDENT_GENDER_INDEX = 5
STUDENT_HEIGHT_INDEX = 6
GENDER_MAP = {'m': '0','w':'1'}
class student():
def __init__(self):
pass
def setId(self, _id):
self.id = _id
def getId(self):
return self.id
def setName(self, _name):
self.name = _name
def getName(self):
return self.name
def setGender(self, _gender):
self.gender = _gender
def getGender(self):
return self.gender
def setHeight(self, _height):
self.height = _height
def getHeight(self):
return self.height
def LoadStudentListXmlFile():
global studentData
if (False == os.path.exists(RESOURCE_LIST_FILE_PATH)):
print(RESOURCE_LIST_FILE_PATH + ' is not exist!')
return
= xlrd.open_workbook xlsData (RESOURCE_LIST_FILE_PATH)
mSheet = xlsData.sheet_by_name ( 'Sheet1') Find Page ##
mRows mSheet.nrows = ## rows
mCols = mSheet.ncols ## column
firstRowData mSheet.row_values = (. 5)
for I in Range (. 5, mRows):
, rowData = mSheet.row_values (I)
Item = Student ()
item.setId (, rowData [STUDENT_ID_INDEX])
item.setName (, rowData [STUDENT_NAME_INDEX])
item.setGender (, rowData [STUDENT_GENDER_INDEX])
item.setHeight ( , rowData [STUDENT_HEIGHT_INDEX])
studentData.append (Item) data of each line ## may be constructed of a student object, all objects placed in a list of studentData
pass
CreateStudentFiles()
pass
def get_context_space(lenght, ctx): ##加空格对齐
space = ' '
if (lenght > len(ctx)):
space = (lenght - len(ctx)) * ' '
return space
def CreateStudentFiles():
global studentData
ctx =''
# Write TT ini files #
for j in range(0, len(studentData)):
tt_ctx = ''
tt_ctx += TAB + 'ADD_STUDENT('
tt_ctx += str(studentData[j].getId()).strip()
tt_ctx += ',' + get_context_space(50, tt_ctx)
tt_ctx += '"' + str(studentData[j].getName()).strip()+ '"'
tt_ctx += ',' + get_context_space(50 + 60, tt_ctx)
tt_ctx += GENDER_MAP[str(studentData[j] .getGender())]
tt_ctx += ',' + get_context_space(50 + 60 + 50, tt_ctx)
tt_ctx += str(studentData[j].getHeight()).strip()
tt_ctx += ');'
ctx += tt_ctx
if (j < (len(studentData) - 1)):
ctx += '\n'
pass
def_file = open(DEFINE_OUT_FILE, 'r')
ctx += '\n'
baseCtx = ''
replacedCtx = ''
baseCtx = def_file.readlines();
def_file.close()
replaceStart = False
for line in baseCtx:
if (line.strip() == START_PORT):## is the starting point to the end point pass, plus ctx at the beginning of each line to read the rest
replaceStart = True
replacedCtx += line
replacedCtx += ctx
else:
if (replaceStart):
if (line.strip() == END_PORT):
replaceStart = False
replacedCtx += line
else:
pass
else:
replacedCtx += line
def_file = open(DEFINE_OUT_FILE, 'w') ##写入指定cpp文件
#print (replacedCtx)
def_file.write(replacedCtx)
def_file.close()
LoadStudentListXmlFile()
Simplified and modified on the basis of the chiefs, only by learning
After school.cpp operates as follows:
School :: School ()
{
/ * List * Start /
ADD_STUDENT (. 1, "fsfd", 0,. 1);
ADD_STUDENT (2, "dgjdsg",. 1, 2);
ADD_STUDENT (. 3 , "1fdfds", 0,. 3);
ADD_STUDENT (. 4, "rgcgdg", 0,. 4);
ADD_STUDENT(5, "3regds", 1, 23);
ADD_STUDENT(6, "3red", 1, 21);
ADD_STUDENT(7, "lcl", 1, 32);
ADD_STUDENT(8, "fd", 1, 21);
ADD_STUDENT(9, "fdfdfds", 0, 15);
ADD_STUDENT(10, "22df", 0, 32);
/* list end */
}
main function simply call:
#include"./school.h"
void main()
{
school msch;
cout << "has students:"<<msch.getStudentNum()<<endl;
int id = 7;
cout<< msch.getStudentNamebyId(id)<<endl;
cin.get();
}
operation result:
On this basis, we can make more complex configurations, adding more options and objects.