Skip to content

Commit

Permalink
daily upload 11.21
Browse files Browse the repository at this point in the history
  • Loading branch information
TGSpock123 committed Jan 21, 2024
1 parent 23594cb commit 9186b78
Show file tree
Hide file tree
Showing 32 changed files with 461 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .idea/dictionaries/tgspock.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions 11/11/11.22.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "s_gets.h"
int main (void)
{
char ch[20], ch2[20];
char ch[5], ch2[5];

printf ("Please enter an array. \n");
s_gets (ch, 20);
s_gets (ch, 5);
printf ("Please enter another array. \n");
s_gets (ch2, 20);
s_gets (ch2, 5);
printf ("strcmp (\"%s\" and \"%s\") is %d. \n", ch, ch2, strcmp (ch, ch2));

return 0;
Expand Down
19 changes: 19 additions & 0 deletions 11/11/11.23.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "s_gets.h"
#define SIZE 80
#define LIM 10
#define STOP "quit"
int main (void)
{
char input[LIM][SIZE];
int ct = 0;

printf ("Enter up to %d lines (type quit to quit): \n", LIM);
while (ct < LIM && s_gets (input[ct], SIZE) != NULL &&
strcmp (input[ct], STOP) != 0 && input[ct][0] != '\0')
{
ct ++;
}
printf ("%d strings entered. \n", ct);

return 0;
}
29 changes: 29 additions & 0 deletions 11/11/11.24.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <string.h>
#define LISTSIZE 6
int main (void)
{
const char * list[LISTSIZE] =
{
"astronomy",
"astounding",
"astrophysics",
"ostracize",
"asterism",
"astrophobia"
};
int count = 0;
int i;

for (i = 0; i < LISTSIZE; i++)
{
if (strncmp (list[i], "astr", 4) == 0)
{
printf ("Found: %s.\n", list[i]);
count ++;
}
}
printf ("The list contained %d words beginning with astr. \n", count);

return 0;
}
30 changes: 30 additions & 0 deletions 11/11/11.25.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "s_gets.h"
#define SIZE 40
#define LIM 5
int main (void)
{
char qwords[LIM][SIZE];
char temp[SIZE];
int i = 0;

printf ("Enter %d words beginning with q: \n", LIM);
while (i < LIM && s_gets(temp, SIZE))
{
if (temp[0] != 'q')
{
printf ("%s doesn't begin with q! \n", temp);
}else
{
strcpy (qwords[i], temp);
i ++;
}
}
puts ("\nHere are the words accepted: ");
for (i = 0; i < LIM; i ++)
{
puts (qwords[i]);
}

return 0;
}
//后面是被复制字符串, 把后面的放进前面的, 参考 mov ax, 4
16 changes: 16 additions & 0 deletions 11/11/11.26.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <string.h>
#define WORD "beast"
#define SIZE 40
int main (void)
{
const char * orig = WORD;
char copy[SIZE] = "Be the best you can be.";
char * ps;

puts (copy);
ps = strcpy (copy + 7, orig);
puts (copy);

return 0;
}
31 changes: 31 additions & 0 deletions 11/11/11.27.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "s_gets.h"
#define SIZE 40
#define TARGETSIZE 7
#define LIM 5
int main (void)
{
char qwords[LIM][TARGETSIZE];
char temp[SIZE];
int i = 0;

printf ("Enter %d words beginning with q: \n", LIM);
while (i < LIM && s_gets(temp, SIZE))
{
if (temp[0] != 'q')
{
printf ("%s doesn't begging with 'q'! \n", temp);
}else
{
strncpy (qwords[i], temp, TARGETSIZE - 1);
qwords[i][TARGETSIZE - 1] = '\0';
i ++;
}
}
puts ("Here are the words accepted:");
for (i = 0; i < LIM; i ++)
{
puts (qwords[i]);
}

return 0;
}
20 changes: 20 additions & 0 deletions 11/11/11.28.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "s_gets.h"
#define MAX 20
int main (void)
{
char first[MAX];
char last[MAX];
char formal[2 * MAX + 10];
double prize;

puts ("Enter your first name: ");
s_gets (first, MAX);
puts ("Enter your last name: ");
s_gets (last, MAX);
puts ("Enter your prize money: ");
scanf ("%lf", &prize);
sprintf (formal, "%s %-19s: $%6.2f\n",first, last, prize);
puts (formal);

return 0;
}
114 changes: 114 additions & 0 deletions 11/11/11.29.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//test
#include "s_gets.h"
int main (void)
{
char ch1[40], ch2[40], q, *qwe;
int i = 5;

printf ("please enter ch1, strcpy wil make ch2: \n");
s_gets (ch1, 40);
printf ("1. %s\n", ch1);
strcpy (ch2, ch1);
printf ("1. %s\n", ch2);

printf ("please enter ch1, strncpy wil make ch2: \n");
s_gets (ch1, 40);
printf ("2. %s\n",ch1);
strncpy (ch2, ch1, i);
ch2[i] = '\0';
printf ("2. %s\n", ch2);

printf ("this test is for strcat, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter ch2: \n");
s_gets (ch2, 40);
strcat (ch1, ch2);
printf ("3. %s\n", ch1);

printf ("this is for strncat, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter ch2: \n");
s_gets (ch2, 40);
strncat (ch1, ch2, 40 - 1 - strlen(ch1));
printf ("4. %s\n", ch1);

printf ("this test is for strcmp, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter ch2: \n");
s_gets (ch2, 40);
printf ("5. %d\n", strcmp (ch1, ch2));

printf ("this is for strncmp, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter ch2: \n");
s_gets (ch2, 40);
printf ("6. %d\n", strncmp (ch1, ch2, i));

//书上这段错了, 具体看示例代码
printf ("this test is for strchr, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter a charater: \n");
q = getchar();
getchar();
if (!strchr (ch1, q))
{
printf ("7. there is no %c in ch1: %s\n", q, ch1);
}else
{
qwe = strchr (ch1, q);
printf ("7. %c is in ch1: %s, after %c is %s\n", q, ch1, q, qwe + 1);
}

//这段也错了
printf ("this test is for strpbrk, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter ch2: \n");
s_gets (ch2, 40);
if (!strpbrk (ch1, ch2))
{
printf ("8. there is no element of ch2: %s in ch1: %s\n", ch2, ch1);
}else
{
qwe = strpbrk (ch1, ch2);
printf ("8. there is at least one element"
"of ch2: %s is in ch1: %s \nafter the first match is %s\n", ch2, ch1, qwe + 1);
while (!strpbrk (qwe + 1, ch2))
{
qwe = strpbrk (qwe + 1, ch2);
printf ("after next match is %s\n", qwe + 1);
}
}

printf ("this test is for strrchr, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter a charater: \n");
q = getchar();
getchar();
if (!strrchr (ch1, q))
{
printf ("9. there is no %c in ch1: %s\n", q, ch1);
}else
{
qwe = strrchr (ch1, q);
printf ("9. %c is in ch1: %s, after %c' last match is %s\n", q, ch1, q, qwe + 1);
}

printf ("this test is for strstr, please enter ch1: \n");
s_gets (ch1, 40);
printf ("please enter ch2: \n");
s_gets (ch2, 40);
if (!strstr (ch1, ch2))
{
printf ("10. ch2: %s is not in ch1: %s\n", ch2, ch1);
}else
{
qwe = strstr (ch1, ch2);
printf ("10. ch2: %s is in ch1: %s, after ch2 is %s\n", ch2, ch1, qwe + strlen(ch2));
}

printf ("this test is for strlen, please enter ch1: \n");
s_gets (ch1, 40);
printf ("the length of ch1: %s is %lu\n", ch1, strlen (ch1));

return 0;
}
47 changes: 47 additions & 0 deletions 11/11/11.30.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "s_gets.h"
#define SIZE 81
#define LIM 20
#define HALT ""

void stsrt (char *strings[], int num)
{
char *temp;
int top, seek;

for (top = 0; top < num - 1; top ++)
{
for (seek = top + 1; seek < num; seek ++)
{
if (strcmp (strings[top], strings[seek]) > 0)
{
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}
}
}

int main (void)
{
char input[LIM][SIZE];
char *ptstr[LIM];
int ct = 0;
int k;

printf ("Input up to %d lines, and I will sort em. \n", LIM);
printf ("Press enter in an empty line to quit. \n");
while (ct < LIM && s_gets(input[ct], SIZE) && input[ct][0])
{
ptstr[ct] = input[ct];
ct ++;
}
stsrt (ptstr, ct);
puts ("\nHere's the sort list: \n");
for (k = 0; k < ct; k ++)
{
puts (ptstr[k]);
}

return 0;
}
17 changes: 5 additions & 12 deletions 11/11/s_gets.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
#include "s_gets.h"
char * s_gets (char *st, int n)
{
char * ret_val;
int i = 0;
char * ret_val, * find;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while ((st[i] != '\n') && (st[i]))
if (strchr (ret_val, '\n'))
{
i ++;
}
if (st[i] == '\n')
{
st[i] = '\0';
find = strchr (ret_val, '\n');
*find = '\0';
}else
{
while ((getchar() == '\n') == 0)
{
continue;
}
while ((getchar() == '\n') == 0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ set(CMAKE_CXX_STANDARD 14)
include_directories(9)

add_executable(C_primer
11/11/11.22.c 11/11/s_gets.c
11/11/11.30.c 11/11/s_gets.c
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
"directoryIndex" : 0,
"id" : "C_primer::@6890427a1f51a3e7e1df",
"jsonFile" : "target-C_primer-Debug-0a97314528f9f09bd539.json",
"jsonFile" : "target-C_primer-Debug-c6656e18f983ae479999.json",
"name" : "C_primer",
"projectIndex" : 0
}
Expand Down
Loading

0 comments on commit 9186b78

Please sign in to comment.