Left off on exercise 1.23

This commit is contained in:
kdeng00
2020-09-06 14:45:41 -04:00
parent 74cf2bfacc
commit 2dcca73fc5
6 changed files with 246 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
/*
*
* Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to
* use a function for conversion.
*
* Author: Kun Deng
*/
#include <stdio.h>
#define UPPER 200
#define LOWER 0
#define STEP 10
double convert_to_celsius(double);
int main(int argc, char **argv)
{
printf("%13s %13s\n", "fahrenheit", "celsius");
for (double fahrenheit = LOWER; fahrenheit <= UPPER; fahrenheit = fahrenheit + STEP)
{
printf("%13.2f %13.2f\n", fahrenheit, convert_to_celsius(fahrenheit));
}
return 0;
}
double convert_to_celsius(double fahrenheit)
{
return ((fahrenheit - 32) * 5.0 / 9.0);
}
+48
View File
@@ -0,0 +1,48 @@
/*
*
* Exercise 1-17. Write a program to print all input lines that are longer than 80
* characters
*
* Author: Kun Deng
*/
#include <stdio.h>
#define LINE_LIMIT_COUNT 80
#define LINE_MAX 1000
int get_line(char line[]);
int main(int argc, char **argv)
{
char line[LINE_MAX];
int length = 0;
printf("Enter a sequence of characters.\n");
printf("Lines that are greater than %d will be printed to the output\n", LINE_LIMIT_COUNT);
while(length = get_line(line))
{
if (length > LINE_LIMIT_COUNT)
{
printf("%s\n", line);
}
}
}
int get_line(char line[])
{
int i = 0;
int c;
for (; ((c = getchar()) != EOF) && c != '\n'; ++i)
{
line[i] = c;
}
line[i] = '\0';
return i;
}
+66
View File
@@ -0,0 +1,66 @@
/*
*
* Exercise 1-19. Write a function reverse(s) that reverses the character string s.
* Use it to write a program that reverses its input a line at a time.
*
* Author: Kun Deng
*/
#include <stdio.h>
#define UPPER 200
#define LOWER 0
#define STEP 10
#define LINE_MAX_LIMIT 1000
int get_line(char line[]);
void reverse(char whole_string[], int length);
int main(int argc, char **argv)
{
char line[LINE_MAX_LIMIT];
int length;
while ((length = get_line(line)))
{
printf("before: %s\n", line);
reverse(line, length);
printf("after: %s\n", line);
}
return 0;
}
int get_line(char line[])
{
int i = 0;
int c;
for (; ((c = getchar()) != EOF) && c != '\n'; ++i)
{
line[i] = c;
}
line[i] = '\0';
return i;
}
void reverse(char whole_string[], int length)
{
int mirrored_index = length - 1;
for (int i = 0; i != (length / 2); ++i)
{
char mirrored_char = whole_string[mirrored_index];
whole_string[mirrored_index] = whole_string[i];
whole_string[i] = mirrored_char;
--mirrored_index;
}
}
+88
View File
@@ -0,0 +1,88 @@
/*
*
* Exercise 1-20. Write a program detab that replaces the tabs in the input with the
* proper number of blanks to space to the next tab stop. Assume a fixed set of tab
* stops, say every n columns. Should n be a variable or a symbolic parameter?
*
* Author: Kun Deng
*/
#include <stdio.h>
#define TAB_REPRESENTER 2 // Represents how many spaces a tab would equal
#define LINE_MAX_LIMIT 1000
char line[LINE_MAX_LIMIT];
int line_length;
int get_line(void);
void replace_tabs(void);
int main()
{
extern int line_length;
printf("Enter a sequence of characters\n");
printf("Any tabs will be replaced with %d white space characters\n", LINE_MAX_LIMIT);
while (line_length = get_line())
{
printf("before: %s\n", line);
replace_tabs();
printf("after: %s\n", line);
}
return 0;
}
int get_line(void)
{
extern char line[];
int i = 0;
int c;
for (; (c = getchar()) != EOF && c != '\n'; ++i)
{
line[i] = c;
}
line[i] = '\0';
return i;
}
void replace_tabs(void)
{
extern char line[];
extern int line_length;
for (int i = 0; i != line_length; ++i)
{
char some_char = line[i];
if (some_char == '\t')
{
int new_line_length = line_length + TAB_REPRESENTER;
line[new_line_length] = '\0';
for (int j = (line_length - 1); j > i; --j)
{
line[j + TAB_REPRESENTER] = line[j];
}
for (int k = i; k < (i + TAB_REPRESENTER); ++k)
{
line[k] = ' ';
}
line_length = new_line_length;
line[i + TAB_REPRESENTER] = 127;
}
}
}
+6
View File
@@ -0,0 +1,6 @@
/*
*
* Exercise
*
* Author: Kun Deng
*/
+6
View File
@@ -0,0 +1,6 @@
/*
*
* Exercise
*
* Author: Kun Deng
*/