C语言取5个随机点放电荷e,求网格每点的电场力及方向、场强及方向

C语言取5个随机点放电荷e,求网格每点的电场力及方向、场强及方向

代码

//Time : 2021/11
//Author : 俊育君
//File : 在x(-10~10),Y(-10~10)的直接坐标系中,
//取5个随机点放电荷e,求网格每点的电场力及方向、场强及方向
//Software : Visual C++ 6.0
//location :铜仁学院

# include <stdio.h>  //使用基本输入输出函数
# include <stdlib.h> //使用随机函数
# include <time.h>  //初始化随机种子使用
# include <math.h>  //数学库
# define K 9.0e9    //静电力常量
# define NE 1.6e-19 //元电荷所带电量数

struct Point    //定义坐标点(Point)结构体
{
	int x;      //x轴坐标
	int y;      //y轴坐标
	void initX(int x0);  //初始化x轴坐标
	void initY(int y0);  //初始化y轴坐标
};
void Point::initX(int x0)  
{
	x = x0;
}
void Point::initY(int y0)
{
	y = y0;
}


struct Ele   //定义电荷(Ele)结构体
{
	Point loc;  //电荷坐标
	double ne; //电荷量
	void init(int x1, int y1); //初始化电荷坐标,及电荷量
	double getPower(int x1, int y1, double e1);  //求两电荷间电场力
	double getPowerX(int x1, int y1, double power);  //将两电荷间电场力分解到x轴
	double getPowerY(int x1, int y1, double power);  //将两电荷间电场力分解到y轴
	double getE(int x1, int y1);   //求(x1, y1)处电荷场强
	double getEX(int x1, int y1, double E);  //将(x1, y1)处场强E分解到x轴
	double getEY(int x1, int y1, double E);  //将(x1, y1)处场强E费解到y轴
	bool isEmpty();  //判断电荷量是否为空,即是否初始化成功
};
//初始化电荷坐标,及电荷量
void Ele::init(int x1, int y1)  
{
	loc.initX(x1);
	loc.initY(y1);
	ne = NE;
}
//求两电荷间电场力
double Ele::getPower(int x1, int y1, double e1)   
{
	double r = sqrt(pow(x1 - loc.x, 2) + pow(y1 - loc.y, 2)); //r为两电荷间的距离
	return K * ne * e1 / pow(r, 2);
}
//将两电荷间电场力分解到x轴
double Ele::getPowerX(int x1, int y1, double power)  
{
	if (y1 == loc.y)
	{
		if (x1 - loc.x > 0)
			return power;
		return -power;
	}
	double r = sqrt(pow(x1 - loc.x, 2) + pow(y1 - loc.y, 2)); 
	return (x1 - loc.x / r) * power;
}
//将两电荷间电场力分解到y轴
double Ele::getPowerY(int x1, int y1, double power)   
{
	if (x1 == loc.x)
	{
		if (y1 - loc.y > 0)
			return power;
		return -power;
	}
	double r = sqrt(pow(x1 - loc.x, 2) + pow(y1 - loc.y, 2));
	return (y1 - loc.y / r) * power;
}
//求(x1, y1)处电荷场强
double Ele::getE(int x1, int y1)
{
	double r = sqrt(pow(x1 - loc.x, 2) + pow(y1 - loc.y, 2)); //r为两电荷间的距离
	return K * ne / pow(r, 2);
}
//将(x1, y1)处场强E分解到x轴
double Ele::getEX(int x1, int y1, double E)
{
	if (y1 == loc.y)
	{
		if (x1 - loc.x > 0)
			return E;
		return -E;
	}
	double r = sqrt(pow(x1 - loc.x, 2) + pow(y1 - loc.y, 2)); 
	return (x1 - loc.x / r) * E; 
}
//将(x1, y1)处场强E费解到y轴
double Ele::getEY(int x1, int y1, double E)   //电场力在y轴的分量
{
	if (x1 == loc.x)
	{
		if (y1 - loc.y > 0)
			return E;
		return -E;
	}
	double r = sqrt(pow(x1 - loc.x, 2) + pow(y1 - loc.y, 2));
	return (y1 - loc.y / r) * E;
}
//判断电荷量是否为空,即是否初始化成功
bool Ele::isEmpty()
{
	if (ne > 0)
		return false;
	return true;
}


void main()
{
	srand(unsigned(time(0)));  //随机数种子

	Point arr[21][21]; //坐标系(当每个点都求电场力(大小、方向)、场强(大小、方向)时使用
	Ele randEle[5]; // 5个随机放在坐标系中电荷

	int i, j;
	
	/** 
	//如果规定坐标范围(如x范围:-10 ~ 10 y范围: -10 - 10)每个点的电场力(大小,方向)、场强(大小、方向)都要求,则需初始化坐标系
	//若只求某个点则无需坐标系
	for (i = 0; i < 21; i++)  //初始化直角坐标系
	{
		for (j = 0; j < 21; j++)
		{
			//初始化 x坐标
			if (j < 10)
				arr[i][j].initX(-(10 - j));
			else if (j == 10)
				arr[i][j].initX(0);
			else if (j == 20)
				arr[i][j].initX(10);
			else 
				arr[i][j].initX(j % 10);
			
			//初始化 y坐标
			if (i < 10)
				arr[i][j].initY(10 - i);
			else if (i == 10)
					arr[i][j].initY(0);
			else if (i == 20)
					arr[i][j].initY(-10);
			else
				arr[i][j].initY(-i % 10);	
		}
	} 
	//打印坐标系

	for (i = 0; i < 21; i++)
	{
		for (j = 0; j < 21; j++)
			printf("(%3d,%3d)  ", arr[i][j].x, arr[i][j].y);
		printf("\n");
	}
	printf("\n\n");
	**/



	//初始化5个随机电荷的位置坐标
	int tempx;  //随机生成x坐标中间变量
	int tempy;  //随机生成y坐标中间变量
	for (i = 0; i < 5; i++)
	{
		tempx = -10 + rand() % 21;  //生成随机x坐标(范围:-10 ~ 10)
		tempy = -10 + rand() % 21;  //生成随机y坐标(范围:-10 ~ 10)
		if (!randEle[i].isEmpty())
		{
			for (j = 0; j < 5; j++)
			{
				if (randEle[i].loc.x == tempx && randEle[i].loc.y == tempy)  //防止随机生成电荷在同一位置
				{
					i--;
					continue;
				}
					randEle[i].init(tempx, tempy);
			}
		}
		else
		{
			randEle[i].init(tempx, tempy);
		}
	}
	printf("\n5个随机电荷的坐标:");
	for (i = 0; i < 5; i++)  //打印5个随机电荷坐标
		printf("(%3d,%3d) ", randEle[i].loc.x, randEle[i].loc.y);
	printf("\n");


	//计算某点电场力大小、方向及场强大小、方向
	double power;  //某点电荷所受电场力
	double E;      //某点处场强
	double directionF; //电场力方向
	double directionE; //场强方向
	double powerX = 0; //某点电荷所受电场力X轴分量总和
	double powerY = 0; //某点电荷所受点场力y轴分量总和
	double EX = 0;     //某点场强x轴分量总和
	double EY = 0;     //某点电场y轴分量总和
	int inputX, inputY; //输入所求电荷网格坐标
	printf("\n请输入网格点(试探电荷)x轴坐标,y轴坐标【注:x轴坐标(-10 ~ 10)y轴坐标(-10 ~ 10)】\n");
	printf("X轴坐标:");
	scanf("%d", &inputX);   //获取输入x轴坐标,y轴坐标
	printf("y轴坐标:");
	scanf("%d", &inputY);   
	if (inputX < -10 || inputX > 10 || inputY < -10 || inputY > 10)  //判断输入坐标是否超界
	{
		printf("坐标(%3d,%3d)超界,请重启程序重新输入!!!\n\n", inputX, inputY);
		exit(0);  //输入坐标超界退出程序
	}

	for (i = 0; i < 5; i++)    //计算试探电荷所受5个电荷电场力在x轴方向分解总和及y轴方向分解总和
	{
		if (randEle[i].loc.x == inputX && randEle[i].loc.y == inputY)    //当输入坐标为随机产生的电荷坐标时,不计此电荷产生电场力
		{
			powerX += 0;
			powerY += 0;
		} 
		else
		{
			powerX += randEle[i].getPowerX(inputX, inputY, randEle[i].getPower(inputX, inputY, NE));
			powerY += randEle[i].getPowerY(inputX, inputY, randEle[i].getPower(inputX, inputY, NE));
		}
	}
	power = sqrt(pow(powerX, 2) + pow(powerY, 2));   //求试探电荷所受电场力
	directionF = powerY / power;       //求试探电荷所受电场力方向

	for (i = 0; i < 5; i++)     //计算试探电荷处坐标处5个电荷场强在x轴方向分解总和及y轴方向分解总和
	{
		if (randEle[i].loc.x == inputX && randEle[i].loc.y == inputY)    //当输入坐标为随机产生的电荷坐标时,不计此电荷产生场强
		{
			EX += 0;
			EY += 0;
		}
		else
		{
			EX += randEle[i].getEX(inputX, inputY, randEle[i].getE(inputX, inputY));
			EY += randEle[i].getEY(inputX, inputY, randEle[i].getE(inputX, inputY));
		}
	}
	E = sqrt(pow(EX, 2) + pow(EY, 2));   //求试探电荷处场强
	directionE = EY / E;              //求试探电荷处场强方向

	printf("\n****************************计算结果*******************************************\n");
	printf("*******************************************************************************\n");
	printf("(%d,%d)点电场力为:%.34lf    方向为:sinQ=%lf\n", inputX, inputY, power, directionF);
	printf("*******************************************************************************\n");
	printf("(%d,%d)点场强为:%.34lf    方向为:sinQ=%lf\n", inputX, inputY, E, directionE);
	printf("*******************************************************************************\n\n");
}

运行截图

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值