all
This commit is contained in:
parent
82a772622a
commit
73d6bd42c2
55
a2/program.c
55
a2/program.c
@ -1,6 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
@ -10,6 +9,8 @@ struct stack{
|
||||
int size;
|
||||
}stack;
|
||||
|
||||
///add bracket to stack
|
||||
|
||||
void push(char ch){
|
||||
if(stack.size < 100){
|
||||
stack.size++;
|
||||
@ -19,6 +20,8 @@ void push(char ch){
|
||||
exit(0);
|
||||
}
|
||||
|
||||
///if i have opening bracket in stack, then decrement size
|
||||
|
||||
void pop(){
|
||||
if(stack.size >= 0)
|
||||
stack.size--;
|
||||
@ -26,6 +29,8 @@ void pop(){
|
||||
exit(0);
|
||||
}
|
||||
|
||||
///compare brackets
|
||||
|
||||
bool find_pair(char a, char b){
|
||||
if(a == '(' && b == ')')
|
||||
return true;
|
||||
@ -37,8 +42,10 @@ bool find_pair(char a, char b){
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
///function to find right pair
|
||||
|
||||
char right_pair(char a){
|
||||
char b;
|
||||
if(a == '(')
|
||||
@ -53,6 +60,8 @@ char right_pair(char a){
|
||||
return b;
|
||||
}
|
||||
|
||||
///find the last opening bracket in the stack
|
||||
|
||||
char gettop(){
|
||||
return stack.data[stack.size];
|
||||
}
|
||||
@ -69,52 +78,60 @@ int main(){
|
||||
|
||||
int l = strlen(line);
|
||||
int i, j = -1, f = -1;;
|
||||
char *l_bracket = calloc(l, sizeof(char));
|
||||
char *r_bracket = calloc(l, sizeof(char));
|
||||
char *o_bracket = calloc(l, sizeof(char));
|
||||
char *c_bracket = calloc(l, sizeof(char));
|
||||
|
||||
for(i = 0; i < l; i++){
|
||||
if(line[i] == '{' || line[i] == '(' || line[i] == '[' || line[i] == '<'){
|
||||
push(line[i]);
|
||||
j++;
|
||||
f++;
|
||||
l_bracket[j] = gettop();
|
||||
r_bracket[f] = right_pair(l_bracket[j]);
|
||||
o_bracket[j] = gettop(); //get opening bracket from the stack
|
||||
c_bracket[f] = right_pair(o_bracket[j]); //get closing bracket
|
||||
}
|
||||
if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){
|
||||
if(stack.size == -1){
|
||||
|
||||
if(stack.size == -1){ //if there is no opening bracket in the stack
|
||||
printf("Unexpected closing bracket %c in %d\n", line[i], i);
|
||||
return 0;
|
||||
|
||||
}
|
||||
else if(!find_pair(gettop(), line[i])){
|
||||
printf("Crossed bracket %c in %d, expected %c \n", line[i], i, r_bracket[j]);
|
||||
else if(!find_pair(gettop(), line[i])){ //if there is another closing bracket
|
||||
printf("Crossed bracket %c in %d, expected %c \n", line[i], i, c_bracket[f]);
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
r_bracket[f] = 0;
|
||||
f--;
|
||||
c_bracket[f] = 0; //if closing bracket is correct then reducee array
|
||||
f--; //if i didn't find a correct bracket, then keep it in the array
|
||||
pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int begin = 0;
|
||||
int end = strlen(r_bracket);
|
||||
int end = strlen(c_bracket);
|
||||
int temp;
|
||||
//printf("%s", b);
|
||||
|
||||
//now i have all missing closing brackets in array
|
||||
|
||||
//flip array
|
||||
while(begin < end){
|
||||
temp = r_bracket[begin];
|
||||
r_bracket[begin++] = r_bracket[--end];
|
||||
r_bracket[end] = temp;
|
||||
temp = c_bracket[begin];
|
||||
c_bracket[begin++] = c_bracket[--end];
|
||||
c_bracket[end] = temp;
|
||||
}
|
||||
|
||||
//if program pop all opening brackets from the stack, then stack size must be -1;
|
||||
if(stack.size == -1){
|
||||
printf("All brackets OK\n");
|
||||
}
|
||||
//if no, then program didn't find closing bracket
|
||||
else {
|
||||
printf("Missing closing brackets: %s\n", r_bracket);
|
||||
printf("Missing closing brackets: %s\n", c_bracket);
|
||||
}
|
||||
free(l_bracket);
|
||||
free(r_bracket);
|
||||
|
||||
free(o_bracket);
|
||||
free(c_bracket);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -30,22 +30,27 @@ int read_pizza(struct pizza* item){
|
||||
}
|
||||
|
||||
int search_string(const char* heap, const char* needle){
|
||||
|
||||
int H = strlen(heap);
|
||||
int N = strlen(needle);
|
||||
int i;
|
||||
int i = 0;
|
||||
for(i = 0; i <= H - N; i++){
|
||||
int j;
|
||||
|
||||
for(j = 0; j < N; j++){
|
||||
if(heap[i+j] != needle[j]){
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
if(j = N){
|
||||
}
|
||||
if(j == N){
|
||||
return i;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
char hacker_script(char c){
|
||||
if(isupper(c)){
|
||||
c = tolower(c);
|
||||
@ -59,14 +64,21 @@ char hacker_script(char c){
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
printf("Zadaj hladanu surovinu:");
|
||||
char key[LINESIZE];
|
||||
memset(key,0,LINESIZE);
|
||||
char* r = fgets(key,LINESIZE,stdin);
|
||||
key[strlen(key)-1] = '\0';
|
||||
int k = 0;
|
||||
for(k = 0; k < strlen(key); k++){
|
||||
key[k] = hacker_script(key[k]);
|
||||
}
|
||||
|
||||
printf("Zadaj jedalny listok:");
|
||||
struct pizza jedalny_listok[100];
|
||||
@ -77,30 +89,29 @@ int main(){
|
||||
int counter = 0;
|
||||
|
||||
|
||||
while(read_pizza(&item)){
|
||||
|
||||
while(stdin,read_pizza(&item)){
|
||||
strcpy(jedalny_listok[counter].name, item.name);
|
||||
jedalny_listok[counter].prize = item.prize;
|
||||
counter++;
|
||||
}
|
||||
|
||||
int i, j, f;
|
||||
int i, j;
|
||||
for(i = 0; i < counter; i++){
|
||||
for(j = 0; j < strlen(jedalny_listok[i].name); j++){
|
||||
pomocny[i].name[j] = hacker_script(jedalny_listok[i].name[j]);
|
||||
//printf("%c", pomocny[i].name[j]);
|
||||
|
||||
}
|
||||
search_string(pomocny[i].name, key);
|
||||
if(search_string(pomocny[i].name, key) != -1){
|
||||
int result = search_string(pomocny[i].name, key);
|
||||
if(result != -1){
|
||||
printf("%s", jedalny_listok[i].name);
|
||||
printf("%.2f\n", jedalny_listok[i].prize);
|
||||
printf("Nacitano %d poloziek.\n", counter);
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
printf("Nacitano %d poloziek\n", counter);
|
||||
return 1;
|
||||
printf("Nacitanych %d poloziek.\n", counter);
|
||||
return 0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define LINESIZE 100
|
||||
|
||||
struct pizza{
|
||||
@ -10,6 +9,9 @@ struct pizza{
|
||||
float prize;
|
||||
};
|
||||
|
||||
//read the name and prize
|
||||
//push them to stack
|
||||
|
||||
int read_pizza(struct pizza* item){
|
||||
|
||||
char line1[LINESIZE];
|
||||
@ -30,20 +32,17 @@ int read_pizza(struct pizza* item){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
int comparename(const void *a, const void *b){
|
||||
|
||||
//compare 2 elements
|
||||
|
||||
int compare(const void *a, const void *b){
|
||||
|
||||
struct pizza* pizza_a = (void*) a;
|
||||
struct pizza* pizza_b = (void*) b;
|
||||
|
||||
return strcmp(pizza_a->name, pizza_b->name);
|
||||
|
||||
}
|
||||
int compareprize(const void *a, const void *b){
|
||||
|
||||
struct pizza* pizza_a = (void*) a;
|
||||
struct pizza* pizza_b = (void*) b;
|
||||
|
||||
float r = (pizza_a->prize > pizza_b->prize) - (pizza_a->prize < pizza_b->prize);
|
||||
if (r == 0){ //if prizes are identical, than check if names are in alphabetical order
|
||||
r = strcmp(pizza_a->name, pizza_b->name);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -55,15 +54,14 @@ int main(){
|
||||
struct pizza item;
|
||||
int counter = 0;
|
||||
|
||||
//write menu to array
|
||||
while(stdin, read_pizza(&item)){
|
||||
strcpy(jedalny_listok[counter].name, item.name);
|
||||
jedalny_listok[counter].prize = item.prize;
|
||||
counter++;
|
||||
}
|
||||
|
||||
qsort(jedalny_listok, counter, sizeof(struct pizza), compareprize);
|
||||
|
||||
//qsort(jedalny_listok, counter, sizeof(struct pizza), comparename);
|
||||
qsort(jedalny_listok, counter, sizeof(struct pizza), compare);
|
||||
|
||||
int i = 0;
|
||||
for(i = 0; i < counter; i++){
|
||||
@ -71,7 +69,5 @@ int main(){
|
||||
printf("%f", jedalny_listok[i].prize);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,27 +1,43 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define STACK_SIZE 100
|
||||
#define STACK_SIZE 10
|
||||
|
||||
struct stack {
|
||||
float values[STACK_SIZE];
|
||||
int size;
|
||||
};
|
||||
|
||||
//push number to the stack
|
||||
|
||||
void push(struct stack* stack,float values){
|
||||
assert(stack->size < STACK_SIZE);
|
||||
if(stack->size < 10){
|
||||
stack->values[stack->size] = values;
|
||||
stack->size += 1;
|
||||
}
|
||||
else{
|
||||
printf("full stack\n");
|
||||
exit(0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//pop number from the stack
|
||||
|
||||
float pop(struct stack *stack){
|
||||
assert(stack->size > 0);
|
||||
float values = stack->values[stack->size-1];
|
||||
if(stack->size > 0){
|
||||
float value = stack->values[stack->size-1];
|
||||
stack->size -= 1;
|
||||
return values;
|
||||
return value;
|
||||
}
|
||||
else{
|
||||
printf("not enough operands\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void print_stack(struct stack *stack){
|
||||
int i;
|
||||
int len = stack->size - 1;
|
||||
@ -30,9 +46,8 @@ void print_stack(struct stack *stack){
|
||||
}
|
||||
if(stack->size != 0){
|
||||
printf("%0.2f ", stack->values[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -40,14 +55,19 @@ int main() {
|
||||
struct stack mystack;
|
||||
memset(&mystack, 0, sizeof(struct stack));
|
||||
int i = 0;
|
||||
float z;
|
||||
for(i = 0; i < 10000; i++){
|
||||
for(i = 0; i < 100; i++){
|
||||
|
||||
char line[10];
|
||||
char *x = fgets(line, 10, stdin);
|
||||
float r = 0;
|
||||
float z = 0;
|
||||
|
||||
//if string is not empty
|
||||
if(line[1] != 0 && x != NULL){
|
||||
|
||||
//if there is a wrong symbol
|
||||
if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){
|
||||
printf("no input");
|
||||
printf("bad input\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -55,29 +75,37 @@ int main() {
|
||||
r = pop(&mystack) + pop(&mystack);
|
||||
}
|
||||
else if(line[0] == '-'){
|
||||
z = pop(&mystack);
|
||||
r = pop(&mystack) - z;
|
||||
z = pop(&mystack); //swap numbers(e.g input "3 4 -"
|
||||
r = pop(&mystack) - z; //output 3 - 4, not 4 - 3)
|
||||
}
|
||||
else if(line[0] == '*'){
|
||||
r = pop(&mystack) * pop(&mystack);
|
||||
}
|
||||
else if(line[0] == '/'){
|
||||
z = pop(&mystack);
|
||||
if(z != 0.0){
|
||||
if(z == 0){
|
||||
printf("division by zero\n");
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
r = pop(&mystack) / z;
|
||||
}
|
||||
}
|
||||
|
||||
//if symbol is not operation, than it is number
|
||||
//change string to float
|
||||
else {
|
||||
r = strtof(line,&x);
|
||||
}
|
||||
|
||||
//push result to stack
|
||||
push(&mystack,r);
|
||||
|
||||
print_stack(&mystack);
|
||||
|
||||
}
|
||||
|
||||
|
||||
else{
|
||||
printf("no input\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user