作业整理12.3

来源:互联网 发布:社会调查数据方法 编辑:程序博客网 时间:2024/05/22 11:47

1. 对 “hello  world” 如何输入,并输出 ,用2种方法 ;

2. 复习 : 这两天的内容;

     c的代码结构 : if else用法 ,switch用法;

    for,do while,while ,break,continue用法 ;

  函数的 值传递,地址传递 ,extern, const,static等关键字 用法 ;

  一维数组,二维数组的用法 ;

 

3. 算法作业 

作业:

1.从键盘输入任意年月,能输出该月的天数

2.编写计算器程序,完成加减乘除运算。

3. 不用库函数strcpy实现字符串的拷贝;

4.不在初始化的时候给字符串赋值,运行中如何实现?

5.闰年的判断

 1. hello  world 如何输入,并输出 

#include <stdio.h>  

int main()  

{  

   //版本1  

   int i = 0;   

    for (;i < 11; i++)  

    {  

        char ch = "Hello word!"[i];  

      printf("%c",ch);  

    }  

  putchar('\n');  

   //版本2  

    char *p = "hello word!";  

    for (i = 0; i < 11; i++)  

    {  

        printf("%c",p[i]);  

    }  

    putchar('\n');  

    //版本3  

    char *q = "hello word!";  

    for (i = 0; i < 11; i++)  

    {  

        printf("%c",*q++);  

    }  

    putchar('\n');  

    //版本4  

    char r[] = "hello word!";  

    for (i = 0; i < 11; i++)  

    {  

        printf("%c",r[i]);  

    }  

    putchar('\n');  

    //版本5  

    char s[] = "hello word!", *t = s;  

   for (i = 0; i < 11; i++)  

   {  

        printf("%c",*t++);  

    }  

    putchar('\n');  

    //版本6  

   char u[] = "hello word!", *v = &u[0];  

   for (i = 0; i < 11; i++)  

    {  

        printf("%c",*v++);  

    }  

      

    return 0;  

}  

 

2编写一个计算器程序,实现加减乘除

#include <stdio.h>

 

int main ()

{

    float num1, num2, result;

    char a;    //变量a用来接收运算符。

    

    scanf ("%f%c%f",&num1, &a, &num2);

 

    switch (a)

    {

        case '+' : result = num1 + num2;

            break;

        case '-' : result = num1 - num2;

            break;

        case '*' : result = num1 * num2;

            break;

        case '/' : result = num1 / num2;

    }

    

    printf ("%f%c%f = %.2f\n",num1, a , num2, result);    

 

    return 0;

}

 

3、从键盘输入任意年月,能输出该月的天数

 

#include <stdio.h>  

#include <stdlib.h>  

int main()  

{  

    int year,month;  

    int isLeap=0;  

    int days;  

    char c;  

    scanf("%d",&year);  

    scanf("%c",&c);  

    scanf("%d",&month);  

    if((year%4==0&&year%100!=0)||(year%400==0))  

        isLeap=1;  

    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)  

        days=31;  

    if(month==4||month==6||month==9||month==11)  

        days=30;  

    if(isLeap==1&&month==2)  

        days=29;  

    if(isLeap==0&&month==2)  

        days=28;  

    printf("%d",days);  

    return 0;  

}  

 

4、不用库函数strcpy实现字符串的拷贝

#include <stdio.h>

 

int main ()

{

    int i;

    

    char a1[] = "hey man!", a2[9];

 

    for (i = 0; a1[i] != '\0'; i++)

    {

        a2[i] = a1[i];

}

a2[8]=’\0’;

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

 

    return 0;

}

 

5不在初始化的时候给字符串赋值,运行中如何实现?

 

#include <stdio.h>

 

int main ()

{

    int i;

    

    char a1[] = "hello", a2[6];

 

    for (i = 0; a1[i] != '\0'; i++)

    {

        a2[i] = a1[i];

    }

    

    a2[5] = ‘\0’;

 

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

 

    return 0;

}

6、闰年的判断

 

 

#include <stdio.h>  

int is_leap_year(int);  

  

int main(){  

    int i,j;  

    printf("please input a number:");  

    scanf("%d",&i);  

    printf("i=%d/n",i);  

    j = is_leap_year(i);  

    if (j){  

        printf("is leap year/n");  

    }  

    else{  

        printf ("not leap year/n");  

    }  

}  

int is_leap_year(int year){  

    if (year % 4  == 0 && year % 100 != 0  || year % 400 == 0)  

        return 1;  

    else  

        return 0;  

}  

原创粉丝点击