今天是2023年7月2日,伟大祖国建党节后的第一天,也是我留校第二天。
在这里立一个flag,争取每天写一个文章记录留校学习日常
今天是收拾捡起之前学习的C语言,写一些经典的C语言代码
C语言是很多学习编程的同学最先接触的编程语言,我个人也是,在大一上就是开始接触,但是我比较特殊,在这之前自己先学习了一些其他的编程语言(但是严格说,我大学第一次学的编程语言就是C语言),然后大一上那会C语言就是没有好好听课,现在准备用一段时间捡起来。
1.helloworld
应该所有人的第一个语言程序都是“hello world”吧,我不信还有同学不是
/* The first C programme */
#include <stdio.h> /* 包含标准输入输出头文件 */
main() /* 主函数 */
{
printf("Hello World!\n"); /* 打印输出信息 */
printf("我要跟着Token_w学习C语言!\n");
}
2.求整数的积
输入已经定义的x,y的值,通过程序运算求值
/* Input two numbers, output the product */
#include <stdio.h>
main()
{
int x,y,m; /* 定义整型变量x,y,m */
printf("Please input x and y\n"); /* 输出提示信息 */
scanf("%d%d",&x,&y); /* 读入两个乘数,赋给x,y变量 */
m=x*y; /* 计算两个乘数的积,赋给变量m */
printf("%d * %d = %d\n",x,y,m); /* 输出结果 */
}
3.比较实数大小
案例是属于两个浮点数字,比较他们的大小,
可以修改为比较整数,比较多个数字
/* 输入两个浮点数,输出它们中的大数 */
#include <stdio.h>
main()
{
float x,y,c; /* 变量定义 */
printf("Please input x and y:\n"); /* 提示用户输入数据 */
scanf("%f%f",&x,&y);
c=x>y?x:y; /* 计算c=max(x,y) */
printf("MAX of (%f,%f) is %f",x,y,c); /* 输出c */
}
4.字符的输出
根据输入字符串,输出字符
/* */
#include <stdio.h>
main()
{
char ch,nch; /* */
int count; /* */
int k; /* */
printf("Please input a string with a # in the end.\n");
scanf("%c",&ch); /* */
while(ch != '#') /* */
{
if(ch >= '0' && ch <= '9')
{
/* */
count = ch-'0'+1; /* */
scanf("%c",&nch); /* */
for(k=0;k<count;k++) /* */
printf("%c",nch);
}
else
printf("%c",ch); /* */
printf(" "); /* */
scanf("%c",&ch); /* */
}
printf("#\n"); /* */
}
5.输出不同类型所占的字节数
/* 输出不同类型所占的字节数*/
#include <stdio.h>
void main()
{
/* sizeof()是保留字,它的作用是求某类型或某变量类型的字节数, */
/* 括号中可以是类型保留字或变量。*/
/*int型在不同的机器,不同的编译器中的字节数不一样,*/
/*一般来说在TC2.0编译器中字节数为2,在VC编译器中字节数为4 */
printf("The bytes of the variables are:\n");
printf("int:%d bytes\n",sizeof(int));
/* char型的字节数为1 */
printf("char:%d byte\n",sizeof(char));
/* short型的字节数为2 */
printf("short:%d bytes\n",sizeof(short));
/* long型的字节数为4 */
printf("long:%d bytes\n",sizeof(long));
/* float型的字节数为4 */
printf("float:%d bytes\n",sizeof(float));
/* double型的字节数为8 */
printf("double:%d bytes\n",sizeof(double));
/* long double型的字节数为8或10或12 */
printf("long double:%d bytes\n",sizeof(long double));
getchar();
}
6.自增自减运算
变量自曾自减
/* */
#include <stdio.h>
main()
{
int a=5,b,c,i=10;
b=a++;
c=++b;
printf("a = %d, b = %d, c = %d\n",a,b,c);
printf("i,i++,i++ = %d,%d,%d\n",i,i++,i++);
printf("%d\n",++i);
printf("%d\n",--i);
printf("%d\n",i++);
printf("%d\n",i--);
printf("%d\n",-i++);
printf("%d\n",-i--);
getchar();
}
7.数列求和
#include <stdio.h>
main()
{
int i,j,n;
long sum=0,temp=0;
printf("Please input a number to n:\n");
scanf("%d",&n);
if(n<1)
{
printf("The n must no less than 1!\n");
return;
}
for(i=1;i<=n;i++)
{
temp=0;
for(j=1;j<=i;j++)
temp+=j;
sum+=temp;
}
printf("The sum of the sequence(%d) is %d\n",n,sum);
getchar();
getchar();
}
8.九九乘法表
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%-4d",j,i,j*i);//注意左对齐格式
}
printf("\n");
}
return 0;
}
9.鸡兔同笼问题
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int m, n;
int x, y;
scanf("%d %d", &m, &n);
y = (n- 2*m)/2;
x = m - y;
printf("%d %d", x, y);
return 0;
}
10.银行利率
给你本金,放在银行,你能知道自己应该那多少利息吗?
#include<stdio.h>
#include<math.h>
#define RATE 0.0225
int main(void)
{
int n;
double capital;
scanf("%d %lf", &n, &capital);
printf("%.6f", capital * pow(1 + RATE, n));
return 0;
}
今天先写这几个,感谢大家的关注点赞,咱们后面继续!