当前位置:网站首页>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;
}边栏推荐
- 树莓派设置wifi自动连接
- Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
- 安装 torch 0.4.1
- VHDL实现任意大小矩阵加法运算
- 20.(arcgis api for js篇)arcgis api for js面采集(SketchViewModel)
- leetcode
- 编译常量、ClassLoader类、系统类加载器深度探析
- Room rate system - login optimization
- [dream database] add the task of automatically collecting statistical information
- How to replace the backbone of the model
猜你喜欢

Basic concepts of Huffman tree

从0开始创建小程序

VHDL实现任意大小矩阵加法运算

线性表的查找

25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)

input_ delay

The latest 2022 review of "small sample deep learning image recognition"
![[cpk-ra6m4 development board environment construction based on RT thread studio]](/img/08/9a847c73d6da6fc74d84af56897752.png)
[cpk-ra6m4 development board environment construction based on RT thread studio]

Search of linear table

哈夫曼树基本概念
随机推荐
VHDL实现单周期CPU设计
Restcloud ETL Community Edition June featured Q & A
Stored procedures and functions (MySQL)
Matlab Error (Matrix dimensions must agree)
Ubuntu20 installation redisjson record
25.(arcgis api for js篇)arcgis api for js线修改线编辑(SketchViewModel)
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
About Estimation Statistics
Ubuntu 20 installation des enregistrements redisjson
Basic concepts of Huffman tree
20. (ArcGIS API for JS) ArcGIS API for JS surface collection (sketchviewmodel)
ubuntu20安裝redisjson記錄
安装 torch 0.4.1
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
“去虚向实”大潮下,百度智能云向实而生
Codeforces round 264 (Div. 2) C gargari and Bishop [violence]
[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial
Function reentry, function overloading and function rewriting are understood by yourself
Flink task exit process and failover mechanism
19. (ArcGIS API for JS) ArcGIS API for JS line acquisition (sketchviewmodel)