#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. Atom 扩展离线安装

    1.下载原始包 2.解压放入atom的packages文件夹中 3.通过nodejs的npm指令进行安装 运行->cmd 4.重启Atom就ok了

  2. Aspcms所有标签调用

    错误号3002: 出现此问题一般是有些频道模板不全,或者缺少一部分模板导致,或者没有单独设置html文件夹,并将所有html页面放进去 基本语法 {aspcms:sitepath} 网站终极目录(可放 ...

  3. 利用逻辑运算符?"三个数字比大小

    static void Main(string[] args)        {            int a, b, c;            while (true)            ...

  4. poj2723

    把每对钥匙看做一个变量,那两个钥匙看做他的两个状态 每一个开门的要求就是一个条件(xi or xj) 很显然有了2sat的基本要素 2sat是一个判定性问题,而这题求最多能过几个门: 不难想到二分答案 ...

  5. LA_4670_Dominating_Patterns_(AC自动机+map)

    描述 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. Azure 媒体服务支持 DASH 实时传送流

    Kilroy Hughes Azure媒体服务数字媒体架构师 本文重点介绍 Azure 媒体服务支持的 DASH 实时传送流功能,同时阐述如何利用这些功能将实时和点播自适应流传送至 Web 浏览器 ...

  7. weblogic配置数据源出错

    Connection test failed. Listener refused the connection with the following error: ORA-12505, TNS:lis ...

  8. 使用C#调用Python脚本,带参数列表 z

    static void Main(string[] args) { string[] strArr;//参数列表 string sArguments = @"Pythons.py" ...

  9. C#索引器的作用及使用

    1. 作用: 可以使得类和实例能够像数组那样使用一样,又称为带参属性 2. 区分 (1)索引器与数组的比较: 索引器的索引值不受类型限制.用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值 ...

  10. 远程连接centos

    yum install tigervnc   yum install tigervnc-server Windows 7下载客户端 tigervnc-1.2.0.exe,在http://sourcef ...