WIP solution to exercise 1-16 using a file on disk as a storage buffer for stdin so that we can step through the input repeatedly.
This commit is contained in:
parent
87e93e98ca
commit
e57d4717d7
2 changed files with 146 additions and 0 deletions
73
KNR C/ex1-16-2.c
Normal file
73
KNR C/ex1-16-2.c
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void store();
|
||||||
|
void enumerate();
|
||||||
|
void recall();
|
||||||
|
int longest = 0;
|
||||||
|
int posLongest = 0;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
store();
|
||||||
|
enumerate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void store(){
|
||||||
|
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen("storage.txt", "w");
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for(int c = getchar(); c != EOF; c = getchar()){
|
||||||
|
fputc(c, fp);
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//void enumerate() will step through the file and write the value of the
|
||||||
|
//longest line encountered to int longest and the position (in terms of line
|
||||||
|
//number) in the file.
|
||||||
|
void enumerate(){
|
||||||
|
|
||||||
|
int position = 1;
|
||||||
|
int length = 0;
|
||||||
|
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen ("storage.txt", "r");
|
||||||
|
for(int c = fgetc(fp); c != EOF; c = fgetc(fp))
|
||||||
|
if(c != '\n'){
|
||||||
|
++length;
|
||||||
|
continue;
|
||||||
|
} else if(c == '\n' && length > longest){
|
||||||
|
longest = length;
|
||||||
|
length = 0;
|
||||||
|
posLongest = position;
|
||||||
|
++position;
|
||||||
|
} else if (c == '\n' && length < longest){
|
||||||
|
++position;
|
||||||
|
length = 0;
|
||||||
|
}
|
||||||
|
fclose (fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void recall (){
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen("storage.txt", "r");
|
||||||
|
|
||||||
|
int position = 1;
|
||||||
|
|
||||||
|
for(int c = fgetc(fp);; c = fgetc(fp)){
|
||||||
|
if(position > posLongest)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if(c == '\n'){
|
||||||
|
++position;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(position == posLongest){
|
||||||
|
putchar(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
73
KNR C/ex1-17.c
Normal file
73
KNR C/ex1-17.c
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void store();
|
||||||
|
void enumerate();
|
||||||
|
void recall();
|
||||||
|
int longest = 0;
|
||||||
|
int posLongest = 0;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
store();
|
||||||
|
enumerate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void store(){
|
||||||
|
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen("storage.txt", "w");
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for(int c = getchar(); c != EOF; c = getchar()){
|
||||||
|
fputc(c, fp);
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//void enumerate() will step through the file and write the value of the
|
||||||
|
//longest line encountered to int longest and the position (in terms of line
|
||||||
|
//number) in the file.
|
||||||
|
void enumerate(){
|
||||||
|
|
||||||
|
int position = 1;
|
||||||
|
int length = 0;
|
||||||
|
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen ("storage.txt", "r");
|
||||||
|
for(int c = fgetc(fp); c != EOF; c = fgetc(fp))
|
||||||
|
if(c != '\n'){
|
||||||
|
++length;
|
||||||
|
continue;
|
||||||
|
} else if(c == '\n' && length > longest){
|
||||||
|
longest = length;
|
||||||
|
length = 0;
|
||||||
|
posLongest = position;
|
||||||
|
++position;
|
||||||
|
} else if (c == '\n' && length < longest){
|
||||||
|
++position;
|
||||||
|
length = 0;
|
||||||
|
}
|
||||||
|
fclose (fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void recall (){
|
||||||
|
FILE *fp;
|
||||||
|
fp = fopen("storage.txt", "r");
|
||||||
|
|
||||||
|
int position = 1;
|
||||||
|
|
||||||
|
for(int c = fgetc(fp);; c = fgetc(fp)){
|
||||||
|
if(position > posLongest)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if(c == '\n'){
|
||||||
|
++position;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(position == posLongest){
|
||||||
|
putchar(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue