Browse Source

Merge branch 'Input_Handling'

master
Sophia Weber 12 months ago
parent
commit
9d71a35bfb
  1. 1
      project.yml
  2. 119
      src/inputHandling.c
  3. 23
      src/inputHandling.h
  4. 26
      src/main.c
  5. 22
      src/outputHandling.c
  6. 10
      src/outputHandling.h
  7. 151
      test/test_inputHandling.c
  8. 29
      test/test_outputHandling.c

1
project.yml

@ -47,6 +47,7 @@
:test:
- *common_defines
- TEST
- UNITY_INCLUDE_DOUBLE
:test_preprocess:
- *common_defines
- TEST

119
src/inputHandling.c

@ -2,57 +2,89 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STRINGL 200
char a[STRINGL];
void deleteWhitespace();
op readFunction(char* data, int length);
void getnumbers(char* data, int length, calc_op* structure_ref);
void printstruct(calc_op* formula);
char formulaBuffer[1000];
calc_op* resultCalc = NULL;
calc_op* currentCalc = NULL;
void input() {
printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n");
fgets(a, STRINGL, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace
deleteWhitespace();
calc_op temp;
temp.formel = a;
temp.funktionstyp = readFunction(a, 10);
getnumbers(a,STRINGL, &temp);
printstruct(&temp);
void processInput(char* formStr, int len) {
deleteWhitespace(formStr, len);
if (resultCalc != NULL){
free(resultCalc);
}
resultCalc = malloc(sizeof(calc_op));
memset(resultCalc, 0, sizeof(calc_op));
resultCalc->functionsType = opResult;
memcpy(formulaBuffer, formStr, len);
calc_op * nextCalc = NULL;
nextCalc= malloc(sizeof(calc_op));
memset(nextCalc, 0, sizeof(calc_op));
nextCalc->formular = formStr;
nextCalc->parent = (void*) resultCalc;
nextCalc->functionsType = detectFunctionOperator(formulaBuffer, 10);
if (getNumbers(formulaBuffer, len, nextCalc) == NULL){
resultCalc->children[0] = (void*) nextCalc;
showStruct(nextCalc);
} else {
printf("Formular %s not supported", resultCalc->formular);
}
}
calc_op* getNextCalc(){
calc_op* newCalc = NULL;
if (currentCalc != NULL){
newCalc = (calc_op*) currentCalc->parent;
if (newCalc == NULL) {
return NULL;
}
newCalc->inputNumbers[0]= currentCalc->result;
free(currentCalc);
} else {
newCalc = (calc_op*) resultCalc->children[0];
}
if (newCalc==NULL){
return NULL;
}
currentCalc = newCalc; // get ext calculation
showStruct(currentCalc);
return currentCalc;
}
//Leerzeichen löschen
void deleteWhitespace(){
for(int i=0; i<STRINGL; i++){
if(a[i]==' '){
for (int j=i; j<STRINGL; j++){
a[j]=a[j+1];
void deleteWhitespace(char* formStr, int len){
for(int stringPos=0; stringPos < len; stringPos++){
if((formStr[stringPos] == ' ') || (formStr[stringPos] == '\n') || (formStr[stringPos] == '\r')){
for (int j=stringPos; j < len; j++){
formStr[j]=formStr[j + 1];
}
stringPos--;
}
}
}
//Einfachste Rechenoperationen lesen
op readFunction(char* data, int length){
for(int i=0; i<length; i++){
switch (data[i]){
op detectFunctionOperator(char* formStr, int len){
for(int stringCount=0; stringCount < len; stringCount++){
switch (formStr[stringCount]){
case '+': return opAdd;
case '-': return opSub;
case '/': return opDiv;
case '/':case ':': return opDiv;
case '*': return opMult;
case '^': return opExp;
case '\0': return opEmpty;
}
}
return opNotSupported;
}
//Zahlen auslesen (+)
void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct
char tmp[length];
int i = 0;
char* getNumbers(char* formStr, int len, calc_op* formRef){ //processInput sind: string, länge vom String, berechnungsstruct
// char tmp[len];
char* splitPnt;
int numPos = 0;
char delimiter;
switch (structure_ref->funktionstyp) {
switch (formRef->functionsType) {
case opAdd:
delimiter = '+';
break;
@ -65,21 +97,28 @@ void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: s
case opMult:
delimiter = '*';
break;
default: return;
default: return NULL;
}
memcpy(tmp, data, length); //string kopiert
char *token = strtok(tmp, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt
// memcpy(tmp, formStr, len); //string kopiert
char *token = strtok(formStr, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt
while (token != NULL) {
structure_ref->array[i]=atof(token); // String zu double konvertiert
i++;
formRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert
numPos++;
splitPnt = token;
token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter
}
structure_ref->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert
formRef->arrayLength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert
op type = detectFunctionOperator(splitPnt, strlen(splitPnt) + 1);
if (type != opNotSupported && type != opEmpty){
return splitPnt;
} else {
return NULL;
}
}
void printstruct(calc_op* formula){
printf("Berechnung: %s", formula->formel);
switch (formula->funktionstyp) {
void showStruct(calc_op* formRef){
printf("\nBerechnung: %s\n", formRef->formular);
switch (formRef->functionsType) {
case opAdd:
printf("Rechenoperation: Addition\n"); break;
case opSub:
@ -92,8 +131,8 @@ void printstruct(calc_op* formula){
printf("Fehler bei Auswahl der Rechenoperationen \n");
}
printf("Calculation Variables:\n");
for (int i = 0; i < formula->arraylength; ++i) {
printf("Array[%i] = %f\n", i, formula->array[i]);
for (int arrayCount = 0; arrayCount < formRef->arrayLength; ++arrayCount) {
printf("Array[%i] = %f\n", arrayCount, formRef->inputNumbers[arrayCount]);
}
printf("Result: %f", formula->result);
printf("Result: %f\n", formRef->result);
}

23
src/inputHandling.h

@ -2,20 +2,25 @@
#define INPUTHANDLING_H
typedef enum{
opAdd, opSub, opDiv, opMult, opExp, opLog, opNotSupported
opAdd, opSub, opDiv, opMult, opExp, opLog, opEmpty, opNotSupported, opResult
}op;
typedef struct {
op funktionstyp;
char* formel;
double array[10];
int arraylength;
void* child;
void* parent;
op functionsType;
char* formular;
double inputNumbers[10];
int arrayLength;
void* children[10]; // all children structs which depends on this struct
void* parent; // the parent struct which this struct depends on
double result;
}calc_op;
extern void input();
extern void processInput(char* formStr, int len);
extern void showStruct(calc_op* formRef);
extern void deleteWhitespace(char* formStr, int len);
extern op detectFunctionOperator(char* formStr, int len);
extern char* getNumbers(char* formStr, int len, calc_op* formRef);
extern void showStruct(calc_op* formRef);
extern calc_op* getNextCalc();
#endif // INPUTHANDLING_H

26
src/main.c

@ -1,6 +1,30 @@
#include "helloWorld.h"
#include "inputHandling.h"
#include "outputHandling.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STRING_LENGTH 200
char inputBuffer[STRING_LENGTH] = {0};
void main() {
input();
calc_op *result = NULL;
printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n");
fgets(inputBuffer, STRING_LENGTH, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace
processInput(inputBuffer, strlen(inputBuffer));
do {
result = getNextCalc();
if (result == NULL) {
break;
}
if (result->functionsType==opResult) {
result->result = result->inputNumbers[0];
showResult(result);
break;
}
/* Calculation*/
result->result = 65478;
} while (1);
result = getNextCalc();
}

22
src/outputHandling.c

@ -0,0 +1,22 @@
#include <stdio.h>
#include "outputHandling.h"
#include "inputHandling.h"
void buildHexString(char* string, int num) {
sprintf(string, "0x%X", num);
}
void buildOctString(char* string, int num) {
sprintf(string, "%o", num);
}
void showResult(calc_op* res) {
char string[60] = {0};
printf("Das Ergebnis ist: %f\n", res->result);
printf("Das Ergebnis in dec: %i\n",(int)res->result);
buildHexString(string, (int) res->result);
printf("Das Ergebnis in buildHexString: 0x%s\n", string); ;
buildOctString(string, (int) res->result);
printf("Das Ergebnis in buildOctString: %s\n", string); ;
}

10
src/outputHandling.h

@ -0,0 +1,10 @@
#ifndef OUTPUTHANDLING_H
#define OUTPUTHANDLING_H
#include "inputHandling.h"
extern void buildHexString(char* string, int num);
extern void buildOctString(char* string, int num);
extern void showResult(calc_op* res);
#endif // OUTPUTHANDLING_H

151
test/test_inputHandling.c

@ -3,18 +3,165 @@
#include "unity.h"
#include "inputHandling.h"
char halloWelt[]="Hallo Welt";
char halloWelt2[]="Hallo Welt";
char halloWelt3[]="Ha llo W el t ";
char halloWelt4[]="Ha\n\nllo \r W el\r\r t ";
calc_op formula = {0};
void setUp(void)
{
formula.functionsType = opNotSupported;
}
void tearDown(void)
{
}
void test_inputHandling_NeedToImplement(void)
void test_inputHandling_deleteOneWhiteSpace(void)
{
TEST_IGNORE_MESSAGE("Need to Implement inputHandling");
deleteWhitespace(halloWelt, 10);
TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt);
}
void test_inputHandling_deleteTwoWhiteSpaces(void)
{
deleteWhitespace(halloWelt2, 11);
TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt2);
}
void test_inputHandling_deleteManyWhiteSpaces(void)
{
deleteWhitespace(halloWelt3, 16);
TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt3);
}
void test_inputHandling_deleteAllOtherCharacter(void)
{
deleteWhitespace(halloWelt4, 19);
TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt4);
}
void test_inputHandling_findAddFunctionType(void)
{
op type = opNotSupported;
type = detectFunctionOperator("4+5", 3);
TEST_ASSERT_TRUE(opAdd == type);
}
void test_inputHandling_findSubFunctionType(void)
{
op type = opNotSupported;
type = detectFunctionOperator("4-5", 3);
TEST_ASSERT_TRUE(opSub == type);
}
void test_inputHandling_findMultiFunctionType(void)
{
op type = opNotSupported;
type = detectFunctionOperator("4*5", 3);
TEST_ASSERT_TRUE(opMult == type);
}
void test_inputHandling_findDivFunctionType(void)
{
op type = opNotSupported;
type = detectFunctionOperator("4/5", 3);
TEST_ASSERT_TRUE(opDiv == type);
type = opNotSupported;
type = detectFunctionOperator("4:5", 3);
TEST_ASSERT_TRUE(opDiv == type);
}
void test_inputHandling_findExpFunctionType(void)
{
op type = opNotSupported;
type = detectFunctionOperator("4^5", 3);
TEST_ASSERT_TRUE(opExp == type);
}
void test_inputHandling_findEmptyFunctionType(void)
{
op type = opNotSupported;
type = detectFunctionOperator(halloWelt, 10);
TEST_ASSERT_TRUE(opEmpty == type);
}
void test_inputHandling_getNumbersNoFormular(void)
{
char* pnt = NULL;
pnt = getNumbers(halloWelt, 10, &formula);
TEST_ASSERT_NULL(pnt);
}
void test_inputHandling_getNumbersAddFormular(void)
{
char* pnt = NULL;
char add[] = "4+5";
formula.functionsType = detectFunctionOperator(add,3);
TEST_ASSERT_TRUE(formula.functionsType == opAdd);
pnt = getNumbers(add, 3, &formula);
showStruct(&formula);
TEST_ASSERT_NULL(pnt);
TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]);
TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]);
TEST_ASSERT_EQUAL_INT(2, formula.arrayLength);
}
void test_inputHandling_getNumbersSubFormular(void)
{
char* pnt = NULL;
char sub[] = "4-5";
formula.functionsType = detectFunctionOperator(sub,3);
TEST_ASSERT_TRUE(formula.functionsType == opSub);
pnt = getNumbers(sub, 3, &formula);
showStruct(&formula);
TEST_ASSERT_NULL(pnt);
TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]);
TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]);
TEST_ASSERT_EQUAL_INT(2, formula.arrayLength);
}
void test_inputHandling_getNumbersMultiFormular(void)
{
char* pnt = NULL;
char multi[] = "4*5";
formula.functionsType = detectFunctionOperator(multi,3);
TEST_ASSERT_TRUE(formula.functionsType == opMult);
pnt = getNumbers(multi, 3, &formula);
showStruct(&formula);
TEST_ASSERT_NULL(pnt);
TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]);
TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]);
TEST_ASSERT_EQUAL_INT(2, formula.arrayLength);
}
void test_inputHandling_getNumbersDivFormular(void)
{
char* pnt = NULL;
char div[] = "4/5";
formula.functionsType = detectFunctionOperator(div,3);
TEST_ASSERT_TRUE(formula.functionsType == opDiv);
pnt = getNumbers(div, 3, &formula);
showStruct(&formula);
TEST_ASSERT_NULL(pnt);
TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]);
TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]);
TEST_ASSERT_EQUAL_INT(2, formula.arrayLength);
}
void test_inputHandling_getNextCalc(void)
{
calc_op* pnt = NULL;
char add[] = "4+5";
processInput(add, 3);
pnt = getNextCalc();
TEST_ASSERT_NOT_NULL(pnt);
TEST_ASSERT_TRUE(pnt->functionsType == opAdd);
pnt = getNextCalc();
TEST_ASSERT_NOT_NULL(pnt);
TEST_ASSERT_TRUE(pnt->functionsType == opResult);
pnt = getNextCalc();
TEST_ASSERT_NULL(pnt);
}
#endif // TEST

29
test/test_outputHandling.c

@ -0,0 +1,29 @@
#ifdef TEST
#include "unity.h"
#include "outputHandling.h"
char string[100]= {0};
void setUp(void)
{
}
void tearDown(void)
{
}
void test_outputHandling_buildOctString(void)
{
buildOctString(string, 5555555);
TEST_ASSERT_EQUAL_STRING("25142543", string);
}
void test_outputHandling_buildHexString(void)
{
buildHexString(string, 5555555);
TEST_ASSERT_EQUAL_STRING("0x54C563", string);
}
#endif // TEST
Loading…
Cancel
Save