chapter7
This commit is contained in:
41
DeitelC/Chapter6/palindromes.c
Normal file
41
DeitelC/Chapter6/palindromes.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int palindromes(const char *c);
|
||||
int testPal(const char *c, int start, int end);
|
||||
|
||||
int palindromes(const char *c) {
|
||||
int length = 0;
|
||||
while (c[length] != '\0') {
|
||||
length++;
|
||||
}
|
||||
|
||||
return testPal(c, 0, length - 1);
|
||||
}
|
||||
|
||||
int testPal(const char *c, int start, int end) {
|
||||
int test = 1;
|
||||
|
||||
if(start >= end) {
|
||||
return test;
|
||||
}
|
||||
|
||||
if(c[start] != c[end]) {
|
||||
return !test;
|
||||
}
|
||||
|
||||
return testPal(c, ++start, --end);
|
||||
|
||||
return test;
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
char str1[] = "radar";
|
||||
char str2[] = "A man, a plan, a canal: Panama";
|
||||
char str3[] = "hello";
|
||||
|
||||
printf("%s is %s a palindrome\n", str1, palindromes(str1) ? "" : "not");
|
||||
printf("%s is %s a palindrome\n", str2, palindromes(str2) ? "" : "not");
|
||||
printf("%s is %s a palindrome\n", str3, palindromes(str3) ? "" : "not");
|
||||
return 0;
|
||||
}
|
63
DeitelC/Chapter7/bubbleSort.c
Normal file
63
DeitelC/Chapter7/bubbleSort.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#define SIZE 10
|
||||
|
||||
void bubbleSort(int a[], size_t size, int(*compare)(int i1, int i2));
|
||||
int asc(int i1, int i2);
|
||||
int des(int i1, int i2);
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int nums[SIZE] = {2, 4, 6, 11, 123, 1000, 3, 42, 90, 1};
|
||||
puts("Nums are ready asc or desc ? (asc: 0/des: 1)");
|
||||
int order = 0;
|
||||
scanf("%d", &order);
|
||||
|
||||
puts("Orginal list: ");
|
||||
for(size_t i = 0; i < SIZE; ++i) {
|
||||
printf(" %d,", nums[i]);
|
||||
}
|
||||
puts("");
|
||||
|
||||
if(order) {
|
||||
// des sort
|
||||
bubbleSort(nums, SIZE, des);
|
||||
puts("In Descending Order: ");
|
||||
} else {
|
||||
bubbleSort(nums, SIZE, asc);
|
||||
puts("In Ascending Order: ");
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < SIZE; ++i) {
|
||||
printf(" %d,", nums[i]);
|
||||
}
|
||||
puts("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void bubbleSort(int a[], size_t size, int(*compare)(int i1, int i2)) {
|
||||
void swap(int *iS1, int *iS2);
|
||||
|
||||
for(size_t pass = 0; pass < SIZE - 1; ++pass) {
|
||||
for(size_t count = 0; count < SIZE - 1; ++count) {
|
||||
if((*compare)(a[count], a[count + 1])) {
|
||||
swap(&a[count], &a[count + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void swap(int *iS1, int *iS2) {
|
||||
int temp = *iS1;
|
||||
*iS1 = *iS2;
|
||||
*iS2 = temp;
|
||||
}
|
||||
|
||||
int asc(int i1, int i2) {
|
||||
return i2 < i1;
|
||||
}
|
||||
int des(int i1, int i2) {
|
||||
return i1 < i2;
|
||||
}
|
52
DeitelC/Chapter7/deckOfCards.c
Normal file
52
DeitelC/Chapter7/deckOfCards.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define SUITS 4
|
||||
#define FACES 13
|
||||
#define CARDS 52
|
||||
|
||||
void shuffle(int deck[][FACES]);
|
||||
void deal(int deck[][FACES], const char *suits[], const char *faces[]);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int deck[SUITS][FACES] = {0};
|
||||
srand((unsigned)time(NULL));
|
||||
shuffle(deck);
|
||||
|
||||
const char *suits[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
|
||||
const char *faces[FACES] = {"Ace", "Deuce", "Three", "Four", "Five",
|
||||
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
|
||||
|
||||
deal(deck, suits, faces);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void shuffle(int deck[][FACES]) {
|
||||
for(size_t card = 1; card <= CARDS; ++card) {
|
||||
size_t row, column;
|
||||
do {
|
||||
row = rand() % SUITS;
|
||||
column = rand() % FACES;
|
||||
} while(deck[row][column] != 0);
|
||||
deck[row][column] = (int)card;
|
||||
}
|
||||
}
|
||||
|
||||
void deal(int deck[][FACES], const char *suits[], const char *faces[]) {
|
||||
for(size_t card = 1; card <= CARDS; ++card) {
|
||||
for(size_t suit = 0; suit < SUITS; ++suit) {
|
||||
for(size_t face = 0; face < FACES; ++face) {
|
||||
if(deck[suit][face] == (int)card) {
|
||||
printf("Card: %5s of %-8s%c",
|
||||
faces[face],
|
||||
suits[suit],
|
||||
(card % 4 == 0) ? '\n' : '\t');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
38
DeitelC/Chapter7/die.c
Normal file
38
DeitelC/Chapter7/die.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int bob = 0; //Bob
|
||||
int alice = 0;
|
||||
int superAlice = 0;
|
||||
int notSuperBob = 0;
|
||||
srand(time(NULL));
|
||||
|
||||
for(int i = 0; i < 1000; ++i){
|
||||
int die = 1 + rand() % 6;
|
||||
|
||||
if(die % 2) {
|
||||
++bob;
|
||||
} else {
|
||||
++alice;
|
||||
}
|
||||
|
||||
if(die != 1){
|
||||
int tempDie = (die % 2) ? die - 1 : die;
|
||||
if(tempDie % 2){
|
||||
++notSuperBob;
|
||||
} else {
|
||||
++superAlice;
|
||||
}
|
||||
} else {
|
||||
++notSuperBob;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Normal Alice: %d, Normal Bob: %d\n", alice, bob);
|
||||
printf("Super Alice: %d, NotSuperBob: %d\n", superAlice, notSuperBob);
|
||||
|
||||
return 0;
|
||||
}
|
33
DeitelC/Chapter7/test.c
Normal file
33
DeitelC/Chapter7/test.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void copy1 (char * const s1, const char * const s2);
|
||||
void copy2 (char * s1, const char * s2);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char str1[10];
|
||||
char *str2 = "Hello"; //str2 points a string
|
||||
copy1(str1, str2);
|
||||
puts(str1);
|
||||
|
||||
char str3[10];
|
||||
char str4[] = "Goodbye!"; //str4 creates an array containing string
|
||||
copy2(str3, str4);
|
||||
puts(str4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// array notation
|
||||
void copy1 (char * const s1, const char * const s2) {
|
||||
for (size_t i = 0; (s1[i] = s2[i]) != '\0' ; i++) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
// pointer notation
|
||||
void copy2 (char * s1, const char * s2) {
|
||||
for (; (*s1 = *s2) != '\0'; ++s1, ++s2) {
|
||||
;
|
||||
}
|
||||
}
|
15
DeitelC/Chapter7/test1.c
Normal file
15
DeitelC/Chapter7/test1.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int x = 5;
|
||||
int *ptr = &x;
|
||||
int a = ++*ptr;
|
||||
printf("*ptr++ %d\n", a);
|
||||
printf("++ptr %d\n", ++ptr);
|
||||
|
||||
void *vptr = &x;
|
||||
printf("void ptr %d\n", vptr);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user