C++程序设计(0401)

冲突检测代码准确:BK2-5-7;csvtest04;csvtest02
/目前存在的问题是:定义数组的长度;以及
#include "stdc++.h"
#include "iostream"
#include "string"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "ctime"
#include "fstream"
#include "sstream"
#include "vector"


using namespace std;


double p[999];
double q[999];
int l[51] = { 0 };//注意更改
int m[101][101];//有向图的邻接矩阵
int x[101];//当前团的解
int bestx[101];//最优解
int n;//表示图的顶点数
int cn = 0;//当前团的大小
int bestn;//当前最优值

template<typename T>
vector<int>  sort_indexes(const vector<T>& v, bool reverse = false) {

    // initialize original index locations
    vector<int>  idx(v.size());
    for (int i = 0; i != idx.size(); ++i) idx[i] = i;

    // sort indexes based on comparing values in v
    if (reverse)
    {
        sort(idx.begin(), idx.end(),
            [&v](int i1, int i2) {return v[i1] > v[i2]; });
    }
    else {
        sort(idx.begin(), idx.end(),
            [&v](int i1, int i2) {return v[i1] < v[i2]; });
    }

    return idx;
}

void BronKerbosch(int i)///自定义B-K算法void BronKerbosch函数用来求解最大团
{
    //printf("求最优解1\n");
    if (i > n) {//递归出口,到根节点时,更新最优值和最优解,返回
        bestn = cn;//更新最优值
        for (int j = 1; j <= n; j++)
            bestx[j] = x[j];
        return;//返回
    }
    x[i] = 1;//先假定x[i]=0;
    for (int j = 1; j < i; j++) {
        if (x[j] == 1 && m[i][j] == 0) {
            x[i] = 0;//如果该点与已知解中的点无边相邻
            break;//则不遍历左子树
        }
    }
    if (x[i] == 1) {//当且仅当x[i]==1时,遍历左子树
        cn++;//该点加入当前解
        BronKerbosch(i + 1);//递归调用
        cn--;//还原当前解
    }
    x[i] = 0;//假定x[i]==0
    if (cn + n - i > bestn) {//如果当前值+右子树可能选择的节点<当前最优解,不遍历左子树
        x[i] = 0;
        BronKerbosch(i + 1);
    }
    return;
}

void xiugai(string& a, int lie1, string xinneirong)
{
    cout << "开始进行修改操作!" << endl;
    string temp;
    string zuizhong;//最终得出的一个字符串,来覆盖原有的字符串的字符串! 
    int cont = 0;
    int flag = 0;
    int neirongshu = 0;//一共有多少内容,来判断有几个逗号! 
    vector<string>aa;
    char b[100];
    char c[100];
    strcpy(b, a.c_str());
    char delims[] = ",";
    char* result = NULL;
    char* shizhi = NULL;
    shizhi = strtok(b, delims);strtok:https://blog.csdn.net/qq_43478260/article/details/109298608
    while (shizhi != NULL)
    {
        neirongshu++;
        shizhi = strtok(NULL, delims);
    }
    strcpy(c, a.c_str());
    result = strtok(c, delims);
    //cout<<"请输入新数据!"<<endl;
    while (result != NULL)
    {
        cont++;
        temp = result;
        if (cont < neirongshu)
        {
            temp += ',';
        }
        aa.push_back(temp);//将每列的内容加上逗号压入vector 
        result = strtok(NULL, delims);
    }
    cout << "将修改数据写入CSV中!" << endl;
    aa[lie1 - 1] = xinneirong + ',';//因为数组是从0开始的 
    for (int ii = 0; ii < aa.size(); ii++)
    {
        zuizhong += aa[ii];
    }
    a = zuizhong;//使传进来的string变成修改后的string 
    return;
}

void xierucsv(char filename[256], int hang, int lie, int xinneirong1)
{
    //int hang;//需要进行修改的行数 
    //int lie;//需要进行修改的列数 
    ifstream infile(filename);
    string temp;
    string line;
    string xinneirong = to_string(xinneirong1);
    int flag = 0;
    vector<string>neirong;//创建动态string数组来储存文件里的数据 

    while (infile.good())//文件是否正确打开 
    {
        getline(infile, line);
        flag++;//记录行数 
        neirong.push_back(line);
    }
    infile.close();
    //cout << "请输入你要修改的行数和列数,以回车作为分隔!" << endl;
    //cin >> hang >> lie;

    while (1)//考虑剪枝
    {
        if (hang <= flag && hang > 0)
        {
            break;
        }
        if (hang > flag)
        {
            cout << "请输入正确的行数!" << endl;
            cin >> hang;
        }
    }//考虑剪枝

    int douhao = 0;//记录逗号数 //考虑剪枝
    for (int i = 0; i <= neirong[hang - 1].size(); i++)//考虑剪枝
    {
        if (neirong[hang - 1][i] == ',')
        {
            douhao++;
        }
    }
    //cout << "逗号数为" << douhao << endl; //考虑剪枝

    while (1)//考虑剪枝
    {
        if (lie <= douhao + 1 && lie > 0)
        {
            break;
        }
        if (lie > douhao + 1)
        {
            cout << "请输入正确的列数!" << endl;
            cin >> lie;
        }
    } //考虑剪枝

    xiugai(neirong[hang - 1], lie, xinneirong);//向xiugai函数传入string(即需要修改的行的内容)和需要修改的列数 
    ofstream onfile(filename);
    cout << "现在进行写入csv文件操作!" << endl;
    for (int l = 0; l < neirong.size(); l++)
    {
        onfile << neirong[l] << endl;
    }
    cout << "写入完毕!" << endl;
    onfile.close();
    cout << "关闭写入流!" << endl;
}

int main()
{
    time_t begin, end;计算程序运行时长
    double ret;
    begin = clock();计时开始

	cout << "将读取CSV文件的数据保存在数组n1中" << endl;
	//读取文件中的数据
	ifstream ifs;							    //创建流对象

	ifs.open("./testdataSS02.csv", ios::in);	//打开文件

	if (!ifs.is_open())						    //判断文件是否打开
	{
		cout << "打开文件失败!!!";
		return 0;
	}

	vector<string> item;		//用于存放文件中的一行数据

	string temp;				//临时存储文件中的一行数据

	while (getline(ifs, temp))  //利用 getline()读取文件中的每一行,并放入到 item 中
	{
		item.push_back(temp);
	}
    int i = 0; int j = 0; int c = 0;
	
	for (auto it = item.begin(); it != item.end(); it++)//遍历文件中的每一行数据
	{
		string str;

		//其作用是把字符串分解为单词(在此处就是把一行数据分为单个数据)
		istringstream istr(*it);

		//将字符串流 istr 中的字符读入到 str 字符串中,读取方式是以逗号为分隔符 
		getline(istr, str, ',');
		//cout << str << "\t";            // str 对应第一列数据

		//atoi(str.c_str())该函数将字符转化为 int 数据            
		p[i] = atof(str.c_str());
		i++;

		//getline(istr, str, ',');
		//cout << str << "\t";            // str 对应第二列数据 

		//getline(istr, str, ',');
		//cout << str << "\t";            // str 对应第三列数据   

		//getline(istr, str, ',');
		//cout << str << "\t";            // str 对应第四列数据 

		getline(istr, str, ',');
		//cout << str << "\t";            // str 对应第五列数据 
        q[j] = atof(str.c_str());
        j++;// str 对应第六列数据   

		getline(istr, str, ',');
		//cout << str << endl;   
		//cout << endl;

		l[c] = atoi(str.c_str());
		c++;// str 对应第六列数据

	}

    cout << "输出威胁度系数l" << endl;
    for (int i = 0; i < 51; i++)
    {
        cout << l[i] << " ";

    }cout << endl;

	double n1[51][2] = { 0 };

	for (int i = 0; i < 51; i++)
	{
		n1[i][0] = p[i];

	}

	for (int i = 0; i < 51; i++)
	{
		n1[i][1] = q[i];

	}

	int num = sizeof(n1) / sizeof(n1[0]);
    n = num;

	printf("输出矩阵n1\n");
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cout << n1[i][j] << " ";
		};
		cout << endl;
	}
	cout << endl;
    
    cout<< "输入图的顶点数n:" <<num<<endl;

    for (int i = 0; i < num; i++)//有逻辑问题得到改正
    {
        for (int j = i + 1; j < num; j++)
            if (((n1[j][0] <= n1[i][0]) && (n1[i][0] < n1[j][1])) || ((n1[j][0] < n1[i][1]) && (n1[i][1] <= n1[j][1])) 
                || ((n1[j][0] <= n1[i][0]) && (n1[i][1] <= n1[j][1])) || ((n1[i][0] <= n1[j][0]) && (n1[j][1] <= n1[i][1])))
            //if (((n1[j][0] < n1[i][0]) && (n1[i][0] < n1[j][1])) || ((n1[j][0] < n1[i][1]) && (n1[i][1] < n1[j][1])) ||
            //    ((n1[j][0] < n1[i][0]) && (n1[i][1] < n1[j][1])) || ((n1[i][0] < n1[j][0]) && (n1[j][1] < n1[i][1])) ||
            //    ((n1[j][0] = n1[i][0]) && (n1[i][1] = n1[j][1])))
            {
                m[i+1][j+1] = 1;
                m[j+1][i+1] = 1;
            };
    }
    cout << endl;

    printf("输出冲突矩阵M\n");
    for (int a = 1; a < num+1; a++)
	{
        for (int b = 1; b < num+1; b++)
        {
            cout << m[a][b] << " ";
        };
		cout << endl;
	}
    cout << endl;

    BronKerbosch(1);//求最优解

    printf("输出SS链序号构成最大团的数目 bestn:");
    cout << bestn << endl;//输出最优值
    int mm[15] = {0};//mm矩阵用于装输出构成最大团的SS链序号
    int cmm = 0;
    printf("输出构成最大团的SS链序号:");

    for (int i = 1; i <= num; i++)//输出
    {
        if (bestx[i])
        {
            cout << i-1 << " ";//输出最优解
            mm[cmm] = i-1;
            cmm++;
        }
    }cout << endl;

    cout<<"mm输出SS链序号构成最大团元素" << endl;
    for (int i = 0; i < bestn; i++)
    {
        cout << mm[i] << " ";
    }cout << endl;

    if (bestn > 8)
    {
        printf("共享WQSS链数目为超过最大通道容量,该WQ发生了资源冲突");
    }
    else
    {
        printf("该WQ无资源冲突");
    }
    cout << endl;
    cout << sizeof(mm)/sizeof(mm[0]) << endl;

    /根据mm输出SS链序号构成最大团元素对应的威胁度系数mm1
    cout << "输出SS链序号构成最大团元素对应的威胁度系数mm1" << endl;
    int mm1[sizeof(mm) / sizeof(mm[0])] = { 0 };

    for (int i=0; i< sizeof(mm) / sizeof(mm[0]); i++)
    {
        mm1[i] = l[mm[i]];
    }

    for (int i = 0; i < sizeof(mm) / sizeof(mm[0]); i++)
    {
        cout << mm1[i] << " ";
    }
    cout << endl;

    // 消解算法第一步
    cout << "sizeof(mm1) / sizeof(mm1[0]):" << bestn << endl;
    vector<int> ll(mm1, mm1 + bestn);
    vector<int> sorted_indx;
    sorted_indx = sort_indexes(ll);
    sorted_indx = sort_indexes(ll, true);
    int tempw[sizeof(mm) / sizeof(mm[0])] = { 0 };//tempwSS链序号的索引
    cout <<"sorted_indx.size():" << sorted_indx.size() << endl;
    cout << "descending sorted: ";
    for (int i = 0; i < sorted_indx.size(); i++)
    {
        cout << sorted_indx[i] << " ";
        tempw[i] = sorted_indx[i];
    }
    cout << endl;
    cout << "输出需要消解的SS链序号的索引: ";
    for (int i = 8; i < sizeof(mm) / sizeof(mm[0]); i++)
    {
        cout << tempw[i] << " ";
    }
    cout << endl;
    // 消解算法第二步
    int tempwx[sizeof(mm) / sizeof(mm[0]) - 8] = { 0 };//除了前8:tempwSS链序号的索引tempwx
    for (int i = 0; i < sizeof(mm) / sizeof(mm[0]) - 8; i++)
    {
        tempwx[i] = tempw[i + 8];
    }

    cout << "除了前8的tempwSS链序号的索引tempwx: ";
    for (int i = 0; i < sizeof(mm) / sizeof(mm[0]) - 8; i++)
    {
        cout << tempwx[i] << " ";
    }cout << endl;

    cout << "输出需要消解的SS链序号: ";
    for (int i = 0; i < sizeof(mm) / sizeof(mm[0])-8; i++)
    {
        cout << mm[tempwx[i]] << " ";
    }
    cout << endl;

    char filename1[256] = "testdataSS02.csv";
计算调整量;进行调整;调整量的计算可以使用PID算法,目的求解出最优的调整量datamodifi
    int datamodifi=0;
    int cmin=0;
    double nn2[sizeof(mm) / sizeof(mm[0])] = { 0 };
    double nn3[sizeof(mm) / sizeof(mm[0])] = { 0 };

    for (int i = 0; i < bestn; i++)
    {
        nn2[i] = n1[mm[i]][0];
        nn3[i] = n1[mm[i]][1];
    }

    cout << "nn2[i]&nn3[i]"<<bestn << endl;
    for (int i = 0; i < bestn; i++)
    {
        cout<<nn2[i]<<" " << nn3[i] << endl;
    }

    double max_num = *max_element(nn2, nn2 + sizeof(mm) / sizeof(mm[0]));
    double min_num = *min_element(nn3, nn3 + sizeof(mm) / sizeof(mm[0]));
    cout << "max_num" << max_num << endl;
    cout << "min_num" << min_num << endl;

    //cout << "sizeof(tempwx) / sizeof(tempwx[0]):" << sizeof(tempwx) / sizeof(tempwx[0]) << endl;
    //for (int i = 0; i < sizeof(tempwx) / sizeof(tempwx[0]); i++)
    //{
    //    xierucsv(filename1, mm[tempwx[i]]+1, 1, double(min_num));
    //    xierucsv(filename1, mm[tempwx[i]]+1, 2, double(min_num+10));//计算调整量
    //}

    end = clock();计时结束
    ret = double(end - begin) / CLOCKS_PER_SEC;//计算程序运行时长
    cout << "程序运行时间为:" << ret << "s" << endl;
	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值