I've got an assignment to do at my university. The assignment is to take a string of some kind, turn it into an integer, do calculation and return the string.
This is what I've done so far, I'm not great at C so any sort of help would be great.
#include <stdio.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
typedef char* verylong;
verylong multiply_verylong(verylong vl1, verylong vl2);
verylong input_long();
verylong add_verylong(verylong vl1, verylong vl2);
verylong input_long()
{
verylong string=NULL;
char arry[1000];
int input;
printf("enter your verylong number");
gets_s("%s",arry);
for (int i = 0; i < 1000; i++)
{
if (arry[i] == '\n')
{
input = i - 1;
string = (verylong)malloc(i);
break;
}
if ((arry[i] < 48) || (arry[i] > 57))
{
printf("invalid number");
return NULL;
}
}
for (int j = 0; j < input; j++)
{
string[j] = arry[j];
}
string[input + 1] = '\0';
return string;
}
verylong add_verylong(verylong vl1, verylong vl2)
{
verylong input;
char res;
int numofvl1, numofvl2, result, i, j;
numofvl1 = atoi(vl1);
numofvl2 = atoi(vl2);
result = numofvl1 + numofvl2;
input = (int)malloc(result);
snprintf(input,10, "%d", result);
return input;
}
verylong multiply_verylong(verylong vl1, verylong vl2) {
verylong input;
int numofvl1, numofvl2, result;
numofvl1 = atoi(vl1);
numofvl2 = atoi(vl2);
result = numofvl1 * numofvl2;
input = (int)malloc(result);
snprintf(input, 10, "%d", result);
return input;
}
void main()
{
verylong a, b, c;
do {
printf("enter the first long integer: ");
a = input_long();
} while (!a);
do {
printf("enter the second long integer: ");
b = input_long();
} while (!b);
c = add_verylong(a, b);
printf("%s + %s = %s\n", a, b, c);
free(c);
c = multiply_verylong(a, b);
printf("%s * %s = %s\n", a, b, c);
free(c);
free(a);
free(b);
system("pause");
}
The "verylong input_long()" function is to check if the user have entered a string only from numbers. If he didnt, it should be returning null and printing invalid number. about the second function I'm not sure. I've tried to use atio() but it doesnt seems to be working.
once again, appreciate any sort of help. thank you guys.
EDIT: something goes wrong with my first function input_long(); if anyone has any idea what i should do it would be great. I'm suppose to get a string and see if it is all numbers, if its not. it should print a message and return null
add_verylong,resultis an integer, but this function is supposed to returnverylong, which is not an integer.sprintf()to write an integer value into a string. But there are some more issues in your code, e.g.: a) never usegets()it's dangerous ans deprecated b) if your string type is calledverylongdo you expect that you can represent the values in anintvariable? If not, your calculations would be a little bit more dificult. c) don't cast the result ofmalloc()(search this site for that), d) don't use magic numbers like 48 and 57, but '0' and '9'intdatatype only has a certain number of bits to represent a number, roughly 32, take one for the sign, and you are left with 2^31 possible numbers in anint. That means that the highest number you will be able to keep in anintis a decimal number with less than 31 digits (actually, I guess it will be something around 10 ? digits at most)