c语言学习
#include <stdio.h>
void sum_diff(int *n1,int *n2,int *sum,int *diff)
{
*sum=*n1+*n2;
*diff=(*n1>*n2)?*n1-*n2:*n2-*n1;
printf("地址sum:%d 地址diff:%d \n",&sum,&diff);
}
int main()
{
int na,nb;
int wa=0,sa=0;
puts("请输入两个数整数:");
printf("整数A:");
scanf("%d",&na);
printf("整数B:");
scanf("%d",&nb);
sum_diff(&na,&nb,&wa,&sa);
printf("两数之和%d 两数之差%d\n",wa,sa);
printf("地址wa:%d 地址sa:%d",&wa,&sa);
return 0;
}
#include "stdio.h"
int main(void)
{
int sato=178;
int sanaka=175;
int hiraki=165;
int masaki=179;
int *isako,*hiroko;
isako=&sato;
hiroko=&masaki;
printf("伊莎喜欢的人的身高:%d\n",*isako);
printf("洋子喜欢的人的身高: %d\n",*hiroko);
isako=&sanaka;
*hiroko=180;
putchar("\n");
printf("sata %d\n",sato);
printf("sanaka %d\n",sanaka);
printf("hiraki %d\n",hiraki);
printf("masaki %d\n",masaki);
printf("伊莎喜欢的人的身高:%d\n",*isako);
printf("洋子喜欢的人的身高 %d\n",*hiroko);
}
#include "stdio.h"
int main()
{
int i;
int vc[5]={10,20,30,40,50};
int *ptr=&vc[0];
for(i=0;i<5;i++)
printf("vc[%d]=%d ptr[%d]=%d *(ptr+%d)=%d\n",i,vc[i],i,ptr[i],i,*(ptr+i));
}
#include "stdio.h"
int main(void)
{
//指针与指针之间也可以进行运算符和关系符的比较
int vc[3];
int *ptr=vc;
printf("vc==ptr:%d\n",vc==ptr);
printf("&vc[1]<=&vc[1]:%d\n",&vc[1]<=&vc[1]);
printf("&vc[1]<=&vc[2]:%d\n",&vc[1]<&vc[2]);
printf("&vc[2]-&vc[0]:%d\n",&vc[2]-&vc[0]);
}
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main()
{
struct horse{
int age;
int height;
char name[20];
char father[20];
char mother[20];
struct horse *next
};
struct horse *first=NULL;
struct horse *current=NULL;
struct horse *previous=NULL;
char test='\0';
for(; ; )
{
printf("enter details of a%s horse(Y or N)?",first !=NULL?"nother":"");
scanf(" %c",&test);
if(tolower(test)=='n') break;
current=(struct horse*)malloc(sizeof(struct horse));
if(first==NULL)
first=current;
if(previous !=NULL)
previous->next=current;
printf("\nEnter the name of the horse:");
scanf("%s",current->name);
printf("\nEnter the age of the horse:");
scanf("%d",¤t->age);
printf("\nEnter the height of the horse:");
scanf("%d",¤t->height);
printf("\nEnter the father of the horse:");
scanf("%s",current->father);
printf("\nEnter the name mother of the horse:");
scanf("%s",current->mother);
current->next=NULL;
previous=current;
}
current=first;
while(current !=NULL)
{
printf("\n\n%s is %d years old,%d hands high,",current->name,current->age,current->height);
printf("and has %s and %s as parents",current->father,current->mother);
previous=current;
current=current->next;
free(previous);
}
}
8、冒泡排序
#include "stdio.h"
#define NUMBER 5
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
void sort(int data[],int n)
{
int k=n-1;
while(k>=0)
{
int i,j;
for(i=1,j=-1;i<=k;i++)
if(data[i-1]>data[i])
{
j=i-1;
swap(&data[i],&data[j]);
}
k=j;
}
}
int main()
{
int i;
int height[]={178,175,173,165,179};
sort(height,NUMBER);
for(i=0;i<NUMBER;i++)
printf("%2d:%4d\n",i+1,height[i]);
return 0;
}
9、结构体
#include "stdio.h"
#include "string.h"
struct gstudent{
char name[20];
int height;
float weight;
long schols;
};
void hiroko(struct gstudent *std)
{
if((*std).height<180) (*std).height=180;
if((*std).weight>180) (*std).weight=80;
}
int main()
{
struct gstudent sanaka={"sanaka",175,181.5,70000};
hiroko(&sanaka);
printf("姓名=%s\n",sanaka.name);
printf("身高=%d\n",sanaka.height);
printf("体重=%f\n",sanaka.weight);
printf("奖学金=%ld\n",sanaka.schols);
return 0;
}
10、结构体二
#include "stdio.h"
#include "string.h"
typedef struct {
char name[20];
int height;
float weight;
long schols;
}student;
void hiroko(student *std)
{
if(std->height < 180) std->height=180;
if(std->weight > 80) std->weight=80;
}
int main()
{
student sanaka={"sanaka",175,181.5,70000};
hiroko(&sanaka);
printf("姓名=%s\n",sanaka.name);
printf("身高=%d\n",sanaka.height);
printf("体重=%f\n",sanaka.weight);
printf("奖学金=%ld\n",sanaka.schols);
return 0;
}
10、返回结构体的函数
#include "stdio.h"
struct xyz{
int mx;
long my;
double mz;
};
//返回结构体xyz
struct xyz set_xyz(int x,long y,double z)
{
struct xyz temp;
temp.mx=x;
temp.my=y;
temp.mz=z;
return (temp);
}
int main(void)
{
struct xyz xyz={0,0,0};
xyz=set_xyz(10,320,13.76);
printf("xyz.mx=%d\n",xyz.mx);
printf("xyz.my=%ld\n",xyz.my);
printf("xyz.mz=%f\n",xyz.mz);
return 0;
}
11、寻找制定元素的指针
#include <stdio.h>
int search(int *apt,int n,int key)
{
int *p;
for(p=apt;p<apt+n;p++)
if(*p==key) return p-apt;
return -1;
}
int *find(int *apt,int n,int key)
{
int *p;
for(p=apt;p<apt+n;p++)
if(*p==key)
return p;
return NULL;
}
int a[]={90,80,70,60,30,20,10,9,8,7,6,5,42,40,50,1,2,3};
int main()
{
int i,key;
printf("the elements of array a is: \n");
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
printf(" %d",a[i]);
printf("\n the address of a[0] is: %d.\n",&a[0]);
puts("\nplease input the key number you want to search:");
scanf("%d",&key);
i=search(a,sizeof(a)/sizeof(a[0]),key);
printf("\nthe label number of the key number %d in the array is:%d.",key,i);
printf("\nthe point value of the key number %d in the array is:%d.",key,find(a,sizeof(a)/sizeof(a[0]),key));
puts("\n\n press any key to quit...");
getchar();
}
12 汽车燃料问题
#include "math.h"
#include "stdio.h"
#define sqr(n) ((n)*(n))
typedef struct {
int x;
int y;
}point;
typedef struct{
double fuel;
point pt;
}car;
void put_info(car c)
{
printf("当前位置:(%d,%d)\n",c.pt.x,c.pt.y);
printf("剩余燃料:%.2f升\n",c.fuel);
}
int move(car *c,int dx,int dy)
{
double dist=sqrt(sqr(dx)+sqr(dy));
if(dist>c->fuel)
return 0;
c->pt.x +=dx;
c->pt.y +=dy;
c->fuel -=dist;
return 1;
}
int main(void)
{
car mycar={90.0,{0,0} };
while(1){
int slct;
int dx,dy;
put_info(mycar);
printf("开动汽车吗 yes1 /no 0");
scanf("%d",&slct);
if(slct !=1) break;
printf("x方向的行驶距离:");
scanf("%d",&dx);
printf("y方向的行驶距离");
scanf("%d",&dy);
if(!move(&mycar,dx,dy))
puts("\a燃料不足无法行驶");
}
return 0;
}
13 无符号数的
#include "stdio.h"
int count_bits(unsigned x)
{
int count=0;
while(x){
if(x&1u) count++;
x >>=1;
}
return(count);
}
int int_bits(void)
{
return(count_bits(~0u));
}
void print_bits(unsigned x)
{
int i;
for(i=int_bits()-1;i>=0;i--)
putchar(((x>>i)&1u)?'1':'0');
}
int main()
{
unsigned nx;
printf("请输入一个非负整数:");
scanf("%u",&nx);
print_bits(nx);
putchar('\n');
return 0;
}
14、统计行数、字符串数、字母数
#include "stdio.h" #define IN 1 #define OUT 0
int main() { int c,n1,nw,nc,state; state=OUT; n1=nw=nc=0; while((c=getchar())!=EOF){ ++nc; if(c=='\n') ++n1; if(c==' '||c=='\n'||c=='\t') state=OUT; else if(state==OUT){ state=IN; ++nw; } } printf("%d %d %d\n",n1,nw,nc); }
15、统计各个数字、空白符、其他字符
#include "stdio.h"
int main()
{
int c,i,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;i++)
ndigit[i]=0;
while((c=getchar()) !=EOF)
if(c>='0' &&c <= '9')
++ndigit[c-'0'];
else if(c==' '||c=='\n'||c=='\t')
++nwhite;
else
++nother;
printf("digits =");
for(i=0;i<10;i++)
{
printf(" %d",ndigit[i]);
}
printf(",white space=%d,other=%d\n",nwhite,nother);
}