Code:
// The FINAL Program
// By Alex Pho
// April 26, 2011
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LENGTH_STRING 101
int ReadingNumber(char *numberToBeRead) {
char c, acceptableCharacters[]="0123456789-,. \n\t", s[]=" ";
//char yesOrno;
char letter, a;
letter=a;
//int continueLoop=1;
int counter=0, errorCheck=0;
while ( (c=getchar())!=EOF && !( c=='=' && counter!=0 ) && counter<MAX_LENGTH_STRING ) {
s[0]=c;
if (c=='=' && counter==0)
// This checks the case the luser typed '=' without a number before
printf("\nGet it right!\n");
else
// so, if you are here, it means you got a character
if (strpbrk(acceptableCharacters, s)==NULL)
// this checks if the character read is acceptable
errorCheck=-1;
// if it isn't, I would type an error message and stop
else // added by Fr. Luca (I would put the next if inside of an else from the previous)
if (strpbrk("0123456789-.", s)!=NULL) {
numberToBeRead[counter++]=c;
}
}
numberToBeRead[counter]=0; //Tells the string to terminate
return 0;
}
int main (int argc, const char * argv[]) {
clock_t initialTicks, finalTicks;
FILE *pointer, *pointer2, *readerpointer, *readerpointer2, *New3, *New2;
char firstNumberToBeRead[101], secondNumberToBeRead[101], stringContainingFormat[101], finalAnswer[102];
int firstNumberLength, secondNumberLength;
// You don't need a structure if it contains only one datum. Just use: char numbers[2][101];
// and here you have 101 myNumbers (which are structures), each one containing a string of 101 characters; not what you meant, I think.
initialTicks=clock();
pointer=fopen("Number1", "w");
printf("Please enter a number up to 100 digits and put an '=' at the end.\n");
ReadingNumber(firstNumberToBeRead);
printf("\nGot it! Give me the second\n");
pointer2=fopen("Number2", "w");
ReadingNumber(secondNumberToBeRead);
printf("\nOkay we got the second\n");
fprintf(pointer,"%s",firstNumberToBeRead);
fprintf(pointer2,"%s",secondNumberToBeRead);
fclose(pointer);
fclose(pointer2);
printf("\nOkay I will add the numbers up for you in a jiffy!\n");
readerpointer=fopen("Number1", "r");
readerpointer2=fopen("Number2", "r");
fscanf(readerpointer, "%s",firstNumberToBeRead); //the variable firstNumberToBeRead is related to the string format
fscanf(readerpointer2, "%s",secondNumberToBeRead);
firstNumberLength = strlen (firstNumberToBeRead);
printf("There are %d digits for the first number\n", firstNumberLength);
secondNumberLength = strlen (secondNumberToBeRead);
printf("There are %d digits for the second number\n", secondNumberLength);
//(d=firstNumber)
//(dd=secondNumber)
char x;
char y;
int i=0.;
int sum;
int carry;
if (firstNumberLength<secondNumberLength) {
New2=fopen("string1", "a");
sprintf(stringContainingFormat,"%%%ds\n",secondNumberLength);
fprintf(readerpointer,stringContainingFormat, secondNumberToBeRead[0]);
printf(stringContainingFormat, secondNumberToBeRead[0]);
fclose(readerpointer);
for (i=0.; i<secondNumberLength; i++) {
}
}
if (firstNumberLength>secondNumberLength) {
New3=fopen("string2", "w");
sprintf(stringContainingFormat,"%%%ds\n",firstNumberLength);
fprintf(New3,stringContainingFormat, firstNumberToBeRead[0]);
printf(stringContainingFormat, firstNumberToBeRead[0]);
fclose(New3);
for (i=0.; i<firstNumberLength; i++) {
}
}
sprintf(x,stringContainingFormat,firstNumberToBeRead);
sprintf(y,stringContainingFormat,secondNumberToBeRead);
//Math Calcuation
/*
finalAnswer=
printf("\nHere is your final answer: %s", finalAnswer); */
finalTicks=clock();
return 0;
}