Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LINK SOAL : https://www.dropbox.com/sh/wussgfrqrmazf9g/AADnp_e-zKkHZWXyAFDQ5yJFa?dl=0
- // LINK TEMPLATE : http://pastebin.com/Rc7A08xG
- #include <stdio.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- struct data{
- char s_name[31];
- char s_address[51];
- char r_name[31];
- char r_address[51];
- char i_name[16];
- float weight;
- char type[10];
- };
- int main(){
- int random1,random2,randomhuruf;//ascii A-Z 65-90
- float totalCost;//karena harus ada comanya
- struct sembuf s;
- int h=shmget(123456,sizeof(struct data),IPC_CREAT|0666);//h=shmid
- int e=semget(123456,1,IPC_CREAT|0666);//e=semid
- semctl(e,0,SETVAL,0);
- struct data *deliv=(struct data*)shmat(h,NULL,0666);
- printf("SNE Delivery Service - Client\n\n");
- int flag=0;
- do{
- s.sem_num=0;
- s.sem_op=-1;
- s.sem_flg=0;
- semop(e,&s,1);
- if(strcasecmp(deliv->s_name,"exit")!=0){//pastiin input bukan exit
- printf("SNE Delivery Receipt\n------------------------------------------\n");
- srand(time(NULL));
- random1=rand()%10;
- srand(time(NULL));
- random2=rand()%10;
- srand(time(NULL));
- randomhuruf=rand()%26+65;//baca jarak random 26 dimulai dari 65(A=65)
- printf("Deliver Code \t\t: TK%d%d%c\n",random1,random2,randomhuruf);//printfnya satu2,%d%d%c
- printf("Sender Name\t\t: %s\n",deliv->s_name);// "\t" untuk tab
- printf("Sender Address\t\t: %s\n",deliv->s_address);
- printf("Receiver Name\t\t: %s\n",deliv->r_name);
- printf("Receiver Address\t: %s\n",deliv->r_address);
- printf("Item Name\t\t: %s\n",deliv->i_name);
- printf("Item Weight\t\t: %.2f kg(s)\n",deliv->weight);// %.2f = 2 angak di belakang koma
- printf("Delivery Type\t\t: %s\n",deliv->type);
- if(strcmp(deliv->type,"Express")==0)
- totalCost=deliv->weight*27500;
- else
- totalCost=deliv->weight*15000;
- printf("Total Cost\t\t: Rp %.2f\n",totalCost);
- printf("\nThank you for the delivery\n\n");
- }
- }while(strcasecmp(deliv->s_name,"exit")!=0);
- shmdt(&deliv);
- shmctl(h,IPC_RMID,0);
- semctl(e,0,IPC_RMID);
- }
Advertisement
Add Comment
Please, Sign In to add comment