-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicOP.cpp
164 lines (148 loc) · 3.09 KB
/
BasicOP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
float index;
float value;
struct Node *next;
};
struct queue{
struct Node *front, *rear;
};
struct queue* createQueue()
{
struct queue* q = (struct queue*)malloc(sizeof(struct queue));
q->front = q->rear = NULL;
return q;
}
struct Node* newNode(float index, float value)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->index = index;
temp->value = value;
temp->next = NULL;
return temp;
}
void display(struct queue* q) {
struct Node *temp;
if ((q->front == NULL) && (q->rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = q->front;
while (temp) {
printf("%.1f,%f\n", temp->index,temp->value);
temp = temp->next;
}
}
}
void write(struct queue* q) {
struct Node *temp;
if ((q->front == NULL) && (q->rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
temp = q->front;
FILE *fpt;
char inp[50];
printf("Enter the signal name: ");
scanf("%s",inp);
fpt = fopen(inp, "w+");
while(temp){
fprintf(fpt,"%.1f, %.2f \n",temp->index,temp->value);
temp = temp->next;
}
fclose(fpt);
}
}
void scale(struct queue* q,float scale) {
struct Node *temp;
if ((q->front == NULL) && (q->rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
temp = q->front;
while (temp) {
float tp;
tp=(float)temp->index;
tp=tp/scale;
temp->index=tp;
temp = temp->next;
}
}
}
void shift(struct queue* q,float shift) {
struct Node *temp;
if ((q->front == NULL) && (q->rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
temp = q->front;
while (temp) {
float tp;
tp = (float)temp->index;
tp=tp-shift;
temp->index = tp;
temp = temp->next;
}
}
}
void enQueue(struct queue* q, int index,float value)
{
struct Node* temp = newNode(index,value);
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
struct Node* deQueue(struct queue* q)
{
if (q->front == NULL)
return NULL;
struct Node* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
return temp;
}
int main(void) {
char inpt[50];
FILE *fp;
char str[50];
char *tok;
printf("Enter the signal: ");
scanf("%s",inpt);
fp = fopen(inpt, "r");
if (fp == NULL) {
printf( "Error reading file\n");
return 1;
}
struct queue *input = createQueue();
int c=0;
float value,indx;
while(fgets(str,50,fp)!=NULL){
tok = strtok(str,",");
int o=0;
while(tok!=NULL){
if(o%2==0)
indx=atof(tok);
if(o%2==1)
value=atof(tok);
o++;
tok=strtok(NULL,",");
}
c++;
enQueue(input,indx,value);
}
fclose(fp);
float scle, shft;
printf("Enter the scaling factor: ");
scanf("%f",&scle);
printf("Enter the shifting factor: ");
scanf("%f",&shft);
display(input);
scale(input, scle);
shift(input, shft);
display(input);
write(input);
return 0;
}