Commit 349e6efe by shtaya

HW3

parent 0a8516b3
File added
#ifndef BOOK_HEADER_H
#define BOOK_HEADER_H
#include <stdio.h>
#include <stdlib.h>
#include "errorcheck.h"
int loadData (const char fname[], int *bookid, int *personid)
{
FILE *fp;
char buf[30];
fileErrorCheck(fp = fopen(fname, "r"));
fscanf(fp, "%s", buf);
fclose(fp);
return 0;
}
#endif
This source diff could not be displayed because it is too large. You can view the blob instead.
#ifndef ERRORCECK_H
#define ERRORCECK_H
#include <stdio.h>
#include <stdlib.h>
void memErrorCheck(void *pt);
void fileErrorCheck(void *pt);
void funcErrorCheck(int i);
void funcErrorCheck(int i)
{
if (i == -1)
{
printf("Function returned an error.\n");
exit (-1);
}
return;
}
void memErrorCheck(void *pt)
{
if (pt == NULL)
{
printf("Memory allocation failed.\n");
exit (-2);
}
return;
}
void fileErrorCheck(void *pt)
{
if (pt == NULL)
{
printf("File cannot be opened.\n");
exit (-3);
}
return;
}
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "book_header.h"
#include "errorcheck.h"
#define INPUTFILE "data.txt"
#define CHUNK 10000
int main(void)
{
int *bookid, *personid;
funcErrorCheck(loadData(INPUTFILE, bookid, personid));
char fname[10] = "";
char jfname[20]= "海";
printf("%c\n",jfname[0]);
return 0;
}
No preview for this file type
......@@ -6,7 +6,9 @@
#include <libpq-fe.h>
void checkConnectionError(PGconn *conn);
void checkInqueryError(PGresult *res);
void checkUnvoidInqueryError(PGresult *res);
void checkVoidInqueryError(PGresult *res);
void dispTable(PGresult *res);
void checkConnectionError(PGconn *conn)
{
......@@ -20,7 +22,7 @@ void checkConnectionError(PGconn *conn)
return;
}
void checkInqueryError(PGresult *res)
void checkUnvoidInqueryError(PGresult *res)
{
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
......@@ -31,7 +33,18 @@ void checkInqueryError(PGresult *res)
return;
}
void dispResult(PGresult *res)
void checkVoidInqueryError(PGresult *res)
{
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("SQL command failed.\n");
PQclear(res);
exit(-1);
}
return;
}
void dispTable(PGresult *res)
{
int numTuple = PQntuples(res);
int numField = PQnfields(res);
......
#ifndef DATABASE_H
#define DATABASE_H
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
void checkConnectionError(PGconn *conn);
void checkUnvoidInqueryError(PGresult *res);
void checkVoidInqueryError(PGresult *res);
void dispTable(PGresult *res);
void checkConnectionError(PGconn *conn)
{
if (PQstatus(conn) == CONNECTION_BAD)
{
printf("We're unable to connect to the database\n");
PQfinish(conn);
exit(-1);
}
return;
}
void checkUnvoidInqueryError(PGresult *res)
{
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
printf("We did not get any data!\n");
PQclear(res);
exit(-1);
}
return;
}
void checkVoidInqueryError(PGresult *res)
{
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("SQL command failed.\n");
PQclear(res);
exit(-1);
}
return;
}
void dispTable(PGresult *res)
{
int numTuple = PQntuples(res);
int numField = PQnfields(res);
int i,j;
printf("%5s\t", "No.");
for (i = 0; i < numField; i++)
{
printf("%15s\t", PQfname(res,i));
}
printf("\n");
for (j = 0; j < numTuple; j++)
{
printf("%5d\t", j+1);
for (i = 0; i < numField; i++)
{
printf("%15s\t", PQgetvalue(res,j,i));
}
printf("\n");
}
return;
}
#endif
//gcc main.c -L/Library/PostgreSQL/9.6/lib -I/Library/PostgreSQL/9.6/include -lpq
#include <stdio.h>
#include "functions.h"
#include "database.h"
#include "ui.h"
#define LOGIN_INFO "dbname=mveb165842 host=ewis.pld.ttu.ee port=5432 user=mveb165842 password=7ez6d0mu"
#define WELCOME_MES "Welcome. This program accesses database mveb165842 at ewis.pld.ttu.ee.\n"
#define MAIN_MENU "Main menu\n1: Edit student record\n2: Edit subject record\n3: Edit grade record\n4: Display students table \n5: Exit\n"
#define EDIT_MENU "1: Insert\n2: Modify\n3: Delete\n"
#define YES_NO "1: Yes\n2: No\n"
int editStudentRecord(PGconn *conn);
int editSubjectRecord(PGconn *conn);
int editGradeRecord(PGconn *conn);
int dispStudentTable(PGconn *conn);
int normalExit(PGconn *conn);
int main()
{
PGconn *conn;
int (*func[5])() = {editStudentRecord, editSubjectRecord, editGradeRecord, dispStudentTable, normalExit};
int exitFlag = 0;
checkConnectionError(conn = PQconnectdb(LOGIN_INFO));
printf(WELCOME_MES);
while ((*func[getInt(1,5,MAIN_MENU)-1])(conn) == 0);
PQfinish(conn);
return 0;
}
int editStudentRecord(PGconn *conn)
{
void (*func[3])() = {insertStudentRecord, modifyStudentRecord, deleteStudentRecord};
do
{
(*func[getInt(1,3,"Editing student record\n" EDIT_MENU)-1])(conn);
} while(getInt(1, 2, "Edit more student record?\n" YES_NO) == 1);
return 0;
}
int editSubjectRecord(PGconn *conn)
{
void (*func[3])() = {insertSubjectRecord, modifySubjectRecord, deleteSubjectRecord};
do
{
(*func[getInt(1,3,"Editing subject record\n" EDIT_MENU)-1])(conn);
} while(getInt(1, 2, "Edit more subject record?\n" YES_NO) == 1);
return 0;
}
int editGradeRecord(PGconn *conn)
{
do
{
GradeRecord(conn);
} while(getInt(1, 2, "Add more grade record?\n" YES_NO) == 1);
return 0;
}
int dispStudentTable(PGconn *conn)
{
do
{
dispStudentTableByGrade(conn);
} while(getInt(1, 2, "Display another table?\n" YES_NO) == 1);
return 0;
}
int normalExit(PGconn *conn)
{
return -1;
}
#ifndef UI_H
#define UI_H
#include <stdio.h>
#include <stdlib.h>
#include "database.h"
#define INV_INPUT "Invailed input was inserted.\n"
double getDouble(double min, double max, const char mess[]);
int getInt(int min, int max, const char mess[]);
double getDouble(double min, double max, const char mess[])
{
double num;
printf("%s", mess);
while(1)
{
printf(">>");
scanf("%lf", &num);
if (min <= num && num <= max)
{
break;
}
else
{
printf(INV_INPUT);
}
}
return num;
}
int getInt(int min, int max, const char mess[])
{
int num;
printf("%s", mess);
while(1)
{
printf(">>");
scanf("%d", &num);
if (min <= num && num <= max)
{
break;
}
else
{
printf(INV_INPUT);
}
}
return num;
}
#endif
#ifndef FUNCTION_H
#define FUNCTION_H
#include <stdio.h>
#include "database.h"
#include "ui.h"
#define LOW_GRADE_LIMIT "Please input lower limit of grade.\n"
#define UP_GRADE_LIMIT "Please input upper limit of grade.\n"
#define STUDENT_EDIT_FUNC_LIST "What do you want to do?\n1: insert\n2: modify\n3: select\n"
typedef struct date {
int day;
int month;
int year;
} date_t;
typedef struct student {
char first_name[32];
char last_name[32];
date_t date;
} student_t;
void editStudent(PGconn *conn);
void insertStudentData(PGconn *conn);
void modifyStudentData(PGconn *conn);
void deleteStudentData(PGconn *conn);
void editSubject(PGconn *conn);
void addGrade(PGconn *conn);
void dispStudentByGrade(PGconn *conn);
void editStudent(PGconn *conn)
{
void (*func[5])() = {insertStudentData, modifyStudentData, deleteStudentData};
while (1)
{
(*func[getInt(1,5,STUDENT_EDIT_FUNC_LIST)-1])(conn);
if (getInt(1,2,"Edit more data?\n1: Yes\n2: No\n") == 2)
{
break;
}
}
return;
}
void insertStudentData(PGconn *conn)
{
char query[128];
student_t addedStudentRecord;
printf("Insert student's first name.\n>>");
scanf("%s", addedStudentRecord.first_name);
printf("Insert student's last name.\n>>");
scanf("%s", addedStudentRecord.last_name);
printf("Insert student's birthdate. (i.e. YYYYMMDD)\n>>");
scanf("%4d%2d%2d", &addedStudentRecord.date.year, &addedStudentRecord.date.month, &addedStudentRecord.date.day);
//error check is needed!!
sprintf(query, "insert into student (first_name,last_name,date) values (%s,%s,%d%d%d)", addedStudentRecord.first_name, addedStudentRecord.last_name, addedStudentRecord.date.year, addedStudentRecord.date.month, addedStudentRecord.date.day);
printf("%s\n" , query);
return;
}
void modifyStudentData(PGconn *conn)
{
return;
}
void deleteStudentData(PGconn *conn)
{
return;
}
void editSubject(PGconn *conn)
{
return;
}
void addGrade(PGconn *conn)
{
return;
}
void dispStudentByGrade(PGconn *conn)
{
PGresult *res;
double lowLimit;
double upLimit;
char query[128];
while (1)
{
lowLimit = getDouble(0, 5, LOW_GRADE_LIMIT);
upLimit = getDouble(lowLimit, 5, UP_GRADE_LIMIT);
sprintf(query, "select * from student where average_grade > %lf and average_grade < %lf order by average_grade desc", lowLimit, upLimit);
res = PQexec(conn, query);
checkInqueryError(res);
dispResult(res);
if (getInt(1,2,"Add more data?\n1: Yes\n2: No\n") == 2)
{
break;
}
}
PQclear(res);
return;
}
#endif
......@@ -289,7 +289,7 @@ void GradeRecord(PGconn *conn)
char mess[128];
int newGrade;
PGresult *res;
char query[128];
char query[256];
subjectId = subjectChoice(conn);
studentCode = getIdtoEdit(conn, "first_name", "student");
......@@ -316,10 +316,10 @@ void GradeRecord(PGconn *conn)
printf("Grade data was updated.\n");
}
sprintf(query, "select cast(sum(grade*credits) as float)/sum(credits) from grade join subject on student_code=student_code where student_code = '%s'", studentCode);
sprintf(query, "select cast(cast(sum(grade*credits) as float)/sum(credits) as float) from grade join subject on student_code=student_code where student_code = '%s'", studentCode);
res = PQexec(conn, query);
checkUnvoidInqueryError(res);
;
sprintf(query, "update student set average_grade = '%s' where student_code = '%s'", PQgetvalue(res,0,0), studentCode);
res = PQexec(conn, query);
......
//gcc main.c -L/Library/PostgreSQL/9.6/lib -I/Library/PostgreSQL/9.6/include -lpq
#include <stdio.h>
#include "functions.h"
#include "database.h"
#include "ui.h"
#include "functions.c"
#define LOGIN_INFO "dbname=mveb165842 host=ewis.pld.ttu.ee port=5432 user=mveb165842 password=7ez6d0mu"
#define WELCOME_MES "Welcome. This program accesses database mveb165842 at ewis.pld.ttu.ee.\n"
#define SERVICE_LIST "Choose a service from the list given below.\n1: Edit student data\n2: Edit subject data\n3: Add grade\n4: Display students' grades\n5: Exit\n"
#define MAIN_MENU "Main menu\n1: Edit student record\n2: Edit subject record\n3: Edit grade record\n4: Display students table \n5: Exit\n"
#define EDIT_MENU "1: Insert\n2: Modify\n3: Delete\n"
#define YES_NO "1: Yes\n2: No\n"
#define LOGIN_INFO "dbname=mveb165842 host=ewis.pld.ttu.ee port=5432 user=mveb165842 password=7ez6d0mu"
int editStudentRecord(PGconn *conn);
int editSubjectRecord(PGconn *conn);
int editGradeRecord(PGconn *conn);
int dispStudentTable(PGconn *conn);
int normalExit(PGconn *conn);
void doExit(PGconn *conn);
int main(void)
int main()
{
PGconn *conn;
PGresult *res;
void (*func[5])() = {editStudent, editSubject, addGrade, dispStudentByGrade, doExit};
int (*func[5])() = {editStudentRecord, editSubjectRecord, editGradeRecord, dispStudentTable, normalExit};
printf(WELCOME_MES);
int exitFlag = 0;
checkConnectionError(conn = PQconnectdb(LOGIN_INFO));
while (1)
printf(WELCOME_MES);
while ((*func[getInt(1,5,MAIN_MENU)-1])(conn) == 0);
PQfinish(conn);
return 0;
}
int editStudentRecord(PGconn *conn)
{
void (*func[3])() = {insertStudentRecord, modifyStudentRecord, deleteStudentRecord};
do
{
(*func[getInt(1,5,SERVICE_LIST)-1])(conn) < 0)
}
(*func[getInt(1,3,"Editing student record\n" EDIT_MENU)-1])(conn);
} while(getInt(1, 2, "Edit more student record?\n" YES_NO) == 1);
return 0;
}
int editSubjectRecord(PGconn *conn)
{
void (*func[3])() = {insertSubjectRecord, modifySubjectRecord, deleteSubjectRecord};
do
{
(*func[getInt(1,3,"Editing subject record\n" EDIT_MENU)-1])(conn);
} while(getInt(1, 2, "Edit more subject record?\n" YES_NO) == 1);
void doExit(PGconn *conn)
return 0;
}
int editGradeRecord(PGconn *conn)
{
PQfinish(conn);
exit(1);
return;
do
{
GradeRecord(conn);
} while(getInt(1, 2, "Add more grade record?\n" YES_NO) == 1);
return 0;
}
int dispStudentTable(PGconn *conn)
{
do
{
dispStudentTableByGrade(conn);
} while(getInt(1, 2, "Display another table?\n" YES_NO) == 1);
return 0;
}
int normalExit(PGconn *conn)
{
return -1;
}
......@@ -3,13 +3,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "database.h"
#define INV_INPUT "Invailed input was inserted.\n"
double getDouble(double min, double max, const char mess[]);
int getInt(int min, int max, const char mess[]);
double getDouble(double min, double max, const char mess[])
{
double num;
......
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
int main()
{
DIR *dp = NULL;
dp = opendir();
if (dp == NULL)
{
printf("Directry failed.");
exit(1);
}
closedir(dp);
return 0;
}
File added
#include <stdio.h>
#include <stdlib.h>
char *memAllocation(int n, int m);
int main(void)
{
char *str = NULL;
str = memAllocation(5, 6);
free(str);
return 0;
}
char *memAllocation(int n, int m)
{
char *str = NULL;
int i;
int size = sizeof(char)*n*m;
str = (char *)malloc(size);
if (str == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
return str;
}
File added
#include <stdio.h>
#include <stdlib.h>
char *memAllocation(int n, int m);
int main(void)
{
char *str = NULL;
str = memAllocation(5, 6);
free(str);
return 0;
}
char *memAllocation(int n, int m)
{
char *str = NULL;
int i;
int size = sizeof(char)*n*m;
str = (char *)malloc(size);
if (str == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
return str;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int genNumberElement(int);
int genElement(void);
int *memAllocation(int);
void genElementArray(int *, int);
void dispArray(int *, int);
int cmpf(const void *a, const void *b);
void output(int *, int);
int main(void)
{
srand(time(NULL));
int nEle = genNumberElement(30);
int *array = NULL;
array = memAllocation(nEle);
genElementArray(array,nEle);
qsort(array, nEle, sizeof(int),cmpf);
output(array, nEle);
free(array);
return 0;
}
int genNumberElement(int max)
{
int result;
result = rand() % max + 1;
return result;
}
int genElement(void)
{
int result;
result = rand();
return result;
}
int *memAllocation(int num)
{
int *array = NULL;
array = calloc(num, sizeof(int));
if (array == NULL)
{
printf("Memory allocation was failed.\n");
exit(0);
}
return array;
}
void genElementArray(int *array, int num)
{
int i;
for (i = 0; i < num; i++)
{
array[i] = genElement();
}
return;
}
void dispArray(int *array, int num)
{
int i;
for (i = 0; i < num; i++)
{
printf("%d\n",array[i]);
}
printf("\n");
return;
}
int cmpf(const void *a, const void *b)
{
return *(int *)b - *(int *)a;
}
void output(int *array, int num)
{
FILE *fp = NULL;
int i;
fp = fopen("top_three_results.txt","w");
if(fp == NULL)
{
printf("Memory allocation was failed.\n");
exit(0);
}
if (num < 3)
{
for (i = 0; i < num; i++)
{
fprintf(fp,"%d\n",array[i]);
printf("%d\n",array[i]);
}
}
else
{
for (i = 0; i < 3; i++)
{
fprintf(fp,"%d\n",array[i]);
printf("%d\n",array[i]);
}
}
fclose(fp);
return;
}
2088808795
1989091733
1982996803
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment