C language: rookie course classic 100 examples (51-71)

100 cases of C language classics Note: the following answers are not identical with the original Examples 1-50 Ex amp ...
Ex amp le 51 - and&
Example 52 - or
Instance 53 - XOR^
Example 54
Example 55 -~
Example 56
Example 57
Example 58
Example 59
Example 60
Example 61 - Yang Hui triangle
Example 62
Example 63
Instance 64
Example 65
Example 66
Example 67
Example 68
Example 69
Example 70
Example 71

100 cases of C language classics
Note: the following answers are not identical with the original

Examples 1-50

Ex amp le 51 - and&

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include <stdio.h> int main() { int a, b; a = 077; b = a & 3; printf("a & b(decimal) by %d \n", b); b &= 7; printf("a & b(decimal) by %d \n", b); return 0; } //A & B (decimal) is 3 //A & B (decimal) is 3

Example 52 - or

Learn to use bitwise or|.

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include<stdio.h> int main() { int a, b; a = 077; b = a | 3; printf("b The value of is %d \n", b); b |= 7; printf("b The value of is %d \n", b); return 0; } //The value of b is 63 //The value of b is 63

Instance 53 - XOR^

Learn to use bitwise exclusive or ^.

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // //0^0=0; 0^1=1; 1^0=1; 1^1=0 . #include <stdio.h> int main() { int a,b; a=077; b=a^3; printf("b The value of is %d \n",b); b^=7; printf("b The value of is %d \n",b); return 0; } //The value of b is 60 //The value of b is 59

Example 54

Take the 4-7 bits of an integer a starting from the right end.

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include <stdio.h> int main() { unsigned a,b,c,d; printf("Please enter an integer:\n"); scanf("%o",&a); b=a>>4; c=~(~0<<4); d=b&c; printf("%o\n%o\n",a,d); return 0; } //36 //36 //1

Example 55 -~

Learn to use bitwise negation ~.

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include <stdio.h> int main() { int a,b; a=234; b=~a; printf("a The bitwise negation of is (decimal) %d \n",b); a=~a; printf("a The bitwise negation value of is (hexadecimal) %x \n",a); return 0; } /* Please enter an integer: a The bitwise negation value of is (decimal) - 235 a The bitwise negation value of is (HEX) ffffff15 */

Example 56

Draw a picture. Learn to draw a circle.

#include <graphics.h> //VC6.0 can't be run in Turbo2.0/3.0 int main() { int driver,mode,i; float j=1,k=1; driver=VGA; mode=VGAHI; initgraph(&driver,&mode,""); setbkcolor(YELLOW); for(i=0;i<=25;i++) { setcolor(8); circle(310,250,k); k=k+j; j=j+0.3; } return 0; }

Example 57

Draw a picture, learn to draw a straight line with line (implemented in TC).

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include "graphics.h" int main() { int driver,mode,i; float x0,y0,y1,x1; float j=12,k; driver=VGA;mode=VGAHI; initgraph(&driver,&mode,""); setbkcolor(GREEN); x0=263;y0=263;y1=275;x1=275; for(i=0;i<=18;i++) { setcolor(5); line(x0,y0,x0,y1); x0=x0-5; y0=y0-5; x1=x1+5; y1=y1+5; j=j+10; } }

Example 58

Learn to draw squares with rectangle. (implemented in TC).

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include "graphics.h" int main() { int x0,y0,y1,x1,driver,mode,i; driver=VGA;mode=VGAHI; initgraph(&driver,&mode,""); setbkcolor(YELLOW); x0=263;y0=263;y1=275;x1=275; for(i=0;i<=18;i++) { setcolor(1); rectangle(x0,y0,x1,y1); x0=x0-5; y0=y0-5; x1=x1+5; y1=y1+5; } settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy(150,40,"How beautiful it is!"); line(130,60,480,60); setcolor(2); circle(269,269,137); }

Example 59

Draw pictures and synthesize examples.

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // # define PAI 3.1415926 # define B 0.809 # include "graphics.h" #include "math.h" int main() { int i,j,k,x0,y0,x,y,driver,mode; float a; driver=CGA;mode=CGAC0; initgraph(&driver,&mode,""); setcolor(3); setbkcolor(GREEN); x0=150;y0=100; circle(x0,y0,10); circle(x0,y0,20); circle(x0,y0,50); for(i=0;i<16;i++) { a=(2*PAI/16)*i; x=ceil(x0+48*cos(a)); y=ceil(y0+48*sin(a)*B); setcolor(2); line(x0,y0,x,y); } setcolor(3);circle(x0,y0,60); /* Make 0 time normal size letters */ settextstyle(DEFAULT_FONT,HORIZ_DIR,0); outtextxy(10,170,"press a key"); getch(); setfillstyle(HATCH_FILL,YELLOW); floodfill(202,100,WHITE); getch(); for(k=0;k<=500;k++) { setcolor(3); for(i=0;i<=16;i++) { a=(2*PAI/16)*i+(2*PAI/180)*k; x=ceil(x0+48*cos(a)); y=ceil(y0+48+sin(a)*B); setcolor(2); line(x0,y0,x,y); } for(j=1;j<=50;j++) { a=(2*PAI/16)*i+(2*PAI/180)*k-1; x=ceil(x0+48*cos(a)); y=ceil(y0+48*sin(a)*B); line(x0,y0,x,y); } } restorecrtmode(); }

Example 60

Draw a picture and synthesize example 2.

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include "graphics.h" #define LEFT 0 #define TOP 0 #define RIGHT 639 #define BOTTOM 479 #define LINES 400 #define MAXCOLOR 15 int main() { int driver,mode,error; int x1,y1; int x2,y2; int dx1,dy1,dx2,dy2,i=1; int count=0; int color=0; driver=VGA; mode=VGAHI; initgraph(&driver,&mode,""); x1=x2=y1=y2=10; dx1=dy1=2; dx2=dy2=3; while(!kbhit()) { line(x1,y1,x2,y2); x1+=dx1;y1+=dy1; x2+=dx2;y2+dy2; if(x1<=LEFT||x1>=RIGHT) dx1=-dx1; if(y1<=TOP||y1>=BOTTOM) dy1=-dy1; if(x2<=LEFT||x2>=RIGHT) dx2=-dx2; if(y2<=TOP||y2>=BOTTOM) dy2=-dy2; if(++count>LINES) { setcolor(color); color=(color>=MAXCOLOR)?0:++color; } } closegraph(); }

Example 61 - Yang Hui triangle

Print out Yanghui triangle (10 lines are required).
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

#include <stdio.h> int main() { int i, j; int a[10][10]; printf("\n"); for (i = 0; i < 10; i++) { a[i][0] = 1; a[i][i] = 1; } for (i = 2; i < 10; i++) for (j = 1; j < i; j++) a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; for (i = 0; i < 10; i++) { for (j = 0; j <= i; j++) printf("%5d", a[i][j]); printf("\n"); } return 0; } /* 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 */

Example 62

Learn putpixel drawing points, (implemented in TC).

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include "stdio.h" #include "graphics.h" int main() { int i,j,driver=VGA,mode=VGAHI; initgraph(&driver,&mode,""); setbkcolor(YELLOW); for(i=50;i<=230;i+=20) for(j=50;j<=230;j++) putpixel(i,j,1); for(j=50;j<=230;j+=20) for(i=50;i<=230;i++) putpixel(i,j,1); }

Example 63

Draw ellipse (implemented in TC).

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include "stdio.h" #include "graphics.h" #include "conio.h" int main() { int x=360,y=160,driver=VGA,mode=VGAHI; int num=20,i; int top,bottom; initgraph(&driver,&mode,""); top=y-30; bottom=y-30; for(i=0;i<num;i++) { ellipse(250,250,0,360,top,bottom); top-=5; bottom+=5; } getch(); }

Instance 64

Drawing with ellipse and rectangle (implemented in TC).

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include "stdio.h" #include "graphics.h" #include "conio.h" main() { int driver=VGA,mode=VGAHI; int i,num=15,top=50; int left=20,right=50; initgraph(&driver,&mode,""); for(i=0;i<num;i++) { ellipse(250,250,0,360,right,left); ellipse(250,250,0,360,20,top); rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2)); right+=5; left+=5; top+=10; } getch(); }

Example 65

One of the most beautiful patterns (implemented in TC).

// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include "graphics.h" #include "math.h" #include "dos.h" #include "conio.h" #include "stdlib.h" #include "stdio.h" #include "stdarg.h" #define MAXPTS 15 #define PI 3.1415926 struct PTS { int x,y; }; double AspectRatio=0.85; void LineToDemo(void) { struct viewporttype vp; struct PTS points[MAXPTS]; int i, j, h, w, xcenter, ycenter; int radius, angle, step; double rads; printf(" MoveTo / LineTo Demonstration" ); getviewsettings( &vp ); h = vp.bottom - vp.top; w = vp.right - vp.left; xcenter = w / 2; /* Determine the center of circle */ ycenter = h / 2; radius = (h - 30) / (AspectRatio * 2); step = 360 / MAXPTS; /* Determine # of increments */ angle = 0; /* Begin at zero degrees */ for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */ rads = (double)angle * PI / 180.0; /* Convert angle to radians */ points[i].x = xcenter + (int)( cos(rads) * radius ); points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio ); angle += step; /* Move to next increment */ } circle( xcenter, ycenter, radius ); /* Draw bounding circle */ for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */ for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */ moveto(points[i].x, points[i].y); /* Move to beginning of cord */ lineto(points[j].x, points[j].y); /* Draw the cord */ } } } int main() { int driver,mode; driver=CGA;mode=CGAC0; initgraph(&driver,&mode,""); setcolor(3); setbkcolor(GREEN); LineToDemo(); }

Example 66

Input 3 numbers a,b,c, and output in order of size.

#include <stdio.h> int main() { int a, b, c; int t; printf("Please enter 3 numbers: \n"); scanf("%d %d %d", &a, &b, &c); if (a > b) { if (b > c) printf("%d > %d > %d\n", a, b, c); else { if(a > c) printf("%d > %d > %d\n", a, c, b); else printf("%d > %d > %d\n", c, a, b); } } else { if (b < c) printf("%d > %d > %d\n", c, b, a); else { if (a > c) printf("%d > %d > %d\n", b, a, c); else printf("%d > %d > %d\n", b, c, a); } } return 0; } /* Please enter 3 numbers: 3 6 2 6 > 3 > 2 */
#include <stdio.h> void swap(int *s1, int *s2) { int t; t = *s1; *s1 = *s2; *s2 = t; } int main() { int a, b, c; int *p1, *p2, *p3; printf("Please enter 3 numbers: \n"); scanf("%d %d %d", &a, &b, &c); p1 = &a; p2 = &b; p3 = &c; if (a > b) swap(p1, p2); if (a > c) swap(p1, p3); if (b > c) swap(p2, p3); printf("%d %d %d\n", a, b, c); return 0; } /* Please enter 3 numbers: 3 6 2 2 3 6 */

Example 67

Input array, the largest with the first element exchange, the smallest with the last element exchange, output array.

#include <stdio.h> int main() { int num[10]; int m, n; int t; printf("Please enter 10 numbers:\n"); scanf("%d %d %d %d %d %d %d %d %d %d", &num[0], &num[1], &num[2], &num[3], &num[4], &num[5], &num[6], &num[7], &num[8], &num[9]); for (int i = 0; i < 10; i++) { m = 0; for (int j = 0; j < 10; j++) { if (num[i] < num[j]) break; else m++; } if (m == 10) { t = num[0]; num[0] = num[i]; num[i] = t; } } for (int i = 0; i < 10; i++) { n = 0; for (int j = 0; j < 10; j++) { if (num[i] > num[j]) break; else n++; } if (n == 10) { t = num[9]; num[9] = num[i]; num[i] = t; } } printf("%d %d %d %d %d %d %d %d %d %d\n", num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8], num[9]); return 0; } /* Please enter 10 numbers: 3 8 7 9 12 4 6 1 5 10 12 8 7 9 3 4 6 10 5 1 */

Example 68

There are n integers, so that the preceding numbers move backward m positions in order, and the last m numbers become the most preceding m numbers.

#include <stdio.h> int main() { int num[32]; int move[32]; int m, n; printf("How many numbers?\n"); scanf("%d", &n); printf("Please enter %d numbers:\n",n); for (int i = 0; i < n; i++) scanf("%d", &num[i]); printf("How many positions do you want to move?\n"); scanf("%d", &m); for (int i = 0; i < n - m; i++) move[i + 3] = num[i]; for (int i = n - m; i < n; i++) move[i - (n - m)] = num[i]; //Print the array you entered printf("before:\n"); for (int i = 0; i < n; i++) printf("%3d ", num[i]); printf("\n"); //Print scrolled array printf("after:\n"); for (int i = 0; i < n; i++) printf("%3d ", move[i]); printf("\n"); return 0; } /* How many numbers? 10 Please enter 10 numbers: 0 1 2 3 4 5 6 7 8 9 How many positions do you want to move? 3 before: 0 1 2 3 4 5 6 7 8 9 after: 7 8 9 0 1 2 3 4 5 6 */

Example 69

There are n people in a circle, in sequence.
Report from the first person (from 1 to 3). When the person who reports to 3 exits the circle, he asks what number he left behind.

reference resources

#include <stdio.h> int main() { int a[50]; int n, count; int flag = 0; printf("How many people?\n"); scanf("%d", &n); count = n; for (int i = 1; i <= n; i++) a[i] = 1;//Everyone in the circle, for 1 for (int i = 1; ; i++) { //If i exceeds n people, it means that i have cycled once and i am back to the first person if (i == n + 1) i = 1; if (a[i] != 0) flag++; else continue; if (flag % 3 == 0) { a[i] = 0; count--; } if (count == 1)//It means there's only one person left in the circle break; } for (int i = 1; i <= n; i++) if (a[i] != 0) printf("The last people is %d", i); return 0; } /* How many people? 8 The last people is 7 */
// Created by www.runoob.com on 15/11/9. // Copyright © 2015 rookie tutorial. All rights reserved // #include <stdio.h> void main() { int num[50],n,*p,j,loop,i,m,k; printf("Please enter the number of people in this circle:\n"); scanf("%d",&n); p=num; //Start numbering these people for (j=0;j<n;j++) { *(p+j)=j+1; } i=0;//I for counting, i.e. move the pointer back m=0;//m record the number of people leaving the circle k=0;//k number 1,2,3 while(m<n-1)//When the number of people who quit is not greater than the total number of people, the number of people left is at least one //This sentence can't be written as m < n, because if there are eight people, when six people quit, the number of people still exits, that is, M + +, //At this time, it's 7 < 8, and the rest of them shout 1, 2, 3 by themselves. Then they quit, and there will be no output { if (*(p+i)!=0)//If the number on the head of this person is not 0, the number on the head is added with 1. The method used here is to reset the number on the head with 3 to 0 { k++; } if (k==3) { k=0; //The report is cleared, i.e. the next person reports from 1 *(p+i)=0;//Reset the number of the person reporting 3 to 0 m++; //Number of exits plus 1 } i++; //Pointer backward if (i==n)//This sentence is very important. If you get to the end of the team, you should make the pointer point to the opposite end again //And it can only be placed after i + +, because i==n is possible only after i + + { i=0; } } printf("Now the rest are:"); for (loop=0;loop<n;loop++) { if (num[loop]!=0) { printf("%2d Number\n",num[loop]); } } } /* Please enter the number of people in this circle: 8 Now the rest are: number seven */

Example 70

Write a function, find the length of a string, input the string in the main function, and output its length.

#include <stdio.h> int length(char *str) { int l = 0; while (*str != '\0') { l++; str++; } //or /* for (int i = 0; i < 64; i++) { if (isprint(str[i]) || ispunct(str[i])) l++; else break; } */ return l; } int main() { char str[64]; printf("Please enter a string:\n"); scanf("%s", str); int len; len = length(str); printf("length = %d\n", len); return 0; } /* Please enter a string: helloworld! length = 11 */

Example 71

Write the input() and output() functions to input and output the data records of 5 students.

#include <stdio.h> #include<stdlib.h> void input(); void output(); typedef struct { char name[20]; char sex[5]; int age; }Stu; int main() { Stu student[5]; printf("Please enter informations of 5 students: "); printf("(Name Sex Age)\n"); input(student); printf("Informations:\n"); output(student); system("pause"); return 0; } void input(Stu *student) { for (int i = 0; i < 5; i++) scanf("%s %s %d", student[i].name, &student[i].sex, &student[i].age); } void output(Stu *student) { for (int i = 0; i < 5; i++) printf("%3s %3s %3d\n", student[i].name, student[i].sex, student[i].age); } /* Please enter informations of 5 students: (Name Sex Age) zzn m 20 hll m 19 yxj m 19 xzg m 20 yc m 22 Informations: zzn m 20 hll m 19 yxj m 19 xzg m 20 yc m 22 */

4 June 2020, 22:27 | Views: 3964

Add new comment

For adding a comment, please log in
or create account

0 comments