Create a list of functions that uses a 64-element array of digits to store integers as large as 64 digits each (we will call these integers Huge Integers).
requirements
The program must contain the following functions:
char* add( char* A , char* B, char* C );
char* subtract( char* A, char* B, char* C );
void negate( char* A, char* B );
void copy( char* A, char*B );
void read( char* A );
void print( char* A );
int getSign( char* A );
int getLength( char* A );
You must also write a main to test these functions.
Description of the functions above
add Adds two Huge Integers (A, B) and returns the result in C. You must take into consideration that the numbers are signed.
subtract Same as above but the operation is subtract.
negate Returns –A in B.
copy Copies A to B.
read Reads A from the keyboard.
print Displays A.
int getSign( char* A ) Returns 0 for positive, 1 for negative.
int getLength( char* A ) Returns the number of valid digits in A.
The numbers are considered signed so a position in the array must be reserved for the sign.
The following C++ libraries functions manipulating strings could be used:
strlen -- returns the length of a given string
#include <cstring>
size_t strlen( char *str );
The strlen() function returns the length of str (determined by the number of characters before null termination). Str may not be null.
strcpy -- copies one string to another
#include <cstring>
char *strcpy( char *dest, const char *src );
The strcpy() function copies characters in the string src to the string dest, including the null termination. The return value is dest. Note that strcpy() does not perform bounds checking, and thus risks overrunning from or to. For a similar (and safer) function that includes bounds checking, see strncpy().
strncpy -- copies one string to another
#include <cstring>
char *strncpy( char *to, const char *from, size_t count );
The strncpy function copies at most count characters of from to the string to. If from has less than count characters, the remainder is padded with '\0' characters. The return value is the resulting string.
You can use the following test to check the validity of a digit i in the Huge Integer number :
If( number[ i ] <= '9' && number[ i ] >= '0' )
k look this is a sample about the function we should use
#include <iostream>
#include <string.h>
using namespace std;
// This function fills an array of characters of size 65 with a string
// of characters composed of digits. The digits values (not codes) are
// copied to the array the least significant (smallest weight) being the
// first element in tha array. For example if the string is "123", the
// array will be filled with {3,2,1,'\0'} not {'3','2','1','\0'}
void build( char const* strNumber, char* str )
{
int length = strlen( strNumber );
cout << length << endl;
for( int i = length - 1 ; i >= 0 ; i-- )
{
if( strNumber[ i ] < '0' || strNumber[ i ] > '9' )
break;
else
str[ length - 1 - i ] = strNumber[ i ] - '0';
}
str[ length ] = '\0';
}
// This functions reads a string of digits from the keyboard and uses it to create
// an array of digit values (see build above)
void read( char* str )
{
char* s = new char[ 65 ];
cout << "Type a number";
cin >> s;
build( s, str );
}
int main ()
{
char* s = new char[ 65 ];
read( s );
cout << s;
You should be able to implement the other functions, things like copy() is easy, the teacher has already given you the answer: char *strncpy( char *to, const char *from, size_t count );
u know my major is civil eng not programing so helping in this HW will help me getting a good grade for that i wont get a lower GPA, because C++ is only for civil an introductory course
hehe man i m in eng stuff one of the best students trust me on this , but programing not my stuff, so man if u want to help i appreciate it , thank u for your time man ))