Commit fe2f2f3fa645cb030f0b6d8c012f94dc79af2db2

Authored by Anton Qiu
1 parent acf223478c
Exists in master

HW4 Finished

Showing 8 changed files with 125 additions and 0 deletions Side-by-side Diff

HW4/compress.c View file @ fe2f2f3
  1 +#include <stdio.h>
  2 +//This program gets a string from the user, removes the space characters as
  3 +// well as vowels and prints the compressed string.
  4 +
  5 +int main() {
  6 +
  7 + char current;
  8 +
  9 + printf("String: ");
  10 + while ((current=getchar()) != '\n') {
  11 + if (current!='a' && current!='e' && current!='i'
  12 + && current!='o' && current!= 'u'
  13 + && current!= ' ' && current != '\t')
  14 + putchar(current);
  15 + }
  16 + putchar('\n');
  17 + return 0;
  18 +}
HW4/compress.sol View file @ fe2f2f3

No preview for this file type

  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <time.h>
  4 +
  5 +int main(){
  6 +
  7 + srand(time(NULL));
  8 + int target = rand() % 20 + 1;
  9 + int guess, number_ties=0;
  10 + printf("Guess my number: ");
  11 +target = 14;
  12 + do {
  13 + scanf("%d", &guess);
  14 + number_ties++;
  15 +
  16 + if (guess < target-4)
  17 + printf("Your guess is too low, guess again: ");
  18 + else if (guess < target)
  19 + printf("Your guess is low, guess again: ");
  20 + else if (guess > target+4)
  21 + printf("Your guess is too high, guess again: ");
  22 + else if (guess > target)
  23 + printf("Your guess is high, guess again: ");
  24 + } while (guess != target);
  25 +
  26 + printf("You guessed correctly in %d tries, congratulations!\n",
  27 + number_ties);
  28 +
  29 + return 0;
  30 +}
HW4/guess.sol View file @ fe2f2f3

No preview for this file type

HW4/tennis.c View file @ fe2f2f3
  1 +#include <stdio.h>
  2 +//This program gets the score of a tennis match and announce it as in
  3 +//the commentaries, e.g. 40-30 is forty thirty, and 40-40 is deuce, 15-0 is
  4 +// fifteen love.
  5 +
  6 +int main(){
  7 +
  8 + int score1, score2;
  9 + printf("Result: ");
  10 + scanf("%d-%d", &score1, &score2);
  11 +
  12 + if (score1 == score2){
  13 + switch (score1) {
  14 + case 0 :
  15 + printf("Announcement: love all\n");
  16 + break;
  17 + case 1 :
  18 + printf("Announcement: fifteen all\n");
  19 + break;
  20 + case 2 :
  21 + printf("Announcement: thirty all\n");
  22 + break;
  23 + case 3 :
  24 + printf("Announcement: deuce\n");
  25 + break;
  26 + }
  27 + } else {
  28 + switch (score1) {
  29 + case 0 :
  30 + printf("Announcement: love ");
  31 + break;
  32 + case 1 :
  33 + printf("Announcement: fifteen ");
  34 + break;
  35 + case 2 :
  36 + printf("Announcement: thirty ");
  37 + break;
  38 + case 3 :
  39 + printf("Announcement: forty ");
  40 + break;
  41 + }
  42 + switch (score2) {
  43 + case 0 :
  44 + printf("love\n");
  45 + break;
  46 + case 1 :
  47 + printf("fifteen\n");
  48 + break;
  49 + case 2 :
  50 + printf("thirty\n");
  51 + break;
  52 + case 3 :
  53 + printf("forty\n");
  54 + break;
  55 + }
  56 +
  57 + }
  58 + return 0;
  59 +
  60 +}
HW4/tennis.sol View file @ fe2f2f3

No preview for this file type

HW4/triangle.c View file @ fe2f2f3
  1 +#include <stdio.h>
  2 +//This program gets three double numbers from the user and
  3 +// declares whether we can have a triangle with these lengths or not.
  4 +
  5 +int main(){
  6 +
  7 + double a, b, c;
  8 + printf("Three lengths: ");
  9 + scanf("%lf %lf %lf", &a, &b, &c);
  10 +
  11 + if (a+b<=c || a+c<=b || b+c<=a)
  12 + printf("Triangle is impossible!\n");
  13 + else
  14 + printf("Triangle is possible!\n");
  15 +
  16 + return 0;
  17 +}
HW4/triangle.sol View file @ fe2f2f3

No preview for this file type