C语言学习笔记(1)
笔记内容:
1.比较两个数的大小
2.完成一个四则运算计算器
(1) 比较两个数的大小
#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int Max(int x,int y)
{
if (x > y)
{
return x;
}
else
{
return y;
}
}
int main()
{
while (1)
{
printf("输入需要比较的两个数字:\n");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
printf("%d的值最大\n",Max(x,y));
}
system("pause");
return 0;
}
(2)计算器
#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int Myadd(int x, int y){
int ans = x + y;
return ans;
}
int Mysub(int x, int y){
int ans = x - y;
return ans;
}
int Mymul(int x, int y){
int ans = x * y;
return ans;
}
int Mydiv(int x, int y){
int ans = x / y;
return ans;
}
int main()
{
printf("请输入需要计算的两个数:\n");
int x = 0;
int y = 0;
int ans = 0;
scanf("%d %d", &x, &y);
printf("\n请输入需要执行的操作:(+-*/)");
char op = 'x';
scanf(" %c",&op);
if ('+'==op)
{
printf("\n%d+%d=%d", x, y, Myadd(x,y));
}
else if ('-' == op)
{
printf("\n%d-%d=%d", x, y, Mysub(x,y));
}
else if ('*' == op)
{
printf("\n%d*%d=%d", x, y, Mymul(x,y));
}
else
{
printf("\n%d/%d=%d", x, y, Mydiv(x,y));
}
system("pause");
return 0;
}