Showing posts with label C প্রোগ্রামিং - 12 অধ্যায়–Structure. Show all posts
Showing posts with label C প্রোগ্রামিং - 12 অধ্যায়–Structure. Show all posts

Wednesday, February 4, 2015

structure pointer

 

structure pointer

 

 

#include<stdio.h>
struct self_ref{
int a;
struct self_ref *next;
 
 
};
void print_all(struct self_ref  pr)
{
   if (pr.a==0)
    {
        return ;
    }
//    printf("%d ",pr.a);
    print_all(*pr.next);
    printf("%d ",pr.a);
}
 
int main()
{
        struct self_ref var1,var2,var3,var4;
 
        var1.a = 5;
        var1.next = &var2;
        var2.a = 6;
        var2.next = &var3;
        var3.a = 7;
        var3.next = &var4;
        var4.a = 0;
    print_all(var1);
}

structure function

#include<stdio.h>
 
struct my{
int x;
float y;
 
};
void print (struct my pr)
{
    printf("x = %d\n",pr.x);
    printf("y = %f\n",pr.y);
 
}
struct my add(struct my a,struct my b)
{
    struct my result;
    result.x = a.x+b.x;
    result.y = a.y+b.y;
    return result;
};
int main()
{
    struct my mem,mem1,sum;
    mem.x=5;
    mem.y=6.6;
    mem1=mem;
    print(mem);
    print(mem1);
 
 
    sum = add(mem,mem1);
    printf("sum\n");
    print(sum);
}

structure pointer

#include<stdio.h>
#include<string.h>
 
 
struct structure
{
    char name [12];
    int value;
};
void swap (struct structure *a,struct structure *b )
{
    struct structure temp;
    strcpy(temp.name,a->name);
    temp.value=a->value;
 
    strcpy(a->name,b->name);
    a->value=b->value;
 
    strcpy(b->name,temp.name);
    b->value=temp.value;
 
}
int main()
{
    struct structure val1,val2;
    printf("Enter the val 1 and value :");
    scanf("%s%d",val1.name,&val2.value);
 
    printf("Enter the val 2 and value :");
    scanf("%s%d",val2.name,&val2.value);
    if(val2.value>val1.value)
        swap(&val2,&val1);
printf("val1 (%d) %s\n",val1.value,val1.name);
 
printf("val2 (%d) %s\n",val2.value,val2.name);
return 0;
}

Nexted structure

Nexted structure

 

#include<stdio.h>
struct birthday
    {
        int day;
        int month;
        int year;
    };
      struct Sex
    {
        int male;
        int female;
    };
    
struct student
{
    char first_name[50];
    char last_name[50];
    struct birthday birth;
    struct Sex sex ;
  
};
struct teacher
{
    char first_name[50];
    char last_name[50];
    char birthday birth;
    char Sex sex;
    int salary;
    
  
};
 
int main()
{
    struct student user;
    printf("ENter First Name :   ");
    scanf("%s",user.first_name);
    printf("ENter Last Name  :   ");
    scanf("%s",user.last_name);
 
    
    printf("\nENter Your Birth info :  ");
    printf("\n\t DAY  :  ");
     scanf("%d",&user.birth.day);
    printf("\t MONTH  :  ");
    scanf("%d",&user.birth.month);
 
    printf("\t Year  :  ");
    scanf("%d",&user.birth.year);
 
    y:
    printf("Enter Your Sex ; m/M=Male F=Female :  \n");
    scanf("%s",sexck);
    if(strcmp(sexck,"M")==0||strcmp(sexck,"m")==0)
        {
            user.sex.male=1;
            user.sex.female=0;
        }
    else if(strcmp(sexck,"F")==0||strcmp(sexck,"f")==0)
    {
            user.sex.male=0;
            user.sex.female=1;
    }
    else
    {
        printf("\nYou have dial Wrong keyword\n");
 
        goto y;
        return 0;
    }
    printf(" \nYOUR information is HERE \n");
printf("FIRST name:%s\n",user.first_name);
printf("Last name:%s\n",user.last_name);
printf("User name:%s\n",user.username);
printf("password:%s\n",user.password);
 
 
printf("User birthday:%d\n",user.birth.day);
printf("User birthmonth:%d\n",user.birth.month);
printf("User birthyear%d\n",user.birth.year);
printf("SEX : ");
 
if(user.sex.male==1)
{
    printf("Male\n");
}
else
{
    printf("Female\n");
}
return 0;
}

C প্রোগ্রামিং - 12 অধ্যায়–Structure () with hide password example

 

#include<stdio.h>
struct creat_account
{
    char first_name[50];
    char last_name[50];
    char username[50];
    char password[51];
    struct birthday
    {
        int day;
        int month;
        int year;
    }birth;
    struct Sex
    {
        int male;
        int female;
    }sex;
};
void pass_gen(char pass[])
{
    int i=0;
    char ch;
    while(1)
    {
        ch=getch();
        if(ch==13)
        {
            break;
        }
        else
        putchar('*');
            pass[i++]=ch;
    }
    pass[i]='\0';
}
int main()
{
    char passck[20];
    char sexck[10];
    struct creat_account user;
    printf("ENter First Name :   ");
    scanf("%s",user.first_name);
    printf("ENter Last Name  :   ");
scanf("%s",user.last_name);
 
    printf("ENter your User Name  :   ");
    scanf("%s",user.username);
z:
    printf("ENter your pasword :   ");
    pass_gen(user.password);
 
    printf("\nENter repeat your password  : ");
    pass_gen(passck);
    if(strcmp(user.password,passck)!=0)
    {
        printf("\nDon't Match\n");
        goto z;
    }
    printf("\nENter Your Birth info :  ");
    printf("\n\t DAY  :  ");
     scanf("%d",&user.birth.day);
    printf("\t MONTH  :  ");
    scanf("%d",&user.birth.month);
 
    printf("\t Year  :  ");
    scanf("%d",&user.birth.year);
 
    y:
    printf("Enter Your Sex ; m/M=Male F=Female :  \n");
    scanf("%s",sexck);
    if(strcmp(sexck,"M")==0||strcmp(sexck,"m")==0)
        {
            user.sex.male=1;
            user.sex.female=0;
        }
    else if(strcmp(sexck,"F")==0||strcmp(sexck,"f")==0)
    {
            user.sex.male=0;
            user.sex.female=1;
    }
    else
    {
        printf("\nYou have dial Wrong keyword\n");
 
        goto y;
        return 0;
    }
    printf(" \nYOUR information is HERE \n");
printf("FIRST name:%s\n",user.first_name);
printf("Last name:%s\n",user.last_name);
printf("User name:%s\n",user.username);
printf("password:%s\n",user.password);
 
 
printf("User birthday:%d\n",user.birth.day);
printf("User birthmonth:%d\n",user.birth.month);
printf("User birthyear%d\n",user.birth.year);
printf("SEX : ");
 
if(user.sex.male==1)
{
    printf("Male\n");
}
else
{
    printf("Female\n");
}
return 0;
}