amol0010
10-05-2009, 06:19 AM
Hi - I am supposed to do the following:
1) In put a main string using getline.
e.g. 124124124+123124124 or 123123123*123412412 or 12312312^123123
2) split this string into three strings - 124124124, + and 123124124 or 123123123, * and 123123123, I guess you get the idea
3) I am supposed to carry out the addition, multiplication and exponentiation operations on these left and right operators using character arrays.
i have written the following program to do this:
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
// Global types and constants ///////////////////////////////////
const int MAX_STRING_LENGTH = 45;
//Define a character array (string) and an integer string
//The source is the array of integers
typedef char String[MAX_STRING_LENGTH];
typedef int Integer[MAX_STRING_LENGTH];
//Populate an integer array from the source string
void PopulateIntArray(const String source, int target[]);
//Add the two integers in the string
void AddInteger(const String l, const String r);
//Mulitply the integers in the string
void MultiplyInteger(String l, String r);
//Exponent the integers in the string
void ExponentInteger(String l, String r);
//Initialize the string to ""
void NullString( String& s );
//Extract the integers from the input string
void Extract( const String& source,int& locinSource, String &target_int );
//Extract the integers from the input string
void ExtractOp( const String& source, String &op );
void CharToString( char c, String& s );
int main()
{
//Three strings, left, right and the operator
String source, l, r, op;
Integer left, right, main;
cout<<"Enter the string with the operator in between the intergers, for e.g.\n\nTo add 2 and type in 2+3\n\nTo multiply 2 and 3 type 2*3\n\nTo raise 2 to 3 enter 2^3"<<endl;
cin.getline(source,MAX_STRING_LENGTH);
int locInSource =0;
//Initialize all left and right strings to null
NullString(l);
NullString(r);
Extract(source,locInSource, l);
Extract(source,locInSource, r);
ExtractOp(source, op);
AddInteger(l,r);
puts(l);
puts(r);
puts(op);
system("PAUSE");
return 0;
}
//Initialize the string to ""
void NullString( String& s )
{ s[0] = '\0';
};
//Extract the integers from the input string
void Extract( const String& source, int &locInSource, String &target_int)
{
NullString(target_int);
String temp;
// Starting at locInSource, skip Leading blanks up
// to first character
while( (source[locInSource] == '+' || source[locInSource] == '*' || source[locInSource] == '^') && ( locInSource < MAX_STRING_LENGTH) )
locInSource++;
while( true )
{ // quit if at symbol to either multiply, divide or raised to
if( source[locInSource] == '+' || source[locInSource] == '*' || source[locInSource] == '^')
break;
//quit if at end of string
if( source[locInSource]=='\0') break;
//convert character and concatenate to string
CharToString( source[locInSource], temp );
strcat_s(target_int, temp );
locInSource++;
}
};
//Extract the operator from the string
void ExtractOp( const String& source, String &op)
{
NullString(op);
String temp;
int locInSource = 0;
// Starting at locInSource, skip Leading blanks up
// to first character
while( isspace(source[locInSource]) && ( locInSource < MAX_STRING_LENGTH))
locInSource++;
while(true){
// quit if at symbol to either multiply, divide or raised to
if( source[locInSource] == '+' || source[locInSource] == '*' || source[locInSource] == '^')
{//convert character and concatenate to string
CharToString( source[locInSource], temp );
strcat_s(op, temp );
break;
}
//quit if at end of strings
if(source[locInSource]=='\0') break;
locInSource++;
}
};
void CharToString( char c, String& s )
{ s[0] = c;
s[1] = '\0'; // or just 0
}
// Copy the string ( character array into an integer array
void PopulateIntArray(const String source, int target[])
{
int val,j=0;
for(int i=0;i<MAX_STRING_LENGTH;i++)
target[i]=0;
for(int i=strlen(source)-1;i>=0;i--)
{
val=(int)source[i]-48;
target[j++]=val;
}
};
//Add the two integers in the string
void AddInteger(const String l, const String r )
{
int arr1[MAX_STRING_LENGTH], arr2[MAX_STRING_LENGTH];
PopulateIntArray(l, arr1);
PopulateIntArray(r, arr2);
int temp[MAX_STRING_LENGTH] = {0};
int carry=0,sum=0, flag = 0;
int i = 0, j = 0;
for( i=0;i<MAX_STRING_LENGTH;i++)
{
sum=(arr1[i]+arr2[i]);
temp[i]=(sum+carry)%10;
carry=(sum+carry)/10;
}
for(i=MAX_STRING_LENGTH, j = 0;i>=0;i--, j++)
{
while(flag==0&&arr1[i]==0)
i--;
if(flag==0)
flag=1;
printf("%d",temp[i]);
}
};
If you can see I am typecasting the character array into integers which is what I am supposed to do.
How do I store the resultant addition into an array ?
The AddArray is correctly outputting the addition of integers, but I am just outputting it character by character.
However, I am supposed to write multiplication and exponentiation functions.
I am not sure how to go about writing a multiplication function i.e. I give two parameters - string 1 and string 2 to the multiplication function and then resultant is string 1*string 2.
I would be really grateful if someone could help me out on this one.
Thanks,
Amol
1) In put a main string using getline.
e.g. 124124124+123124124 or 123123123*123412412 or 12312312^123123
2) split this string into three strings - 124124124, + and 123124124 or 123123123, * and 123123123, I guess you get the idea
3) I am supposed to carry out the addition, multiplication and exponentiation operations on these left and right operators using character arrays.
i have written the following program to do this:
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
// Global types and constants ///////////////////////////////////
const int MAX_STRING_LENGTH = 45;
//Define a character array (string) and an integer string
//The source is the array of integers
typedef char String[MAX_STRING_LENGTH];
typedef int Integer[MAX_STRING_LENGTH];
//Populate an integer array from the source string
void PopulateIntArray(const String source, int target[]);
//Add the two integers in the string
void AddInteger(const String l, const String r);
//Mulitply the integers in the string
void MultiplyInteger(String l, String r);
//Exponent the integers in the string
void ExponentInteger(String l, String r);
//Initialize the string to ""
void NullString( String& s );
//Extract the integers from the input string
void Extract( const String& source,int& locinSource, String &target_int );
//Extract the integers from the input string
void ExtractOp( const String& source, String &op );
void CharToString( char c, String& s );
int main()
{
//Three strings, left, right and the operator
String source, l, r, op;
Integer left, right, main;
cout<<"Enter the string with the operator in between the intergers, for e.g.\n\nTo add 2 and type in 2+3\n\nTo multiply 2 and 3 type 2*3\n\nTo raise 2 to 3 enter 2^3"<<endl;
cin.getline(source,MAX_STRING_LENGTH);
int locInSource =0;
//Initialize all left and right strings to null
NullString(l);
NullString(r);
Extract(source,locInSource, l);
Extract(source,locInSource, r);
ExtractOp(source, op);
AddInteger(l,r);
puts(l);
puts(r);
puts(op);
system("PAUSE");
return 0;
}
//Initialize the string to ""
void NullString( String& s )
{ s[0] = '\0';
};
//Extract the integers from the input string
void Extract( const String& source, int &locInSource, String &target_int)
{
NullString(target_int);
String temp;
// Starting at locInSource, skip Leading blanks up
// to first character
while( (source[locInSource] == '+' || source[locInSource] == '*' || source[locInSource] == '^') && ( locInSource < MAX_STRING_LENGTH) )
locInSource++;
while( true )
{ // quit if at symbol to either multiply, divide or raised to
if( source[locInSource] == '+' || source[locInSource] == '*' || source[locInSource] == '^')
break;
//quit if at end of string
if( source[locInSource]=='\0') break;
//convert character and concatenate to string
CharToString( source[locInSource], temp );
strcat_s(target_int, temp );
locInSource++;
}
};
//Extract the operator from the string
void ExtractOp( const String& source, String &op)
{
NullString(op);
String temp;
int locInSource = 0;
// Starting at locInSource, skip Leading blanks up
// to first character
while( isspace(source[locInSource]) && ( locInSource < MAX_STRING_LENGTH))
locInSource++;
while(true){
// quit if at symbol to either multiply, divide or raised to
if( source[locInSource] == '+' || source[locInSource] == '*' || source[locInSource] == '^')
{//convert character and concatenate to string
CharToString( source[locInSource], temp );
strcat_s(op, temp );
break;
}
//quit if at end of strings
if(source[locInSource]=='\0') break;
locInSource++;
}
};
void CharToString( char c, String& s )
{ s[0] = c;
s[1] = '\0'; // or just 0
}
// Copy the string ( character array into an integer array
void PopulateIntArray(const String source, int target[])
{
int val,j=0;
for(int i=0;i<MAX_STRING_LENGTH;i++)
target[i]=0;
for(int i=strlen(source)-1;i>=0;i--)
{
val=(int)source[i]-48;
target[j++]=val;
}
};
//Add the two integers in the string
void AddInteger(const String l, const String r )
{
int arr1[MAX_STRING_LENGTH], arr2[MAX_STRING_LENGTH];
PopulateIntArray(l, arr1);
PopulateIntArray(r, arr2);
int temp[MAX_STRING_LENGTH] = {0};
int carry=0,sum=0, flag = 0;
int i = 0, j = 0;
for( i=0;i<MAX_STRING_LENGTH;i++)
{
sum=(arr1[i]+arr2[i]);
temp[i]=(sum+carry)%10;
carry=(sum+carry)/10;
}
for(i=MAX_STRING_LENGTH, j = 0;i>=0;i--, j++)
{
while(flag==0&&arr1[i]==0)
i--;
if(flag==0)
flag=1;
printf("%d",temp[i]);
}
};
If you can see I am typecasting the character array into integers which is what I am supposed to do.
How do I store the resultant addition into an array ?
The AddArray is correctly outputting the addition of integers, but I am just outputting it character by character.
However, I am supposed to write multiplication and exponentiation functions.
I am not sure how to go about writing a multiplication function i.e. I give two parameters - string 1 and string 2 to the multiplication function and then resultant is string 1*string 2.
I would be really grateful if someone could help me out on this one.
Thanks,
Amol