Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

// Endeiktiki Lysi tis Askisis 3.1 - Ak.

Etos 2020-2021

/* Simple calculator */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
int result, i;
Το όνομα αρχείου, το σύμβολο της πράξης και τους δύο τουλάχιστον τελεστέους
if (argc < 4) /* Xreiazomaste toulaxiston 4 orismata */
return 1;

/* Sigkrinw to prwto orisma me ta simvola twn praksewn +, -,


x, / */
if (strcmp(argv[1], "+") == 0) Όλα τα ορίσματα είναι string - άρα συγκρίνω με strcmp( )
{
result = 0;

/* Prosthetw ta orismata */
for (i=2; i<argc; i++)
result += atoi(argv[i]);
}
else if (strcmp(argv[1], "-") == 0)
{
result = atoi(argv[2]);

/* Afairw ta ipoloipa orismata apo to deutero */


for (i=3; i<argc; i++)
result -= atoi(argv[i]);
}
else if (strcmp(argv[1], "x") == 0)
{
result = 1;

/* Pollaplasiazw ta orismata */
for (i=2; i<argc; i++)
result *= atoi(argv[i]);
}
else if (strcmp(argv[1], "/") == 0)
{
result = atoi(argv[2]);

/* Diairw ta ipoloipa orismata apo to deutero */


for (i=3; i<argc; i++)
result /= atoi(argv[i]);
}
else
{
/* An den dothike swsta i praksi */
printf("Wrong syntax!\n");
return 1;
}

printf("Result = %d\n", result);


return 0;
}
// Endeiktiki Lysi tis Askisis 3.2 - Ak. Etos 2020-2021

#include <stdio.h>
#define N 10
δείκτης σε δείκτη ακεραίων
void search(int array[], int size, int num, int **res); /* prototype
*/

int main(void)
{
/* Arxikopoiw tous pinakes */
int A[N] = {0, 4, 8, 2, -2, 100, 45, -12, 45, 31};
int *res;
που είναι μεταβλητή δείκτη σε ακέραιο.
/* To res prepei na perastei me anafora, ara pernaw ti d/nsi
tou */
search(A, N, 100, &res);
if (res != NULL)
{
printf("Number=%d, position(0-%d)=%ld\n", *res, N-1,
res-A);
}
else
{
printf("Number not found!\n");
}
return 0;
}

void search(int array[], int size, int num, int **res)


{
int i;

for (i=0; i<size; i++)


{
if (array[i] == num)
{
*res = &(array[i]); /* Otan to vrw epistrefw
tin dieuthinsi tou */
return;
}
}
*res = NULL;
}
// Endeiktiki Lysi tis Askisis 3.3 - Ak. Etos 2020-2021

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void charstats(char *string);

int stats[26]; /* global, initialized to 0 by default */

int main()
{
char input[100];
int i;

printf("Enter sentences; terminate by giving a line that


begins with ###\n");
do
{
fgets(input, 100, stdin);
if (strncmp(input, "###", 3) != 0)
charstats(input);
} while (strncmp(input, "###", 3) != 0);

for (i = 0; i < 26; i++)


printf("%c appeared %2d times\n", 'a'+i, stats[i]);

return 0;
}

void charstats(char *string)


{
int n;

while (*string != '\0')


{
if (isalpha(*string))
{
n = tolower(*string) - 'a';
stats[n]++;
}
string++;
}
}
// Endeiktiki Lysi tis Askisis 3.4 - Ak. Etos 2020-2021

/* credit card checker */

#include <stdio.h>

#define DIGITS 16
int check_card_number(char *str);

int main(int argc, char *argv[])


{
int i, res;

if (argc < 2)
printf("please re-run and supply 16-digit credit card
numbers\n");
else
for (i = 1; i < argc; i++)
{
res = check_card_number(argv[i]);
printf("Card %s is %s\n", argv[i], (res == 1)
? "valid" : "invalid");
}
return 0;
}

int check_card_number(char *str)


{
int i, pos = 1;
int sum = 0;
int num_int;

/* Check the characters starting from the last one */


for (i=DIGITS-1; i>=0; i--, pos++)
{
num_int = (int)(*(str+i) - '0'); /* Integer value
of character */
if (pos % 2 == 0) /* If we are in
an even psoition */
{
num_int *= 2; /* Double
the number */

/* Get the sum of its digits:


* 5 -> 10 -> 1, 6 -> 12 -> 3, ..., 9 -> 18 ->
9
* Basically, just subtract 9 from the two-
digit number.
*/
if (num_int >= 10)
num_int = num_int - 9;
}
sum += num_int; /* Sum the digits
*/
}

/* Equivallent to: return ( sum % 10 == 0 ); */


if (sum % 10 == 0)
return 1;
else
return 0;
}
// Endeiktiki Lysi tis Askisis 3.5 - Ak. Etos 2020-2021

/* Small dictionary */

#include <stdio.h>
#include <string.h>

#define N 8
#define M 32

int main(void)
{
/* Initialize the dictionary */
char *dic[N] = {
"Hello",
"World",
"This",
"is",
"a",
"very",
"small",
"dictionary"
};
char user_input[M];
int i;
int found;

while (1)
{
/* Prompt for input */
printf("\n\nType a word or \"fin\" to exit program:
");
/* If we used fgets(), we should remove the trailing
\n */
scanf("%s", user_input);

/* Check for program termination */


if (strcmp(user_input, "fin") == 0)
break;

/* Search dictionary for the given word */


found = 0;
for (i=0; i<N; i++)
{
if (strcmp(user_input, dic[i]) == 0)
{
found = 1;
break;
}
}

if (found)
printf("Word: \"%s\" exists in dictionary\n",
user_input);
else
printf("Word: \"%s\" DOES NOT exist in
dictionary\n", user_input);
}

return 0;
}
// Endeiktiki Lysi tis Askisis 3.6 - Ak. Etos 2020-2021

/* Nameday calendar */

#include <stdio.h>
#include <string.h>

#define N 5

char *dates[N] = {
"12/12",
"6/12",
"30/11",
"12/6",
"13/5"
};
char *names[N] = {
"Spiros, Spiridoula",
"Nikolaos, Nikoletta, Niki",
"Andreas, Andriana, Andromaxi, Maxi",
"Onoufrios",
"Glykeria"
};

/* Usage: ./a.out -d date or ./a.out -n name


*/
int main(int argc, char *argv[])
{
int found = 0;
int i;

if (argc != 3) /* Xreiazomaste 3 orismata */


{
printf("Usage: %s [OPTION]\n", argv[0]);
printf("Available options:\n");
printf("\t-d DD/MM (e.g. -d 30/11)\n");
printf("\t-n NAME (e.g. -n Spiros)\n");
return 1;
}

/* Sigkrinw to prwto orisma me ta epitrepomena modes -d


(date) -n (name) */
if (strcmp(argv[1], "-d") == 0) /* O xristis edwse ws orisma
imerominia */
{
/* Psaxnw ton pinaka me tis imerominies */
for (i=0; i<N; i++)
{
if (strcmp(argv[2], dates[i]) == 0)
{
/* An i imerominia iparxei ektipwnw ta
antistoixa onomata */
found = 1;
printf("%s is the nameday of: %s\n",
dates[i], names[i]);
}
}
if (found == 0)
printf("This date is not included in the
calendar.\n");
}
else if (strcmp(argv[1], "-n") == 0) /* O xristis edwse ws
orisma onoma */
{
/* Psaxnw ton pinaka me ta onomata */
for (i=0; i<N; i++)
{
if (strstr(names[i], argv[2]) != NULL)
{
/* An to onoma iparxei ektipwnw tin
antistoixi imerominia */
found = 1;
printf("%s nameday is on: %s\n",
names[i], dates[i]);
}
}
if (found == 0)
printf("This name is not included in the
calendar.\n");
}
else
{
/* An den dothike to swsta input */
printf("Usage: %s [OPTION]\n", argv[0]);
printf("Available options:\n");
printf("\t-d DD/MM (e.g. -d 30/11)\n");
printf("\t-n NAME (e.g. -n Spiros)\n");
return 1;
}

return 0;
}

You might also like