Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoHN committed Apr 23, 2024
1 parent 7a5d57f commit a690b8d
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions trivial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_subdirectory(leetcode)
add_subdirectory(json)
add_subdirectory(thread_pool)
add_subdirectory(single)
add_subdirectory(cprogram)

if(UNIX)
add_subdirectory(coroutine)
Expand Down
7 changes: 7 additions & 0 deletions trivial/cprogram/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_BASE}/cprogram)

link_libraries(m)

add_executable(reverse_words reverse_words.c)
add_executable(min_common_value min_common_value.c)
add_executable(bookshop bookshop.c)
3 changes: 3 additions & 0 deletions trivial/cprogram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# C Program

《C 语言程序设计》课程相关题目
71 changes: 71 additions & 0 deletions trivial/cprogram/bookshop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdio.h>
#include <string.h>

struct Book {
int id;
char title[50];
char author[50];
int price;
};

struct BookShop {
char name[32];
struct Book books[100];
int nbooks;
};

int main(int argc, char const* argv[]) {
struct BookShop shops[10];
int nshops;
scanf("%d", &nshops);
for (int i = 0; i < nshops; i++) {
struct BookShop* shop = &shops[i];
scanf("%s", shop->name);
scanf("%d", &shop->nbooks);
for (int j = 0; j < shop->nbooks; j++) {
struct Book* book = &shop->books[j];
scanf("%d", &book->id);
scanf("%s", book->title);
scanf("%s", book->author);
scanf("%d", &book->price);
}
}

// input an author name, find all the books written by the author, and print the
// shop name, book title, and price of each book
char author[50];
scanf("%s", author);
for (int i = 0; i < nshops; i++) {
struct BookShop* shop = &shops[i];
for (int j = 0; j < shop->nbooks; j++) {
struct Book* book = &shop->books[j];
if (strcmp(book->author, author) == 0) {
printf("%s %s %d\n", shop->name, book->title, book->price);
}
}
}

return 0;
}

/*give an example input
3
shop1
3
1 book1 author1 105
2 book2 author1 520
3 book3 author2 200
shop2
2
4 book4 author1 350
5 book5 author2 240
shop3
4
6 book6 author3 230
7 book7 author4 380
8 book8 author1 105
9 book9 author5 260
author1
*/
41 changes: 41 additions & 0 deletions trivial/cprogram/min_common_value.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#define INT_MAX 1000000

int main(int argc, char const* argv[]) {
int n1, n2;
int arr1[50], arr2[50];

scanf("%d %d", &n1, &n2);
for (int i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
for (int i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}

int* ptr1 = arr1;
int* ptr2 = arr2;

int min = INT_MAX;

while (ptr1 < arr1 + n1) {
while (ptr2 < arr2 + n2) {
if (*ptr1 == *ptr2) {
if (*ptr1 < min) {
min = *ptr1;
}
}
ptr2++;
}
ptr2 = arr2;
ptr1++;
}

if (min == INT_MAX) {
printf("-1\n");
} else {
printf("%d\n", min);
}

return 0;
}
68 changes: 68 additions & 0 deletions trivial/cprogram/reverse_words.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Reverse the words in a string using pointer
*
* Example:
* Input: "Hello World"
* Output: "World Hello"
*/
#include <stdio.h>
#include <string.h>

void simplify(char* str) {
char* p1 = str;
char* p2 = str;
int n = 0;

while (*p2) {
if (*p2 == ' ' && (n == 0 || *(p2 + 1) == ' ' || *(p2 + 1) == '\0')) {
p2++;
} else {
*p1 = *p2;
p1++;
p2++;
n++;
}
}

*p1 = '\0';
}

void reverse(char* start, char* end) {
char temp;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}

int main(int argc, char const* argv[]) {
char str[100];
scanf("%[^\n]", str);

simplify(str);

reverse(str, str + strlen(str) - 1);

char* start = str;
char* end = str;

while (*end) {
if (*(end + 1) == '\0') {
reverse(start, end);
} else if (*end == ' ') {
reverse(start, end - 1);
while (*(end + 1) && *(end + 1) == ' ') {
end++;
}
start = end + 1;
}
end++;
}

printf("%s\n", str);

return 0;
}

0 comments on commit a690b8d

Please sign in to comment.