当前位置:网站首页>QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)
QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)
2022-07-07 03:41:00 【Program yuan ZCC】
QT Project practice needs :
Insert the program column forward and backward or set the name of the new column : If it has been deleted before, first find the missing column in the middle add to , Otherwise, find the maximum , The name can also be modified at any time 、 repeatable , Various situations of project analysis are as follows :
For example, next : Find the maximum , A new column is created The name is show 5
For example, next : Find the minimum value missing in the middle , A new column is created The name is show 2
For example, next : A new column is created The name is show 1 ( show 1ss Do not conform to the show ”+ Numbers )
Main code :
QVector<ProgramCol*> programColList;// Store column array
class ProgramCol{
int programId = 0;// show id
int m_col = 0;// Column
string m_name =" show ";// Program name
}
// Insert 、 New column col Pass the new position
int ProgramManagement::insertProgramCol(int col)
{
int len = programColList.size();
int index = 0;
for (int n = 0; n < len; n++) {
if(programColList[n]->m_col == col ){
programColList[n]->m_col+=1;
index = n;
}else if(programColList[n]->m_col > col){
programColList[n]->m_col+=1;
}
}
int id = getMaxProgramColId();
id ++;
int nameIndex = findColNameIndex();
ProgramBaseAttr* info = new ProgramBaseAttr();
info->programId = id;
info->m_col = col;
info->m_name = " show " + to_string(nameIndex);
programColList.insert(index,info);
qDebug()<< "programColList col== " << col<<nameIndex;
return nameIndex;
}
bool ProgramManagement::isNum(string str)
{
stringstream sin(str);
double d;
char c;
if(!(sin >> d))
{
/* explain :
sin>>t Express the sin convert to double The variable of ( In fact, for int and float Type will receive ),
If the conversion is successful , Then the value is non 0, If the conversion is unsuccessful, it returns 0
*/
return false;
}
if (sin >> c)
{
/* explain :
This part is used to detect wrong input , The input form of numbers plus strings ( for example :34.f), In the upper part (sin>>t)
The digital part of the input has been received and converted , stay stringstream The corresponding part will also be removed ,
What I'm receiving at this time is .f This part , So the condition holds , return false
*/
return false;
}
return true;
}
// Find the missing number in the array
vector<int> ProgramManagement::findDisappearedNumbers(vector<int>& num)
{
vector<int>res;
if(num.size() == 0) return res;
sort(num.begin(),num.end());
num.erase(unique(num.begin(), num.end()), num.end()); //unique The return value is the starting position of the repeating element
vector<int>::iterator it;
for(int j=1;j<=num[num.size()-1];j++)
{
it=find(num.begin(),num.end(),j);
if(it==num.end())
{
res.push_back(j);
}
}
for(int i=0;i<res.size();i++)
{
cout<<res[i]<<endl;
}
return res;
}
// The program line is inserted forward and backward 、 New column : If it has been deleted before, first find the missing column in the middle add to , Otherwise, find the maximum ( Can't take Find the missing value assigned to id, If you change your name before, it will repeat , show id Still looking id Maximum +1)
int ProgramManagement::findColNameIndex()
{
vector<int> nums = {};
for(int i = 0;i<programColList.size();i++) {
string name1 = programColList[i]->m_name.substr(0, 6);// Intercept the beginning " show "
qDebug() << "name1==== " << QString::fromStdString(name1);
if(name1 == " show ") {
string name2 = programColList[i]->m_name.substr(6);// Intercept " show " after
qDebug() << "name2==== " << QString::fromStdString(name2);
if(isNum(name2) && (name2.substr(0, 1) != "0"))// Judge as a number and Don't to 0 start
{
int numA = atoi(name2.c_str());//string Turn into int
qDebug() << " The string is converted to int Greater than 0 also Does not start with 0 Add array === =" << numA;
if(numA > 0) {
nums.push_back(numA);
}
}
}
}
qDebug() << "nums size == " << nums.size();
int nameIndex = 1;
if(nums.size() > 0) { // I haven't changed my name accord with “ show ”+ Numbers The array is greater than 0 Find missing . =0 Have been changed, do not meet the name nameIndex It's directly equal to 1
vector<int> l = findDisappearedNumbers(nums);
qDebug() << "l size == " << l.size();
if(l.size() > 0) {
// Find the missing number nameIndex Equal to the first term
nameIndex = l[0];
} else {
// No missing found Just look for Name array Maximum +1 ,( Program name 、 The order can be changed , look for col forbid )
vector<int>::iterator itMax = max_element(nums.begin(), nums.end());
nameIndex = *itMax + 1;
}
}
qDebug() << "nameIndex== " << nameIndex;
return nameIndex;
}
int ProgramManagement::getMaxProgramColId() {
int maxid = 0;
foreach(auto scr, programColList)
{
if(scr->programId > maxid)
{
maxid = scr->programId;
}
}
return maxid;
}
边栏推荐
- 小程序能运行在自有App中,且实现直播和连麦?
- First understand the principle of network
- The latest 2022 review of "small sample deep learning image recognition"
- [security attack and Defense] how much do you know about serialization and deserialization?
- 25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)
- Leetcode-02 (linked list question)
- 存储过程与函数(MySQL)
- 22.(arcgis api for js篇)arcgis api for js圆采集(SketchViewModel)
- 自适应非欧表征广告检索系统AMCAD
- 如何自定义Latex停止运行的快捷键
猜你喜欢
About Tolerance Intervals
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
19.(arcgis api for js篇)arcgis api for js线采集(SketchViewModel)
1200.Minimum Absolute Difference
VHDL实现单周期CPU设计
【安全攻防】序列化与反序列,你了解多少?
Calculation of time and space complexity (notes of runners)
ubuntu20安裝redisjson記錄
Mobile measurement and depth link platform - Branch
Restcloud ETL Community Edition June featured Q & A
随机推荐
codeforces每日5题(均1700)-第七天
25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)
23.(arcgis api for js篇)arcgis api for js椭圆采集(SketchViewModel)
22.(arcgis api for js篇)arcgis api for js圆采集(SketchViewModel)
Jerry's question about DAC output power [chapter]
本机mysql
Clock in during winter vacation
25.(arcgis api for js篇)arcgis api for js线修改线编辑(SketchViewModel)
如何替换模型的骨干网络(backbone)
Graphical tools package yolov5 and generate executable files exe
Sub pixel corner detection opencv cornersubpix
Flink Task退出流程与Failover机制
[dream database] add the task of automatically collecting statistical information
Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]
Decoration design enterprise website management system source code (including mobile source code)
Principle of attention mechanism
SSL certificate deployment
Open3D 网格滤波
Do you know the five most prominent advantages of E-bidding?
Docker部署Mysql8的实现步骤