Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void copyLEToLE(int value, void *adresa);
- void copyLEToBE(int value, void *adresa);
- void createBuffer(char *buffer, void *podatak1, void *podatak2);
- float Calculate(char* buffer);
- int main()
- {
- char buffer[10];
- int a, b;
- float resenje;
- printf("Unesite podatke: ");
- scanf("%d %d",&a,&b);
- createBuffer(buffer, &a, &b);
- resenje = Calculate(buffer);
- printf("Resenje: %.2f",resenje);
- return 0;
- }
- void copyLEToLE(int value, void *adresa)
- {
- int i;
- char *pAdresa = (char*)adresa;
- char *pValue = (char *)&value;
- for(i=0;i<sizeof(int);i++){
- *(pAdresa+i)=*(pValue+i);
- }
- }
- void copyLEToBE(int value, void *adresa)
- {
- int i;
- char *pAdresa = (char*)adresa;
- char *pValue = (char *)&value;
- for(i=0;i<sizeof(int);i++){
- *(pAdresa+i)=*(pValue+sizeof(int)-i);
- }
- }
- void createBuffer(char *buffer, void *podatak1, void *podatak2)
- {
- char ch;
- int i;
- printf("Tip zapisa podataka (L B): ");
- do{
- ch=getch();
- }while(ch!='L' && ch!='B');
- printf("%c\n",ch);
- *buffer = ch;
- printf("Znak operacije (+ - * /): ");
- do{
- ch=getch();
- }while(ch!='+' && ch!='-' && ch!='*' && ch!='/');
- printf("%c",ch);
- *(buffer+1)=ch;
- switch(*(buffer)){
- case 'L': copyLEToLE(*((int*)podatak1), buffer+2);
- copyLEToLE(*((int*)podatak2), buffer+6);
- break;
- case 'B': copyLEToBE(*((int*)podatak1), buffer+2);
- copyLEToBE(*((int*)podatak2), buffer+6);
- break;
- }
- }
- float Calculate(char* buffer)
- {
- float resenje=0;
- int a, b;
- if(*buffer == 'L'){
- copyLEToLE(*((int *)(buffer+2)),&a);
- copyLEToLE(*((int *)(buffer+6)),&b);
- }else{
- copyLEToBE(*((int *)(buffer+2)),&a);
- copyLEToBE(*((int *)(buffer+6)),&b);
- }
- switch(*(buffer+1)){
- case '+': resenje = a + b;
- break;
- case '-': resenje = a - b;
- break;
- case '*': resenje = a * b;
- break;
- case '/': if(b!=0) resenje = a / b;
- else{
- printf("Greska!\nDeljenje sa nulom!");
- resenje = 0;
- }
- break;
- }
- return resenje;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement