← cproj

Data structures

69e32b7be2b3a4350caad382a5a2f97d81d37c83 · ovidiucs · Nov 30, 2019 21:15

+112 −40 · 4 files changed

  • graph/db/Makefile +1 −1
  • graph/db/sqltest.c +30 −39
  • graph/ds/ds1.c +35 −0
  • graph/ds/ds2.c +46 −0
diff --git a/graph/db/Makefile b/graph/db/Makefile
index 796bfa0..9057dd4 100644
--- a/graph/db/Makefile
+++ b/graph/db/Makefile
@@ -2,7 +2,7 @@
 
 # Definitions.
 BinName = sqltest
-Libs = -lc -lsqlite3
+Libs = -lc -lsqlite3 -lreadline
 
 # Options and arguments to the C compiler.
 CC = clang
diff --git a/graph/db/sqltest.c b/graph/db/sqltest.c
index 5041bad..fed4a78 100644
--- a/graph/db/sqltest.c
+++ b/graph/db/sqltest.c
@@ -1,5 +1,8 @@
 #include <stdio.h> // fprintf, printf
 #include <stdlib.h> // exit,
+#include <readline/readline.h> // readline
+#include <readline/history.h> //
+
 #include "../../include/sqlite3.h"
 //#include "./sqltest.h"
   // Set a glbal string that will hold some SQL sample
@@ -19,11 +22,12 @@ char *sSql = "DROP TABLE IF EXISTS Friends;"
 
 char *uSql = "UPDATE Friends SET Name = 'Jerry' WHRE id = 1;";
 // ---------------------------------FUNCTION DECLARATIONS--------------------------------------------
+
 int mainmenu(sqlite3 *db);
 int dbInsert(sqlite3 *db, char *data);
 int dbSelect(sqlite3 *db, char *data);
 int dbUpdate(sqlite3 *db, char *data);
-sqlite3 *setup(sqlite3 *db);
+int setup(sqlite3 **db);
 static int callback(void *data, int argc, char **argv, char **azColName );
 
 // ---------------------------------START MAIN------------------------------------------------------
@@ -35,8 +39,7 @@ int main(int argc, char **argv) {
   // to an instance of the opaque structure named sqlite3
   sqlite3 *db = NULL;
 
-			fprintf(stderr,"1. db Sqlite3 address is: %p vs is \n", &db);
-
+  int rc;
 
   // This string will hold the english language of the error description
   char *zErrMsg;
@@ -44,18 +47,15 @@ int main(int argc, char **argv) {
 
 
 
-  // Ensure that the proper arguments are given.
+  // Ensure that the proper arguments are given.````````
   if (argc != 3 && argc != 1) {
       fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
       // Return error code
       return 1;
   } else if (argc == 3) {
-			db = setup(&db);
-			int i[5] = {0,1,2,3,4};
-			
+			rc = setup(&db);
   } else {
-			db = setup(db);
-			fprintf(stderr,"2. db Sqlite3 address is: %p\n", &db);
+			rc = setup(&db);
       // Execute the main menu function
 	    mainmenu(db);
   }
@@ -65,16 +65,14 @@ int main(int argc, char **argv) {
 
 // -----------------------------END MAIN-------------------------------------------------------
 int setup(sqlite3 **p_db) {
-	
-	sqlite3 *db = NULL;
-			fprintf(stderr,"3.db Sqlite3 address is: %p\n", &db);
 
+	sqlite3 *db = NULL;
   // Return code - if the database is opened or created successfully SQLITE_OK is returned
   // otherwise an error is returned
   int rc;
 
 #if MEMWRITE == 1
-	  rc = sqlite3_open(":memory:", &db);
+	  rc = psqlite3_open(":memory:", &db);
 #else
 	  rc = sqlite3_open("sqltest.db", &db);
 #endif
@@ -99,7 +97,6 @@ int setup(sqlite3 **p_db) {
 
 		// Let us know if the database connection was succesfull.
 		fprintf(stdout,"Opened database connection succesfully\n");
-		fprintf(stderr,"4.db Sqlite3 address is: %p\n", &db);
 
 	  // Display the SQLite version - debug
 
@@ -157,26 +154,26 @@ int mainmenu(sqlite3 *db) {
 	//last item should be quit as long
 	//as there are no errors
 	char response[1024];
+	char *buf;
 	int rc=0;
-	unsigned int item;
+	unsigned int item = 0;
 	for (;;) {
 		fputs(menu,stdout);
 
 		fputs("Enter a choice > ",stdout);
-		// fflush();
+//		fflush();
 
 		if(fgets(response, sizeof(response), stdin) == NULL) {
 			exit(1);
 		}
 		if(sscanf(response,"%u",&item) != 1) {
-			fputs("Error reading commnad. Please enter a valid integer\n",stdout);
+			fprintf(stderr,"Error reading commnad. Please enter a valid integer\n");
 		} else {
 		switch (item) {
 			case 1:
-				fputs("Enter query: > ",stdout);
-				fgets(response, sizeof(response), stdin);
-				fputs("Calling dbSelect()...\n", stdout);
-				dbSelect(db, response);
+				buf = readline("Enter query: > ");
+				dbSelect(db, buf);
+				free(buf);
 				break;
 			case 2:
 				fputs("dbDelete`\n",stdout);
@@ -187,13 +184,9 @@ int mainmenu(sqlite3 *db) {
 				fputs("dbUpdate()\n",stdout);
 				break;
 			case 41:
-				fputs("Insert test data via command.\n",stdout);
-				fgets(response, sizeof(response), stdin);
-					if (!dbInsert(db,response) ) {
-					fprintf(stdout,"Inserted data ok\n");
-					} else {
-						setup(db);
-						fprintf(stdout,"Error\n");
+				buf = readline("Insert test data via command.\n");
+					if (!dbInsert(db,buf) ) {
+					fprintf(stdout,"Inserted data OK\n");
 					}
 				break;
 			case 42:
@@ -208,10 +201,12 @@ int mainmenu(sqlite3 *db) {
 				fputs("Please choose a valid menu option\n",stdout);
 			}
 		}
+
 	}
-Exit:
+		Exit:
 	return 0;
 }
+//}
 // --------------------------- End MainMenu ---------------------------
 
 int dbInsert(sqlite3 *db, char *data) {
@@ -236,9 +231,8 @@ int dbInsert(sqlite3 *db, char *data) {
 		// the database closes. Syntax errors should not close the DB .
 		// Determine the correct behavior.
 			if ( rc != SQLITE_OK ) {
-				fprintf(s`tderr, "SQL error occured: %s\n", zErrMsg);
+				fprintf(stderr, "SQL error occured: %s\n", zErrMsg);
 				sqlite3_free(zErrMsg);
-				//sqlite3_close(db);
 
 				return rc;
 				}
@@ -283,7 +277,6 @@ int dbSelect(sqlite3 *db, char *data) {
   if ( rc != SQLITE_OK ) {
 	 fprintf(stderr, "SQL error occured: %s with code %d\n", zErrMsg, rc);
 	 sqlite3_free(zErrMsg);
-	 sqlite3_close(db);
 
 	 return 1;
   }
@@ -313,7 +306,6 @@ int dbSelect(sqlite3 *db, char *data) {
   if ( rc != SQLITE_OK ) {
 	  fprintf(stderr,"SQL error occured on sqlite3_finalize(): %s with code: %d\n",zErrMsg, rc);
 	  sqlite3_free(zErrMsg);
-	  sqlite3_close(db);
 
 	  return 1;
   }
@@ -343,13 +335,12 @@ int dbUpdate (sqlite3 *db, char *data) {
    }
    // Prepare & check
    rc = sqlite3_prepare_v2(db, data, -1, &res, 0);
-   if (rc != SQLITE_OK) {
-	 fprintf(stderr, "SQL error occured: %s with code %d\n", zErrMsg, rc);
-	 sqlite3_free(zErrMsg);
-	 sqlite3_close(db);
+			if (rc != SQLITE_OK) {
+			fprintf(stderr, "SQL error occured: %s with code %d\n", zErrMsg, rc);
+			sqlite3_free(zErrMsg);
 
-	 return 1;
-   }
+			return 1;
+			}
 //    rc = sqlite3_exec (db, data,callback,(void*) msg, &zErrMsg);
     fprintf(stdout,"%d\n",rc);
 
diff --git a/graph/ds/ds1.c b/graph/ds/ds1.c
new file mode 100644
index 0000000..5eca328
--- /dev/null
+++ b/graph/ds/ds1.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stdlib.h>
+#define NUMELTS 5
+void aver() {
+  int num[NUMELTS];     /***** Array of numbers *****/
+
+  int i;
+  int total;            /***** Sum of the numbers *****/
+
+  float avg;            /***** Average of the numbers *****/
+
+  intmax_t diff;           /***** diffenrece between each *****/
+                        /***** number and te average  *****/
+
+  total = 0;
+  for ( i=0; i< NUMELTS ; i++ ) {
+    /***** read the number in the array and add them *****/
+    scanf("%d", &num[i]);
+    total += num[i];
+  } /*** end for ***/
+  avg = total / NUMELTS;
+  printf ("\nnumber difference"); /*** print heading ***/
+  for ( i=0;i< NUMELTS;i++ ) {
+      diff = num[i] - avg;
+      printf("\n %d %.2f", num[i], diff);
+
+  } /*** end for ***/
+  printf("\naverage is: %.2f\n", avg); /*** end aver ***/
+
+}
+int main (int argc, char** argv) {
+
+   aver ();
+   return EXIT_SUCCESS;
+}
diff --git a/graph/ds/ds2.c b/graph/ds/ds2.c
new file mode 100644
index 0000000..e23b6e7
--- /dev/null
+++ b/graph/ds/ds2.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define STRSIZE 80
+char string[STRSIZE];
+//string = "test__";
+char s1[] = "test1";
+//char s2[] = "test2";
+
+int strln(char string[]);
+int strpos(char s1[],char s2[]);
+
+int strln(char string[]) {
+//  char *string;
+  int i;
+  for ( i=0;string[i] != '\0';i++ )
+      ;
+  return i;
+}
+int strpos(char s1[],char s2[]) {
+  int len1;
+  int len2;
+  int i;
+  int j1;
+  int j2;
+
+  len1 = strln(s1);
+  len2 = strln(s2);
+  for ( i=0;i+len2 <= len1;i++ ) {
+      for ( j1=i,j2=0;j2 <= len2 && s1[j1] == s2[j2] ; j1++, j2++ ) {
+
+        if ( j2 == len2 ) {
+            return(i);
+        }
+        return(-1);
+
+      }
+  }
+
+}
+
+int main () {
+   fprintf(stdout,"String length is: %d\n",strln("abracadabrdaasda"));
+
+   return EXIT_SUCCESS;
+}