From: hackbard Date: Thu, 6 Jun 2002 11:28:03 +0000 (+0000) Subject: added beginner c sources X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=62e4aa7ef68064df6c9c2d72734e21af2b672758;p=my-code%2Fbeginners.git added beginner c sources --- diff --git a/calc.c b/calc.c new file mode 100644 index 0000000..a4e99aa --- /dev/null +++ b/calc.c @@ -0,0 +1,130 @@ +#include + +char input[50]; /* for operator and number */ +char choice[3]; /* for the choice i give you, hahahaha */ +char operator; /* tells me how to calculate */ +char quit; /* to quit the averager */ + +float result; /* the result of the calculations */ +int yachoi; /* for ya choice that i gave you, hahahaha */ +float value; /* the value i have to calculate with */ +int counter; /* counting the amount of numbers to average */ + +int main() +{ + printf ("Welcome to Frank's Calculator.\n"); + printf ("\n"); + + while(1) /* Loop 1 -- ya choice ! */ + { + + printf ("What do you want to do:\n"); + printf ("1=Calculate\n"); + printf ("2=Average an amount of numbers\n"); + printf ("3=Quit Program\n"); + printf ("\n"); + printf ("Your choice: "); + + fgets(choice, sizeof(choice), stdin); + sscanf(choice, "%d", &yachoi); + + if (yachoi == 3) + { + printf ("Thanks for using the Frankulator -- see ya!\n"); + break; + } + + switch (yachoi) + { + + case 1: + + result = 0; /* I don't want the computer to get a number by chance */ + + printf ("The CaLcUlAtOr -- press 'q` to go to main menu!\n"); + printf ("\n"); + while(1) /* Loop 1.1 -- the calculator */ + { + printf ("\n"); + printf ("Result = %f !\n", result); + printf ("\n"); + printf ("Enter operator and number: "); + + fgets(input, sizeof(input), stdin); + sscanf(input, "%c %f", &operator, &value); + + if (operator == 'q') + { + printf ("See ya dude!\n"); + break; + } + + switch (operator) + { + case '+': + result += value; + break; + case '-': + result -= value; + break; + case '*': + result *= value; + break; + case '/': + if (value == 0) + { + printf("Illegal you stoopid! ...try again\n"); + break; + } + result /= value; + break; + default: + printf ("Sorry, don't know operator...try another!\n"); + break; + } + } + break; + + case 2: + + printf ("Welcome to the aVeRaGeR -- press 'q` for main menu!\n"); + printf ("\n"); + + result = 0; + counter = 1; + + while(1) + { + printf ("Enter number %d : ",counter); + + fgets(input, sizeof(input), stdin); + sscanf(input, "%c", &operator); + + if (operator == 'q') + { + if (counter == 1) + { + printf ("Stoopido... try anothertime, he!\n"); + break; + } + printf ("The average is : %f !\n",result/(counter-1)); + break; + } + + sscanf(input, "%f", &value); + result += value; + ++counter; + } + break; + + default: + + printf ("Come on try again...stoopid!\n"); + printf ("\n"); + break; + } + } +return (0); +} + + diff --git a/length.c b/length.c new file mode 100644 index 0000000..7abd55a --- /dev/null +++ b/length.c @@ -0,0 +1,37 @@ +/* Type something ... and I tell you it's length! */ +#include +#include + +char first[100]; /* string for firstname */ +char last[100]; /* etc. */ +char full[200]; /* string for the full name */ +char line[4]; /* string for age */ +int age; /* variable for Age */ + +int main() +{ + printf(" -----------------------------------------------------------\n"); + printf("Yoh dude, ready to let me count...:\n"); + + printf("Enter first name: "); + fgets(first, sizeof(first), stdin); + first[strlen(first) - 1] = '\0'; + + printf("now the last name : "); + fgets(last, sizeof(last), stdin); + last[strlen(last) - 1] = '\0'; + + printf("...and your age: "); + fgets(line, sizeof(line), stdin); + sscanf(line ,"%d" ,&age); + + strcpy(full, first); + strcat(full, " "); + strcat(full, last); + + printf("%s got %d cHaRaCtErS man!\n", full, strlen(first) + strlen(last) - 2); /* -- \0 must not be counted -- */ + printf("...and you are %d years old!\n",age ); + return(0); +} + + diff --git a/pie_new.c b/pie_new.c new file mode 100644 index 0000000..860c816 --- /dev/null +++ b/pie_new.c @@ -0,0 +1,63 @@ +#include +#include +#include + +int counter1; +// float counter3; +float counter2; +int steps; +int hits; +float x; +float x2; +float y; +float y2; +float pie; +float pie2; +char stepschar[100]; + +int main() +{ + + printf("\n"); + printf("\n"); + printf("Berechne Pie...\n"); + printf("\n"); + printf("Genauigkeit - [steps] ? : "); + + fgets(stepschar, sizeof(stepschar), stdin); + sscanf(stepschar, "%d", &steps); + + counter2 = 0; + hits = 0; + pie2 = 1; + + for (counter1 = 1; counter1 <= steps; ++counter1) + { + x = rand( ); + y = rand( ); + + x2 = x / RAND_MAX; + y2 = y / RAND_MAX; + + printf("%7d. Vektor: ( %f , %f )\n", counter1, x2, y2); + + if( ( x2 * x2 + y2 * y2) <= 1 ) + ++hits; + + // counter3 = counter1; + + pie2 = pie2 * ((float)(( 2 * counter1 ) * ( 2 * counter1 )) / (float)((( 2 * counter1 ) * ( 2 * counter1 )) - 1 )); + + counter2 = counter2 + x2 + y2; + } + + pie = (4.0 * hits) / steps; + pie2 = 2 * pie2; + + printf("\n"); + printf("\tPie :\t %1.8f\n", pie); + printf("\tProbe:\t %1.8f (nah an 0.5 ?...)\n\n", counter2 / ( 2.0 * steps )); + printf("\tAus Rekursion:\t %1.8f\n", pie2); + +return (0); +} diff --git a/term.c b/term.c new file mode 100644 index 0000000..7618f38 --- /dev/null +++ b/term.c @@ -0,0 +1,36 @@ +/* Term */ + +#include + +float term; /* Variable für den Term */ +float fak; /* Vielfachenfaktor */ +int z; /* Zähler des Terms */ +int ne; /* Nenner des Terms */ +char input[10]; /* Input des Users */ + +int main() +{ +printf("\n"); +printf("Vielfaches eines Terms .. check out ;-) [Term aus ganzen Zahlen] \n"); +printf("\n"); + + printf("Faktor: "); + fgets(input, sizeof(input), stdin); + sscanf(input, "%f", &fak); + +printf("\n"); + + printf("Zähler des Terms: "); + fgets(input, sizeof(input), stdin); + sscanf(input, "%d", &z); + + printf("Nenner des Terms: "); + fgets(input, sizeof(input), stdin); + sscanf(input, "%d", &ne); + +term = fak * z / ne; +printf("Der %f - fache Wert des Terms %d / %d ist %f\n", fak, z, ne , term); +return (0); +} + +