高级语言程序设计作业 10/21

6.16编程练习

1

/* 6.16.1.c -- 利用数组,打印26个小写字母 */
#include <stdio.h>

int main()
{
    char alphabet[26];
    for (int i = 0; i < 26; i++)
    {
    	alphabet[i] = 97 + i;
    	printf("%c\n", alphabet[i]);
	}
    return 0;
}

5

/* 6.16.5.c -- 金字塔 */
#include <stdio.h>

int main()
{
    char ch;
    printf("Please enter an upper character: ");
    scanf("%c", &ch);
    
    int off = ch - 65;
    for (int i = 0; i <= off; i++)
    {
        // 处理空格。 
        for (int sp = 0; sp < off - i; sp++)
            printf(" ");
        
        // 处理顺序字母输出。
        int center = i;
        for (int j = 0; j <= center; j++)
        {
            printf("%c", j + 65);
        }
        
        // 处理倒序字母输出。
        for (int j = center - 1; j >= 0; j--)
        {
            printf("%c", j + 65);
        } 
        
        printf("\n");
    } 
    return 0;
}

7

/* 6.16.7.c -- 倒序打印一个单词 */
#include <stdio.h>
#include <string.h>

int main()
{
	char word[100];
	scanf("%s", word);
	
	int len = strlen(word);  // strlen读到第一个\0为止,因此声明数组时不用担心长度问题 
	for (int i = len - 1; i >= 0; i--)
	{
		printf("%c", word[i]);
	}
	return 0;
}

8

/* 6.16.8.c -- 输入两个浮点数,打印两数之差除以两数之积的结果。*/
#include <stdio.h>

int main()
{
	double n1, n2;
	
	while (1)
	{
		printf("Please enter two floating numbers, "
			"seperated by space:\n");
		if (scanf("%lf %lf", &n1, &n2))
		{
			double result = fabs(n1 - n2) / (n1 * n2);
			printf("Result is %.2f", 
				result);
		}
		printf("Please make sure you entered two floating numbers!\n");
        return 0;
	}	
}

10

/* 6.16.10.c -- 计算平方和 */
#include <stdio.h>

int main()
{
	int lower, upper;
	
	while (1)
	{
		printf("Enter lower and upper integer limits: ");
		scanf("%d %d", &lower, &upper);
	
		if (lower >= upper)
			break;
			
		int losqr = lower * lower;
		int upsqr = upper * upper;
		int sum = 0;
		for (int current = lower; current <= upper; current++)
		{
			sum += current * current;
		}
		printf("The sums of the squares from %d to %d is %d\n", losqr, upsqr, sum);
	}
	return 0;
}

12

/* 6.16.12.c -- 计算无限序列 */
#include <stdio.h>
#include <math.h>

int main()
{
	int count;
	
	while (1)
	{
		printf("Please enter a iterator count: ");
		scanf("%d", &count);
	
		if (count <= 0)
			break;
			
		// 第一个序列:1 + 1/2 + 1/3 + ...
		{
			double sum = 0;
			for (int i = 0; i < count; i++)
			{
				sum += 1.0 / (i + 1);	
			}
			printf("After interating %d times,\n", count);
			printf("1 + 1/2 + 1/3 + 1/4 + ... = %.3f\n", sum);
		}
		
		// 第二个序列,1 - 1/2 + 1/3 - ...
		{
			double sum = 0;
			for (int i = 0; i < count; i++)
			{ 
				sum += pow(-1, i) * 1.0 / (i + 1);	
			}
			printf("1 - 1/2 + 1/3 - 1/4 + ... = %.3f\n", sum);
		}
	}
	return 0;
}

13

/* 6.16.13.c -- 打印2的前8次幂 */
#include <stdio.h>
#include <math.h>

int main()
{
	int arr[8];
	for (int i = 0; i < 8; i++)
	{
		arr[i] = pow(2, i + 1);
	}
	
	int i = 0;
	do
	{
		printf("%d ", arr[i]);
	}
	while (++i < 8);
	
	return 0;
}

15

/* 6.16.15.c -- 倒序输出一行 */
#include <stdio.h>
#include <string.h>

int main()
{
	char line[256];
	printf("Please enter a line:\n");
	scanf("%s", line);
	
	int len = strlen(line);
	for (int i = len -1; i >= 0; i--)
	{
		printf("%c", line[i]);	
	}
	return 0;
}

16

/* 6.16.16.c -- 利息计算 */
#include <stdio.h>
#include <math.h>

int main()
{
	int year = 1;
	
	double daphne = 100, 
	       deirdre = 100;
	while (1)
	{
	    daphne += 100.0 * 0.1;
		deirdre *= (1 + 0.05);
		
		if (daphne < deirdre)
		{
			printf("第%d年超过", year);
			break;
		}
		year++;
	}
	return 0;
}

18

/* 6.16.18.c -- Dunbar's number */
#include <stdio.h>
#include <string.h>

int main()
{
    int friends = 5;
    int i = 1;
    do
    {
        friends -= i;
        friends *= 2;
        printf("第%d周有%d个朋友。\n", i, friends);
        i++;
    } while (friends <= 150);
    return 0;
}

7.12课后作业

1

/* 7.12.1.c -- 捕获用户输入,直到#为止,并输出指定字符被捕获的次数。 */
#include <stdio.h>
#include <string.h>

int main()
{
    char ch;
    int space = 0,
           lb = 0,
        other = 0;
        
    while ((ch = getchar()) != '#')
    {
        switch (ch)
        {
            case ' ': space++; break;
            case '\n': lb++;   break;
            default:  other++; break;
        }
    }
    printf("Space count: %d\n", space);
    printf("Line break count: %d\n", lb);
    printf("Other characters count: %d\n", other);
    return 0;
}

2

/* 7.12.2.c -- 捕获用户输入,直到#为止,并输出所有捕获的字符,一行8个。 */
#include <stdio.h>
#include <string.h>

int main()
{
    char cache[100];
    char ch;
    int i = 0;
    while ((ch = getchar()) != '#')
        cache[i++] = ch;
    
    int counter = 0;
    while (counter < i)
    {
        char c = cache[counter];
        printf("%c-%d ", c, c);
        
        if (++counter % 8 == 0)
            printf("\n");
    }
    return 0;
}

4

/* 7.12.4.c -- 捕获用户输入,直到#为止,并做替换。 */
#include <stdio.h>
#include <string.h>

int main()
{
    char cache[100];
    char ch;
    int i = 0;
    int changes = 0;
    while ((ch = getchar()) != '#')
    {
        if (ch == '.')
        {
            cache[i++] = '!';
            changes++;
        }
        else if (ch == '!')
        {
            cache[i++] = '!';
            cache[i++] = '!';
            changes++;
        }
        else
            cache[i++] = ch;
    }
    
    printf("共发生了%d次替换。\n", changes);
    printf("替换后:\n");
    for (int j = 0; j < i; j++)
        printf("%c", cache[j]);
    return 0;
}

6

/* 7.12.6.c -- 捕获用户输入,直到#为止,打印ei出现的次数。 */
#include <stdio.h>
#include <string.h>

int main()
{
    char ch,
         lch;
    int times = 0;
    while ((ch = getchar()) != '#')
    {
        if (lch == 'e'
          && ch == 'i')
        {
            times++;
        }
        lch = ch;
    }
    
    printf("There're %d \"ei\".", times);
    return 0;
}

7

/* 7.12.7.c -- 打印工资、税金、净收入。 */
#include <stdio.h>

#define PAY 10.00
#define OVERTIME 40

int main()
{
    double hours;
    printf("Please enter your working hours count per week: ");
    scanf("%lf", &hours);
    
    if (hours > OVERTIME)
        hours *= 1.5;
    double paysum = hours * PAY;
    double taxes;
    if (paysum <= 300)
        taxes = paysum * 0.15;
    else if (300 < paysum && paysum <= 450)
        taxes = 300 * 0.15 + (paysum - 300) * 0.20;
    else
        taxes = 300 * 0.15 + 150 * 0.20 + (paysum - 450) * 0.25;
    double netincome = paysum - taxes;
    printf("工资总额:%.2f\n", paysum);
    printf("税金:%.2f\n", taxes);
    printf("净收入:%.2f\n", netincome);
    return 0;
}

8

/* 7.12.8.c -- 打印工资、税金、净收入2.0 */
#include <stdio.h>

#define OVERTIME 40

int main()
{
    double hours;
    printf("Please enter your working hours count per week: ");
    scanf("%lf", &hours);
    
    while (1)
    {
        printf("*******************************************************************\n"
           "Enter the number corresponding to the desired pay rate or action:\n"
           "1) $8.75/hr                        2) $9.33/hr\n"
           "3) $10.00/hr                       $) $11.20/hr\n"
           "5) quit\n"
           "*******************************************************************\n");
        int choice;
        scanf("%d", &choice);
        if (choice == 5)
            break;
        double pay;
        switch (choice)
        {
          case 1: pay = 8.75; break;
          case 2: pay = 9.33; break;
          case 3: pay = 10.0; break;
          case 4: pay = 11.2; break;
          default: 
              printf("You should enter a number between 1~5.\n");
              continue;
        }
        
        if (hours > OVERTIME)
            hours *= 1.5;
        double paysum = hours * pay;
        double taxes;
        if (paysum <= 300)
            taxes = paysum * 0.15;
        else if (300 < paysum && paysum <= 450)
            taxes = 300 * 0.15 + (paysum - 300) * 0.20;
        else
            taxes = 300 * 0.15 + 150 * 0.20 + (paysum - 450) * 0.25;
        double netincome = paysum - taxes;
        printf("工资总额:%.2f\n", paysum);
        printf("税金:%.2f\n", taxes);
        printf("净收入:%.2f\n", netincome);
    }
    
    return 0;
}


输入非1~5的数时,程序提醒重新输入。

9

/* 7.12.9.c -- 打印所有小于等于某数的素数。 */
#include <stdio.h>
#include <stdbool.h>

bool isPrime(int num);

int main()
{
    int num;
    
    while (1)
    {
        printf("Please enter an integer above zero: ");
        scanf("%d", &num);
        if (num > 0)
            break;
    }
    
    for (int current =1; current <= num; current++)
    {
        if (isPrime(current))
            printf("%d\n", current);
    }
    return 0;
}

bool isPrime(int num)
{
    if (num == 1 || num == 2)
       return false;
       
    for (int i = 2; i < num; i++)
    {
        if (num % i == 0)
            return false;
    }
    return true;
}

10

/* 7.12.10.c -- 税金种类&税金。 */
#include <stdio.h>

int main()
{
    double income;
    printf("Please enter your income: ");
    scanf("%lf", &income);
    
    int category;
    printf("Please enter your tax category: ");
    scanf("%d", &category);
    
    double taxes;
    double limit;
    switch (category)
    {
        case 1: limit = 17850; break;
        case 2: limit = 23900; break;
        case 3: limit = 29750; break;
        case 4: limit = 14875; break;
        default:
            printf("Invaid category. Quit.");
            return 0;
    }
    taxes = income >= limit
        ? 0.15 * limit + 0.28 * (income - limit)
        : 0.15 * income;
    printf("税金为%.2f", taxes);
    return 0;
}

11

/* 7.12.11.c -- */
#include <stdio.h>

#define APRICE 2.05
#define BPRICE 1.15
#define CPRICE 1.09

int main()
{
    char cmd;
    int apound = 0,
        bpound = 0,
        cpound = 0;
    
    buy:
    while (1)
    {
        printf("输入指令(a/b/c/q):");
        scanf("%c", &cmd); 
        switch (cmd)
        {
            case 'a':
                printf("请输入洋蓟磅数:");
                scanf("%d", &apound);
                goto buy;
            case 'b':
                printf("请输入甜菜磅数:");
                scanf("%d", &bpound);
                goto buy;
            case 'c':
                printf("请输入胡萝卜磅数:");
                scanf("%d", &cpound);
                goto buy;
            case 'q':
                goto buy_ok;
        }
    }
    
    buy_ok:
    {
        double price = apound * APRICE
                     + bpound * BPRICE
                     + cpound * CPRICE;
        double pounds = apound + bpound + cpound;
        double discount = price >= 100 ? 0.05 : 0;
        double freight;
        if (pounds <= 5)
           freight = 6.5;
        else if (5 < pounds && pounds <= 20)
           freight = 14;
        else
           freight = 14 + (pounds - 20) * 0.5;
           
        printf("购物信息:\n"
               "洋蓟 - %d磅,费用%.2f美元\n"
               "甜菜 - %d磅,费用%.2f美元\n"
               "胡萝卜 - %d磅,费用%.2f美元\n"
               "蔬菜费用 - %.2f美元\n"
               "折扣 - %.2f%%\n"
               "运费包装费 - %.2f\n"
               "总费用 - %.2f",
               apound, apound * APRICE, 
               bpound, bpound * BPRICE, 
               cpound, cpound * CPRICE,
               price,
               discount * 100,
               freight,
               price * (1 - discount) + freight);
    }
    return 0;
}

posted @ 2024-10-21 15:56  _vertigo  阅读(46)  评论(0编辑  收藏  举报