#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",&current->age);
printf("\nEnter the height of the horse:");
scanf("%d",&current->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);
}

c语言学习的更多相关文章

  1. C语言学习<输入输出函数,函数的调用>

    #include <stdio.h> /* 输入输出函数的学习 函数的调用 2017.05.25 soulsjie */ //输入连个数字求最大值 void main(){ int Max ...

  2. c语言的输入输出函数

    参考文章: http://blog.sina.com.cn/s/blog_784f40b80100psg9.html C语言输入输出函数分为两类: 1.格式化输入输出函数 2.非格式化输入输出 --- ...

  3. C语言博客作业—函数嵌套调用

    一.实验作业 1.1 PTA题目:递归法对任意10个数据按降序排序 1.1.1设计思路 void sort(int a[],int n) { 定义整型循环变量i,中间变量temp,最小值min: 令m ...

  4. C语言中输入输出函数

    1.1.1 格式化输入输出函数Turbo C2.0 标准库提供了两个控制台格式化输入. 输出函数printf() 和scanf(), 这两个函数可以在标准输入输出设备上以各种不同的格式读写数据.pri ...

  5. C语言 · 字符串输入输出函数

    算法提高 3-2字符串输入输出函数   时间限制:1.0s   内存限制:512.0MB      描述 编写函数GetReal和GetString,在main函数中分别调用这两个函数.在读入一个实数 ...

  6. Go语言学习笔记(7)——函数和方法

    Go语言中同时有函数和方法! 函数: go程序必须要包含一个main函数.main函数不能有任何参数和返回值! 1. 定义方法示例: func max(num1, num2 int) int { // ...

  7. 【Go语言学习】匿名函数与闭包

    前言 入坑 Go 语言已经大半年了,却没有写过一篇像样的技术文章,每次写一半就搁笔,然后就烂尾了. 几经思考,痛定思痛,决定金盆洗手,重新做人,哦不,重新开始写技术博文. 这段时间在研究Go语言闭包的 ...

  8. 【Go语言学习笔记】函数做参数和闭包

    函数做参数 在Go语言中,函数也是一种数据类型,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型.类似于重写(同名覆盖). 回调函数:函数有一个参数是函数类型,这个 ...

  9. 【C语言学习】-06 函数

    函数分类.函数声明和定义.函数调用,形参实参.函数嵌套调用

  10. go语言基础之普通函数的调用流程

    函数调用流程:先调用后返回,先进后出,函数递归,函数调用自己本分,利用此物点 1.普通函数的调用流程 package main //必须 import "fmt" func fun ...

随机推荐

  1. Visual Studio 那些隐藏的调试功能(转)

    原文出处:  微软互联网开发支持   Visual Studio 是一个强大的调试工具,里面很多隐藏功能少有人问津,但是在特定场景可以节省你很多时间,本文主要介绍一些Visual Studio调试相关 ...

  2. mapreduce: 揭秘InputFormat--掌控Map Reduce任务执行的利器

    随着越来越多的公司采用Hadoop,它所处理的问题类型也变得愈发多元化.随着Hadoop适用场景数量的不断膨胀,控制好怎样执行以及何处执行map任务显得至关重要.实现这种控制的方法之一就是自定义Inp ...

  3. 调试UnhandledExceptionFilter

    kernel32!UnhandledExceptionFilter通过判断当前进程是否附加了调试器,如果附加,就把异常交给调试器,如果没有,就把异常交给进程的UnhandledExceptionFil ...

  4. Frame 处理

    # -*- coding:utf-8 -*- """ 通过 id 或 name 识别处理 fram 框架 """ from selenium ...

  5. Cocos2d-x获取随机数

    计算机是无法产生真正的随机数的,都是伪随机.获取随机数的方式和算法多种多样,这里只给出一种方法,基于最新的C++11. 1 2 3 4 5 #include <random> std::u ...

  6. Android Fragment 基本介绍[转]

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

  7. 如何将SQL Server运行到Windows Azure上

    从2012年6月6日开始,Windows Azure上一些强大的新功能现在可用于预览,包括新的Windows Azure虚拟机(VM).其中有关Windows Azure虚拟机最强大的一件事是他们利用 ...

  8. (Step by Step)How to setup IP Phone Server(VoIP Server) for free.

    You must have heard about IP Phone and SIP (Software IP Phone).Nowadays standard PSTN phone are bein ...

  9. php5.5 yum源

    PHP官网在下载页面中只有源代码下载,当然使用编译安装也是可以的,但是使用二进制包安装会非常快速.虽然PHP官网没有提供二进制安装包,但是它推荐了两个第三方的YUM源(CentOS中默认的YUM安装的 ...

  10. 【原】用PHP搭建基于swoole扩展的socket服务(附PHP扩展的安装步骤及Linux/shell在线手册)

    最近公司的一项目中,需要用PHP搭建一个socket服务. 本来PHP是不适合做服务的,因为和第三方合作,需要采用高效而稳定的TCP协议进行数据通信.经过多次尝试,最终选择了开源的PHP扩展:swoo ...