Loading lang_c_tr_02...
printf("\n~~~~ %s() ~~~~\n", __func__);
<string.h>pour vérifier que le résultat obtenu est bien conforme aux attentes.
<string.h>pour vérifier que le résultat obtenu est bien conforme aux attentes.
const char *longText= " I am the passenger and I ride and I ride \n" " I ride through the city's backsides \n" " I see the stars come out of the sky \n" " Yeah, the bright and hollow sky \n" " You know it looks so good tonight \n" " \n" " I am the passenger \n" " I stay under glass \n" " I look through my window so bright \n" " I see the stars come out tonight \n" " I see the bright and hollow sky \n" " Over the city's ripped backsides \n" " And everything looks good tonight \n" " Singing la la la la la.. lala la la, \n" " la la la la.. lala la la etc \n" " \n" " Get into the car \n" " We'll be the passenger \n" " We'll ride through the city tonight \n" " We'll see the city's ripped backsides \n" " We'll see the bright and hollow sky \n" " We'll see the stars that shine so bright \n" " Stars made for us tonight \n" " \n" " Oh, the passenger \n" " How, how he rides \n" " Oh, the passenger \n" " He rides and he rides \n" " He looks through his window \n" " What does he see? \n" " He sees the sign and hollow sky \n" " He sees the stars come out tonight \n" " He sees the city's ripped backsides \n" " He sees the winding ocean drive \n" " And everything was made for you and me \n" " All of it was made for you and me \n" " 'Cause it just belongs to you and me \n" " So let's take a ride and see what's mine \n" " Singing la la la la la.. lala la la, \n" " la la la la.. lala la la etc \n" " \n" " Oh the passenger \n" " He rides and he rides \n" " He sees things from under glass \n" " He looks through his window side \n" " He sees the things that he knows are his \n" " He sees the bright and hollow sky \n" " He sees the city sleep at night \n" " He sees the stars are out tonight \n" " And all of it is yours and mine \n" " And all of it is yours and mine \n" " So let's ride and ride and ride and ride \n" " Oh, oh, Singing la la la la lalalala \n";
//----------------------------------------------------------------------------
#ifndef REALUTILS_H #define REALUTILS_H
#include <stdbool.h>
void Real_swap(double *inout_a, double *inout_b);
void Real_normalise(double *inout_a, double *inout_b);
void RealArray_show(const double *content, int count);
double RealArray_mean(const double *content, int count);
void RealArray_reverse(double *content, int count);
void RealArray_copy(double *destination, const double *source, int count);
void RealArray_reverseCopy(double *destination, const double *source, int count);
void RealArray_min(const double *content, int count, double *out_value, int *out_index);
void RealArray_max(const double *content, int count, double *out_value, int *out_index);
void RealArray_ascendingSort(double *content, int count);
void RealArray_descendingSort(double *content, int count);
#endif // REALUTILS_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "realUtils.h" #include <stdio.h> #include <float.h>
void Real_swap(double *inout_a, double *inout_b) { //-- load inout-parameters -- double a=*inout_a; double b=*inout_b; //-- store out-parameters -- *inout_a=b; *inout_b=a; }
void Real_normalise(double *inout_a, double *inout_b) { //-- load inout-parameters -- double a=*inout_a; double b=*inout_b; //-- perform work -- double factor=1.0/(a+b); a*=factor; b*=factor; //-- store out-parameters -- *inout_a=a; *inout_b=b; }
void RealArray_show(const double *content, int count) { printf("[ "); for(int i=0; i<count; ++i) { if(i!=0) { printf(", "); } printf("%g", content[i]); } printf(" ]"); }
double RealArray_mean(const double *content, int count) { double sum=0.0; for(int i=0; i<count; ++i) { sum+=content[i]; } return sum/count; }
void RealArray_reverse(double *content, int count) { int mid=count/2; for(int i=0; i<mid; ++i) { Real_swap(content+i, content+count-1-i); } }
void RealArray_copy(double *destination, const double *source, int count) { for(int i=0; i<count; ++i) { destination[i]=source[i]; } }
void RealArray_reverseCopy(double *destination, const double *source, int count) { for(int i=0; i<count; ++i) { destination[count-1-i]=source[i]; } }
void RealArray_min(const double *content, int count, double *out_value, int *out_index) { double minValue=content[0]; int minIndex=0; for(int i=1; i<count; ++i) { double value=content[i]; if(value<minValue) { minValue=value; minIndex=i; } } //-- store out-parameters -- *out_value=minValue; *out_index=minIndex; }
void RealArray_max(const double *content, int count, double *out_value, int *out_index) { double maxValue=content[0]; int maxIndex=0; for(int i=1; i<count; ++i) { double value=content[i]; if(value>maxValue) { maxValue=value; maxIndex=i; } } //-- store out-parameters -- *out_value=maxValue; *out_index=maxIndex; }
void RealArray_ascendingSort(double *content, int count) { for(int i=0; i<count; ++i) { double value; int index; RealArray_min(content+i, count-i, &value, &index); if(index) { Real_swap(content+i, content+i+index); } } }
void RealArray_descendingSort(double *content, int count) { for(int i=0; i<count; ++i) { double value; int index; RealArray_max(content+i, count-i, &value, &index); if(index) { Real_swap(content+i, content+i+index); } } }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#ifndef STRUTILS_H #define STRUTILS_H
#include <stdbool.h>
void Str_upper(char *str);
void Str_lower(char *str);
char * // new dynamic-string Str_repeat(char symbol, int repeat);
char * // new dynamic-string Str_create(const char *str, int length);
char * // new dynamic-string Str_add(const char *str1, const char *str2);
void Str_append(char **inout_str, const char *tail);
bool // success Str_findWord(const char *str, int *out_begin, int *out_length);
#endif // STRUTILS_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "strUtils.h" #include <ctype.h> #include <string.h> #include <stdlib.h>
void Str_upper(char *str) { int length=(int)strlen(str); for(int i=0; i<length; ++i) { str[i]=(char)toupper(str[i]); } }
void Str_lower(char *str) { int length=(int)strlen(str); for(int i=0; i<length; ++i) { str[i]=(char)tolower(str[i]); } }
char * // new dynamic-string Str_repeat(char symbol, int length) { char *dst=(char *)malloc(length+1); // '\0' included if(dst==NULL) { abort(); } for(int i=0; i<length; ++i) // '\0' excluded { dst[i]=symbol; } dst[length]='\0'; // ensure termination return dst; }
char * // new dynamic-string Str_create(const char *str, int length) { if(length<0) { length=(int)strlen(str); } char *dst=(char *)malloc(length+1); // '\0' included if(dst==NULL) { abort(); } strncpy(dst, str, length); // '\0' excluded dst[length]='\0'; // ensure termination return dst; }
char * // new dynamic-string Str_add(const char *str1, const char *str2) { int length1=(int)strlen(str1); // '\0' excluded int size2=1+(int)strlen(str2); // '\0' included char *dst=(char *)malloc(length1+size2); // '\0' included if(dst==NULL) { abort(); } strncpy(dst, str1, length1); // '\0' excluded strncpy(dst+length1, str2, size2); // '\0' included return dst; }
void Str_append(char **inout_str, const char *tail) { //-- load inout-parameters -- char *str=*inout_str; //-- perform work -- int length1=(int)strlen(str); // '\0' excluded int size2=1+(int)strlen(tail); // '\0' included str=(char *)realloc(str,length1+size2); // '\0' included if(str==NULL) { abort(); } strncpy(str+length1, tail, size2); // '\0' included //-- store out-parameters -- *inout_str=str; }
static // internal use bool // c is a valid character for a word Str_validInWord(int c) { return c&&!ispunct(c)&&!isspace(c); }
bool // success Str_findWord(const char *str, int *out_begin, int *out_length) { int length=(int)strlen(str); int begin=-1; for(int i=0; i<length; ++i) { if(Str_validInWord(str[i])) { begin=i; break; } } if(begin==-1) { return false; } int end=begin; for(int i=begin; i<=length; ++i) // reach '\0' to detect word end { if(!Str_validInWord(str[i])) { end=i; break; } } //-- store out-parameters -- *out_begin=begin; *out_length=end-begin; return true; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include <stdio.h> #include "realUtils.h"
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "strUtils.h"
void test_Real_swap(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double a=1.1, b=2.2; printf("before: a=%g b=%g\n", a, b); Real_swap(&a, &b); printf("after: a=%g b=%g\n", a, b); }
void test_Real_normalise(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double a=1.5, b=4.5; printf("before: a=%g b=%g\n", a, b); Real_normalise(&a, &b); printf("after: a=%g b=%g\n", a, b); }
void test_RealArray_show(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("values: "); RealArray_show(values, count); printf("\n"); }
void test_RealArray_mean(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("values: "); RealArray_show(values, count); printf("\n"); printf("mean: %g\n", RealArray_mean(values, count)); }
void test_RealArray_reverse(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("before: "); RealArray_show(values, count); printf("\n"); RealArray_reverse(values, count); printf("after: "); RealArray_show(values, count); printf("\n"); }
void test_RealArray_copy(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values1[10]={0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}; double values2[10]; int count=(int)(sizeof(values1)/sizeof(values1[0])); printf("values1: "); RealArray_show(values1, count); printf("\n"); RealArray_copy(values2, values1, count); printf("values2: "); RealArray_show(values2, count); printf("\n"); }
void test_RealArray_reverseCopy(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values1[10]={0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}; double values2[10]; int count=(int)(sizeof(values1)/sizeof(values1[0])); printf("values1: "); RealArray_show(values1, count); printf("\n"); RealArray_reverseCopy(values2, values1, count); printf("values2: "); RealArray_show(values2, count); printf("\n"); }
void test_RealArray_min(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={8.8, 5.5, 0.0, 3.3, 7.7, 4.4, 1.1, 9.9, 2.2, 6.6}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("values: "); RealArray_show(values, count); printf("\n"); double minValue; int minIndex; RealArray_min(values, count, &minValue, &minIndex); printf("min: %g at index %d\n", minValue, minIndex); }
void test_RealArray_max(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={8.8, 5.5, 0.0, 3.3, 7.7, 4.4, 1.1, 9.9, 2.2, 6.6}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("values: "); RealArray_show(values, count); printf("\n"); double maxValue; int maxIndex; RealArray_max(values, count, &maxValue, &maxIndex); printf("max: %g at index %d\n", maxValue, maxIndex); }
void test_RealArray_ascendingSort(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={8.8, 5.5, 0.0, 3.3, 7.7, 4.4, 1.1, 9.9, 2.2, 6.6}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("before: "); RealArray_show(values, count); printf("\n"); RealArray_ascendingSort(values, count); printf("after: "); RealArray_show(values, count); printf("\n"); }
void test_RealArray_descendingSort(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={8.8, 5.5, 0.0, 3.3, 7.7, 4.4, 1.1, 9.9, 2.2, 6.6}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("before: "); RealArray_show(values, count); printf("\n"); RealArray_descendingSort(values, count); printf("after: "); RealArray_show(values, count); printf("\n"); }
void test_Str_upper(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char msg[]="This message is _REALLY_ useful! Isn't it?"; printf("before: <%s>\n", msg); Str_upper(msg); printf("after: <%s>\n", msg); }
void test_Str_lower(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char msg[]="This message is _REALLY_ useful! Isn't it?"; printf("before: <%s>\n", msg); Str_lower(msg); printf("after: <%s>\n", msg); }
void test_Str_repeat(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char *str=Str_repeat('*', 16); printf("result: <%s>\n", str); if(strcmp(str, "****************")==0) { printf("match\n"); } else { printf("mistmatch!\n"); } free(str); }
void test_Str_create(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const char *msg="Is it the same?"; printf("original: <%s>\n", msg); char *str=Str_create(msg, -1); printf("copy: <%s>\n", str); if(strcmp(str, msg)==0) { printf("match\n"); } else { printf("mistmatch!\n"); } free(str); }
void test_Str_add(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const char *msg1="It starts like this... "; const char *msg2="Then it ends like that!"; printf("original: <%s> <%s>\n", msg1, msg2); char *msg=Str_add(msg1, msg2); printf("copy: <%s>\n", msg); if((strncmp(msg, msg1, strlen(msg1))==0)&& // starts with msg1? (strcmp(msg+strlen(msg1), msg2)==0)) // ends with msg2? { printf("match\n"); } else { printf("mistmatch!\n"); } free(msg); }
void test_Str_append(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const char *msg1="It starts like this... "; const char *msg2="Then it ends like that!"; printf("original: <%s> <%s>\n", msg1, msg2); char *msg=Str_create(msg1, -1); Str_append(&msg, msg2); printf("copy: <%s>\n", msg); if((strncmp(msg, msg1, strlen(msg1))==0)&& // starts with msg1? (strcmp(msg+strlen(msg1), msg2)==0)) // ends with msg2? { printf("match\n"); } else { printf("mistmatch!\n"); } free(msg); }
void test_Str_findWord(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const char *longText= " I am the passenger and I ride and I ride \n" " I ride through the city's backsides \n" " I see the stars come out of the sky \n" " Yeah, the bright and hollow sky \n" " You know it looks so good tonight \n" " \n" " I am the passenger \n" " I stay under glass \n" " I look through my window so bright \n" " I see the stars come out tonight \n" " I see the bright and hollow sky \n" " Over the city's ripped backsides \n" " And everything looks good tonight \n" " Singing la la la la la.. lala la la, \n" " la la la la.. lala la la etc \n" " \n" " Get into the car \n" " We'll be the passenger \n" " We'll ride through the city tonight \n" " We'll see the city's ripped backsides \n" " We'll see the bright and hollow sky \n" " We'll see the stars that shine so bright \n" " Stars made for us tonight \n" " \n" " Oh, the passenger \n" " How, how he rides \n" " Oh, the passenger \n" " He rides and he rides \n" " He looks through his window \n" " What does he see? \n" " He sees the sign and hollow sky \n" " He sees the stars come out tonight \n" " He sees the city's ripped backsides \n" " He sees the winding ocean drive \n" " And everything was made for you and me \n" " All of it was made for you and me \n" " 'Cause it just belongs to you and me \n" " So let's take a ride and see what's mine \n" " Singing la la la la la.. lala la la, \n" " la la la la.. lala la la etc \n" " \n" " Oh the passenger \n" " He rides and he rides \n" " He sees things from under glass \n" " He looks through his window side \n" " He sees the things that he knows are his \n" " He sees the bright and hollow sky \n" " He sees the city sleep at night \n" " He sees the stars are out tonight \n" " And all of it is yours and mine \n" " And all of it is yours and mine \n" " So let's ride and ride and ride and ride \n" " Oh, oh, Singing la la la la lalalala \n"; int wordCount=0; for(int begin, length; Str_findWord(longText, &begin, &length); longText+=begin+length) { char *str=Str_create(longText+begin, length); ++wordCount; printf("<%s> ", str); free(str); } printf("\n%d words\n", wordCount); }
int main(void) { test_Real_swap(); test_Real_normalise(); test_RealArray_show(); test_RealArray_mean(); test_RealArray_reverse(); test_RealArray_copy(); test_RealArray_reverseCopy(); test_RealArray_min(); test_RealArray_max(); test_RealArray_ascendingSort(); test_RealArray_descendingSort(); test_Str_upper(); test_Str_lower(); test_Str_repeat(); test_Str_create(); test_Str_add(); test_Str_append(); test_Str_findWord(); return 0; }
//----------------------------------------------------------------------------