完成练习15
This commit is contained in:
parent
330c192cd7
commit
c47627dcab
Binary file not shown.
|
@ -0,0 +1,65 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("ERROR: You need one argument.\n");
|
||||
|
||||
// this is how you abort a program
|
||||
return 1;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
char *cur_argv = *(argv + 1);
|
||||
|
||||
|
||||
|
||||
for (i = 0; *(cur_argv + i) != '\0'; ++i)
|
||||
{
|
||||
// char letter = argv[1][i];
|
||||
char letter = *(cur_argv + i);
|
||||
|
||||
switch (letter)
|
||||
{
|
||||
case 'a':
|
||||
case 'A':
|
||||
printf("%d: 'A'\n", i);
|
||||
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
printf("%d: 'E'\n", i);
|
||||
|
||||
break;
|
||||
case 'i':
|
||||
case 'I':
|
||||
printf("%d: 'I'\n", i);
|
||||
|
||||
break;
|
||||
case 'o':
|
||||
case 'O':
|
||||
printf("%d: 'O'\n", i);
|
||||
|
||||
break;
|
||||
case 'u':
|
||||
case 'U':
|
||||
printf("%d: 'U'\n", i);
|
||||
|
||||
break;
|
||||
case 'y':
|
||||
case 'Y':
|
||||
if (i > 2)
|
||||
{
|
||||
// it's only sometimes y
|
||||
printf("%d: 'Y'\n", i);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
printf("%d: %c is not a vowel\n", i, letter);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
http://ewm.ptpress.com.cn:8085/preview?qrCode=qr2018001488
|
56
ex15/ex15.c
56
ex15/ex15.c
|
@ -1,31 +1,58 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int my_print(char *names[], int credits[], int n)
|
||||
{
|
||||
for (int i = 0; i < n ;++i)
|
||||
{
|
||||
printf("%s%s\n%s%d\n", "name: ", names[i], "credits: ", credits[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// create two arrays we care about
|
||||
int ages[] = {23, 43, 12, 89, 2};
|
||||
int credits[] = {23, 43, 12, 89, 2};
|
||||
char *names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};
|
||||
|
||||
// safely get the size of ages
|
||||
int count = sizeof(ages) / sizeof(int);
|
||||
// safely get the size of credits
|
||||
int count = sizeof(credits) / sizeof(int);
|
||||
int i = 0;
|
||||
|
||||
// first way using indexing
|
||||
for (i = 0; i < count; ++i)
|
||||
// for (i = count - 1; i >= 0; --i)
|
||||
// {
|
||||
// printf("%s gets %d credits.\n", names[i], credits[i]);
|
||||
// }
|
||||
// my_print(names, credits, count);
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
printf("%s has %d years alive.\n", names[i], ages[i]);
|
||||
printf("%s%s\n%s%d\n", "names: ", names[i], "credits: ", credits[i]);
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// set up the pointers to the start of the arrays
|
||||
int *cur_age = ages;
|
||||
// int *cur_credits = credits;
|
||||
int *cur_credits = credits;
|
||||
char **cur_name = names;
|
||||
|
||||
// second way using pointers
|
||||
for (i = 0; i < count; ++i)
|
||||
// for (i = 0; i < count; ++i)
|
||||
// {
|
||||
// printf("%s gets %d credits.\n", *(cur_name + i), *(cur_credits + i));
|
||||
// }
|
||||
// my_print(cur_name, cur_credits, count);
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
printf("%s is %d years old.\n", *(cur_name + i), *(cur_age + i));
|
||||
printf("%s%s\n%s%d\n", "names: ", *(cur_name + i), "credits: ", *(cur_credits + i));
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
@ -33,17 +60,22 @@ int main(int argc, char *argv[])
|
|||
// third way, pointers are just arrays
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s is %d years old again.\n", cur_name[i], cur_age[i]);
|
||||
printf("%s gets %d credits again.\n", cur_name[i], cur_credits[i]);
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// fourth way with pointers in a stupid complex way
|
||||
for (cur_name = names, cur_age = ages; (cur_age - ages) < count; ++cur_name, ++cur_age)
|
||||
for (cur_name = names, cur_credits = credits; (cur_credits - credits) < count; ++cur_name, ++cur_credits)
|
||||
{
|
||||
printf("%s lived %d years so far.\n", *cur_name, *cur_age);
|
||||
printf("%s gets %d creditss so far.\n", *cur_name, *cur_credits);
|
||||
}
|
||||
// int *pointer = ages;
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s%p\n%s%p\n", "cur_name address: ", cur_name + i, "cur_credits address: ", cur_credits + i);
|
||||
}
|
||||
// int *pointer = credits;
|
||||
|
||||
// printf("sizeof(pointer):%ld", sizeof(pointer));
|
||||
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// create two arrays we care about
|
||||
int ages[] = {23, 43, 12, 89, 2};
|
||||
char *names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};
|
||||
|
||||
// safely get the size of ages
|
||||
int count = sizeof(ages) / sizeof(int);
|
||||
int i = 0;
|
||||
|
||||
// first way using indexing
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s has %d years alive.\n", names[i], ages[i]);
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// set up the pointers to the start of the arrays
|
||||
int *cur_age = ages;
|
||||
char **cur_name = names;
|
||||
|
||||
// second way using pointers
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s is %d years old.\n", cur_name[i], cur_age[i]);
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// third way, pointers are just arrays
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s is %d years old again.\n", cur_name [i], cur_age[i]);
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// fourth way with pointers in a stupid complex way
|
||||
for (cur_name = names, cur_age = ages; (cur_age - ages) < count; ++cur_name, ++cur_age)
|
||||
{
|
||||
printf("%s lived %d years so far.\n", cur_name[0], cur_age[0]);
|
||||
}
|
||||
// int *pointer = ages;
|
||||
|
||||
// printf("sizeof(pointer):%ld", sizeof(pointer));
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// create two arrays we care about
|
||||
int ages[] = {23, 43, 12, 89, 2};
|
||||
char *names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};
|
||||
|
||||
// safely get the size of ages
|
||||
int count = sizeof(ages) / sizeof(int);
|
||||
int i = 0;
|
||||
|
||||
// first way using indexing
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s has %d years alive.\n", names[i], ages[i]);
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// set up the pointers to the start of the arrays
|
||||
int *cur_age = ages;
|
||||
char **cur_name = names;
|
||||
|
||||
// second way using pointers
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s is %d years old.\n", *(cur_name + i), *(cur_age + i));
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// third way, pointers are just arrays
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s is %d years old again.\n", cur_name[i], cur_age[i]);
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// fourth way with pointers in a stupid complex way
|
||||
for (cur_name = names, cur_age = ages; (cur_age - ages) < count; ++cur_name, ++cur_age)
|
||||
{
|
||||
printf("%s lived %d years so far.\n", *cur_name, *cur_age);
|
||||
}
|
||||
// int *pointer = ages;
|
||||
|
||||
// printf("sizeof(pointer):%ld", sizeof(pointer));
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// create two arrays we care about
|
||||
int ages[] = {23, 43, 12, 89, 2};
|
||||
char *names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};
|
||||
|
||||
// safely get the size of ages
|
||||
int count = sizeof(ages) / sizeof(int);
|
||||
int i = 0;
|
||||
|
||||
// first way using indexing
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s has %d years alive.\n", *(names + i), *(ages + i));
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// set up the pointers to the start of the arrays
|
||||
int *cur_age = ages;
|
||||
char **cur_name = names;
|
||||
|
||||
// second way using pointers
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s is %d years old.\n", *(cur_name + i), *(cur_age + i));
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// third way, pointers are just arrays
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
printf("%s is %d years old again.\n", *(cur_name + i), *(cur_age + i));
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// fourth way with pointers in a stupid complex way
|
||||
for (cur_name = names, cur_age = ages; (cur_age - ages) < count; ++cur_name, ++cur_age)
|
||||
{
|
||||
printf("%s lived %d years so far.\n", *cur_name, *cur_age);
|
||||
}
|
||||
// int *pointer = ages;
|
||||
|
||||
// printf("sizeof(pointer):%ld", sizeof(pointer));
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue