initial commit

This commit is contained in:
root
2024-06-03 16:43:53 +03:00
commit 21e3fd0797
1535 changed files with 60499 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
#include "auth.h"
ld
toRadians(ld degree){
ld one_deg = (M_PI)/180;
return (one_deg * degree);
}
ld
sq_root(ld value){
ld err = 1e-10;
ld ans = 0;
ld low = 0 , high = 0;
if(value < 1){
low = value , high = 1;
}
else{
low = 1 , high = value;
}
for(int i = 1 ; i <= 67 ; i++ ){
ld mid = low + (high - low)/2.0;
if( mid*mid <= value ){
ans = mid;
low = mid + err;
}
else{
high = mid - err;
}
}
return ans;
}
ld
orthodromic_distance(ld stu_lat,ld stu_long,ld teach_lat,ld teach_long){
stu_lat = toRadians(stu_lat);
stu_long = toRadians(stu_long);
teach_lat = toRadians(teach_lat);
teach_long = toRadians(teach_long);
ld diff_lat = teach_lat - stu_lat;
ld diff_long = teach_long - stu_long;
ld ans = pwr2(sin(diff_lat/2)) +
cos(stu_lat) * cos(teach_lat) *
pwr2(sin(diff_long/2));
ans = 2 * asin(sq_root(ans));
// Radius of Earth in
// Kilometers, R = 6371
// Use R = 3956 for miles
ld R = 6371;
//ans in meter...
ans = ans * R * (ld)1000;
return ans;
}
ld
altitude_difference(ld teach_alti, ld stu_alti)
{
ld diff_alti = teach_alti - stu_alti;
return diff_alti;
}
bool
check_range(ld dist, ld alti){
//better implementation for future....
// the 100 needs to be changed to (db.Room.length)
return dist <= (ld)100 && (alti >= -3 && alti <= 4);
}

View File

@@ -0,0 +1,30 @@
#ifndef __AUTH__
#define __AUTH__
#ifndef _MATH_H_
#include <math.h>
#endif
#ifndef _STDBOOL_H
#include <stdbool.h>
#endif
#define ld long double
#define pwr2(x) (x * x)
ld
sq_root(ld x);
ld
toRadians( ld degree );
ld
orthodromic_distance(ld stu_lat, ld stu_long, ld teach_lat, ld teach_long);
ld
altitude_difference(ld teach_alti, ld stu_alti);
bool
check_range(ld dist, ld alti);
#endif // __AUTH__

View File

@@ -0,0 +1,93 @@
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<mysql.h>
#include "auth.h"
void
error_found(MYSQL *con){
printf("%s\n",mysql_error(con));
mysql_close(con);
}
int
main(int argv , char **argc){
//Pass the Teacher latitude and longitude from command line argument.
//Teacher latitude and longitude...
ld teach_lat = strtold(argc[1],NULL) , teach_long = strtold(argc[2],NULL);
//Altitude as the 3rd argument.
ld teach_alti = strtold(argc[2],NULL);
MYSQL *con = mysql_init(NULL);
if( con == NULL ){
printf("mysql_init() failed\n");
return 0;
}
if( mysql_real_connect(con,"localhost","usr","admin@1224"
,"bpitattendance", 0,NULL,0) == NULL ){
error_found(con);
}
if( mysql_query(con, "SELECT * FROM attendance") ){
error_found(con);
}
MYSQL_RES *result = mysql_store_result(con);
if( result == NULL ){
error_found(con);
}
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
int enroll = 0;
long double stu_lat = 0 , stu_long = 0;
long double dist = 0;
char en_num[15];
char query[150]="";
while( (row = mysql_fetch_row(result) ) ){
enroll = (int)strtoll(row[0],NULL,10);
stu_lat = strtold(row[1],NULL);
stu_long = strtold(row[2],NULL);
stu_alti = strtold(row[3], NULL);
strcpy(query,"");
sprintf(en_num,"%d",enroll);
dist = orthodromic_distance(stu_lat,stu_long,teach_lat,teach_long);
alti = altitude_difference(teach_alti,stu_alti);
if( check_range(dist,alti) ){
strcat(query,"UPDATE attendance SET is_present = true WHERE enroll_num =");
strcat(query,en_num);
strcat(query,";");
if( mysql_query(con, query ) ){
error_found(con);
}
printf("%ld is present\n",enroll);
}
else{
strcat(query,"UPDATE attendance SET is_present = false WHERE enroll_num =");
strcat(query,en_num);
strcat(query,";");
if( mysql_query(con,query ) ){
error_found(con);
}
printf("%ld is absent\n",enroll);
}
}
mysql_free_result(result);
mysql_close(con);
return 0;
}