I have one more question. I am making a calulator for long intergers the operations I can perform are + and *;
I am having problem making an add function. I don't know what to do I am lost.
The functions that I am stuck in are:
int add_two_digits(int d1, int d2, int *carry_in, int * result, int *carry_out);
list_head * add_two_numbers (list_head N1, list_head N2);
I tried to do the formating of the code but since I bring from textpad to this bord the formating becomes horrible.
The code that I have now is:
Code:
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
#define FALSE 0
char c = '\0';
int currentChar = 0;
int numOfCharsInC = 0;
typedef struct node
{
int integerone;
struct node * link;
} list_head;
list_head*list1,*list2,*result;
//Concating the 2 lists together
list_head * concat(list_head *p,list_head *q)
{
list_head *x,*r;
if (p==NULL)
r=q;
if (q==NULL)
r=p;
else
{
x=p;
r=x;
while(x->link!=NULL)
x=x->link;
x->link=q;
}
return(r);
}
//Inserting in the list
int insert_to_list(list_head**head,int digit){
list_head *newNode= malloc(sizeof(list_head));
newNode->integerone=digit;
newNode->link=NULL;
if(*head==NULL){
*head=newNode;
return 1;
}
else{
list_head*temp=*head;
while(temp->link!=NULL){
temp=temp->link;
}
temp->link=newNode;
}
return 0;
}
//Menu choices
void options(){
printf("\n1. Insert first number\n");
printf("1. Insert second number\n");
printf("3. Add numbers\n");
printf("4. Multiply numbers\n");
printf("5. Process file\n");
printf("6. Quit\n");
printf("Enter Option: ");
scanf("%c",&c);
fflush(stdin);
}
//Printing the list Contents
void printList(list_head*head){
list_head*temp=head;
while(temp){
printf("%d " , temp->integerone);
temp=temp->link;
}
}
//Reverses the list
list_head *reverse_list(list_head *head){
list_head *p,*q,*r;
p=head;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->link;
q->link=r;
}
head = q;
printList(head);
}
//Add two digits <!!!!!THIS IS THE FUNCTION THAT I NEED HELP WITH!!!!!!!>
int add_two_digits(int d1, int d2, int *carry_in, int * result, int *carry_out){
*carry_in=0;
for (i=0;; i++) {
*result =*result = d1+d2+*carry_in;
if(result>=10){
*result=*result-10;
*carry_out=1;
}
else{
*carry_out=0;
}
}
//allocate memory for a new list header
list_head *create_list(void){
list_head*p=NULL;
p=malloc(sizeof(list_head));
p->link=NULL;
return p;
}
//Main function
int main(void){
options();
while (c!='6'){
while ((currentChar = getchar()) != '\n' && currentChar != EOF){
if(numOfCharsInC==0) {
c = currentChar;
}
numOfCharsInC++;
}
if(c=='1'){
fflush(stdin);
printf("\n1. Insert first number\n");
scanf("%c",&c);
fflush(stdin);
do{
insert_to_list(&list1,c-'0');
} while((c = getchar()) != '\n');
numOfCharsInC = 0;
options();
}
else if(c=='2'){
fflush(stdin);
printf("\n2. Insert second number\n");
scanf("%c",&c);
fflush(stdin);
do{
insert_to_list(&list2,c-'0');
} while((c = getchar()) != '\n');
numOfCharsInC = 0;
options();
}
else if (c== '3'){
fflush(stdin);
printf("\n3. Add numbers\n");
numOfCharsInC = 0;
options();
}
else if(c== '4'){
fflush(stdin);
printf("\n4. Multiply numbers\n");
numOfCharsInC = 0;
options();
}
else if(c=='5'){
fflush(stdin);
printf("\n5. Process file\n");
numOfCharsInC = 0;
options();
}
else if(c=='6'){
fflush(stdin);
numOfCharsInC = 0;
options();
break;
}
else{
printf ("Invalid menu choice - try again\n");
break;
}
}
return 0;
}
Thanks alot for help. I really appreciate it.
Sara