โปรแกรมคำนวณหาค่า แรงดันไฟฟ้า กระแสไฟฟ้า และกำลังงานไฟฟ้าในวงจร

เป็นมินิโปรเจ็คสมัยเรียนเขียนโปรแกรมเบื้องต้นในหนึ่งโปรแกรมสามารถเลือกฟังก์ชันการคำนวณได้สามแบบ หนึ่งคือ โปรแกรมคำนวณค่า แรงดันไฟฟ้า(V) กระแสไฟฟ้า(I) และกำลังงานไฟฟ้า§ ในวงจร R ผสม ให้เราป้อนค่าต่างๆ ที่ต้องการลงไปโปรแกรมก็จะคำนวณออกมาให้ สองคือโปรแกรมแก้สมการสองตัวแปร และสาม โปรแกรมแก้สมการสามตัวแปร แต่ในกระทู้นี้จะขออธิบายโปรแกรมคำนวณค่าต่างๆ ในวงจรก่อนนะครับ
วงจรสำหรับเป็นแม่แบบในการคำนวณนี้คือ วงจร R ผสมดังภาพ

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
            int eh=0;
	char ch;
	float vol=0.00,temp=0.00;
	float volt[4],res[4],current[4],power[4];
	float r234=0.00,v234=0.00,ptotal=0.00;
	while(eh == 0) {
        while(eh == 0) {
            printf("\n+++++++Manu Program++++++++\n");
			printf(" Press 1 to Calculate Circuit.\n");
			printf(" Press 2 to Calculate Equation XY and XYZ.\n");
			printf(" Press 3 to Exit program.\n");
			ch = getch();
			system("cls");
 switch(ch) {

case'1':
    	printf("######Calculate Curcuit Program######\n");
    	printf("-------R1----------------------------\n");
    	printf("|              |          |         |\n");
    	printf("|              |          |         |\n");
    	printf("Vs             R2         R3        R4\n");
    	printf("|              |          |         |\n");
    	printf("|              |          |         |\n");
    	printf("-------------------------------------\n");
    	//Input Value
    	printf("Please Enter V (volt) : ");
    	scanf("%f",&vol); 
    	for(int i = 0;i<4;i++)	 {
    		printf("Please Enter R%d (ohm) : ",i+1);
    		scanf("%f",&res[i]);
    }
    	while(eh == 0) {
    	                  temp = 0.00;
    			for(int i = 1;i<4;i++)	 {
    			  temp+= 1/res[i];
    }
    			r234 = 1/temp;
    			
    			v234 = (r234/(res[0]+r234))*vol;
    			//Voltage V
    	for(int i = 0;i<4;i++)	 {
    	          if(i==0) 	volt[i] = vol-v234;
    		    else volt[i] = v234;
    }  
                 //Current I
    	for(int i = 0;i<4;i++)	 {
    			  current[i] = volt[i]/res[i];
    }
                //Power P
    			  ptotal = 0.00;
    	for(int i = 0;i<4;i++)	 {
    			  power[i] = current[i]*volt[i];
    			  ptotal += power[i];			
    }
          system("cls");
          printf("------R1=%.2f ohm-------------------------------------------\n",res[0]);
          printf("| VR1=%.2fv              | VR2=%.2fv          | VR3=%.2fv       | VR4=%.2fv \n",volt[0],volt[1],volt[2],volt[3]);
          printf("| IR1=%.2fA               | IR2=%.2fA           | IR3=%.2fA        | IR4=%.2fA \n",current[0],current[1],current[2],current[3]);
          printf("| PR1=%.2fW             | PR2=%.2fW        | PR3=%.2fW      | PR4=%.2fW \n",power[0],power[1],power[2],power[3]);
          printf("Vs=%.2f v            R2=%.2f ohm       R3=%.2f ohm        R4=%.2f ohm\n",vol,res[1],res[2],res[3]);
          printf("|                       |                 |                |\n");
          printf("|                       |                 |                |\n");
          printf("|                       |                 |                |\n");
          printf("------------------------------------------------------------\n");
          printf("                                |                            \n");
          printf("                              -----                           \n");
          printf("                               ---                            \n");
          printf("P Total = %.2f\n",ptotal);
    	  printf("\nYou want to be continue ?\n");
    	  printf(" Press a to Calculated by the same R value.\n");
    	  printf(" Press b to Calculate the new value of R and V.\n");
    	  printf(" Press c Back to Manu Program.\n");
          ch = getch();
    	if(ch == 'a')     {
    				system("cls");
    			   printf("Please Enter V (volt) : ");
    				scanf("%f",&vol);
    }	
        else if(ch == 'b'){
                   system("cls");
                  printf("######Calculate Curcuit Program######\n");
    	   printf("-------R1----------------------------\n");
    	printf("|              |          |         |\n");
    	printf("|              |          |         |\n");
    	printf("Vs             R2         R3        R4\n");
    	printf("|              |          |         |\n");
    	printf("|              |          |         |\n");
    	printf("-------------------------------------\n");
              printf("Please Enter V (volt) : ");
              scanf("%f",&vol);
                for(int i = 0;i<4;i++)	 {
    		  printf("Please Enter R%d (ohm) : ",i+1);
    		  scanf("%f",&res[i]);
    }		
        }
        else if(ch == 'c') {
                    system("cls");
                     break;
        }    
    }
                    break;   

ประกาศ

float vol=0.00,temp=0.00;
float volt[4],res[4],current[4],power[4];
float r234=0.00,v234=0.00,ptotal=0.00;  

เป็นตัวแปรแบบอาเรย์ float ใช้เก็บค่าทศนิยมของ volt res current power สี่ตัวซึ่งจะมีตำแหน่งโดย volt[4] เก็บค่า volt res[4] เก็บค่า res current[4] เก็บค่า current power[4] เก็บค่า power โดยมีตำแหน่งในการเก็บค่าดังนี้

          R1 =  res[0]  :  VR1 = volt[0]   :  IR1 = current [0]   :  PR1 = power [0]
          R2 =  res[1]  :  VR2 = volt[1]   :  IR2 = current [1]   :  PR2 = power [1]
          R3 =  res[2]  :  VR3 = volt[2]   :  IR3 = current [2]   :  PR3 = power [2]
          R4 =  res[3]  :  VR4 = volt[3]   :  IR4 = current [3]   :  PR4 = power [3]

ใช้คำสั่ง printf แสดงข้อความทั้งหมดออกมา

               printf("######Calculate Curcuit Program######\n");
        printf("-------R1------------------------------------\n");
        printf("|                    |                    |                 |\n");
        printf("|                    |                    |                 |\n");
         printf("Vs               R2                  R3               R4\n");
         printf("|                   |                    |                 |\n");
         printf("|                   |          	         |                 |\n");
         printf("-----------------------------------------------\n");

Output Program:

ใช้คำสั่ง scanf รับค่า Vs จากคีย์บอร์ด โดยรับค่าเป็นจำนวนเต็ม %d รับค่าใช้ &ไปเก็บไว้ที่ตัวแปรอาเรย์ของแต่ละตัวโดย [i] ที่ใส่ไว้จะเป็นการระบุตำแหน่งของตัวแปรนั้นๆ

            printf("Please Enter V (volt) : ");
            scanf("%f",&vol);

Output Program:

ใช้คำสั่ง for ( ) { }
เพื่อทำการรับค่า R จากคีย์บอร์ด โดยกำหนดเงื่อนไขใน for ว่า i เริ่มต้นเท่ากับ 0 โดยที่ i ต้องน้อยกว่า 4 จึงจะทำงานได้ และเพิ่มค่า i ที่ละหนึ่งค่า แล้วใช้คำสั่ง printf แสดงค่า R%d โดย %d เป็นการแสดงค่าเป็นจำนวนเต็มค่าที่แสดงก็จะขึ้นอยู่กับ i+1 ของแต่ละครั้งที่ทำการวนลูปเริ่มตันจากศูนย์แล้วจึงทำการบวกเพิ่มที่ละหนึ่ง จากนั้นก็ใช้ คำสั้ง scanf รับค่า R[i] จากคีย์บอร์ด โดยรับค่าเป็นจำนวนเต็ม %d รับค่าใช้ &ไปเก็บไว้ที่ตัวแปรอาเรย์ของแต่ละตัวโดย [i] ที่ใส่ไว้จะเป็นการระบุตำแหน่งของตัวแปรนั้นๆ

       for(int i = 0;i<4;i++)	 {
	  printf("Please Enter R%d (ohm) : ",i+1);
	  scanf("%f",&res[i]); }

Output Program:

ใช้ชุดคำสั่ง while(eh==0){ }
เพื่อให้โปรแกรมทำงานซ้ำ โดยกำหนดเงื่อนไขว่า eh ต้องเท่ากับ 0 จึงจะทำงานซ้ำในลูป while ได้ซึ่งการทำงานของชุดคำสั่งต่อไปนี้จะเป็นการทำงานในลูป while ทั้งสิ้น
ประกาศ temp = 0.00; เพื่อเอาไว้ทดค่า

ใช้คำสั่ง for ( ) { }
เพื่อทำการคำนวณค่า temp โดยกำหนดเงื่อนไขใน for ว่า i เริ่มต้นเท่ากับ 1 โดยที่ i ต้องน้อยกว่า 4 จึงจะทำงานได้ และเพิ่มค่า i ที่ละหนึ่งค่า ซึ่งค่า temp มีค่าเท่ากับ (1/R2)+(1/R3)+(1/R4) แล้วทำการเก็บผลลัพธ์ที่ได้ไว้ที่ temp เพื่อรอที่จะดึงข้อมูลมาใช้ และเนื่องจากค่า R มีที่ต้องการเป็น R2 , R3 , R4 จึงต้องกำหนดค่าเริ่มต้นในลูป for ให้ i เริ่มต้นที่ 1 นั้นเอง และ ”+= “ หมายความว่าให้มันทำการบวกเพิ่มค่าไปเรื่อยๆ

                       for(int i = 1;i<4;i++)	 {
                              temp+= 1/res[i];      }  

จากนั้นก็คำนวณหาค่า R234 มีค่าเท่ากับ (1/temp) แล้วทำการเก็บผลลัพธ์ที่ได้ไว้ที่ r234 คำนวณหาค่า VR234 โดยดึงค่า r234 มาใช้

                r234 = 1/temp;
		v234 = (r234/(res[0]+r234))*vol;

ใช้คำสั่ง for ( ) { }
เพื่อทำการคำนวณค่า volt โดยกำหนดเงื่อนไขใน for ว่า i เริ่มต้นเท่ากับ 0 โดยที่ i ต้องน้อยกว่า 4 จึงจะทำงานได้ และเพิ่มค่า i ที่ละหนึ่งค่าภายใน for ใช้ชุดคำสั่ง if () โดยกำหนดเงื่อนไขว่า i= =0 ให้ใช้ volt[i] = vol-v234; ถ้าไม่ใช่ก้ให้ใช้ volt[i] = v234

                  for(int i = 0;i<4;i++)	 {
                           if(i==0) 	volt[i] = vol-v234;
	            else volt[i] = v234; }  

และคำนวณหาค่ากระแสกับกำลังงานก็ใช้ชุดคำสั่ง for ( ) { } กำหนดค่าเงื่อนไขให้ด้วยเช่นกัน และ โดยการหาค่ากำลังงานรวมประกาศค่า ptotal = 0.00 เพิ่ม และใช้ “+=” เพื่อให้มันบวกค่าเพิ่มไปเรื่อยๆนั้นเอง จะได้

//Current I 
       for(int i = 0;i<4;i++)	 {
			  current[i] = volt[i]/res[i];
}	
		//Power P
       ptotal = 0.00;
	for(int i = 0;i<4;i++)	 {
			  power[i] = current[i]*volt[i];
			  ptotal += power[i];
}

ใช้คำสั่ง system(“cls”); ทำการเคลียร์หน้าเจอและ printf แสดงผลลัพธ์เป็นจำนวนเต็ม %d และ ทศนิยมสองตำแหน่ง %.2f โดยค่าที่แสดงก็ตามที่ได้ระบุไว้ให้แต่ละตำแหน่งตามลำดับ

system("cls");
       printf("------R1=%.2f ohm-------------------------------------------\n",res[0]);
       printf("| VR1=%.2fv             | VR2=%.2fv          | VR3=%.2fv         | VR4=%.2fv \n",volt[0],volt[1],volt[2],volt[3]);
       printf("| IR1=%.2fA             | IR2=%.2fA           | IR3=%.2fA           | IR4=%.2fA \n",current[0],current[1],current[2],current[3]);
      printf("| PR1=%.2fW             | PR2=%.2fW       | PR3=%.2fW       | PR4=%.2fW \n",power[0],power[1],power[2],power[3]);
      printf("Vs=%.2f v                   R2=%.2f ohm       R3=%.2f ohm        R4=%.2f ohm \n",vol,res[1],res[2],res[3]);
      printf("|                       |                 |                |\n");
      printf("|                       |                 |                |\n");
      printf("|                       |                 |                |\n");
      printf("------------------------------------------------------------\n");
      printf("                                |                            \n");
      printf("                              -----                           \n");
      printf("                               ---                            \n");
      printf("P Total = %.2f\n",ptotal);

Output Program

ทดลองซิมด้วย everycircuit

2 Likes