Tuesday, 19 August 2014

C++ Using Calculator Using Function

#include
#include
#include
void Input (float &x, float &y);

float a=1.0, b=1.0, result;
char operation;


int main ()
{
cout << "Program which performs addition, subtraction, multiplication and subtraction. \n\n";
cout << "Please input calculation operation (eg. 1 + 2): \n";
cin >> a >> operation >> b;
Input (a,b);

cout << "The answer is: " << result << endl;
system ("pause");
return 0;
}


void Input (float &x, float &y)
{
a = x;
b = y;

switch (operation)
{
case '+':
result = x + y;
break;

case '-':
result = x - y;
break;

case '*':
result = x * y;
break;

case '/':
result = x / y;
break;

default:
cout << "Improper operation. Please input a correct calculation operation: \n";
cin >> a >> operation >> b;
Input (a, b);
}
}

Factorial off a number using do-while-loop

#include
#include
void main(){int n,f=1;clrscr();printf("enter any number=");scanf("%d",&n);do{f=f*n;n--;}while(n>0);printf("factorial number is=%d",f);getch();}

C program to find the sum of individual digits of a positive integer


#include

#include

void main()

{

int num, k=1, sum=0;

clrscr();

printf(“Enter the number whose digits are to be added:”);

scanf(“%d”,&num);

while(num!=0)

{

k=num%10;

sum=sum+k;

k=num/10;

num=k;

}

printf(“Sum of the digits:%d”,sum);

getch();

}

C Program to create a linear linked list interactively and print out the list and the total number of items in the list



#include
#include
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head = (node *)malloc(sizeof(node));
create(head);
printf("\n");
printf(head);
printf("\n");
printf("\nNumber of items = %d \n", count(head));
}
void create(node *list)
{
printf("Input a number\n");
printf("(type -999 at end): ");
scanf("%d", &list -> number); /* create current node */
if(list->number == -999)
{
list->
next = NULL;
}
else /*create next node */
{
list->
next = (node *)malloc(sizeof(node));
create(list->next); */ Recursion occurs */
}
return;
}
void print(node *list)
{
if(list->next != NULL)
{
printf("%d-->",list ->number); /* print current item */
if(list->next->next == NULL)
printf("%d", list->next->number);
print(list->next); /* move to next item */
}
return;
}
int count(node *list)
{
if(list->next == NULL)
return (0);
else return(1+ count(list->next));
}

Assign Value Of Pointer In C

#include<stdio.h>
int main()
{
int num[2] = {10,20};
const int *ptr;

ptr = &num[0];

*ptr = 30;

printf("%d",*ptr);

return(0);
}

Splitting a string into tokens in C

#include 
#include 
#include 

#define MAX_LINE_LEN 256
#define NULL_TERM '\0'

/* A function that will print the content of the token_list */
int printcharlist(char **tok_list)
{
    char **pptr = tok_list;

    while (*pptr) {
        printf ("++ %s\n", *pptr);
        pptr++;
    }

    return 0;
}

/* returns the string between 2 pointers (not forgetting to append NULL_TERM */
char *getStr(char *start, char *end)
{
    int length=end-start;
    int i=0;
    char *result = (char *)malloc (sizeof (char) * (length+1));

    while (i < length) {
    result[i] = *start;
    start++;
    i++;
    }

    result[i] = NULL_TERM;

    return result;
}

Finding Tokens in a String Using Strock

#include
#include
#include

void main()
{
char str[80];
char s[]={' ',',',':',';'};
char *t;

clrscr();
printf("enter value\n");
gets(str);
/* get the first token */
t = strtok(str, s);
printf("token is ::\n");
/* walk through other tokens */
while( t != NULL )
{


printf( " %s\n", t );

t = strtok(NULL, s);
;

}