当前位置:网站首页>QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)
QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)
2022-07-06 20:28:00 【程序媛zcc】
QT项目练习需求:
节目列向前向后插入或者新建列的名称设置:如之前被删除先找中间缺少的列 添加,否则找最大值,名称也可随时修改、可重复,项目分析各种情况如下:
例如下:找最大值, 此时新建列 名称为 节目5

例如下:找中间缺的最小值, 此时新建列 名称则为 节目2
![]()
例如下: 此时新建列 名称为 节目1 (节目1ss 不符合 节目”+数字)

主要代码:
QVector<ProgramCol*> programColList;//存储列数组
class ProgramCol{
int programId = 0;//节目id
int m_col = 0;// 列
string m_name ="节目";//节目名称
}
//插入、新建列 col传新的位置
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 = "节目" + 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))
{
/*解释:
sin>>t表示把sin转换成double的变量(其实对于int和float型的都会接收),
如果转换成功,则值为非0,如果转换不成功就返回为0
*/
return false;
}
if (sin >> c)
{
/*解释:
此部分用于检测错误输入中,数字加字符串的输入形式(例如:34.f),在上面的的部分(sin>>t)
已经接收并转换了输入的数字部分,在stringstream中相应也会把那一部分给清除,
此时接收的是.f这部分,所以条件成立,返回false
*/
return false;
}
return true;
}
//找到数组中消失的数字
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返回值是重复元素的开始位置
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;
}
// 节目列向前向后插入、新建列:如之前被删除先找中间缺少的列 添加,否则找最大值(不能把 找到缺失的值赋给id,如果之前改过名会重复, 节目id还是找id最大+1)
int ProgramManagement::findColNameIndex()
{
vector<int> nums = {};
for(int i = 0;i<programColList.size();i++) {
string name1 = programColList[i]->m_name.substr(0, 6);//截取开头 "节目"
qDebug() << "name1==== " << QString::fromStdString(name1);
if(name1 == "节目") {
string name2 = programColList[i]->m_name.substr(6);//截取"节目"之后
qDebug() << "name2==== " << QString::fromStdString(name2);
if(isNum(name2) && (name2.substr(0, 1) != "0"))//判断为数字并且 不以0开头
{
int numA = atoi(name2.c_str());//string 转成int
qDebug() << "字符串转换成int 大于0 并且 开头不为0 加入数组 === =" << numA;
if(numA > 0) {
nums.push_back(numA);
}
}
}
}
qDebug() << "nums size == " << nums.size();
int nameIndex = 1;
if(nums.size() > 0) { //没改过名 符合“节目”+数字 数组大于0 查找缺失。 =0 都被改过不符合名称 nameIndex就直接等于1
vector<int> l = findDisappearedNumbers(nums);
qDebug() << "l size == " << l.size();
if(l.size() > 0) {
//找到缺失数字 nameIndex等于第一项
nameIndex = l[0];
} else {
//没有找到缺失 就找 名称数组 最大值+1 ,(节目列名称、顺序都可以改,找col 不准)
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;
}边栏推荐
- Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]
- 从0开始创建小程序
- 21.(arcgis api for js篇)arcgis api for js矩形采集(SketchViewModel)
- First understand the principle of network
- CMB's written test - quantitative relationship
- 23. (ArcGIS API for JS) ArcGIS API for JS ellipse collection (sketchviewmodel)
- Sub pixel corner detection opencv cornersubpix
- Shell programming basics
- Search of linear table
- The latest 2022 review of "small sample deep learning image recognition"
猜你喜欢

树莓派设置wifi自动连接

Mathematical induction and recursion

Make (convert) ICO Icon

R data analysis: how to predict Cox model and reproduce high score articles

1200.Minimum Absolute Difference

Appx代码签名指南

22.(arcgis api for js篇)arcgis api for js圆采集(SketchViewModel)

Stored procedures and functions (MySQL)

VHDL实现单周期CPU设计

RestClould ETL 社区版六月精选问答
随机推荐
My brave way to line -- elaborate on what happens when the browser enters the URL
【达梦数据库】备份恢复后要执行两个sql语句
PIP download only, not install
[swift] learning notes (I) -- familiar with basic data types, coding styles, tuples, propositions
Leetcode-02 (linked list question)
20.(arcgis api for js篇)arcgis api for js面采集(SketchViewModel)
海思万能平台搭建:颜色空间转换YUV2RGB
Decoration design enterprise website management system source code (including mobile source code)
装饰设计企业网站管理系统源码(含手机版源码)
Significance and measures of source code confidentiality
美国空军研究实验室《探索深度学习系统的脆弱性和稳健性》2022年最新85页技术报告
25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)
An error in SQL tuning advisor ora-00600: internal error code, arguments: [kesqsmakebindvalue:obj]
Cryptography series: detailed explanation of online certificate status protocol OCSP
Mathematical induction and recursion
Netperf and network performance measurement
Flink task exit process and failover mechanism
Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications
Matlab Error (Matrix dimensions must agree)
Set static IP for raspberry pie