hi,欢迎访问本站!
当前位置: 首页学习笔记正文

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

用户投稿 学习笔记 9阅读
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);elsearr[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");} 运行截图

标签:
声明:无特别说明,转载请标明本文来源!
发布评论
正文 取消