C语言程序设计现代方法第二版课后习题 第三章习题

C语言程序设计现代方法第二版课后习题 第三章

1
#include <stdio.h>
int main(){
    int month,day,year;
    printf("Enter a date (mm/dd/yyyy): ");
    scanf("%d/%d/%d", &month, &day, &year);
    printf("You entered the date %d%.2d%d", year, month, day);
    return 0;
}
2
#include <stdio.h>
int main(){
    int i_n,month,day,year;
    double price;
    printf("Enter item number: ");
    scanf("%d", &i_n);
    printf("Enter unit price: ");
    scanf("%lf", &price);
    printf("Enter purchase date (mm/dd/yyyy): ");
    scanf("%d/%d/%d", &month, &day, &year);
    printf("Item\tUnit\t\tPurchase\n");
    printf("\t\tPrice\t\tDate\n");
    printf("%-d\t\t$%7.2f\t%-.2d/%-.2d/%-.4d", i_n, price, month, day, year);
    return 0;
}
3
#include <stdio.h>
int main(){
    int GP, GI, PC, IN, CD;
    printf("Enter ISBN: ");
    scanf("%d-%d-%d-%d-%d", &GP, &GI, &PC, &IN, &CD);
    printf("GS1 prefix: %d\n", GP);
    printf("Group identifier: %d\n", GI);
    printf("Publisher code: %d\n", PC);
    printf("Item number: %d\n", IN);
    printf("Check digit: %d", CD);
    return 0;
}
4
#include <stdio.h>
int main(){
    int a, b, c;
    printf("Enter phone number [(xxx) xxx-xxxx]: ");
    scanf("(%d) %d-%d", &a, &b, &c);
    printf("You entered %d.%d.%d", a, b, c);
    return 0;
}
5
#include <stdio.h>
   int main(){
       int a[16];
       printf("Enter the numbers from 1 to 16 in any order: \n");
       for(int i=0; i<16; i++){
       scanf("%d", &a[i]);
   }
       for(int i=0, k=0; i<4; i++){
           for(int j=0; j<4; j++){
               printf("%2d\t", a[k++]);
           }
           printf("\n");
       }
       printf("Row sums: ");
       for(int i=0, k=0; i<4; i++){
           int b = 0;
           for(int j=0; j<4; j++){
               b += a[k++];
           }
           printf("%d ", b);
           if (i==3) printf("\n");
       }
       printf("Column sums: ");
       for(int i=0; i<4; i++){
           int b = 0;
           for(int j=0; j<13+i; j+=4){
               b += a[j];
           }
           printf("%d ", b);
           if(i==3) printf("\n");
       }
       printf("Diagonal sums: %d %d", a[0]+a[5]+a[10]+a[15], a[3]+a[6]+a[9]+a[12]);
       return 0;
}
6
#include <stdio.h>
int main(){
    int num1, denom1, num2, denom2, result_num, result_denom;
    printf("Enter two fractions separated by a plus sign: ");
    scanf("%d/%d+%d/%d", &num1, &denom1, &num2, &denom2);
    result_num = num1 * denom2 + num2 * denom1;
    result_denom = denom1 * denom2;
    printf("The sum is %d/%d\n", result_num, result_denom);
    return 0;
}

以上代码仅供参考,可能存在错误欢迎大家交流指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值