Update progress of 1-23

This commit is contained in:
caskd 2019-10-21 20:45:33 +02:00
parent 858c80c23b
commit 03c0df163f
No known key found for this signature in database
GPG Key ID: 79DB21404E300A27
4 changed files with 71 additions and 1 deletions

View File

@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0)
project(learningc LANGUAGES C)
set(CHAPTER 1)
set(EXERCISE 22)
set(EXERCISE 23)
add_executable(learningc chapters/${CHAPTER}/exercises/${EXERCISE}.c)

11
chapters/1/exercises/23.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include "shared/functions.h"
#include "shared/functions/uncomment.c"
int main() {
char line[MAXLINE];
while (uncomment(line,MAXLINE) != EOF) {
printf("%s", line);
}
return 0;
}

View File

@ -7,3 +7,4 @@ int getlines(char line[], int maxline);
int detab(char s[],int lim, int col);
int entab(char s[], int lim, int col);
int fold(char s[], int lim, int cpr);
int uncomment(char s[],int lim);

View File

@ -0,0 +1,58 @@
#include <stdio.h>
int com = 0;
/*
* TODO:
* Keep backbuffer of 1 character (current & i-1) to check for comment ends
* Handle double slashes nicely
* Find a cleaner way to achieve this
*/
int uncomment(char s[],int lim) {
int c, i, quo = 0, wnl = 0;
/* c is character integer
* i is column
* com is current comment state
* quo is current quote state */
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) {
/* Check if we are in a character constant or between quotes, if yes ignore comments */
if (c == '"' || (c == '\'' && s[i-1] != '"')) {
if (quo == 1) {
quo = 0; //test
} else {
quo = 1;
}
} else if (quo == 0 && (c == '/' || c == '*')) {
if (com == 0) {
if (c == '*' && s[i-1] == '/') {
com = 1;
} else if (c == '/' && s[i-1] == '/') {
wnl = 1;
s[i-1] = ' ';
}
} else if (com == 1 && c == '/' && s[i-1] == '*') {
com = 0;
}
}
if (com == 0 && wnl == 0) {
s[i] = c;
} else {
s[i] = ' ';
}
}
/* If the line is completly empty, do not print a new line, jump to the next line */
for (;i > 0 && (s[i-1] == ' ' || s[i-1] == '\t'); i--);
if (i == 0) {
s[i] = '\0';
} else {
s[i] = '\n';
s[i+1] = '\0';
}
/* If we have hit the end of the line, tell the previous function, else return the lenght */
if (c == EOF) {
return EOF;
} else {
return i;
}
}