You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

783 lines
19 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "recommend.h"
#include "ui_recommend.h"
#include "home.h"
#include"time.h"
#include<random>
#include<QTime>
#include <QFile>
#include <QDebug>
#include <QString>
#include<iostream>
#include<math.h>
#include<string>
#include <fstream>
using namespace std;
#define N 99
QString addapt_pet = "";
int num[6] = {1,1,1,1,1,1};
int dog_num[11] = {1,1,1,1,1,1,1,1,1,1,1};
int back_index;
//A:1,B:2,C:3
int the_type;
int *next_test;
QString the_result(int type,int type2)
{
QString result;
if(type==1)
{
if(type2==1) result = "阿拉斯加";
if(type2==2) result = "哈士奇";
if(type2==3) result = "金毛";
if(type2==4) result = "柯基";
if(type2==5) result = "拉布拉多";
if(type2==6) result = "雪纳瑞";
if(type2==7) result = "斗牛犬";
if(type2==8) result = "比熊犬";
if(type2==9) result = "松狮";
if(type2==10) result = "吉娃娃";
if(type2==11) result = "柴犬(中华田园犬)";
if(type2==12) result = "秋田犬";
if(type2==13) result = "萨摩耶";
if(type2==14) result = "巴哥";
if(type2==15) result = "贵宾犬";
if(type2==16) result = "德牧";
}
if(type==2)
{
if(type2==1) result = "美国短毛猫";
if(type2==2) result = "英国短毛猫";
if(type2==3) result = "加菲猫";
if(type2==4) result = "波斯猫";
if(type2==5) result = "布偶猫";
if(type2==6) result = "暹罗猫";
if(type2==7) result = "狸花猫";
if(type2==8) result = "缅因猫";
if(type2==9) result = "孟加拉豹猫";
}
return result;
}
int Question_1(int value[])//第一部分问题
{
int result = 0;
for (int i = 0; i < 5; i++)
{
if (value[i] == 1)//'A'
result++;
else//'B'
result--;
}
if (value[5] == 1)//'A'
{
if (result > 0)
return 1;//dog
else
return 2;//cat
}
else
if (value[5] == 2)//'B'
return 3;//rabbit
else
return 4;//tortoise
}
int* Question_2(int type, int value[])//第二部分问题靠type区分1dog还是2cat返回在txt表中的宠物的序号
{
int total_num;
string cat[9];
cat[0] = "美国短毛猫 3 1 2 2 2 1 1 1 1 1";
cat[1] = "英国短毛猫 2 1 2 1 1 1 1 1 1 1";
cat[2] = "加菲猫 2 2 1 1 1 1 1 2 1 2";
cat[3] = "波斯猫 2 1 1 1 2 1 1 2 1 2";
cat[4] = "布偶猫 2 2 1 1 2 2 1 1 1 1";
cat[5] = "暹罗猫 1 1 2 1 2 1 1 2 1 1";
cat[6] = "狸花猫 3 1 2 2 2 2 2 2 2 2";
cat[7] = "缅因猫 2 1 1 2 2 1 2 2 2 2";
cat[8] = "孟加拉豹猫 1 1 1 1 1 2 1 2 2 1";
string dog[17];
dog[0] = "阿拉斯加 3 2 2 1 1 1 2 2 1 1";
dog[1] = "哈士奇 2 2 2 1 2 2 2 2 2 1";
dog[2] = "金毛 3 2 2 2 2 2 1 1 1 2";
dog[3] = "柯基 1 1 2 1 1 1 1 1 1 1";
dog[4] = "拉布拉多 3 2 2 1 2 2 1 1 1 2";
dog[5] = "雪纳瑞 2 1 2 2 1 2 1 1 1 2";
dog[6] = "斗牛犬 2 1 1 1 2 1 2 2 1 1";
dog[7] = "比熊犬 1 1 1 2 1 2 2 1 1 2";
dog[8] = "松狮 3 2 2 2 2 1 2 2 2 2";
dog[9] = "吉娃娃 1 1 1 1 2 1 2 1 1 2";
dog[10] = "博美犬 1 1 1 2 1 2 2 1 1 1";
/*
dog[11] = "2 2 2 2 2 2 1 2 1 2";
dog[12] = "3 2 2 2 2 1 2 1 1 2";
dog[13] = "2 1 2 2 2 2 1 1 1 2";
dog[14] = "1 1 1 1 2 2 2 1 2 1";
dog[15] = "1 1 2 1 2 2 1 1 1 2";
dog[16] = "3 2 2 2 2 2 1 1 2 2";
*/
//抛弃dog第8题但用户仍需要回答
if (type == 1)
{
for (int i = 7; i < 11; i++)
value[i] = value[i + 1];
}
static int recommend_index[3] = { 0,0,0 };
int recommend_value[3] = { 100,100,100 };
if (type == 1)
{
total_num = 11;
}
else
{
total_num = 9;
}
string temp;
int k = 0;
for (int j2 = 0; j2 < total_num; j2++)
{
srand(time(NULL));
if (type == 2) temp = cat[j2];
if (type == 1) temp = dog[j2];
int out_value = 0;
int j = 0;
for (int i = 0; i <= (int)(temp.size() - 1); i++)
{
if (temp[i] >= '0' && temp[i] <= '9')
{
out_value += pow(abs(temp[i] - '0' - value[j]),2);
j++;
}
}
if (out_value <= recommend_value[2])
{
if (out_value == recommend_value[2])
{
if ((rand()%(N+1))/(float)(N+1) > 0.5)
{
recommend_index[2] = k;
recommend_value[2] = out_value;
}
}
else
{
recommend_index[2] = k;
recommend_value[2] = out_value;
}
}
if (out_value <= recommend_value[1])
{
if (out_value == recommend_value[1])
{
if ((rand() % (N + 1)) / (float)(N + 1) > 0.5)
{
recommend_index[2] = recommend_index[1];
recommend_value[2] = recommend_value[1];
recommend_index[1] = k;
recommend_value[1] = out_value;
}
}
else
{
recommend_index[2] = recommend_index[1];
recommend_value[2] = recommend_value[1];
recommend_index[1] = k;
recommend_value[1] = out_value;
}
}
if (out_value <= recommend_value[0])
{
if (out_value == recommend_value[0])
{
if (((rand() % (N + 1)) / (float)(N + 1)) > 0.5)
{
recommend_index[1] = recommend_index[0];
recommend_value[1] = recommend_value[0];
recommend_index[0] = k;
recommend_value[0] = out_value;
}
}
else
{
recommend_index[1] = recommend_index[0];
recommend_value[1] = recommend_value[0];
recommend_index[0] = k;
recommend_value[0] = out_value;
}
}
k++;
}
for (int i = 0; i < 3; i++)
recommend_index[i] += 1;
return recommend_index;
}
recommend::recommend(QWidget *parent) :
QWidget(parent),
ui(new Ui::recommend)
{
ui->setupUi(this);
ui->change_page->setStyleSheet("QPushButton{border-radius:5px;background:rgb(255,43,89);color:black;}");
ui->last_page->setStyleSheet("QPushButton{border-radius:5px;background:rgb(255,43,89);color:black;}");
ui->stackWidget->setCurrentIndex(0);
QFont ft;
ft.setPointSize(16);
ui->test_result->setFont(ft);
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::red);
ui->test_result->setPalette(pa);
ui->test_result->setWordWrap(true);
connect(ui->page1_A,SIGNAL(clicked(bool)),this,SLOT(q1_A()));
connect(ui->page1_B,SIGNAL(clicked(bool)),this,SLOT(q1_B()));
connect(ui->page2_A,SIGNAL(clicked(bool)),this,SLOT(q2_A()));
connect(ui->page2_B,SIGNAL(clicked(bool)),this,SLOT(q2_B()));
connect(ui->page3_A,SIGNAL(clicked(bool)),this,SLOT(q3_A()));
connect(ui->page3_B,SIGNAL(clicked(bool)),this,SLOT(q3_B()));
connect(ui->page4_A,SIGNAL(clicked(bool)),this,SLOT(q4_A()));
connect(ui->page4_B,SIGNAL(clicked(bool)),this,SLOT(q4_B()));
connect(ui->page5_A,SIGNAL(clicked(bool)),this,SLOT(q5_A()));
connect(ui->page5_B,SIGNAL(clicked(bool)),this,SLOT(q5_B()));
connect(ui->page6_A,SIGNAL(clicked(bool)),this,SLOT(q6_A()));
connect(ui->page6_B,SIGNAL(clicked(bool)),this,SLOT(q6_B()));
connect(ui->page6_C,SIGNAL(clicked(bool)),this,SLOT(q6_C()));
connect(ui->dog1_A,SIGNAL(clicked(bool)),this,SLOT(dog1_A()));
connect(ui->dog1_B,SIGNAL(clicked(bool)),this,SLOT(dog1_B()));
connect(ui->dog1_C,SIGNAL(clicked(bool)),this,SLOT(dog1_C()));
connect(ui->dog2_A,SIGNAL(clicked(bool)),this,SLOT(dog2_A()));
connect(ui->dog2_B,SIGNAL(clicked(bool)),this,SLOT(dog2_B()));
connect(ui->dog3_A,SIGNAL(clicked(bool)),this,SLOT(dog3_A()));
connect(ui->dog3_B,SIGNAL(clicked(bool)),this,SLOT(dog3_B()));
connect(ui->dog4_A,SIGNAL(clicked(bool)),this,SLOT(dog4_A()));
connect(ui->dog4_B,SIGNAL(clicked(bool)),this,SLOT(dog4_B()));
connect(ui->dog5_A,SIGNAL(clicked(bool)),this,SLOT(dog5_A()));
connect(ui->dog6_A,SIGNAL(clicked(bool)),this,SLOT(dog6_A()));
connect(ui->dog6_B,SIGNAL(clicked(bool)),this,SLOT(dog6_B()));
connect(ui->dog7_A,SIGNAL(clicked(bool)),this,SLOT(dog7_A()));
connect(ui->dog7_B,SIGNAL(clicked(bool)),this,SLOT(dog7_B()));
connect(ui->dog8_A,SIGNAL(clicked(bool)),this,SLOT(dog8_A()));
connect(ui->dog8_B,SIGNAL(clicked(bool)),this,SLOT(dog8_B()));
connect(ui->dog9_A,SIGNAL(clicked(bool)),this,SLOT(dog9_A()));
connect(ui->dog9_B,SIGNAL(clicked(bool)),this,SLOT(dog9_B()));
connect(ui->dog10_A,SIGNAL(clicked(bool)),this,SLOT(dog10_A()));
connect(ui->dog10_B,SIGNAL(clicked(bool)),this,SLOT(dog10_B()));
connect(ui->dog11_A,SIGNAL(clicked(bool)),this,SLOT(dog11_A()));
connect(ui->dog11_B,SIGNAL(clicked(bool)),this,SLOT(dog11_B()));
connect(ui->cat1_A,SIGNAL(clicked(bool)),this,SLOT(dog1_A()));
connect(ui->cat1_B,SIGNAL(clicked(bool)),this,SLOT(dog1_B()));
connect(ui->cat1_C,SIGNAL(clicked(bool)),this,SLOT(dog1_C()));
connect(ui->cat2_A,SIGNAL(clicked(bool)),this,SLOT(dog2_A()));
connect(ui->cat2_B,SIGNAL(clicked(bool)),this,SLOT(dog2_B()));
connect(ui->cat3_A,SIGNAL(clicked(bool)),this,SLOT(dog3_A()));
connect(ui->cat3_B,SIGNAL(clicked(bool)),this,SLOT(dog3_B()));
connect(ui->cat4_A,SIGNAL(clicked(bool)),this,SLOT(dog4_A()));
connect(ui->cat4_B,SIGNAL(clicked(bool)),this,SLOT(dog4_B()));
connect(ui->cat5_A,SIGNAL(clicked(bool)),this,SLOT(dog5_A()));
connect(ui->cat6_A,SIGNAL(clicked(bool)),this,SLOT(dog6_A()));
connect(ui->cat6_B,SIGNAL(clicked(bool)),this,SLOT(dog6_B()));
connect(ui->cat7_A,SIGNAL(clicked(bool)),this,SLOT(dog7_A()));
connect(ui->cat7_B,SIGNAL(clicked(bool)),this,SLOT(dog7_B()));
connect(ui->cat8_A,SIGNAL(clicked(bool)),this,SLOT(dog8_A()));
connect(ui->cat8_B,SIGNAL(clicked(bool)),this,SLOT(dog8_B()));
connect(ui->cat9_A,SIGNAL(clicked(bool)),this,SLOT(dog9_A()));
connect(ui->cat9_B,SIGNAL(clicked(bool)),this,SLOT(dog9_B()));
connect(ui->cat10_A,SIGNAL(clicked(bool)),this,SLOT(dog10_A()));
connect(ui->cat10_B,SIGNAL(clicked(bool)),this,SLOT(dog10_B()));
}
recommend::~recommend()
{
delete ui;
}
void recommend::on_commandLinkButton_clicked()
{
Home* recommend_to_home = new Home;
recommend_to_home->set_background();
recommend_to_home->show();
delete this;
}
void recommend::on_RandomRecommend_clicked()
{
int type;
int num=3;
int total=6;
int dog_list[11]={3,2,2,2,2,2,2,2,2,2,2};
int cat_list[10]={3,2,2,2,2,2,2,2,2,2};
int random_list[11]={1,1,1,1,1,1,1,1,1,1,1};
srand(time(NULL));
//dog
{
type=1;
for (int i = 0; i < (int)(sizeof(dog_list) / sizeof(dog_list[0])); i++)
{
random_list[i] = rand() % dog_list[i]+1;
}
next_test = Question_2(type,random_list);
for(int i=0;i<3;i++)
{
if(num!=0)
{
if(num==total)
{
addapt_pet += the_result(type,next_test[i]);
addapt_pet += " ";
num--;
}
else
{
if ((rand() % (N + 1) / (float)(N + 1) > 0.5))
{
addapt_pet += the_result(type,next_test[i]);
addapt_pet += " ";
num--;
}
}
}
total--;
}
}
//cat
{
type=2;
for (int i = 0; i < (int)(sizeof(cat_list) / sizeof(cat_list[0])); i++)
{
random_list[i] = rand() % cat_list[i]+1;
}
next_test = Question_2(type,random_list);
for(int i=0;i<3;i++)
{
if(num!=0)
{
if(num==total)
{
addapt_pet += the_result(type,next_test[i]);
addapt_pet += " ";
num--;
}
else
{
if ((rand() % (N + 1) / (float)(N + 1) > 0.5))
{
addapt_pet += the_result(type,next_test[i]);
addapt_pet += " ";
num--;
}
}
}
total--;
}
}
back_index=ui->stackWidget->currentIndex();
int next_index= 32;
ui->test_result->setText(addapt_pet);
addapt_pet.clear();
ui->stackWidget->setCurrentIndex(next_index);
}
void recommend::recommend_set_background()
{
this->setWindowTitle("智能推荐");
// ui->label->setStyleSheet("background-color:red");//背景色
this->setMinimumSize(1480,880);
this->setMaximumSize(1480,880);
this->setAutoFillBackground(true);
QPalette palette;
QPixmap pixmap = QPixmap(":/image/recommend_background.jpg").scaled(this->size());
palette.setBrush(QPalette::Window, QBrush(pixmap));
this->setPalette(palette);
}
void recommend::on_change_page_clicked()
{
//int nCount = ui->stackWidget->count();//总共的页数
int choose1 = Question_1(num);
int current_index = ui->stackWidget->currentIndex(); //当前的页数
int next_index;
if(current_index>=0&&current_index<6)
{
back_index=current_index;
next_index = current_index + 1;
}
if(current_index==6)
{
for(int i=0;i<6;i++)
{
qDebug("%d",num[i]);
}
choose1 = Question_1(num);
if(choose1==1)
{
next_index = 7;
the_type = 1;
}
else if(choose1==2)
{
the_type = 2;
next_index=19;
}
else if(choose1==3)
{
back_index=current_index;
next_index=30;
}
else if(choose1==4)
{
back_index=current_index;
next_index=31;
}
}
if(current_index>=7&&current_index<18)
{
back_index=current_index;
next_index = current_index + 1;
}
if(current_index==18)
{
back_index=current_index;
next_index = 32;
next_test = Question_2(the_type,dog_num);
for(int i=0;i<3;i++)
{
qDebug("new:%d",next_test[i]);
addapt_pet += the_result(1,next_test[i]);
addapt_pet += " ";
qDebug()<<addapt_pet;
}
ui->test_result->setText(addapt_pet);
addapt_pet.clear();
}
if(current_index>=19&&current_index<=28)
{
back_index=current_index;
next_index = current_index + 1;
}
if(current_index==29)
{
back_index=current_index;
next_index = 32;
next_test = Question_2(the_type,dog_num);
for(int i=0;i<3;i++)
{
qDebug("new:%d",next_test[i]);
addapt_pet += the_result(2,next_test[i]);
addapt_pet += " ";
qDebug()<<addapt_pet;
}
ui->test_result->setText(addapt_pet);
addapt_pet.clear();
}
if(current_index==30)
{
back_index=current_index;
next_index = 33;
}
if(current_index==31)
{
back_index=current_index;
next_index = 33;
}
ui->stackWidget->setCurrentIndex(next_index);
// 当需要显示的页面索引大于等于总页面时,切换至首页,因为是从0开始计数的
if (current_index==32)
{
Home* to_home = new Home;
to_home->set_background();
to_home->show();
delete this;
}
}
void recommend::on_last_page_clicked()
{
int nIndex2 = ui->stackWidget->currentIndex(); //当前的页数
if(nIndex2==32 or nIndex2==33)
ui->stackWidget->setCurrentIndex(back_index);
else
{
if(nIndex2==7 or nIndex2==19 or nIndex2==30 or nIndex2==31)
ui->stackWidget->setCurrentIndex(6);
else
{
nIndex2--;
if(nIndex2<0) nIndex2=0;
ui->stackWidget->setCurrentIndex(nIndex2);
}
}
}
void recommend::q1_A()
{
num[0] = 1;
}
void recommend::q1_B()
{
num[0] = 2;
}
void recommend::q2_A()
{
num[1] = 1;
}
void recommend::q2_B()
{
num[1] = 2;
}
void recommend::q3_A()
{
num[2] = 1;
}
void recommend::q3_B()
{
num[2] = 2;
}
void recommend::q4_A()
{
num[3] = 1;
}
void recommend::q4_B()
{
num[3] = 2;
}
void recommend::q5_A()
{
num[4] = 1;
}
void recommend::q5_B()
{
num[4] = 2;
}
void recommend::q6_A()
{
num[5] = 1;
}
void recommend::q6_B()
{
num[5] = 2;
}
void recommend::q6_C()
{
num[5] = 3;
}
void recommend::dog1_A()
{
dog_num[0] = 1;
}
void recommend::dog1_B()
{
dog_num[0] = 2;
}
void recommend::dog1_C()
{
dog_num[0] = 3;
}
void recommend::dog2_A()
{
dog_num[1] = 1;
}
void recommend::dog2_B()
{
dog_num[1] = 2;
}
void recommend::dog3_A()
{
dog_num[2] = 1;
}
void recommend::dog3_B()
{
dog_num[2] = 2;
}
void recommend::dog4_A()
{
dog_num[3] = 1;
}
void recommend::dog4_B()
{
dog_num[3] = 2;
}
void recommend::dog5_A()
{
dog_num[4] = 1;
}
void recommend::dog5_B()
{
dog_num[4] = 2;
}
void recommend::dog6_A()
{
dog_num[5] = 1;
}
void recommend::dog6_B()
{
dog_num[5] = 2;
}
void recommend::dog7_A()
{
dog_num[6] = 1;
}
void recommend::dog7_B()
{
dog_num[6] = 2;
}
void recommend::dog8_A()
{
dog_num[7] = 1;
}
void recommend::dog8_B()
{
dog_num[7] = 2;
}
void recommend::dog9_A()
{
dog_num[8] = 1;
}
void recommend::dog9_B()
{
dog_num[8] = 2;
}
void recommend::dog10_A()
{
dog_num[9] = 1;
}
void recommend::dog10_B()
{
dog_num[9] = 2;
}
void recommend::dog11_A()
{
dog_num[10] = 1;
}
void recommend::dog11_B()
{
dog_num[10] = 2;
}
void recommend::cat1_A()
{
dog_num[0] = 1;
}
void recommend::cat1_B()
{
dog_num[0] = 2;
}
void recommend::cat1_C()
{
dog_num[0] = 3;
}
void recommend::cat2_A()
{
dog_num[1] = 1;
}
void recommend::cat2_B()
{
dog_num[1] = 2;
}
void recommend::cat3_A()
{
dog_num[2] = 1;
}
void recommend::cat3_B()
{
dog_num[2] = 2;
}
void recommend::cat4_A()
{
dog_num[3] = 1;
}
void recommend::cat4_B()
{
dog_num[3] = 2;
}
void recommend::cat5_A()
{
dog_num[4] = 1;
}
void recommend::cat5_B()
{
dog_num[4] = 2;
}
void recommend::cat6_A()
{
dog_num[5] = 1;
}
void recommend::cat6_B()
{
dog_num[5] = 2;
}
void recommend::cat7_A()
{
dog_num[6] = 1;
}
void recommend::cat7_B()
{
dog_num[6] = 2;
}
void recommend::cat8_A()
{
dog_num[7] = 1;
}
void recommend::cat8_B()
{
dog_num[7] = 2;
}
void recommend::cat9_A()
{
dog_num[8] = 1;
}
void recommend::cat9_B()
{
dog_num[8] = 2;
}
void recommend::cat10_A()
{
dog_num[9] = 1;
}
void recommend::cat10_B()
{
dog_num[9] = 2;
}