C语言成长学习题(三)

十、编写一个含有逻辑表达式的程序。

 1 #include <stdio.h>
 2
 3 void main(void)
 4 {
 5     int x;
 6
 7     x = 1;
 8     printf("%d  ", x>=0 && x<=2);
 9
10     x = 5;
11     printf("%d  ", x>=0 && x<=2);
12     printf("%d  ", x<-3 || x>3);
13
14     x = 0;
15     printf("%d  ", x<-3 || x>3);
16     printf("%d  ", !x);
17
18     x = 5;
19     printf("%d  ", !x);
20     printf("%d  ", 3 && ‘A‘);
21     printf("%d  ", (x=2) || 0);
22     printf("x = %d\n", x);
23 }

结果:

1  0  1  0  1  0  1  1  x=2

  “&&"为逻辑与, ”||“为逻辑或, "!"为逻辑非。

  (1) 当关系表达式的判断结果为“真”(“假”)时,关系表达式的值为1(0),如(5>3)+7的值为8(1+7).

  (2) 0<=x<=2永远成立,即为真,因为先判断0<=x,其结果是0或1,而0<=2(1<=2)的判断结果为“真".

十一、编写一个含有特殊逻辑表达式的程序。

 1 #include <stdio.h>
 2
 3 void main(void)
 4 {
 5     int a, b;
 6
 7     a = 1;
 8     printf("%d  ", 0 && (a=2));                //没执行a=2
 9     printf("a=%d  ", a);
10     printf("%d  ", 5 && (a=2));                //执行了a=2
11     printf("a=%d  ", a);
12
13     b=1;
14     printf("%d  ", 5 || (b=2));                    //没执行b=2
15     printf("b=%d  ", b);
16     printf("%d  ", 0 || (b=2));                    //执行了b=2
17     printf("b=%d  ", b);
18 }

结果:

0  a=1  1  a=2  1  b=1  1  b=2

十二、输入一个字符,如果是数字字符,则转换成对应的数字。

 1 #include <stdio.h>
 2
 3 void main(void)
 4 {
 5     char ch;
 6     int a = -1;
 7
 8     printf("Input ch: ");
 9     ch = getchar();
10     if(ch >= ‘0‘ && ch <= ‘9‘)
11         a = ch - ‘0‘;
12     printf("ch=%c, a=%d\n", ch, a);
13 }

结果:

1.Input ch: 2

 ch=2, a=2

2.Input ch: x

 ch=x, a=-1

  注:"ch-‘0‘"是"ch"的字符对应的十进制减48(‘0‘对应的十进制为48)

    小写字母的ASCII码数(十进制):97~122(a~z)

    大写字母的ASCII码数(十进制):65~90(A~Z)

    数字字符的ASCII码数(十进制):48~57(0~9)

时间: 2024-08-06 23:26:25

C语言成长学习题(三)的相关文章

C语言成长学习题(七)

二十六.输出1!.2!.3!.4!.....n!的值. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 int i, n; 6 float fac; 7 8 printf("Input n: "); 9 scanf("%d", &n); 10 for (i = 1; i<=n; i++) 11 { 12 fac = fac * i; 13 printf("%d! = %.0f\n&qu

C语言成长学习题(四)

十三.编写输出如下分段函数值的程序,要求x的值从键盘输入. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 float x, y; 6 7 printf("Input x:\n"); 8 scanf("%f", &x); 9 if(x <= 0) 10 y = x + 1; 11 else if(x <= 1) 12 y = 1; 13 else 14 y = x; 15 printf(&

C语言成长学习题(十五)

66.编写字符串复制的程序(用指针变量处理). 1 #include <stdio.h> 2 3 void main(void) 4 { 5 char a[50], b[80], *p, *q; 6 7 p = a; 8 q = b; 9 printf("Input data: "); 10 gets(a); 11 while (*p != '\0') 12 *q++ = *p++; 13 *q = '\0'; 14 puts(b); 15 } Mark: 复制操作结束后,

C语言成长学习题(十)

四十一.编写实现以下功能的程序:若从键盘输入字符Y(或y)或N(或n),则终止循环,否则一直等待输入. 1 #include <stdio.h> 2 #include <conio.h> 3 4 void main(void) 5 { 6 char c; 7 8 printf("是否继续(Y/N)?\n"); 9 do 10 { 11 c = getch(); 12 if (c == 'Y' || c == 'y' || c == 'N' || c =='n')

C语言成长学习题(十六)

72.假设一维数组中存放互不相同的十个整数,要求根据输入的下标值,即可直接删除. 1 #include <stdio.h> 2 3 int mydel (int *a, int n, int k) 4 { 5 int i; 6 7 for (i = k; i < n - 1; i++) 8 *(a+i) = *(a+i+1); 9 n--; 10 11 return n; 12 } 13 14 void myout (int *a, int n) 15 { 16 while (n >

C语言成长学习题(十四)

六十一.定义4*6的实型数组,并将各行前5列元素的平均值分别放在同一行的第6列上. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 float a[4][6], sum; 6 int i, j; 7 8 for (i = 0; i < 4; i++) 9 for (j = 0; j < 5; j++) 10 a[i][j] = i * j + 1; 11 for (i = 0; i < 4; i++) 12 { 13 sum =

C语言成长学习题(十三)

五十六.编写求字符串长度的程序. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 char a[80]; 6 int i = 0, count = 0; 7 8 gets(a); 9 while (a[i] != '\0') 10 { 11 count++; 12 i++; 13 } 14 printf("%s = %d\n", a, count); 15 } 五十七.编写字符串复制的程序. 1 #include <std

C语言成长学习题(五)

十七.求一元二次方程ax2+bx+c=0的实根(要求a.b.c的值从键盘输入,a!=0). 1 #include <stdio.h> 2 #include <math.h> 3 4 void main(void) 5 { 6 int a, b, c; 7 float delta, x1, x2; 8 9 printf("Input a, b, c:\n"); 10 scanf("%d%d%d", &a, &b, &c)

C语言K&R习题系列——句子中一个空格代替多个空格的四种方法

原题: Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. 第一种: 这种最常用,设置一个inspace作为布尔变量,标志当前输入是否在字符中,或在字符外 #include <stdio.h>   int main(void) {   int c;   int inspace=0;     while((c = getcha