//指针实践
#include <stdio.h>
int main(){
int a;int b;int *p1,*p2,*p;
//calculate the maxmium of a and b;
printf("please insert two integer\n");
scanf("%d %d",&a,&b);
p1=&a;p2=&b;
if(a<b){
p=p1;p1=p2;p2=p;
}
printf("a=%d b=%d \n",a,b);
printf("max is %d min is %d",*p1,*p2);
}
//数值型数据的存储
#include <stdio.h>
int main(){
int a,b;
a=2147483642;
printf("a=%d\n",a);
b=a+10;
printf("b=%d\n",b);
return 0;
//b的实际值是-a-2 因为a+b的运算结果超过了int的范围
}
#include <stdio.h>
int main()
{
float a,b,c;
a=12345.67890;
b=987.65432;
c=a+b;
printf("%12.5f\n",c);
return 0;
//double是15位 float7位
}
//项目1 分离个位数用空格分开
#include <stdio.h>
int main(){
printf("insert a three-digit number");
int a,a1,a2,a3;
scanf("%d",&a);
a1=a%10;a=a/10;
a2=a%10;a=a/10;
a3=a%10;
printf("%d %d %d",a1,a2,a3);
}
//项目2分离整数和小数部分
#include <stdio.h>
int main(){
printf("insert a float,with three digits after point\n");
float a;int b;float c;
scanf("%f",&a);
b=(int)a; //integer
c=a-b; //points
int b1,b2,b3,c1,c2,c3;
b1=b%10;b/=10;b2=b%10;b/=10;b3=b%10;
c1=(int)(c*10);c2=(int)(c*100)%10;c3=(int)(c*1000)%10;
printf("%d %d %d %d %d %d",b3,b2,b1,c1,c2,c3);
}
//Project 3:Buy roses
#include <stdio.h>
#define price 5
int main(){
int n;//money
printf("Money is\n") ;
scanf("%d",&n);
int num_rose=(n/5);int addrose20=0;int addrose5=0;
if(num_rose>5){
if(num_rose>=20){
addrose20=5*(num_rose/20);// add 5 roses when num is bigger than 20
addrose5=(num_rose%20)/5;//add 1 rose when remain num is bigger than 5
num_rose=num_rose+addrose20+addrose5;//sum them
}
else{
addrose5=num_rose/5;//add 1 when ...
num_rose=num_rose+addrose5;
}
}
printf("Ming can get %d roses\n",num_rose);
}
//Project 4 play with numbers
#include <stdio.h>
#include <math.h>
int main(){
printf("insert three doubles\n");
float a,b,c; //不可以是double
scanf("%f %f %f",&a,&b,&c); //&a "&" cannot be ignored
float sum,average,square_sum,sqrt_sum_of_square;
sum=a+b+c; //summary
average=(a+b+c)/3.0;
square_sum=a*a+b*b+c*c; //calculate a*a+b*b+c*c
sqrt_sum_of_square=sqrt(a*a+b*b+c*c); //calculate sqrt()
printf("sum is:\n %f \naverage is:\n %f \nsum of square is:\n %f \nsqrt of their square's summary is:\n %f \n",sum,average,square_sum,sqrt_sum_of_square);
}
//极坐标转换 将极坐标转换为直角坐标
//
#include <stdio.h>
#include <math.h>
#define pi 3.141592653
int main(){
printf("please insert r theta (double)\n");
double r,theta;//坐标
scanf("%lf %lf",&r,&theta);//load x y
double x,y;
theta=theta/180.0*pi; //1°=pi/180
x=r*cos(theta);
y=r*sin(theta);
printf("x=%lf y=%lf\n",x,y);
}