Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there another way to do this instead of using strcpy?

Status
Not open for further replies.

jaclynhi

Technical User
Sep 10, 2002
2
AU
for (i=1; i<length(formula); i++)
{
strcpy(numString[j], portionofinput-1);
j++;
i++;
lastOp = i;
}

strcpy(numString[j], lastpartofinput);

how to do this instead of using strcpy?
how to get last part of input?
how to get portion of input?

input is: 1+1
should print: 2
 
Hi:

I think you'd get more response if you posted all of your variable declarations.

What is &quot;length&quot;? is this a macro replacement for the strlen function?

Regards,

Ed
 
below is my entire code...i cant think of how to change one part in double calArray(char* formula) to something else instead of using string.h.....could someone please help!!! i'm totally lost!!

if user input 1+2+4-3*5
output is 20

#include <stdio.h>
#include <stdlib.h>

#include &quot;arithCalculation.h&quot;

#define TRUE 1
#define FALSE 0

void mainarithCalculation()
{
char formula[70];

printf(&quot;\n **********************************************************************\n&quot;);
printf(&quot;\n\t\tWELCOME to Task 1: Basic Arithmetic Calculation \n&quot;);
printf(&quot;\n **********************************************************************\n&quot;);

while(TRUE)
{
/* get user input */
printf(&quot;\n\nPlease enter a formula:&quot;);
fgets(formula, 70, stdin);
printf(&quot;Input: \t%s &quot;, formula);

/* if input is over maximum length*/
if (length(formula)>70)
{
printf(&quot;\nEquation length is more then 70!&quot;);
continue;
}

/* if input is lesser then 3 in length*/
else if (length(formula)<3)
{
printf(&quot;\nEquation must have two numbers and an operator!&quot;);
continue;
}

/* if input does not contain at least 2 values and operator and doesn't end with an operator */
else if (checkInput(formula))
{
printf(&quot;\nInvalid input!&quot;);
continue;
}
else
{
returnResult(formula);
clrscr();
break;
}
}
}

/*check for errors
Operators will not be the last and first
There must be at least two numbers*/
int checkInput(char* formula)
{
char one, two;
int i;

/*check if first character is a operator*/
if (checkValidOperator(formula[0]))
{
return FALSE;
}

/*checks for operator errors*/
for(i = 1; i< length(formula); i++)
{
one = formula[i-1];
two = formula;
if (checkValidOperator(one) && checkValidOperator(two))
{
return FALSE;
}
}

/*check for valid characters*/
for (i = 0; i< length(formula); i++)
{
if(!checkValidNumber(formula) && !checkValidOperator(formula))
{
return FALSE;
}
}

/*check if last character is a operator*/
if(checkValidOperator(formula))
{
return FALSE;
}
return TRUE;
}

/*check if input is integer*/
int checkValidNumber(char valid)
{
switch(valid)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
return TRUE;
default:
return FALSE;
}
}

int checkValidOperator(char op)
{
switch(op)
{
case '+':
case '-':
case '*':
case '/':
return TRUE;
default:
return FALSE;
}
}

/*check length of input and returns the length*/
int length(char* formula)
{
int i;
for(i = 0; formula!= '\0'; i++)
{
/* no code */
}
return i;
}

/*calculate the input formula*/
double calArray(char* formula)
{
double num1, num2;
char numString[70][20];
int lastOp = 0;
int i = 0;
int j = 0;
double result;

/*************do this without using string.h************/
for(i = 1; i < length(formula); i++)
{
if(checkValidOperator(formula)
{
/*strcpy(numstring[j], portion of formula - 1);
j++;
i++;
lastOp = i;
*/
}
}
/* strcpy(numstring[j], last part of formula);*/
/*********************************************************/



/* first number */
result = numstring[0];

/* calculate everything */
for(i = 0; i <= j-2; i+=2)
{
result = cal(result, atod(numstring[i+2]), numstring[i+1][0]);
}

return result;
}

double cal(double num1, double num2, char op)
{
double result;
switch(op)
{
case '+':
result = (num1 + num2);
break;
case '-':
result = (num1 - num2);
break;
case '*':
result = (num1 * num2);
break;
case '/':
if (num2 != 0)
{
result = (num1 / num2);
}
else
{
printf(&quot;Divide by 0 error!&quot;);
exit(EXIT_FAILURE);
}
default :
printf(&quot;Illegal operator!&quot;);
exit(EXIT_FAILURE);
}
return result;
}

/*to return result and print out*/
void returnResult(char* formula)
{
printf(&quot;\nResult: \t%6.2lf&quot;, calArray(formula));
}

void clrscr()
{
system(&quot;cls&quot;);
}

/*start of program*/
void mainarithCalculation();

/*get result and return to main*/
void returnResult(char* formula);

/*check is user enter correct input*/
int checkInput(char* formula);

/*check if user entered an integer*/
int checkValidNumber(char valid);

/*check if user entered an operator*/
int checkValidOperator(char op);

/*check the length of the input*/
int length(char* formula);

/*calculate the input formula*/
double calArray(char* numString);
double cal(double num1, double num2, char op);

/*clear screen*/
void clrscr();
 
/*
Hi:

Sorry, it's taken so long to get back to you.

First, of all, on my solaris 7 box, I couldn't get your
strcpy method to work. It core dumped.

Anyway, if you have a two-dimensional array like myString[70][20],
you can set individual characters of another string (like formula[] or
*formula) to the multi-dimensional array, but you have to
use both coordinates of the array. Below I'm placing individual
characters of formula to numString[1][0], numString[2][0], etc.

I've hacked up your code to show you what I mean:

Regards,

Ed

*/
#include <stdio.h>

void main()
{
int i;
int j=0;
/*char formula[]=&quot;1+4-6&quot;; */
char *formula=&quot;1+4-6&quot;;
char numString[70][20];

/* I'm starting at 0 and not 1 */
for (i=0; i<strlen(formula); i++)
{
numString[j][0] = formula;
j++;
}

for (i=0; i<strlen(formula); i++)
printf(&quot;%s\n&quot;, numString);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top