added weekly exercises of isp-ws25
This commit is contained in:
1
c/isp-ws25/weeklies/week09/quote.txt
Executable file
1
c/isp-ws25/weeklies/week09/quote.txt
Executable file
@@ -0,0 +1 @@
|
||||
The first 90 minutes are the most important
|
||||
1
c/isp-ws25/weeklies/week09/quote_copy.txt
Executable file
1
c/isp-ws25/weeklies/week09/quote_copy.txt
Executable file
@@ -0,0 +1 @@
|
||||
The first 90 minutes are the most important
|
||||
22
c/isp-ws25/weeklies/week09/sentences.txt
Executable file
22
c/isp-ws25/weeklies/week09/sentences.txt
Executable file
@@ -0,0 +1,22 @@
|
||||
A forked purple lightning bolt, on black field speckled with four-pointed stars.
|
||||
Can a man still be brave if he's afraid?
|
||||
That is the only time a man can be brave.
|
||||
What is dead may never die.
|
||||
Our Sun Shines Bright.
|
||||
As High as Honor.
|
||||
The North remembers.
|
||||
It is rare to meet a Lannister who shares my enthusiasm for dead Lannisters.
|
||||
A good act does not wash out the bad, nor a bad act the good.
|
||||
Each should have its own reward.
|
||||
Winter is coming.
|
||||
What is dead may never die.
|
||||
What is dead may never die.
|
||||
When you play the game of thrones, you win or you die.
|
||||
A good act does not wash out the bad, nor a bad act the good.
|
||||
Each should have its own reward.
|
||||
More pigeon pie, please.
|
||||
A forked purple lightning bolt, on black field speckled with four-pointed stars.
|
||||
Words are like wind.
|
||||
The wolf and the lion.
|
||||
The winds of Winter.
|
||||
It is rare to meet a Lannister who shares my enthusiasm for dead Lannisters.
|
||||
104
c/isp-ws25/weeklies/week09/w09ex01.c
Executable file
104
c/isp-ws25/weeklies/week09/w09ex01.c
Executable file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
In this task, you should get familiar with file I/O operations in C
|
||||
by writing a program that reads the text file sentences.txt and counts
|
||||
both the number of words and the number of characters (excluding line breaks).
|
||||
|
||||
Tasks:
|
||||
- Open "sentences.txt" using fopen.
|
||||
- if open fails (i.e., fopen returns NULL):
|
||||
- Print the error message "Could not open file\n"
|
||||
- return 1
|
||||
- User Input: line number that should be analyzed
|
||||
- Line numbers are 1-based (if user enters "1", analyze first line)
|
||||
- Read file (e.g., using fgetc)
|
||||
- analyze correct line
|
||||
- Count words separated by whitespace.
|
||||
- Count characters (excluding line break).
|
||||
- Close file properly after processing.
|
||||
- Print word count and character count according to example output below.
|
||||
- If entered line number is not in range line numbers of file:
|
||||
- print "Could not analyze line\n".
|
||||
|
||||
Examples:
|
||||
|
||||
Line number to analyze: 5
|
||||
Word Count: 4
|
||||
Character Count: 22
|
||||
|
||||
Line number to analyze: 1
|
||||
Word Count: 12
|
||||
Character Count: 80
|
||||
|
||||
Line number to analyze: 22
|
||||
Word Count: 14
|
||||
Character Count: 76
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Line number to analyze: ");
|
||||
int line_number;
|
||||
scanf("%d", &line_number);
|
||||
if (line_number < 1)
|
||||
{
|
||||
printf("Could not analyze line\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *file = fopen("sentences.txt", "r");
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("Could not open file\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int current_line = 1;
|
||||
int word_count = 0;
|
||||
int char_count = 0;
|
||||
int character = 0;
|
||||
|
||||
int saw_char = 0;
|
||||
|
||||
while ((character = fgetc(file)) != EOF)
|
||||
{
|
||||
if(current_line != line_number && character != '\n') continue;
|
||||
if (character == '\n')
|
||||
{
|
||||
if (current_line == line_number) break;
|
||||
current_line++;
|
||||
continue;
|
||||
}
|
||||
if (current_line == line_number)
|
||||
{
|
||||
if (character != '\n') char_count++;
|
||||
//It is rare to meet a Lannister who shares my enthusiasm for dead Lannisters.
|
||||
if (isspace(character))
|
||||
{
|
||||
saw_char = 0;
|
||||
}
|
||||
else if (isgraph(character) && !saw_char)
|
||||
{
|
||||
word_count++;
|
||||
saw_char = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (current_line < line_number)
|
||||
{
|
||||
printf("Could not analyze line\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Word Count: %d\n", word_count);
|
||||
printf("Character Count: %d\n", char_count);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return 0;
|
||||
}
|
||||
77
c/isp-ws25/weeklies/week09/w09ex02.c
Executable file
77
c/isp-ws25/weeklies/week09/w09ex02.c
Executable file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
In this task, you should get familiar with file manipulation in C
|
||||
by performing operations like reading the file content,
|
||||
moving the file cursor, and writing the predefined string
|
||||
"Halftime is for the real fans" to a specific position in a file.
|
||||
|
||||
Your tasks are:
|
||||
- Open file quote.txt in read/write mode
|
||||
if fopen == NULL
|
||||
- Print "Could not open file\n"
|
||||
- return 1
|
||||
- Print original content of file to console
|
||||
- Ask user to enter a position to insert a predefined string
|
||||
- move cursor to the specified position
|
||||
if cannot be moved
|
||||
- Print "Could not move the file cursor\n"
|
||||
- return 1
|
||||
- overwrite existing content (i.e. use fseek to move the cursor)
|
||||
- Write the predefined string into the file using fputs
|
||||
- Rewind the file to the beginning and print the updated content of the file to the console
|
||||
- Close the file properly after processing.
|
||||
|
||||
Examples:
|
||||
|
||||
Original file content: The first 90 minutes are the most important
|
||||
Position to insert the string: 4
|
||||
Updated file content: The Halftime is for the real fans important
|
||||
|
||||
Original file content: The first 90 minutes are the most important
|
||||
Position to insert the string: -5
|
||||
Could not move the file cursor
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
FILE *file = fopen("quote.txt", "r+");
|
||||
if (NULL == file) {
|
||||
printf("Could not open file\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Original file content: ");
|
||||
int character = 0;
|
||||
while ((character = fgetc(file)) != EOF)
|
||||
{
|
||||
printf("%c", character);
|
||||
}
|
||||
printf("\n");
|
||||
rewind(file);
|
||||
|
||||
long insert_position = 0;
|
||||
printf("Position to insert the string: ");
|
||||
scanf("%ld", &insert_position);
|
||||
|
||||
if (fseek(file, insert_position, SEEK_CUR))
|
||||
{
|
||||
printf("Could not move the file cursor\n");
|
||||
fclose(file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* text_to_insert = "Halftime is for the real fans";
|
||||
fputs(text_to_insert, file);
|
||||
rewind(file);
|
||||
|
||||
printf("Updated file content: ");
|
||||
while ((character = fgetc(file)) != EOF)
|
||||
{
|
||||
printf("%c", character);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
fclose(file);
|
||||
return 0;
|
||||
}
|
||||
136
c/isp-ws25/weeklies/week09/w09ex03.c
Executable file
136
c/isp-ws25/weeklies/week09/w09ex03.c
Executable file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
In this task, you should get familiar with enum.
|
||||
The program allows the user to enter ratings for a movie
|
||||
using integers that should correspond to an enum.
|
||||
Only ratings between 1 and 5 are considered.
|
||||
|
||||
The program processes the ratings,
|
||||
prints the average rating using stars (i.e., *) and
|
||||
prints a recommendation based on the average ratings.
|
||||
|
||||
Your task is:
|
||||
|
||||
- Declare a typedef’d enum for the 5 categories of the rating system
|
||||
- 1 == worst rating
|
||||
- 5 == best rating
|
||||
- all ratings are entered by the user
|
||||
- calculate average rating using total_ratings and weighted_ratings
|
||||
- total_ratings == total number of ratings entered by the user
|
||||
- weighted_ratings == ratings weighted by their category
|
||||
- ratings in 1st category: weighted with 1
|
||||
- ratings in 2nd category: weighted with 3
|
||||
|
||||
This is necessary to calculate the average rating by dividing weighted_ratings by total_ratings
|
||||
Use your typedef’d enum as data type of the variable in which you store the average ratings
|
||||
Use an integer division to calculate the average rating
|
||||
(e.g., an average rating of 3.84 corresponds to the 3rd category).
|
||||
|
||||
Print the average rating using stars.
|
||||
For instance, if the average rating of the movie is in the
|
||||
3rd category of the rating system, then print Average rating: ***.
|
||||
|
||||
Print a recommendation based on the rating category of the movie’s average rating:
|
||||
- 1st category: The movie is terrible, do not watch it
|
||||
- 2nd category: The movie is bad and not worth watching
|
||||
- 3rd category: The movie is average, neither especially good nor bad
|
||||
- 4th category: The movie is good and worth watching
|
||||
- 5th category: The movie is excellent and a must-see
|
||||
- Print "Invalid rating for the movie" if none of the above cases apply.
|
||||
|
||||
|
||||
Examples:
|
||||
|
||||
Enter movie ratings (1 = TERRIBLE, 2 = BAD, 3 = AVERAGE, 4 = GOOD, 5 = EXCELLENT). End with -1:
|
||||
3 4 3 3 2 3 5 4 4 3 3 -1
|
||||
Average rating: ***
|
||||
Recommendation: The movie is average, neither especially good nor bad
|
||||
|
||||
|
||||
Enter movie ratings (1 = TERRIBLE, 2 = BAD, 3 = AVERAGE, 4 = GOOD, 5 = EXCELLENT). End with -1:
|
||||
5 4 3 4 5 4 3 4 5 4 3 5 5 5 5 5 3 3 3 -1
|
||||
Average rating: ****
|
||||
Recommendation: The movie is good and worth watching
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_RATING 5
|
||||
|
||||
// TODO: Declare a (typedef'd) enum for a movie rating scale that consists of 5 categories
|
||||
|
||||
typedef enum _Ratings_ {
|
||||
TERRIBLE = 1,
|
||||
BAD,
|
||||
AVERAGE,
|
||||
GOOD,
|
||||
EXCELLENT
|
||||
} Ratings;
|
||||
|
||||
void giveRecommendation(Ratings rating)
|
||||
{
|
||||
printf("Recommendation: ");
|
||||
switch (rating) {
|
||||
case TERRIBLE:
|
||||
printf("The movie is terrible, do not watch it\n");
|
||||
break;
|
||||
case BAD:
|
||||
printf("The movie is bad and not worth watching\n");
|
||||
break;
|
||||
case AVERAGE:
|
||||
printf("The movie is average, neither especially good nor bad\n");
|
||||
break;
|
||||
case GOOD:
|
||||
printf("The movie is good and worth watching\n");
|
||||
break;
|
||||
case EXCELLENT:
|
||||
printf("The movie is excellent and a must-see\n");
|
||||
break;
|
||||
default:
|
||||
printf("Invalid rating for the movie\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Enter movie ratings (1 = TERRIBLE, 2 = BAD, 3 = AVERAGE, 4 = GOOD, 5 = EXCELLENT). End with -1:\n");
|
||||
|
||||
int input = 0;
|
||||
int ratings[MAX_RATING] = {0};
|
||||
while (1)
|
||||
{
|
||||
scanf("%d", &input);
|
||||
if (input == -1) break;
|
||||
|
||||
if (input > 0 && input <= MAX_RATING)
|
||||
{
|
||||
ratings[--input]++;
|
||||
}
|
||||
}
|
||||
|
||||
int total_ratings = 0;
|
||||
int weighted_ratings = 0;
|
||||
for (int i = 0; i < sizeof(ratings)/sizeof(ratings[0]); i++)
|
||||
{
|
||||
total_ratings += ratings[i];
|
||||
weighted_ratings += (ratings[i] * (i+1));
|
||||
}
|
||||
|
||||
if (total_ratings == 0)
|
||||
{
|
||||
printf("No ratings entered\n");
|
||||
return 1;
|
||||
}
|
||||
// TODO: Store the average rating in a variable. The data type of the variable should be your typedef'd enum
|
||||
Ratings average_rating = weighted_ratings / total_ratings;
|
||||
|
||||
// TODO: Print the average rating by displaying stars
|
||||
printf("Average rating: ");
|
||||
for(int i = 0; i < average_rating; i++) printf("%c", '*');
|
||||
printf("\n");
|
||||
|
||||
// TODO: Print the recommendation
|
||||
giveRecommendation(average_rating);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user