⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rrrxin.cpp

📁 本程序为通信中
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// rrrr.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include <stdlib.h>
#include "conio.h"
//#define cycle 100000
#define maxand 10000
#define MaxLAMBDA1 maxand

void DelayTime();
void throughput();
void throughput2();
void DelayTime2();

struct tongguoliang
{
	long unsigned  int times;
	float tongguo;
	float tongguo1;
	float tongguo2;
};

struct Sdata
{
	int LAMBDA1;
	double delay1;
	int LAMBDA2;
	double delay2;
	struct Sdata *next;
};

int cycle;
int channel,channel1,channel2;

int main(int argc, char* argv[])
{
	char ch;
	FILE *fp1;
	fp1 = fopen("times.txt","r");
	fscanf(fp1,"%d ",&cycle);
	fclose(fp1);
	printf("Please input 1,2,3,or 4!\n");
	printf(" 1 : 当LAMBDA1的优先级大于LAMBDA2时,系统的吞吐量!\n");
	printf(" 2 : 当LAMBDA1的优先级大于LAMBDA2时,系统的延时!\n");
	printf(" 3 : 当LAMBDA1和LAMBDA2各占信道50%时,系统的吞吐量!\n");
	printf(" 4 : 当LAMBDA1和LAMBDA2各占信道50%时,系统的延时!\n");
	while (1) 
	{
		ch = getch();
		if(ch>48 && ch<54)
		{
			switch(ch) 
			{
			case '1':
				throughput();
				break;
			case '2':
				DelayTime();
				break;
			case '3':
				throughput2();	// 50% to 50%
				break;
			case '4':
				DelayTime2();
				break;
			default:
				break;
			}
		}
		printf("Please input 1,2,3,or 4!\n");
		printf(" 1 : 当LAMBDA1的优先级大于LAMBDA2时,系统的吞吐量!\n");
		printf(" 2 : 当LAMBDA1的优先级大于LAMBDA2时,系统的延时!\n");
		printf(" 3 : 当LAMBDA1和LAMBDA2各占信道50%时,系统的吞吐量!\n");
		printf(" 4 : 当LAMBDA1和LAMBDA2各占信道50%时,系统的延时!\n");
	}
	return 0;
}
void throughput()
{
	int i,j;
	int B1=10,B2=5;

	int channeltmp;
	int LAMBDA1,LAMBDA2;
	long int LAMBDA,LAMBDA1Remain,LAMBDA2Remain;
	struct tongguoliang tongji[maxand];
	float tongguotemp;
	float result;
	float total;
	int LAMBDAAll;
	float temp;
	int maxandtemp;
	int LAMBDAnum = 0;
	FILE *fp1,*fp2,*fp3;

	for(i=0;i<maxand;i++)
	{
		tongji[i].times = 0;
		tongji[i].tongguo = 0;
		tongji[i].tongguo1 = 0;
		tongji[i].tongguo2 = 0;
	}

	fp1 = fopen("data1.txt","r");

	fp2 = fopen("data2.txt","r");

	for(i=0;i<cycle;i++)
	{
		fscanf(fp1,"%d ",&LAMBDA1);
		fscanf(fp2,"%d ",&LAMBDA2);
		LAMBDA = LAMBDA1 + LAMBDA2;
		tongji[LAMBDA].times ++ ; 
		LAMBDA1Remain = 0;
		LAMBDA2Remain = 0;
		tongguotemp = 0;

		fp3 = fopen("gaosi1.txt","r");
		for(j=0;j<cycle;j++)
		{
			LAMBDA1Remain += LAMBDA1;
			LAMBDA2Remain += LAMBDA2;
			fscanf(fp3,"%d ",&channel);
			// LAMBDA1 process
			if(LAMBDA1Remain <= channel)
			{
				tongguotemp += LAMBDA1Remain * B1;
				channeltmp = channel - LAMBDA1Remain;
				LAMBDA1Remain = 0;
			}
			else
			{
				tongguotemp += channel * B1;
				channeltmp = 0;
				LAMBDA1Remain = LAMBDA1Remain - channel;
			}
			// LAMBDA2 process
			if(channeltmp > 0)
			{
				if(LAMBDA2Remain <= channeltmp)
				{
					tongguotemp += LAMBDA2Remain * B2;
					LAMBDA2Remain = 0;
				}
				else
				{
					tongguotemp += channeltmp * B2;
					LAMBDA2Remain = LAMBDA2Remain - channeltmp;
				}
			}
		}
		tongguotemp = tongguotemp / cycle;
		tongji[LAMBDA].tongguo += tongguotemp;
		fclose(fp3);
		if(i%100 == 0)
		{
			temp = 100*i/((float)cycle);
			system("cls"); 
			printf("The first part finish %4.2f %% \n",temp);
		}		
	}
	fclose(fp1);
	fclose(fp2);

	fp1 = fopen("result.txt","w+");
	fp2 = fopen("number.txt","w+");
	fp3 = fopen("tuntu.txt","w+");
	for(i=0;i<maxand;i++)
	{

		if(tongji[i].times !=0)
		{
			LAMBDAnum ++ ;
			result = (float)tongji[i].tongguo / tongji[i].times;
			printf("When LAMBDA = %d,the 通过量为: %f \n",i,result);
			fprintf(fp1,"%d %f \n",i,result);
			fprintf(fp2,"%d ",i);
			fprintf(fp3,"%f ",result);
		}
	}
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);


	total = 0;

	fp1 = fopen("max.txt","w+");
	for(i=0;i<LAMBDAnum;i++)
	{
		LAMBDAAll = 0;
		total = 0;
		fp3 = fopen("number.txt","r");
		fp2 = fopen("gaosi1.txt","r");
		fscanf(fp3,"%d ",&maxandtemp);
		for(j=0;j<cycle;j++)
		{
			LAMBDAAll += maxandtemp; 
			fscanf(fp2,"%d ",&channel);
			if(LAMBDAAll <= channel)
			{
				total = total + LAMBDAAll * B1;
				LAMBDAAll = 0;
			}
			else
			{
				LAMBDAAll = LAMBDAAll - channel;
				total = total + channel * B1;
			}
		}
		fclose(fp2);
		fclose(fp3);
		fprintf(fp1,"%d %f \n",maxandtemp,(total/cycle));
	}
	fclose(fp1);

}

void DelayTime()
{
	FILE *fp1,*fp2,*fp3;
	double TotalDelayTime = 0.0;
	double LAMBDA1TotalDelayTime = 0;
	double LAMBDA2TotalDelayTime = 0;
	unsigned int TotalLAMBDA = 0;
	unsigned int TotalLAMBDA1 = 0;
	unsigned int TotalLAMBDA2 = 0;
	unsigned int SuccessTotalLAMBDA1 = 0;
	unsigned int SuccessTotalLAMBDA2 = 0;
	unsigned int TransmitSuccess = 0;
	unsigned int TotalChannel = 0;
	struct Sdata *pHead,*pTail,*pLAMBDA1,*pLAMBDA2,*ptemp,*pDelete;
	int i;
	int channel;
	int channelRemain =0;
	int LAMBDA1= 0 ,LAMBDA2 = 0;
	int Readtemp;
	double temp;
	double AverageDelay;
	fp1 = fopen("data1.txt","r");
	fp2 = fopen("data2.txt","r");
	fp3 = fopen("gaosi1.txt","r");
	for(i=0;i<cycle;i++)
	{
	//	fscanf(fp3,"%d ",&channel);
		ptemp = (struct Sdata *)malloc(sizeof(struct Sdata));
		fscanf(fp1,"%d ",&Readtemp);
		ptemp->LAMBDA1 = Readtemp;
		LAMBDA1 = Readtemp;
		TotalLAMBDA1 +=  Readtemp;
		ptemp->delay1 = 0.5;
		fscanf(fp2,"%d ",&Readtemp);
		ptemp->LAMBDA2 = Readtemp;
		LAMBDA2 = Readtemp;
		TotalLAMBDA2 +=  Readtemp;
		ptemp->delay2 = 0.5;
		ptemp->next = NULL;
		TotalLAMBDA = TotalLAMBDA + ptemp->LAMBDA1 + ptemp->LAMBDA2;
		if(i == 0)
		{
			pHead = ptemp;
			pTail = ptemp;
			pDelete = ptemp;
			pLAMBDA1 = ptemp;
			pLAMBDA2 = ptemp;
		}
		else
		{
			pTail->next = ptemp;
			pTail = ptemp;
		}
		// LAMBDA1 process
		channelRemain = channel;
		while(pLAMBDA1 != pTail)
		{
			if(pLAMBDA1->LAMBDA1 <= channelRemain)
			{
				channelRemain = channelRemain - pLAMBDA1->LAMBDA1;
				TotalDelayTime += pLAMBDA1->LAMBDA1 * pLAMBDA1->delay1;
				LAMBDA1TotalDelayTime += pLAMBDA1->LAMBDA1 * pLAMBDA1->delay1;
				TransmitSuccess += pLAMBDA1->LAMBDA1;
				SuccessTotalLAMBDA1 += pLAMBDA1->LAMBDA1;
				pLAMBDA1->LAMBDA1 = 0;
				pLAMBDA1 = pLAMBDA1->next;
			}	
			else
			{
				TotalDelayTime += channelRemain * pLAMBDA1->delay1;
				LAMBDA1TotalDelayTime += channelRemain * pLAMBDA1->delay1;
				pLAMBDA1->LAMBDA1 = pLAMBDA1->LAMBDA1 - channelRemain;
				TransmitSuccess += channelRemain;
				SuccessTotalLAMBDA1 += channelRemain;
				pLAMBDA1->delay1 = pLAMBDA1->delay1 + 1;
				channelRemain = 0;
			}
			if(channelRemain == 0)
			{
				break ;
			}
		} 
		//	LAMBDA2 process
		while((pLAMBDA2 != pTail) & (channelRemain != 0))
		{
			if(pLAMBDA2->LAMBDA2 <= channelRemain)
			{
				channelRemain = channelRemain - pLAMBDA2->LAMBDA2;
				TotalDelayTime += pLAMBDA2->LAMBDA2 * pLAMBDA2->delay2;
				LAMBDA2TotalDelayTime += pLAMBDA2->LAMBDA2 * pLAMBDA2->delay2;
				TransmitSuccess += pLAMBDA2->LAMBDA2;
				SuccessTotalLAMBDA2 += pLAMBDA2->LAMBDA2;
				pLAMBDA2->LAMBDA2 = 0;
				pLAMBDA2 = pLAMBDA2->next;
			}
			else
			{
				TotalDelayTime += channelRemain * pLAMBDA2->delay2;
				LAMBDA2TotalDelayTime += channelRemain * pLAMBDA2->delay2;
				pLAMBDA2->LAMBDA2 = pLAMBDA2->LAMBDA2 - channelRemain;
				TransmitSuccess += channelRemain;
				SuccessTotalLAMBDA2 += channelRemain;
				pLAMBDA2->delay2 = pLAMBDA2->delay2 + 1;
				channelRemain = 0;
			}
			if(channelRemain == 0)
			{
				break;
			}
		}
		while (pHead != pTail)
		{
			if((pHead->LAMBDA1 == 0) && (pHead->LAMBDA2 == 0))
			{
				pDelete = pHead;
				pHead = pHead->next;
				free(pDelete);
			}
			else
			{
				break;
			}
		}
		fscanf(fp3,"%d ",&channel);	
		TotalChannel += channel;
		// Display
		if(i%100 == 0)
		{
			temp = 100*i/((float)cycle);
			system("cls"); 
			printf("The second part finish %4.2f %% \n",temp);
		}
	}
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	system("cls"); 
	TotalLAMBDA = TotalLAMBDA - LAMBDA1 - LAMBDA2;
	AverageDelay = TotalDelayTime / TotalLAMBDA;
	printf("Total channel number is %d \n",TotalChannel);
	printf("Need to be transmit is  %d .\n",TotalLAMBDA);
	printf("Suceessful transmit     %d . \n",TransmitSuccess);
	printf("remain %d can't be transmit!\n",TotalLAMBDA - TransmitSuccess);
	printf("Suceess transmit ratio is %4.2f %%\n",100*(float)TransmitSuccess/TotalLAMBDA);
	printf("Channel utilize  ratio is %4.2f %%\n",100*(float)TransmitSuccess/TotalChannel);
	printf("TotalDelayTime = %4.2f.\n",TotalDelayTime);
	printf("The Average delay time is %7.4f slot!\n",AverageDelay);
	printf("Success transmit average delay time is %7.4f slot.\n",(float)TotalDelayTime/TransmitSuccess);

	printf("\nLAMBDA1 Total Delay Time is   %4.2f .\n",LAMBDA1TotalDelayTime);
	printf("Total LAMBDA1 is %d \n",TotalLAMBDA1);
	printf("LAMBDA1 Average delay time is %7.4f slot!\n",(float)LAMBDA1TotalDelayTime/SuccessTotalLAMBDA1);
	printf("User1 success transmit ratio is %4.2f %%\n",100*(float)SuccessTotalLAMBDA1/(TotalLAMBDA1 - LAMBDA1));

	printf("\nLAMBDA2 Total Delay Time id   %4.2f .\n",LAMBDA2TotalDelayTime);
	printf("Total LAMBDA2 is %d \n",TotalLAMBDA2);
	if(SuccessTotalLAMBDA2 != 0)
	{
		printf("LAMBDA2 Average delay time is %7.4f slot!\n",(float)LAMBDA2TotalDelayTime/SuccessTotalLAMBDA2);
	}
	else
	{
		printf("User2 not data to be transmit\n");
	}
	printf("User2 success transmit ratio is %4.2f %%\n",100*(float)SuccessTotalLAMBDA2/(TotalLAMBDA2 - LAMBDA2));

	fp1 = fopen("AveDelay.txt","w");
	fprintf(fp1,"%f ",AverageDelay);
	fclose(fp1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -