学好C语言就等于夯实地基(2)

粗心是代码之路上最大的绊脚石

随着对于代码的熟练,并且自己进行debug后,发现自己更多的问题在于追求了敲代码的速度,而忘记了它的准确性和正确性,写下这句话,是为了能够更好的提醒自己,认真的敲出来每一行代码,去追求它的正确性和准确性。
因为作为一名硬件专业的本科生,每天有着上不完的实验课,所以想要对于C语言编程方面有着更加深入的学习和了解,只能够更多的靠自己努力,所以每一天都会编写7个左右的程序,来进行长时间的积累,但是博客只能够在周末的时候进行更新,也是对于自己所书写的程序进行一个温故而知新。

1.1**两个数进行交换**


#include <sdtio.h>
#include <stdlib.h>

int main (){
    int a,b;
    scanf("%d%d",&a,&b);
    int temp=0;
    temp=a;
    a=b;
    b=temp;
    printf("a=%d\nb=%d\n",a,b);
    system("pause");
    return 0;

有时候对于很多题使用了好几种方式来进行编写,所以会在后续发现改进的时候进行新的修改重新发布,也会进行升级版的写法,如下面的1.1或1.2等,都是第一个题,但是包含了不同的写法

1.2 **两个数再不创建新变量的情况下进行互换**


#include <stdio.h>
int main()
{
	int a;
	int b;
	printf("请输入a和b的值:");
	scanf("%d%d", &a, &b);
	a = a + b;
	b = a - b;
	a = a - b;
	printf("a=%d\nb=%d\n", a, b);
	system("pause");
	return 0;
}

1.3**使用异或的形式,将两个数进行交换**


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

int main(){
	int a, b;
	scanf("%d%d", &a, &b);
	a = a^b;
	b = a^b;
	a = a^b;
	printf("%d %d", a, b);
	system("pause");
	return 0;
	}

2.0**输入两个数,求出其中的最大值并进行输出**

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a = 1;
	int b = 2;
	//比较a和b的值,若a大于b,则输出a的值
	if (a > b){
		printf("%d", a);
	}
	else //否则输出b的值
	{
		printf("%d", b);
	}
	system("pause");
	return 0;
}

2.1 **用函数来进行两个值的比较,输出其中较大值**

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

	int max(int x, int y);// 声明max函数
	int a, b, c;
	scanf("%d,%d", &a, &b);
	c = max(a, b);//调用max函数,将其返回值赋予c
	printf("max=%d\n", c);//输出最大值c
	system("pause");
	return 0;
}
int max(int x, int y)//定义max函数
{
	int z;
	if (x > y)z = x;
	else z = y;
	return(z);//返回最大值z
}

这也是第一个接触到使用函数的一个程序,感觉到了对主函数的简短和精简,也让我逐渐感觉使用到了调用函数的好处。

3.0 **十个数,输出其中的最大值**

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int i, max;
	int a[10] = { 3, 5, 7, 9, 11, 15, 13, 18, 21, 28 };//输入数组;
	max = a[0];//对最大值初始化
	for (i = 0; i <= 9;i++)
	if (a[i] > max){
		max = a[i];//将这个数传递给max;
	}
	printf("max=%d\n", max);
	system("pause");
	return 0;
}
3.1**5个数,采用比较排序的方式输出其中最大数和最小数**

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

int main(){
	int a[5] = { 0 };
	int i, j;
	for (i = 0; i < 5; i++){
		scanf("%d", &a[i]);
	}
	int temp = 0;
	for (i = 0; i < 5; i++){
		for (j = 0; j < 5; j++){
			if (a[i]>a[j]){
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
	printf("Max=%d\nMin=%d\n", a[0], a[4]);
	system("pause");
	return 0;

}

4.0**将三个数,按从大到小的顺序排序(ASCII码排列)**

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

int main(){
	char a, b, c;
	char temp;
	scanf("%c%c%c", &a, &b, &c);
	if (a > b){
		temp = a;
		a = b;
		b = temp;
	}
	if (a > c){
		temp = a;
		a = c;
		c = temp;
	}
	if (b > c){
		temp = b;
		b = c;
		c = temp;
	}
	printf("%c %c %c\n", a, b, c);
	system("pause");
	return 0;
}

在进行这个程序的编写的时候,让我知道字符串输入的时候不能输入空格,否则会将空格当作字符数组输入进程序之中!

5.0**最大公约数的求法**

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a, b;
	printf("请输入两个值a和b:\n");
	scanf("%d %d", &a, &b);
		while (a != b)
		{
			if (a > b)
			a = a - b;
			else
			b = b - a;
		}
		printf("最大公约数:%d\n",a);
		system("pause");
			return 0;

一天天的努力和加油,正像是那句话,你只管努力,其他交给天意,真的希望自己能够正儿八经的编写出那种精致的,且能够起到作用的实用性程序啊。

发布了18 篇原创文章 · 获赞 12 · 访问量 971

猜你喜欢

转载自blog.csdn.net/Luckily0818/article/details/103059414
今日推荐