📄 selsolutionsc5.txt
字号:
return 0;
}
PE 11-13
/* Programming Exercise 11-13 */
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <math.h> /* for pow() */
/* #include <console.h> */ /* Macintosh adjustment */
int main(int argc, char *argv[])
{
double num, exp;
/* argc = ccommand(&argv); */ /* Macintosh adjustment */
if (argc != 3)
printf("Usage: %s number exponent\n", argv[0]);
else
{
num = atof(argv[1]);
exp = atof(argv[2]);
printf("%f to the %f power = %g\n", num, exp, pow(num,exp));
}
return 0;
}
PE 11-15
/* Programming Exercise 11-15 */
#include <stdio.h>
#include <ctype.h>
/* #include <console.h> */ /* Macintosh adjustment */
int main(int argc, char *argv[])
{
char mode = 'p';
int ok = 1;
int ch;
/*argc = ccommand(&argv); */ /* Macintosh adjustment */
if (argc > 2)
{
printf("Usage: %s [-p | -u | -l]\n", argv[0]);
ok = 0; /* skip processing input */
}
else if (argc == 2)
{
if (argv[1][0] != '-')
{
printf("Usage: %s [-p | -u | -l]\n", argv[0]);
ok = 0;
}
else
switch(argv[1][1])
{
case 'p' :
case 'u' :
case 'l' : mode = argv[1][1];
break;
default : printf("%s is an invalid flag; ", argv[1]);
printf("using default flag (-p).\n");
}
}
if (ok)
while ((ch = getchar() ) != EOF)
{
switch(mode)
{
case 'p' : putchar(ch);
break;
case 'u' : putchar(toupper(ch));
break;
case 'l' : putchar(tolower(ch));
}
}
return 0;
}
Chapter 12
PE 12-1
/* pe12-1.c -- deglobalizing global.c */
/* Programming Exercise 12-1 */
/* one of several approaches */
#include <stdio.h>
void critic(int * u);
int main(void)
{
int units; /* units now local */
printf("How many pounds to a firkin of butter?\n");
scanf("%d", &units);
while ( units != 56)
critic(&units);
printf("You must have looked it up!\n");
return 0;
}
void critic(int * u)
{
printf("No luck, chummy. Try again.\n");
scanf("%d", u);
}
// or use a return value:
// units = critic();
// and have critic look like this:
/*
int critic(void)
{
int u;
printf("No luck, chummy. Try again.\n");
scanf("%d", &u);
return u;
}
*/
// or have main() collect the next value for units
PE 12-3
//pe12-3a.h
#define METRIC 0
#define US 1
#define USE_RECENT 2
void check_mode(int *pm);
void get_info(int mode, double * pd, double * pf);
void show_info(int mode, double distance, double fuel);
// pe12-3a.c
#include <stdio.h>
#include "pe12-3a.h"
void check_mode(int *pm)
{
if (*pm != METRIC && *pm != US)
{
printf("Invalid mode specified. Mode %d\n", *pm);
printf("Previous mode will be used.\n");
*pm = USE_RECENT;
}
}
void get_info(int mode, double * pd, double * pf)
{
if (mode == METRIC)
printf("Enter distance traveled in kilometers: ");
else
printf("Enter distance traveled in miles: ");
scanf("%lf",pd);
if (mode == METRIC)
printf("Enter fuel consumed in liters: ");
else
printf("Enter fuel consumed in gallons: ");
scanf("%lf", pf);
}
void show_info(int mode, double distance, double fuel)
{
printf("Fuel consumption is ");
if (mode == METRIC)
printf("%.2f liters per 100 km.\n", 100 * fuel / distance);
else
printf("%.1f miles per gallon.\n", distance / fuel);
}
// pe12-3.c
#include <stdio.h>
#include "pe12-3a.h"
int main(void)
{
int mode;
int prev_mode = METRIC;
double distance, fuel;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
check_mode(&mode);
if (mode == USE_RECENT)
mode = prev_mode;
prev_mode = mode;
get_info(mode, &distance, &fuel);
show_info(mode, distance, fuel);
printf("Enter 0 for metric mode, 1 for US mode");
printf(" (-1 to quit): ");
scanf("%d", &mode);
}
printf("Done.\n");
return 0;
}
PE 12-5
/* pe12-5.c */
#include <stdio.h>
#include <stdlib.h>
void print(const int array[], int limit);
void sort(int array[], int limit);
#define SIZE 100
int main(void)
{
int i;
int arr[SIZE];
for (i = 0; i < SIZE; i++)
arr[i] = rand() % 10 + 1;
puts("initial array");
print(arr,SIZE);
sort(arr,SIZE);
puts("\nsorted array");
print(arr,SIZE);
return 0;
}
/* sort.c -- sorts an integer array in decreasing order */
void sort(int array[], int limit)
{
int top, search, temp;
for (top = 0; top < limit -1; top++)
for (search = top + 1; search < limit; search++)
if (array[search] > array[top])
{
temp = array[search];
array[search] = array[top];
array[top] = temp;
}
}
/* print.c -- prints an array */
void print(const int array[], int limit)
{
int index;
for (index = 0; index < limit; index++)
{
printf("%2d ", array[index]);
if (index % 10 == 9)
putchar('\n');
}
if (index % 10 != 0)
putchar('\n');
}
PE 12-7
/* pe12-7.c */
#include <stdio.h>
#include <stdlib.h> /* for srand() */
#include <time.h> /* for time() */
int rollem(int);
int main(void)
{
int dice, count, roll;
int sides;
int set, sets;
srand((unsigned int) time(0)); /* randomize rand() */
printf("Enter the number of sets; enter q to stop.\n");
while ( scanf("%d", &sets) == 1)
{
printf("How many sides and how many dice?\n");
scanf("%d %d", &sides, &dice);
printf("Here are %d sets of %d %d-sided throws.\n", sets, dice,
sides);
for (set = 0; set < sets; set++)
{
for (roll = 0, count = 0; count < dice; count++)
roll += rollem(sides);
/* running total of dice pips */
printf("%4d ", roll);
if (set % 15 == 14)
putchar('\n');
}
if (set % 15 != 0)
putchar('\n');
printf("How many sets? Enter q to stop.\n");
}
printf("GOOD FORTUNE TO YOU!\n");
return 0;
}
int rollem(int sides)
{
int roll;
roll = rand() % sides + 1;
return roll;
}
Chapter 13
PE 13-2
/* Programming Exercise 13-2 */
#include <stdio.h>
#include <stdlib.h>
//#include <console.h> /* Macintosh adjustment */
int main(int argc, char *argv[])
{
int byte;
FILE * source;
FILE * target;
// argc = ccommand(&argv); /* Macintosh adjustment */
if (argc != 3)
{
printf("Usage: %s sourcefile targetfile\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((source = fopen(argv[1], "rb")) == NULL)
{
printf("Could not open file %s for input\n", argv[1]);
exit(EXIT_FAILURE);
}
if ((target = fopen(argv[2], "wb")) == NULL)
{
printf("Could not open file %s for output\n", argv[2]);
exit(EXIT_FAILURE);
}
while ((byte = getc(source)) != EOF)
{
putc(byte, target);
}
if (fclose(source) != 0)
printf("Could not close file %s\n", argv[1]);
if (fclose(target) != 0)
printf("Could not close file %s\n", argv[2]);
return 0;
}
PE 13-4
/* Programming Exercise 13-4 */
#include <stdio.h>
#include <stdlib.h>
#include <console.h> /* Macintosh adjustment */
int main(int argc, char *argv[])
{
int byte;
FILE * source;
int filect;
argc = ccommand(&argv); /* Macintosh adjustment */
if (argc == 1)
{
printf("Usage: %s filename[s]\n", argv[0]);
exit(EXIT_FAILURE);
}
for (filect = 1; filect < argc; filect++)
{
if ((source = fopen(argv[filect], "r")) == NULL)
{
printf("Could not open file %s for input\n", argv[filect]);
continue;
}
while ((byte = getc(source)) != EOF)
{
putchar(byte);
}
if (fclose(source) != 0)
printf("Could not close file %s\n", argv[1]);
}
return 0;
}
PE 13-5
/* Programming Exercise 13-5 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <console.h> /* Macintosh adjustment */
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source, FILE *dest);
int main(int argc, char *argv[])
{
FILE *fa, *fs;
int files = 0;
int fct;
// argc = ccommand(&argv); /* Macintosh adjustment */
if (argc < 3)
{
printf("Usage: %s appendfile sourcefile[s]\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((fa = fopen(argv[1], "a")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create output buffer\n", stderr);
exit(EXIT_FAILURE);
}
for (fct = 2; fct < argc; fct++)
{
if (strcmp(argv[fct], argv[1]) == 0)
fputs("Can't append file to itself\n",stderr);
else if ((fs = fopen(argv[fct], "r")) == NULL)
fprintf(stderr, "Can't open %s\n", argv[fct]);
else
{
if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create output buffer\n",stderr);
continue;
}
append(fs, fa);
if (ferror(fs) != 0)
fprintf(stderr,"Error in reading file %s.\n",
argv[fct]);
if (ferror(fa) != 0)
fprintf(stderr,"Error in writing file %s.\n",
argv[1]);
fclose(fs);
files++;
printf("File %s appended.\n", argv[fct]);
}
}
printf("Done. %d files appended.\n", files);
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE]; // allocate once
while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)
fwrite(temp, sizeof (char), bytes, dest);
}
PE 13-7
/* Programming Exercise 13-7a */
/* code assumes that end-of-line immediately precedes end-of-file */
#include <stdio.h>
#include <stdlib.h>
#include <console.h> /* Macintosh adjustment */
int main(int argc, char *argv[])
{
int ch1, ch2;
FILE * f1;
FILE * f2;
argc = ccommand(&argv); /* Macintosh adjustment */
if (argc != 3)
{
printf("Usage: %s file1 file2\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((f1 = fopen(argv[1], "r")) == NULL)
{
printf("Could not open file %s for input\n", argv[1]);
exit(EXIT_FAILURE);
}
if ((f2 = fopen(argv[2], "r")) == NULL)
{
printf("Could not open file %s for input\n", argv[2]);
exit(EXIT_FAILURE);
}
ch1 = getc(f1);
ch2 = getc(f2);
while (ch1 != EOF || ch2 != EOF)
{
while (ch1 != EOF && ch1 != '\n') /* skipped after EOF reached */
{
putchar(ch1);
ch1 = getc(f1);
}
if (ch1 != EOF)
{
putchar('\n');
ch1 = getc(f1);
}
while (ch2 != EOF && ch2 != '\n') /* skipped after EOF reached */
{
putchar(ch2);
ch2 = getc(f2);
}
if (ch2 != EOF)
{
putchar('\n');
ch2 = getc(f2);
}
}
if (fclose(f1) != 0)
printf("Could not close file %s\n", argv[1]);
if (fclose(f2) != 0)
printf("Could not close file %s\n", argv[2]);
return 0;
}
/* Programming Exercise 13-7b */
/* code assumes that end-of-line immediately precedes end-of-file */
#include <stdio.h>
#include <stdlib.h>
#include <console.h> /* Macintosh adjustment */
int main(int argc, char *argv[])
{
int ch1, ch2;
FILE * f1;
FILE * f2;
argc = ccommand(&argv); /* Macintosh adjustment */
if (argc != 3)
{
printf("Usage: %s file1 file2\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((f1 = fopen(argv[1], "r")) == NULL)
{
printf("Could not open file %s for input\n", argv[1]);
exit(EXIT_FAILURE);
}
if ((f2 = fopen(argv[2], "r")) == NULL)
{
printf("Could not open file %s for input\n", argv[2]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -