Browse Source

refactoring: rename stod to balanceToDouble and its internal variable. The purpose of this function is to convert the user input for the account balance into a double and the new names states this purpose better.

remotes/origin/development
fdai7057 2 years ago
parent
commit
fe311209d7
  1. 16
      src/helperFunctions.c
  2. 3
      src/helperFunctions.h
  3. 2
      test/test_helperFunctions.c

16
src/helperFunctions.c

@ -117,22 +117,22 @@ char *generateCheckString(unsigned int customerID, char *customerPassword)
return checkString; return checkString;
} }
double stod(char *s)
double balanceToDouble(char *balanceAsString)
{ {
double result, power; double result, power;
int i=0, sign; int i=0, sign;
sign = (*(s) == '-') ? -1 : 1;
if(*(s)=='+'||*(s)=='-'){
sign = (*(balanceAsString) == '-') ? -1 : 1;
if(*(balanceAsString)=='+'||*(balanceAsString)=='-'){
++i; ++i;
} }
for(result = 0.0; *(s+i)!='.';++i){
result = 10.0 * result + (*(s+i) - '0');
for(result = 0.0; *(balanceAsString+i)!='.';++i){
result = 10.0 * result + (*(balanceAsString+i) - '0');
} }
if(*(s+i)=='.'){
if(*(balanceAsString+i)=='.'){
++i; ++i;
} }
for(power = 1.0;*(s+i)!='\0';++i){
result = 10.0 * result + (*(s+i)- '0');
for(power = 1.0;*(balanceAsString+i)!='\0';++i){
result = 10.0 * result + (*(balanceAsString+i)- '0');
power *= 10.0; power *= 10.0;
} }
return sign * result / power; return sign * result / power;

3
src/helperFunctions.h

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#define UNITY_INCLUDE_CONFIG_H
char *stringConcatenation(char *, char *); char *stringConcatenation(char *, char *);
char *to_string(int); char *to_string(int);
char *generateCheckString(unsigned int, char *); char *generateCheckString(unsigned int, char *);
@ -11,5 +12,5 @@ unsigned int toUnsignedInteger(char *);
unsigned int power(unsigned int, unsigned int); unsigned int power(unsigned int, unsigned int);
bool everyCharacterIsDigit(char *); bool everyCharacterIsDigit(char *);
bool isLetterOfAlphabet(char *); bool isLetterOfAlphabet(char *);
double stod(char *);
double balanceToDouble(char *);
#endif #endif

2
test/test_helperFunctions.c

@ -2,9 +2,9 @@
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <unity.h> #include <unity.h>
#include "unity_config.h"
#include "../src/helperFunctions.c" #include "../src/helperFunctions.c"
void test_isLetterOfAlphabet() void test_isLetterOfAlphabet()
{ {
/*test block 1*/ /*test block 1*/

Loading…
Cancel
Save