turtle
This commit is contained in:
44
DeitelC/Chapter6/test.c
Normal file
44
DeitelC/Chapter6/test.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#define PERSON 4
|
||||
#define PRODUCT 5
|
||||
|
||||
int main (void) {
|
||||
double sales[PERSON][PRODUCT] = {0};
|
||||
int person, product;
|
||||
double amount;
|
||||
|
||||
while(1) {
|
||||
puts("Enter Sale Person (1-4)");
|
||||
puts("Wrong data will be ignored! Enter -1 to stop");
|
||||
scanf("%d", &person);
|
||||
|
||||
if (person == -1) {
|
||||
break;
|
||||
} else if (person < 1 || person > PERSON) {
|
||||
puts("Invalid salesperson number! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
puts("Enter Product (1-5) and Amount:");
|
||||
scanf("%d%lf", &product, &amount);
|
||||
|
||||
if (product < 1 || product > PRODUCT || amount < 0) {
|
||||
puts("Invalid product or amount! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
sales[person - 1][product - 1] += amount;
|
||||
}
|
||||
|
||||
printf("%10s%10s%10s%10s%10s\n", "Product", "Person1", "Person2", "Person3", "Person4");
|
||||
for (int i = 0; i < PRODUCT; i++) {
|
||||
printf("%10s%d", "Product", i + 1);
|
||||
for (int j = 0; j < PERSON; j++) {
|
||||
printf("%10.2lf", sales[j][i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
44
DeitelC/Chapter6/totalSales.c
Normal file
44
DeitelC/Chapter6/totalSales.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#define PERSON 4
|
||||
#define PRODUCT 5
|
||||
|
||||
int main (void) {
|
||||
double sales[PERSON][PRODUCT] = {0};
|
||||
int person, product;
|
||||
double amount;
|
||||
|
||||
while(1) {
|
||||
puts("Enter Sale Person (1-4)");
|
||||
puts("Wrong data will be ignored! Enter -1 to stop");
|
||||
scanf("%d", &person);
|
||||
|
||||
if (person == -1) {
|
||||
break;
|
||||
} else if (person < 1 || person > PERSON) {
|
||||
puts("Invalid salesperson number! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
puts("Enter Product (1-5) and Amount:");
|
||||
scanf("%d%lf", &product, &amount);
|
||||
|
||||
if (product < 1 || product > PRODUCT || amount < 0) {
|
||||
puts("Invalid product or amount! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
sales[person - 1][product - 1] += amount;
|
||||
}
|
||||
|
||||
printf("%10s%10s%10s%10s%10s\n", "Product", "Person1", "Person2", "Person3", "Person4");
|
||||
for (int i = 0; i < PRODUCT; i++) {
|
||||
printf("%10s%d", "Product", i + 1);
|
||||
for (int j = 0; j < PERSON; j++) {
|
||||
printf("%10.2lf", sales[j][i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
60
DeitelC/Chapter6/turtle.c
Normal file
60
DeitelC/Chapter6/turtle.c
Normal file
@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#define size 50
|
||||
|
||||
int main(void) {
|
||||
int turtle[size][size] = {0};
|
||||
int penUp = 1;
|
||||
double in = 0.00;
|
||||
int way = 0; // 0 right, 1 down, 2 left, 3 up
|
||||
int row = 0, column = 0;
|
||||
|
||||
while (1) {
|
||||
puts("Enter command between 1-9 (e.g., 5.12 for move 12 steps):");
|
||||
scanf("%lf", &in);
|
||||
|
||||
if (in == 9.00) {
|
||||
break;
|
||||
} else if (in == 1.00) {
|
||||
penUp = 1;
|
||||
} else if (in == 2.00) {
|
||||
penUp = 0;
|
||||
} else if (in == 3.00) {
|
||||
way = (way + 1) % 4; // Turn right
|
||||
} else if (in == 4.00) {
|
||||
way = (way - 1 + 4) % 4; // Turn left
|
||||
} else if (in == 6.00) {
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
for (size_t j = 0; j < size; j++) {
|
||||
printf("%c", turtle[i][j] ? '*' : ' ');
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
} else if (in >= 5.00 && in < 6.00) {
|
||||
float temp = (in - 5.00) * 100;
|
||||
int steps = (int)temp;
|
||||
|
||||
for (int i = 0; i < steps; i++) {
|
||||
if (!penUp) {
|
||||
turtle[row][column] = 1;
|
||||
}
|
||||
|
||||
switch (way) {
|
||||
case 0: // Right
|
||||
if (column < size - 1) column++;
|
||||
break;
|
||||
case 1: // Down
|
||||
if (row < size - 1) row++;
|
||||
break;
|
||||
case 2: // Left
|
||||
if (column > 0) column--;
|
||||
break;
|
||||
case 3: // Up
|
||||
if (row > 0) row--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
54
DeitelC/Chapter6/turtle0.c
Normal file
54
DeitelC/Chapter6/turtle0.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#define size 50
|
||||
|
||||
int main (void) {
|
||||
int turtle[size][size] = {0};
|
||||
int penUp = 1;
|
||||
double in = 0.00;
|
||||
int way = 0; //0 right, 1 left
|
||||
int row, column = 0;
|
||||
|
||||
puts("Enter command between 1-9 with .0!");
|
||||
scanf("%.2lf", &in);
|
||||
|
||||
while(in != 9.00) {
|
||||
puts("Enter command between 1-9 with .0!");
|
||||
scanf("%.1lf", &in);
|
||||
|
||||
if(in == 2.00) {
|
||||
penUp = 0;
|
||||
} else if (in == 6.00) {
|
||||
for(size_t i = 0; i < size; i++) {
|
||||
for(size_t j = 0; j < size; j++) {
|
||||
printf("%d\n", turtle[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
} else if (in == 3.00) {
|
||||
way = 0;
|
||||
} else if (in == 4.00) {
|
||||
way = 1;
|
||||
} else if (in > 4.00 && in < 6.00) {
|
||||
float temp = (in * 100) - 500;
|
||||
int tIn = (int) temp;
|
||||
|
||||
if(!way && penUp == 0 && tIn < 50 && column < 50) {
|
||||
for(int i = 0; i < tIn; i++) {
|
||||
turtle[row][i] = 1;
|
||||
}
|
||||
column = tIn;
|
||||
|
||||
} else if(way && penUp == 0 && tIn < 50 && row < 50) {
|
||||
for(int i = 0; i < tIn; i++) {
|
||||
turtle[i][column] = 1;
|
||||
}
|
||||
row = tIn;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user