formated code and renamed index.c to gui.c

This commit is contained in:
superdimensional 2021-04-18 15:31:26 -04:00
parent d53b048b9f
commit 34aae499f8
4 changed files with 163 additions and 157 deletions

View file

@ -1,5 +1 @@
# cli-algebra-calculator # cli-algebra-calculator
cli-algebra-calculator
Make into apple store ios app and run ads on it?
Make into linux app

View file

@ -1,37 +1,41 @@
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
double exponent( double par){ double exponent(double par)
{
double raisedNum; double raisedNum;
raisedNum = par * par; raisedNum = par * par;
return raisedNum; return raisedNum;
}; };
double hypotenuse(double par, double par2){ double hypotenuse(double par, double par2)
{
double sideC; double sideC;
sideC = sqrt( exponent(par) + exponent(par2) ); sideC = sqrt(exponent(par) + exponent(par2));
return sideC; return sideC;
}; };
void quadratic(double numA, double numB, double numC, double *awn1ptr, double *awn2ptr){ void quadratic(double numA, double numB, double numC, double *awn1ptr, double *awn2ptr)
{
double awn1 = (-numB) + sqrt( exponent(numB) - 4 * numA * numC);
double awn1 = (-numB) + sqrt(exponent(numB) - 4 * numA * numC);
double awn1F = awn1 / (2 * numA); double awn1F = awn1 / (2 * numA);
double awn2 = (-numB) - sqrt( exponent(numB) - 4 * numA * numC); double awn2 = (-numB) - sqrt(exponent(numB) - 4 * numA * numC);
double awn2F = awn2 / (2 * numA); double awn2F = awn2 / (2 * numA);
*awn1ptr = awn1F; *awn1ptr = awn1F;
*awn2ptr = awn2F; *awn2ptr = awn2F;
}; };
double areaOfCircle(double radius){ double areaOfCircle(double radius)
{
double area; double area;
area = acos(-1) * exponent(radius); area = acos(-1) * exponent(radius);
return area; return area;
} }
@ -39,26 +43,26 @@ int buildMatrix(int numRows, int numColumns)
{ {
int matrix[numRows][numColumns]; int matrix[numRows][numColumns];
printf("Enter elements in matrix of size %dx%d \n", numRows, numColumns); printf("Enter elements in matrix of size %dx%d \n", numRows, numColumns);
for(int i = 0; i < numRows; i++) for (int i = 0; i < numRows; i++)
{ {
for(int j = 0; j < numColumns; j++) for (int j = 0; j < numColumns; j++)
{ {
scanf("%d", &matrix[i][j]); scanf("%d", &matrix[i][j]);
} }
} }
printf("\nElements in matrix are: \n"); printf("\nElements in matrix are: \n");
for(int i = 0; i < numRows; i++) for (int i = 0; i < numRows; i++)
{ {
for(int j = 0; j < numColumns; j++) for (int j = 0; j < numColumns; j++)
{ {
printf("%d ", matrix[i][j]); printf("%d ", matrix[i][j]);
} }
printf("\n"); printf("\n");
} }
return 0; return 0;
} }

View file

@ -3,81 +3,84 @@
#include <stdbool.h> #include <stdbool.h>
//import C libraries //import C libraries
//gcc -Wall -Werror -pedantic -std=c11 index.c `pkg-config --cflags --libs gtk+-3.0` //gcc -Wall -Werror -pedantic -std=c11 gui.c `pkg-config --cflags --libs gtk+-3.0`
//using GTK+ 3.0 //using GTK+ 3.0
/* Function called when the user clicks the show button. */ /* Function called when the user clicks the show button. */
void on_show_button_1_clicked(GtkWidget *show_button1, GtkWidget *hello_display) void on_show_button_1_clicked(GtkWidget *show_button1, GtkWidget *hello_display)
{ {
printf("1 has been clicked\n"); printf("1 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>1</span>"); "<span font='Italic 30'>1</span>");
} }
void on_show_button_2_clicked(GtkWidget *show_button2, GtkWidget *hello_display) void on_show_button_2_clicked(GtkWidget *show_button2, GtkWidget *hello_display)
{ {
printf("2 has been clicked\n"); printf("2 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>2</span>"); "<span font='Italic 30'>2</span>");
} }
void on_show_button_3_clicked(GtkWidget *show_button3, GtkWidget *hello_display) void on_show_button_3_clicked(GtkWidget *show_button3, GtkWidget *hello_display)
{ {
printf("3 has been clicked\n"); printf("3 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>3</span>"); "<span font='Italic 30'>3</span>");
} }
void on_show_button_4_clicked(GtkWidget *show_button4, GtkWidget *hello_display) void on_show_button_4_clicked(GtkWidget *show_button4, GtkWidget *hello_display)
{ {
printf("4 has been clicked\n"); printf("4 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>4</span>"); "<span font='Italic 30'>4</span>");
}void on_show_button_5_clicked(GtkWidget *show_button5, GtkWidget *hello_display) }
void on_show_button_5_clicked(GtkWidget *show_button5, GtkWidget *hello_display)
{ {
printf("5 has been clicked\n"); printf("5 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>5</span>"); "<span font='Italic 30'>5</span>");
} }
void on_show_button_6_clicked(GtkWidget *show_button6, GtkWidget *hello_display) void on_show_button_6_clicked(GtkWidget *show_button6, GtkWidget *hello_display)
{ {
printf("6 has been clicked\n"); printf("6 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>6</span>"); "<span font='Italic 30'>6</span>");
}void on_show_button_7_clicked(GtkWidget *show_button7, GtkWidget *hello_display) }
void on_show_button_7_clicked(GtkWidget *show_button7, GtkWidget *hello_display)
{ {
printf("7 has been clicked\n"); printf("7 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>7</span>"); "<span font='Italic 30'>7</span>");
} }
void on_show_button_8_clicked(GtkWidget *show_button8, GtkWidget *hello_display) void on_show_button_8_clicked(GtkWidget *show_button8, GtkWidget *hello_display)
{ {
printf("8 has been clicked\n"); printf("8 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>8</span>"); "<span font='Italic 30'>8</span>");
}void on_show_button_9_clicked(GtkWidget *show_button9, GtkWidget *hello_display) }
void on_show_button_9_clicked(GtkWidget *show_button9, GtkWidget *hello_display)
{ {
printf("9 has been clicked\n"); printf("9 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>9</span>"); "<span font='Italic 30'>9</span>");
} }
void on_show_button_0_clicked(GtkWidget *show_button0, GtkWidget *hello_display) void on_show_button_0_clicked(GtkWidget *show_button0, GtkWidget *hello_display)
{ {
printf("0 has been clicked\n"); printf("0 has been clicked\n");
gtk_label_set_markup(GTK_LABEL(hello_display), gtk_label_set_markup(GTK_LABEL(hello_display),
"<span font='Italic 30'>0</span>"); "<span font='Italic 30'>0</span>");
} }
/* Function called when the user clicks the clear button */ /* Function called when the user clicks the clear button */
void on_clear_button_clicked(GtkWidget *clear_button, GtkWidget *hello_display) void on_clear_button_clicked(GtkWidget *clear_button, GtkWidget *hello_display)
@ -99,13 +102,13 @@ void build_window(void)
show_button1 = gtk_button_new(); show_button1 = gtk_button_new();
show_button2 = gtk_button_new(); show_button2 = gtk_button_new();
show_button3 = gtk_button_new(); show_button3 = gtk_button_new();
show_button4 = gtk_button_new(); show_button4 = gtk_button_new();
show_button5 = gtk_button_new(); show_button5 = gtk_button_new();
show_button6 = gtk_button_new(); show_button6 = gtk_button_new();
show_button7 = gtk_button_new(); show_button7 = gtk_button_new();
show_button8 = gtk_button_new(); show_button8 = gtk_button_new();
show_button9 = gtk_button_new(); show_button9 = gtk_button_new();
show_button0 = gtk_button_new(); show_button0 = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(show_button1), "1"); gtk_button_set_label(GTK_BUTTON(show_button1), "1");
gtk_button_set_label(GTK_BUTTON(show_button2), "2"); gtk_button_set_label(GTK_BUTTON(show_button2), "2");
@ -117,46 +120,46 @@ void build_window(void)
gtk_button_set_label(GTK_BUTTON(show_button8), "8"); gtk_button_set_label(GTK_BUTTON(show_button8), "8");
gtk_button_set_label(GTK_BUTTON(show_button9), "9"); gtk_button_set_label(GTK_BUTTON(show_button9), "9");
gtk_button_set_label(GTK_BUTTON(show_button0), "0"); gtk_button_set_label(GTK_BUTTON(show_button0), "0");
clear_button = gtk_button_new(); clear_button = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(clear_button), "Clear"); gtk_button_set_label(GTK_BUTTON(clear_button), "Clear");
/* Connects the "clicked" input events happening at the buttons to /* Connects the "clicked" input events happening at the buttons to
* their callback functions (on_show_button_clicked() and * their callback functions (on_show_button_clicked() and
* on_clear_button_clicked()). */ * on_clear_button_clicked()). */
g_signal_connect(show_button1, "clicked", g_signal_connect(show_button1, "clicked",
G_CALLBACK(on_show_button_1_clicked), G_CALLBACK(on_show_button_1_clicked),
hello_display); hello_display);
g_signal_connect(show_button2, "clicked", g_signal_connect(show_button2, "clicked",
G_CALLBACK(on_show_button_2_clicked), G_CALLBACK(on_show_button_2_clicked),
hello_display); hello_display);
g_signal_connect(show_button3, "clicked", g_signal_connect(show_button3, "clicked",
G_CALLBACK(on_show_button_3_clicked), G_CALLBACK(on_show_button_3_clicked),
hello_display); hello_display);
g_signal_connect(show_button4, "clicked", g_signal_connect(show_button4, "clicked",
G_CALLBACK(on_show_button_4_clicked), G_CALLBACK(on_show_button_4_clicked),
hello_display); hello_display);
g_signal_connect(show_button5, "clicked", g_signal_connect(show_button5, "clicked",
G_CALLBACK(on_show_button_5_clicked), G_CALLBACK(on_show_button_5_clicked),
hello_display); hello_display);
g_signal_connect(show_button6, "clicked", g_signal_connect(show_button6, "clicked",
G_CALLBACK(on_show_button_6_clicked), G_CALLBACK(on_show_button_6_clicked),
hello_display); hello_display);
g_signal_connect(show_button7, "clicked", g_signal_connect(show_button7, "clicked",
G_CALLBACK(on_show_button_7_clicked), G_CALLBACK(on_show_button_7_clicked),
hello_display); hello_display);
g_signal_connect(show_button8, "clicked", g_signal_connect(show_button8, "clicked",
G_CALLBACK(on_show_button_8_clicked), G_CALLBACK(on_show_button_8_clicked),
hello_display); hello_display);
g_signal_connect(show_button9, "clicked", g_signal_connect(show_button9, "clicked",
G_CALLBACK(on_show_button_9_clicked), G_CALLBACK(on_show_button_9_clicked),
hello_display); hello_display);
g_signal_connect(show_button0, "clicked", g_signal_connect(show_button0, "clicked",
G_CALLBACK(on_show_button_0_clicked), G_CALLBACK(on_show_button_0_clicked),
hello_display); hello_display);
g_signal_connect(clear_button, "clicked", g_signal_connect(clear_button, "clicked",
G_CALLBACK(on_clear_button_clicked), G_CALLBACK(on_clear_button_clicked),
hello_display); hello_display);
/* Create a layout element to visually organize widgets. */ /* Create a layout element to visually organize widgets. */
layout = gtk_grid_new(); layout = gtk_grid_new();
@ -193,11 +196,12 @@ void build_window(void)
* to the gtk_main_quit() function, which asks to stop the event * to the gtk_main_quit() function, which asks to stop the event
* loop, so that the program can finish. */ * loop, so that the program can finish. */
g_signal_connect(hello_window, "destroy", G_CALLBACK(gtk_main_quit), g_signal_connect(hello_window, "destroy", G_CALLBACK(gtk_main_quit),
NULL); NULL);
} }
/* main() function called when program starts. */ /* main() function called when program starts. */
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
/* Call the gtk initialization function */ /* Call the gtk initialization function */
gtk_init(&argc, &argv); gtk_init(&argc, &argv);

138
main.c
View file

@ -3,7 +3,10 @@
#include <stdlib.h> #include <stdlib.h>
#include "functions.h" #include "functions.h"
int main (){ // to compile use "gcc main.c -lm"
int main()
{
system("clear"); system("clear");
printf("Welcome to Calculator Collection™.\nPlease select which calculator you would like to use."); printf("Welcome to Calculator Collection™.\nPlease select which calculator you would like to use.");
printf("\n\n 1. Hypotenuse calculator"); printf("\n\n 1. Hypotenuse calculator");
@ -13,83 +16,82 @@ int main (){
printf("\n\n Enter your choice here:_____"); printf("\n\n Enter your choice here:_____");
printf("\033[D"); printf("\033[D");
printf("\033[D"); printf("\033[D");
printf("\033[D"); printf("\033[D");
int userInput; int userInput;
scanf("%i", &userInput); scanf("%i", &userInput);
switch(userInput){
case 1:
system("clear");
printf("\n ~~ Hypotenuse calculator ~~");
printf("\n\n /|\n");
printf(" / |\n");
printf(" c / |\n");
printf(" / | a\n");
printf(" / |\n");
printf(" /_____|\n\n");
printf(" b\n");
printf("\nPlease type the length of the triangle below:\n"); switch (userInput)
double sideA; {
scanf("%lf", &sideA); case 1:
printf("\nPlease type the width of the triangle:\n"); system("clear");
double sideB; printf("\n ~~ Hypotenuse calculator ~~");
scanf("%lf", &sideB); printf("\n\n /|\n");
printf(" / |\n");
printf(" c / |\n");
printf(" / | a\n");
printf(" / |\n");
printf(" /_____|\n\n");
printf(" b\n");
printf("The Hypotnuse is equal to %f.\n", hypotenuse(sideA, sideB)); printf("\nPlease type the length of the triangle below:\n");
break; double sideA;
scanf("%lf", &sideA);
case 2: printf("\nPlease type the width of the triangle:\n");
system("clear"); double sideB;
printf("\n ~~ Quadratic formula calculator ~~\n\n"); scanf("%lf", &sideB);
printf("Please enter the value for \"a\":\n"); printf("The Hypotnuse is equal to %f.\n", hypotenuse(sideA, sideB));
double numA; break;
scanf("%lf", &numA);
printf("Please enter the value for \"b\":\n");
double numB;
scanf("%lf", &numB);
printf("Please enter the value for \"c\":\n");
double numC;
scanf("%lf", &numC);
double awn1ptr, awn2ptr; case 2:
quadratic(numA, numB, numC, &awn1ptr, &awn2ptr); system("clear");
printf("\n ~~ Quadratic formula calculator ~~\n\n");
printf("The zeros are: %f and %f!\n", awn1ptr, awn2ptr); printf("Please enter the value for \"a\":\n");
break; double numA;
scanf("%lf", &numA);
case 3: printf("Please enter the value for \"b\":\n");
system("clear"); double numB;
printf("\n ~~ Area of a circle calculator ~~\n\n"); scanf("%lf", &numB);
printf(" o o\n o o\n o o\n o o\n o o\n o o\n"); printf("Please enter the value for \"c\":\n");
printf("\n enter a radius:\n"); double numC;
scanf("%lf", &numC);
double radius; double awn1ptr, awn2ptr;
scanf("%lf", &radius); quadratic(numA, numB, numC, &awn1ptr, &awn2ptr);
printf("The area is %f!\n", areaOfCircle(radius));
break;
case 4:
system("clear");
printf("\n ~~ matrix *beta* ~~\n\n");
int numRows;
int numColumns;
printf("\nPlease enter the number of rows:");
scanf("%d", &numRows);
printf("\nPlease enter the number of columns:");
scanf("%d", &numColumns);
buildMatrix(numRows, numColumns);
break;
default: printf("The zeros are: %f and %f!\n", awn1ptr, awn2ptr);
printf("\n Incorrect input, try again.\n"); break;
case 3:
system("clear");
printf("\n ~~ Area of a circle calculator ~~\n\n");
printf(" o o\n o o\n o o\n o o\n o o\n o o\n");
printf("\n enter a radius:\n");
double radius;
scanf("%lf", &radius);
printf("The area is %f!\n", areaOfCircle(radius));
break;
case 4:
system("clear");
printf("\n ~~ matrix *beta* ~~\n\n");
int numRows;
int numColumns;
printf("\nPlease enter the number of rows:");
scanf("%d", &numRows);
printf("\nPlease enter the number of columns:");
scanf("%d", &numColumns);
buildMatrix(numRows, numColumns);
break;
default:
printf("\n Incorrect input, try again.\n");
}; };
return 0; return 0;
}; };