高级语言程序设计第五次作业
这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C/
这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/2024C/homework/13298
学号:092300125
姓名:张天荣
8.11
#include<stdio.h>
int main(void){
char ch;
int a = 0;
while ((ch = getchar()) != EOF)
a++;
printf("读取到%d个字符。\n", a);
getchar();
return 0;
}
2.
#include<stdio.h>
int main()
{
char ch;
int i = 1;
printf("Please input some things:");
while ((ch = getchar()) != EOF)
{
if (ch >= 32)
{
putchar(ch);
printf(" %3d, ", ch);
}
else if (ch == '\n')
{
printf("\\n");
printf(" %2d, ", ch);
}
else if (ch == '\t')
{
printf("\\t");
printf(" %2d, ", ch);
}
else
{
putchar('^');
putchar(ch + 64);
printf(" %2d, ", ch);
}
if (++i == 10)
{
putchar('\n');
i = 1;
}
}
return 0;
}
3.
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
int capital_letter = 0, lower_letter = 0;
printf("Please input some sentences:");
while ((ch = getchar()) != EOF)
{
if (isupper(ch))
capital_letter++;
if (islower(ch))
lower_letter++;
}
printf("There are %d capital letters and %d lower"
" letters in these sentences.\n", capital_letter, lower_letter);
return 0;
}
4.
#include<stdio.h>
int main()
{
char ch, current = ' ', last = ' ';
int words = 0;
printf("Please input some sentences:");
while ((ch = getchar()) != EOF)
{
current = ch;
if (last == ' ' && (current != ' ' || current != '\t' || current != '\n'))
words++;
last = current;
}
printf("There are %d words in these sentences.\n", words);
return 0;
}
5.
#include<stdio.h>
int main()
{
char ch;
int guess = 50, little = 100, big = 1;
printf("Is your number %d?\n", guess);
while (((ch = getchar()) != 'y'))
{
if (getchar() != '\n')
continue;
else
{
switch (ch)
{
case 'l':
big = guess;
guess = (guess + little) / 2;
break;
case 'b':
little = guess;
guess = (guess + big) / 2;
break;
}
printf("Well, then, is it %d?\n", guess);
}
}
printf("I knew I could do it!\n");
return 0;
}
6.
#include<stdio.h>
int get_first();
int main()
{
char ch;
ch = get_first();
putchar(ch);
return 0;
}
int get_first(void)
{
int ch;
ch = getchar();
while (ch == ' ' || ch == ' ' || ch == '\n')
ch = getchar();
return ch;
}
7.
#include<stdio.h>
#define STD_TIME 40
#define TIMES 1.5
#define SALARY_FIRST 300
#define RATE_FIRST 0.15
#define SALARY_NEXT 150
#define RATE_NEXT 0.2
#define RATE_OTHERS 0.25
int main()
{
int hours;
char chose;
float salaries = 0, taxes = 0, net_wages = 0, salary_per_h = 0.0;
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("a)$8.75/hr b)$9.33/hr\n"
"c)$10.00/hr d)$11.20/hr\n"
"e)quit\n");
printf("*****************************************************************\n");
while ((chose = getchar()) != 'e')
{
if (chose == '\n')
continue;
if (chose != 'a' && chose != 'b' && chose != 'c' && chose != 'd')
{
printf("Please input a-e,again:\n");
continue;
}
else
{
switch (chose)
{
case 'a':
salary_per_h = 8.75;
break;
case 'b':
salary_per_h = 9.33;
break;
case 'c':
salary_per_h = 10.00;
break;
case 'd':
salary_per_h = 11.20;
break;
default:
break;
}
}
printf("Please input the hours:");
scanf("%d", &hours);
if (hours > 40)
{
salaries = STD_TIME * salary_per_h;
salaries += (hours - STD_TIME) * TIMES * salary_per_h;
}
else
salaries += hours * salary_per_h;
if (salaries <= SALARY_FIRST)
taxes = salaries * RATE_FIRST;
else if (salaries <= (SALARY_FIRST + SALARY_NEXT))
{
taxes = (SALARY_FIRST)*RATE_FIRST + (salaries - SALARY_FIRST) * RATE_NEXT;
}
else
{
taxes = SALARY_FIRST * RATE_FIRST + SALARY_NEXT * RATE_NEXT + (salaries - SALARY_FIRST - SALARY_NEXT) * RATE_OTHERS;
}
net_wages = salaries - taxes;
printf("The salary is %.2f, taxes is %.2f, net salary is %.2f\n", salaries, taxes, net_wages);
salaries = 0; taxes = 0; net_wages = 0;
}
return 0;
}
8.
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch, ch_arr[100];
float first, second;
printf("Enter the opration of your choice:\n"
"a.add \t\t\t s.subtract\n"
"m.multiply \t\t\t d.divide\nq.quit\n");
while ((ch = getchar()) != 'q')
{
if (getchar() != '\n')
continue;
if (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd')
{
printf("Please input a 'a' or 's' or 'm' or 'd' or 'q':\n");
continue;
}
printf("Enter first number:");
while (scanf("%f", &first) != 1)
{
scanf("%s", ch_arr);
printf("%s is not a number.\n"
"Please input a number,such as 2.5,-1.78E8,or 3:", ch_arr);
}
printf("Enter second number:");
again: while (scanf("%f", &second) != 1)
{
scanf("%s", ch_arr);
printf("%s is not a number.\n"
"Please input a number,such as 2.5,-1.78E8,or 3:", ch_arr);
}
switch (ch)
{
case 'a':
printf("%f + %f = %f\n", first, second, first + second);
break;
case 's':
printf("%f - %f = %f\n", first, second, first - second);
break;
case 'm':
printf("%f * %f = %f\n", first, second, first * second);
break;
case 'd':
if (second != 0)
printf("%f / %f = %f\n", first, second, first / second);
else
{
printf("Enter a number other than 0:");
goto again;
}
break;
}
if (getchar() != '\n')
continue;
printf("Enter the opration of your choice:\n"
"a.add \t\t\t s.subtract\n"
"m.multiply \t\t\t d.divide\nq.quit\n");
}
printf("Bye.\n");
return 0;
}
9.11
#include<stdio.h>
double min(double, double);
int main()
{
double first, second;
printf("Please input two doubles\n");
printf("The first one is :");
scanf("%lf", &first);
printf("The second one is :");
scanf("%lf", &second);
printf("The minimum number is:%lf\n", min(first, second));
return 0;
}
double min(double x, double y)
{
return (x > y) ? y : x;
}
2.
#include<stdio.h>
void chline(char, int, int);
int main()
{
char ch;
int r, l;
printf("Please input the char:\n");
ch = getchar();
while (getchar() != '\n')
continue;
scanf("%d", &r);
scanf("%d", &l);
chline(ch, r, l);
return 0;
}
void chline(char ch, int r, int l)
{
int i=0,j=0;
for (i = 0; i < r; i++) {
for (j = 0; j < l; j++)
printf("%c ",ch);
printf("\n");
}
}
3.
#include<stdio.h>
void chline(char, int, int);
int main()
{
char ch;
int num, line;
printf("Please input the char:\n");
ch = getchar();
while (getchar() != '\n')
continue;
printf("Please input the char's numbers in each line:\n");
scanf("%d", &num);
printf("Please input the all lines you want:\n");
scanf("%d", &line);
chline(ch, num, line);
return 0;
}
void chline(char ch, int i, int j)
{
int num, line;
for (line = 0; line < j; line++)
{
for (num = 0; num < i; num++)
putchar(ch);
putchar('\n');
}
}
4.
#include<stdio.h>
double avg_combine(double, double);
int main()
{
double first, second;
printf("Please input the first number:\n");
scanf("%lf", &first);
printf("Please input the second number:\n");
scanf("%lf", &second);
printf("The avg_combine is %lf\n", avg_combine(first, second));
return 0;
}
double avg_combine(double a, double b)
{
return 1.0 / ((1.0 / a + 1.0 / b) / 2.0);
}
5.
#include<stdio.h>
void large_of(double, double);
int main()
{
double first, second;
printf("Please input the first number:\n");
scanf("%lf", &first);
printf("Please input the second number:\n");
scanf("%lf", &second);
large_of(first, second);
return 0;
}
void large_of(double x, double y)
{
y = (x > y) ? x : y;
x = (x > y) ? x : y;
printf("The first number is %lf now.\n"
"The second number is %lf now.\n", x, y);
}
6.
#include<stdio.h>
void change(double* p1, double* p2, double* p3);
int main(void){
double a, b, c;
while (scanf_s("%lf %lf %lf", &a, &b, &c) == 3)
{
change(&a, &b, &c);
printf("交换后的值为%lf和%lf和%f\n", a, b, c);
printf("继续输入(输入q退出):");
}
getchar();
return 0;
}
void change(double* p1, double* p2, double* p3){
double max, min, mid;
max = *p1;
if (max < *p2)
max = *p2;
if (max < *p3)
max = *p3;
min = *p1;
if (min > *p2)
min = *p2;
if (min > *p3)
min = *p3;
mid = *p1 + *p2 + *p3 - max - min;
* p1 = min;
* p2 = mid;
* p3 = max;
}
7.
#include<stdio.h>
#include<ctype.h>
int letter(char);
int main()
{
char ch;
printf("Please input something:");
while ((ch = getchar()) != EOF)
{
if (ch == '\n')
continue;
if (letter(ch) != -1)
continue;
else
printf("%c is not a alphabet\n", ch);
}
return 0;
}
int letter(char ch)
{
if (isalpha(ch))
{
if (isupper(ch))
printf("%c is the %3d letter in the alphabet\n", ch, ch - 64);
else
printf("%c is the %3d letter in the alphabet\n", ch, ch - 96);
return 0;
}
else
return -1;
}
8.
#include<stdio.h>
double power(double n, int p);
int main()
{
double x, xpow;
int exp;
printf("Enter a number and an integer power"
" to which the number will be raised.\nEnter q to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow = 1;
int i;
if (p == 0 || n == 0.0)
return 1;
else if (p < 0)
{
for (i = p; i < 0; i++)
pow /= n;
return pow;
}
else
{
for (i = 1; i <= p; i++)
pow *= n;
return pow;
}
}
9.
#include<stdio.h>
double power(double n, int p);
int main()
{
double x, xpow;
int exp;
printf("Enter a number and an integer power"
" to which the number will be raised.\nEnter q to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow;
if (p > 0)
pow = n * power(n, p - 1);
else if (p < 0)
pow = power(n, p + 1) / n;
else
pow = 1;
return pow;
}
10.
#include<stdio.h>
void to_base_n(unsigned long, int);
int main()
{
unsigned long number;
int base;
printf("Enter an integer(q to quit):\n");
while (scanf("%ul", &number) == 1)
{
printf("Enter the base binary(2-8):\n");
scanf("%d", &base);
printf("When changed into %d binary, it is: ", base);
to_base_n(number, base);
putchar('\n');
printf("Enter an integer (q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_base_n(unsigned long n, int base)
{
int r;
r = n % base;
if (n >= base)
to_base_n(n / base, base);
putchar('0' + r);
}
11.
#include<stdio.h>
void Fibonacci(long);
int main()
{
long number;
printf("How many fibonacci do you want?(2<=n<=46, q to quit)\nn=");
while (scanf("%ld", &number) == 1)
{
Fibonacci(number);
printf("Enter an integer (q to quit):\n");
}
printf("Done.\n");
return 0;
}
void Fibonacci(long n)
{
long last = 1, current = 1, temp = 1;
long i = 2;
printf("%ld ", last);
printf("%ld ", current);
while (i < n)
{
temp = current;
current = last + current;
last = temp;
printf("%ld ", current);
i++;
}
printf("\n");
}