Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #ifndef DEBUG
- #define DEBUG(...) printf(__VA_ARGS__)
- #endif
- // Napisati funkciju koja cisti string od nepotrebnih znakova
- void clean_string(char a[], char b[], char c[]) {
- int i, posmak = 0;
- for(i = 0; i < strlen(a); i++) {
- a[i - posmak] = tolower(a[i]);
- if(!isalpha(a[i])) {
- posmak++;
- }
- }
- a[i - posmak] = '\0';
- posmak = 0;
- for(i = 0; i < strlen(b); i++) {
- b[i - posmak] = tolower(b[i]);
- if(!isalpha(b[i])) {
- posmak++;
- }
- }
- b[i - posmak] = '\0';
- posmak = 0;
- for(i = 0; i < strlen(c); i++) {
- c[i - posmak] = tolower(c[i]);
- if(!isalpha(c[i])) {
- posmak++;
- }
- }
- c[i - posmak] = '\0';
- }
- char get_operator(char o[]) {
- if (!strcmp("plus", o)) {
- return '+';
- }
- if (!strcmp("minus", o)) {
- return '-';
- }
- if (!strcmp("puta", o)) {
- return '*';
- }
- if (!strcmp("podijeljeno", o)) {
- return '/';
- }
- if (!strcmp("modulo", o)) {
- return '%';
- }
- return ' ';
- }
- // Napisati funkciju koja iz striga odredi vrijednost operanda
- char get_operand(char n[]) {
- char broj[10][256] = {"nula", "jedan", "dva", "tri", "cetiri", "pet", "sest", "sedam", "osam", "devet"};
- for(int i = 0; i < 10; i++) {
- if(!strcmp(n, broj[i])) {
- return i;
- }
- }
- return 0;
- }
- int calculate_operation(char a[], char b[], char o[]) {
- switch(get_operator(o)) {
- case '+':
- return get_operand(a) + get_operand(b);
- case '-':
- return get_operand(a) - get_operand(b);
- case '*':
- return get_operand(a) * get_operand(b);
- case '/':
- return get_operand(a) / get_operand(b);
- case '%':
- return get_operand(a) % get_operand(b);
- }
- return -1;
- }
- int main() {
- int n, i;
- char a[64], b[64], o[64];
- scanf("%d", &n);
- // Deklaracija i otvaranje datoteke
- FILE *fp;
- fp = fopen("input.dat", "r");
- // Procitati sadrzaj datoteke i ispisati rezultat operacije
- for(i = 0; i < n; i++) {
- while (fscanf(fp, "%s %s %s\n", a, o, b) > 0) {
- clean_string(a, b, o);
- printf("%d ", calculate_operation(a, b, o));
- }
- }
- // Zatvoriti datoteku
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement