C语言带知识点的代码&C语言经典代码&每个程序含一个知识点

1、输入密码:(注意,盲文密码)

#include<conio.h>
#include<stdio.h>

int main(void)
{
	char   pwd[32];
	int   ch;
	int   i = 0;
	while (1) {
		ch = getch();
		if (ch == '\r') {
			break;
		}
		pwd[i] = ch;
		i++;
		putch('*');
	}
	pwd[i] = 0;
	printf("\n您输入的密码是:%s\n", pwd);
	return   0;
}

运行结果;

2、 9*9口诀:

#include<stdio.h>
void main()
{
	int i, j, result;
	 
	for (i = 1; i < 10; i++)
	{
		for (j = 1; j < 10; j++)
		{
			result = i * j;
			printf("%d*%d=%-3d", i, j, result); /*-3d表示左对齐,占3位*/
		}
		printf("\n"); /*每一行后换行*/

	}
}

运行结果:

3、 斐波那契数列

题目:Fibonacci 数列的特点是第1、2个数为1、1;从第三个数开始,该数是前两个数之和,求这个数列的前30个元素;

#include<stdio.h>
int main()
{
	int i;
	int long F[31];
	F[1] = 1; //第一个和第二个数为1
	F[2] = 1;
	for (i = 3; i < 31; i++)
	{
		F[i] = F[i - 1] + F[i - 2]; //从第三项开始,每项为前三项之和
	}
	for (i = 1; i < 31; i++)
	{
		printf("%-15d", F[i]);
		if (i % 5 == 0) //控制每行输出5个值
		{
			printf("\n");
		}
	}
	return 0;
}

运行结果:

4、 /*判断101-200之间有多少个素数,并输出所有素数及素数的个数。

解析:程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

#include "math.h"
#include <stdio.h>
int main()
{
	int m, i, k, h = 0, leap = 1;
	for (m = 101; m <= 200; m++)
	{
		k = sqrt(m + 1);
		for (i = 2; i <= k; i++)
			if(m%i == 0)
		{
			leap = 0; break;
		}
		if (leap) /*内循环结束后,leap依然为1,则m是素数*/
		{
			printf("%-4d", m); h++;
			if (h % 10 == 0)
				printf("\n");
		}
		leap = 1;
	}
	printf("\nThetotal is %d", h);
	return 0;
}

运行结果:

5、 完善程序,实现将输入的字符串反序输出:逆序输出

方法1:

#include <stdio.h>
#include <string.h>
int main(void)
{
	char c[200], c1;
	int i, j, k;
	printf("Enter a string: ");
	scanf("%s", c);
	k = strlen(c);
	for (i = 0, j = k - 1; i<k/2;i++,j--)
		{
		c1 = c[i]; 
		c[i] = c[j];
		c[j] = c1; 
		}
	printf("%s\n", c);
	return 0;
}

运行结果:

存在的问题:如果输入:hello world,情况是啥?

答案:gets()和getchar()的用法

方法2和方法3:

//字符串逆序
#include <stdio.h>
#include <string.h>

//实现方式1:数组方式
void str_reverse(char *str) {

	int low=0, high;
	char temp;
	high = strlen(str) - 1;
	while(low < high) {
		temp = str[low];
		str[low] = str[high];
		str[high] = temp;
		low++;
		high--;
	}
}
//实现方式2;指针方式
void str_reverse1(char *str) {

	char temp;
	char *p, *q;
	int n;
	p = str; //指向数组首地址
	n = strlen(str);
	q = &str[n-1]; //指向数组末地址
	while (p < q) {
		temp = *p;
		*p = *q;
		*q = temp;
		p++;
		q--;
	}
}
int main() {
	char a[] = {"abcdefg"};
	printf("逆序前的字符串:%s\n", a);
	str_reverse(a);
	printf("逆序后的字符串:%s\n",a);
	str_reverse1(a);
	printf("逆序后的字符串:%s\n", a);
	return 0;
}

输出结果:

6、打印杨辉三角:

#include <stdio.h>
int main()
{
	int i, j, a[6][6];
	for (i = 0; i <= 5; i++)
	{
		a[i][i] = 1; a[i][0] = 1;
	}
	for (i = 2; i <= 5; i++)
		for (j = 1; j <= i - 1; j++)
			a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
	for (i = 0; i <= 5; i++)
	{
		for (j = 0; j <= i; j++)
			printf("%4d", a[i][j]);
		printf("\n");
	}
	return 0;
}

运行结果:

7、程序的功能是:从字符数组s中删除存放在c中的字符,然后输出数组

#include <stdio.h>
int main(void)
{
	char s[80], c;
	int j, k;
	printf("Enter a string:");
	gets_s(s);
	printf("\nEnter a character: ");
	c = getchar();
	for (j = k = 0; s[j] != '\0'; j++)
	{
		if (s[j] != c)
		{
			s[k++] = s[j];
		}
	}
	s[k] = '\0';
	printf("\n%s", s);
	return 0;
}

运行结果:

 8、古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

解体思路:兔子的规律为数列1,1,2,3,5,8,13,21....

#include <stdio.h>
int main()
{
	long f1, f2;
	int i;
	f1 = f2 = 1;
	for (i = 1; i <= 20; i++)
	{
		printf("%12ld%12ld", f1, f2);
		if (i % 2 == 0) printf("\n");/*控制输出,每行四个*/
		f1 = f1 + f2;/*前两个月加起来赋值给第三个月*/
		f2 = f1 + f2; /*前两个月加起来赋值给第三个月*/
	}
	return 0;
}

运行结果:

9、编写一个voidsort(int *x,int n)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out中*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(int *x, int n)
{
	int i, j, k, t;
	for (i = 0; i<n;i++)
	{
		k = i;
		for (j = i + 1; j < n; j++) 
		{
			if (x[j] > x[k])
			{
				k = j;
			}
			if (k != i)
			{
				t = x[i];
				x[i] = x[k];
				x[k] = t;
			}
		}
	}
}
void main()
{
	FILE *fp;
	int num = 10;
	int *p, i, a[10];
	fp = fopen("p9_1.out", "w");
	p = a;
	printf("Input 10 numbers:");
	for (i = 0; i < num; i++)
		scanf("%d", p++);
	p = a; //将P拉回到a的首地址
	sort(p, num);
	//for (i=0; *(p+i)!='\0';i++) //不可以用这个语句
	for(i=0;i<sizeof(a)/sizeof(int);i++)
	{ 
		printf("%d ",*(p+i));
		fprintf(fp,"%d ",*(p+i)); 
	}
	fclose(fp);
	system("pause");
}

运行结果:

10、编写函数replace(char *s,char c1,char c2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中

#include <stdio.h>
#include <stdlib.h>
void replace(char*s, char c1, char c2)
{
	while (*s != '\0')
	{
		if (*s == c1)
			*s = c2;
		s++;
	}
}
int main()
{
	FILE *fp;
	char str[100], a, b;
	char *p;
	p = str;
	if ((fp = fopen("p10_2.out", "w")) == NULL)
	{
		printf("cannot open thefile\n");
		exit(0);
	}
	printf("Enter a string:\n");
	gets_s(str);
	printf("Enter a&&b:\n");
	scanf("%c,%c", &a, &b);
	printf("%s\n", str);
	fprintf(fp, "%s\n", str);
	//replace(str, a, b);
	replace(p, a, b); //上下两种方式都可以
	printf("Thenew string is----%s\n", str);
	fprintf(fp, "Thenew string is----%s\n", str);
	fclose(fp);
	return 0;
}

运行结果:

11、在一个字串s1中查找一子串s2,若存在则返回子串在主串中的起始位置,不存在则返回-1。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int search(char s1[], char s2[])
{
	int i = 0, j, len = strlen(s2);
	while (s1[i]) 
	{
		for (j = 0; j<len;j++)
		{
			if (s1[i] != s2[j])
				break;

			if (j == (len - 1))
				return i;
		}
		i++;
	}
	return -1;
}

int main()
{
	char s1[100],s2[100];
	gets_s(s1);
	gets_s(s2);
	printf("%d\n", search(s1, s2));
	system("pause");
	return 0;
}

运行结果:

12、用指针变量输出结构体数组元素

需要注意的点:就是结构体中含有成员指针,然后定义一个结构体数组,怎么初始化;

两种方式,初始化其中的成员指针:

1、在.cpp程序中,用(char *)“ 字符串”;

2、或者是在.c程序中,直接 “ 字符串”

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
	int num;
	char *name;
	char sex;
	int age;
}Stu;
Stu stu[5] = { {1001,(char *)"lihua",'F',18},{1002,(char *)"liuxing",'M',19},{1003,(char *)"huangke",'F',19},{1004,(char *)"fengshou",'F',19},{1005,(char *)"Wangming",'M',18}};

int main()
{
	int i;
	struct student *ps;
	printf("Num \tName\t\t\tSex\tAge\t\n");
	/*用指针变量输出结构体数组元素。 */
	for (ps = stu; ps < stu + 5; ps++)
	printf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);
	/*用数组下标法输出结构体数组元素学号和年龄。 */
	for (i = 0; i < 5; i++)
	printf("%d\t%d\t\n",stu[i].num,stu[i].age);
	system("pause");
	return 0;
}

运行结果:

13、strlen与sizeof()

#include <stdio.h>
#include <string.h>

int main() {

	char c1[] = {"0123456789"};
	char c2[] = {"A\0BC\0D"};
	printf("字符串c1:strlen(c1)=%d \t sizeof(c1)=%d\n",strlen(c1),sizeof(c1));
	printf("字符串c2:strlen(c2)=%d \t sizeof(c2)=%d\n", strlen(c2), sizeof(c2));
	return 0;
}

 运行结果:

14、将数字变为字符串

包含两个知识点:一个是求余,存进字符串中;另外一个是逆序(逆序详见5)

//将一个数字变为字符串
#include <stdio.h>
#include <string.h>
void str_reverse(char *str) {

	int low = 0, high;
	char temp;
	high = strlen(str) - 1;
	while (low < high) {
		temp = str[low];
		str[low] = str[high];
		str[high] = temp;
		low++;
		high--;
	}
}

int main() {

	int number = 1234; //待转换的数字
	int i;
	int j = 0;
	char str[10];
	while (number > 0) {

		i = number % 10; //余数,中间变量
		str[j] = i + '0' - 0; //此时,str是反序排列的,需要将其逆序
		number = number / 10;
		j++;
	}
	str[j] = '\0';
	printf("逆序前的字符串:%s\n", str);
	str_reverse(str);
	printf("逆序前的字符串:%s\n", str);
	return 0;
}

运行结果:

15、将若干个字符串升序后,并输出

 字符型指针数组,和字符指针,主要是通过字符指针交换,实现排序

示例代码:

#include <stdio.h>
#include <string.h>

int main() {
	//字符指针数组
	const char *str[] = { {"Follow me"},{"BASIC"},{"Great Wall"},{"Fortran"},{"Computer"} };
	const char *p; //字符 常量指针
	int i, j, k;
	for (i = 0; i < 4; i++) {
		k = i;
		for (j = i+1; j < 5; j++) {
			if (strcmp(str[k], str[j]) > 0) {
				k = j;
			}
		}
		if (k != i) {
			p = str[k];
			str[k] = str[i];
			str[i] = p;
		}
	}
	for (i = 0; i < 5; i++) {
		printf("%s  ",str[i]);
	}
	return 0;
}

运行结果:

16、定义整型常量,前面有无‘0’,是否有区别?

如果一个整形常量的第一个字符是数字 0 ,那么该常量将被视作八进制数。

示例代码:

#include <stdio.h>
int main() {

	int i = 10;
	int j = 010; //前面加 0,视作八进制数
	printf("i=%d\n",i);
	printf("j=%d\n", j);
	return 0;
}

运行结果:

17、判断是否为闰年

条件:

1、能被4整除而不能被100整除.(如2004年就是闰年,1900年不是)

2、能被400整除(如2000年是闰年)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {

	int year,i;
	int day[] = {31,28,31,30,31,30,31,31,30,30,30,31};
	//判断输入年份是否为闰年
	printf("输入年份:");
	scanf("%d",&year);
	if ((year%4==0 && year % 100 != 0)||(year % 400 == 0)) //闰年
	{
		day[1] = 29;
	}
	for (i = 0; i < 12; i++) {

		printf("%2d月份:%d\n", (i + 1), day[i]);
	}

	return 0;
}

18、C++ 实例 - 创建各类三角形图案

https://www.runoob.com/cplusplus/cpp-examples-pyramid-pattern.html

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值