Commit acf223478c754312d9c3c3ad715eaa6c0181cb04

Authored by Anton Qiu
1 parent d11243fa23
Exists in master

De-conditionals for P2-4

Showing 6 changed files with 27 additions and 3 deletions Inline Diff

HW3/multiples.c View file @ acf2234
#include <stdio.h> 1 1 #include <stdio.h>
2 2
int main() { 3 3 int main() {
int i[6]; 4 4 int i[6];
int d3 = 0, 5 5 int d3 = 0,
d7 = 0, 6 6 d7 = 0,
d21 = 0; 7 7 d21 = 0;
printf("Six integers: "); 8 8 printf("Six integers: ");
scanf("%d %d %d %d %d %d", i, i+1, i+2, i+3, i+4, i+5); 9 9 scanf("%d %d %d %d %d %d", i, i+1, i+2, i+3, i+4, i+5);
for (int j = 0; j < 6; j++) { 10 10 for (int j = 0; j < 6; j++) {
int each = i[j]; 11 11 int each = i[j];
12 d3 += (each%3 == 0);
13 d7 += (each%7 == 0);
14 d21 += (each%21 == 0);
15 /*
if (each % 21 == 0) { 12 16 if (each % 21 == 0) {
d3++; d7++; d21++; 13 17 d3++; d7++; d21++;
} 14 18 }
else if (each % 3 == 0) { 15 19 else if (each % 3 == 0) {
d3++; 16 20 d3++;
} 17 21 }
else if (each % 7 == 0) { 18 22 else if (each % 7 == 0) {
d7++; 19 23 d7++;
} 20 24 }
25 */
} 21 26 }
/* 22 27 /*
d3 = 6-(i[0]%3+i[1]%3+i[2]%3+i[3]%3+i[4]%3+i[5]%3); 23 28
d7 = 6-(i[0]%7+i[1]%7+i[2]%7+i[3]%7+i[4]%7+i[5]%7); 24 29 d7 = 6-(i[0]%7+i[1]%7+i[2]%7+i[3]%7+i[4]%7+i[5]%7);
d21 = 6-(i[0]%21+i[1]%21+i[2]%21+i[3]%21+i[4]%21+i[5]%21); 25 30 d21 = 6-(i[0]%21+i[1]%21+i[2]%21+i[3]%21+i[4]%21+i[5]%21);
*/ 26 31 */
printf("%d divisible by 3,\n%d divisible by 7,\n%d divisible by both.\n", d3, d7, d21); 27 32 printf("%d divisible by 3,\n%d divisible by 7,\n%d divisible by both.\n", d3, d7, d21);
return 0; 28 33 return 0;
} 29 34 }
HW3/multiples.out View file @ acf2234

No preview for this file type

HW3/rating.c View file @ acf2234
#include <stdbool.h> 1 1 //#include <stdbool.h>
#include <stdio.h> 2 2 #include <stdio.h>
3 3
int main() { 4 4 int main() {
char rate; 5 5 char rate;
int age = 0; 6 6 int age = 0;
bool suit = false; 7 7 //bool suit = false;
8 8
printf("Enter the movie rating (G, P, or R): "); 9 9 printf("Enter the movie rating (G, P, or R): ");
scanf("%c", &rate); 10 10 scanf("%c", &rate);
printf("How old are you? "); 11 11 printf("How old are you? ");
scanf("%d", &age); 12 12 scanf("%d", &age);
13 13
14 printf("%s\n", (rate=='G' || (rate=='P' && age>=13) ||
15 (rate=='R' && age >= 17)) ? "Suitable movie. Enjoy!" :
16 "Unsuitable movie. Enjoy the review!");
17
18 /*
if (rate == 'G') { 14 19 if (rate == 'G') {
suit = true; 15 20 suit = true;
} 16 21 }
else if (rate == 'P' && age >= 13) { 17 22 else if (rate == 'P' && age >= 13) {
suit = true; 18 23 suit = true;
} 19 24 }
else if (rate == 'R' && age >= 17) { 20 25 else if (rate == 'R' && age >= 17) {
suit = true; 21 26 suit = true;
} 22 27 }
else { 23 28 else {
suit = false; 24 29 suit = false;
} 25 30 }
26 31
if (suit) 27 32 if (suit)
printf("Suitable movie. Enjoy!\n"); 28 33 printf("Suitable movie. Enjoy!\n");
else 29 34 else
printf("Unsuitable movie. Enjoy the review!\n"); 30 35 printf("Unsuitable movie. Enjoy the review!\n");
36 */
HW3/rating.out View file @ acf2234

No preview for this file type

#include <stdio.h> 1 1 #include <stdio.h>
#define BB1 43750 2 2 #define BB1 43750
#define BB2 97750 3 3 #define BB2 97750
#define BT1 0.1 4 4 #define BT1 0.1
#define BT2 0.25 5 5 #define BT2 0.25
#define BT3 0.28 6 6 #define BT3 0.28
#define TB1 25000 7 7 #define TB1 25000
#define TB2 50000 8 8 #define TB2 50000
#define TB3 150000 9 9 #define TB3 150000
#define TT1 0.0 10 10 #define TT1 0.0
#define TT2 0.1 11 11 #define TT2 0.1
#define TT3 0.2 12 12 #define TT3 0.2
#define TT4 0.25 13 13 #define TT4 0.25
14 14
15 15
int main() { 16 16 int main() {
int salary = 0; 17 17 int salary = 0;
double t_tax = 0.0, 18 18 double t_tax = 0.0,
b_tax = 0.0; 19 19 b_tax = 0.0;
printf("Salary in dollars: "); 20 20 printf("Salary in dollars: ");
scanf("%d", &salary); 21 21 scanf("%d", &salary);
22
23 /*
if (salary >= 0 && salary <= TB1) { 22 24 if (salary >= 0 && salary <= TB1) {
t_tax = salary * TT1; 23 25 t_tax = salary * TT1;
} 24 26 }
else if (salary <= TB2) { 25 27 else if (salary <= TB2) {
t_tax = TB1 * TT1 + (salary-TB1) * TT2; 26 28 t_tax = TB1 * TT1 + (salary-TB1) * TT2;
} 27 29 }
else if (salary <= TB3) { 28 30 else if (salary <= TB3) {
t_tax = TB1 * TT1 + (TB2 - TB1) * TT2 + (salary - TB2) * TT3; 29 31 t_tax = TB1 * TT1 + (TB2 - TB1) * TT2 + (salary - TB2) * TT3;
} 30 32 }
else { 31 33 else {
t_tax = TB1 * TT1 + (TB2 - TB1) * TT2 + (TB3 - TB2) * TT3 + (salary - TB3) * TT4; 32 34 t_tax = TB1 * TT1 + (TB2 - TB1) * TT2 + (TB3 - TB2) * TT3 + (salary - TB3) * TT4;
} 33 35 }
printf("Make America great again! Tax amount: $%.2lf\n",t_tax); 34 36 printf("Make America great again! Tax amount: $%.2lf\n",t_tax);
35 37
if (salary >= 0 && salary <= BB1) { 36 38 if (salary >= 0 && salary <= BB1) {
b_tax = salary * BT1; 37 39 b_tax = salary * BT1;
} 38 40 }
else if (salary <= BB2) { 39 41 else if (salary <= BB2) {
b_tax = BB1 * BT1 + (salary - BB1) * BT2; 40 42 b_tax = BB1 * BT1 + (salary - BB1) * BT2;
} 41 43 }
else { 42 44 else {
b_tax = BB1 * BT1 + (BB2 - BB1) * BT2 + (salary - BB2) * BT3; 43 45 b_tax = BB1 * BT1 + (BB2 - BB1) * BT2 + (salary - BB2) * BT3;
} 44 46 }
47 */
48
49 t_tax = (salary>TB1 && salary <= TB2)*(salary-TB1)*TT2 + (salary >
50 TB2)*(TB2-TB1)*TT2 + (salary>TB2 &&
51 salary<=TB3)*(salary-TB2)*TT3 + (salary>TB3)*((TB3-TB2)*TT3
52 + (salary-TB3)*TT4);
53 printf("Make America great again! Tax amount: $%.2lf\n",t_tax);
54 b_tax = (salary>=0 && salary<=BB1)*(salary*BT1) +
55 (salary>BB1)*(BB1*BT1) + (salary>BB1 &&
56 salary<=BB2)*(salary-BB1)*BT2 + (salary>BB2)*((BB2-BB1)*(BT2)
57 + (salary-BB2)*BT3);

No preview for this file type