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);
}
c语言学习的更多相关文章
- C语言学习<输入输出函数,函数的调用>
#include <stdio.h> /* 输入输出函数的学习 函数的调用 2017.05.25 soulsjie */ //输入连个数字求最大值 void main(){ int Max ...
- c语言的输入输出函数
参考文章: http://blog.sina.com.cn/s/blog_784f40b80100psg9.html C语言输入输出函数分为两类: 1.格式化输入输出函数 2.非格式化输入输出 --- ...
- C语言博客作业—函数嵌套调用
一.实验作业 1.1 PTA题目:递归法对任意10个数据按降序排序 1.1.1设计思路 void sort(int a[],int n) { 定义整型循环变量i,中间变量temp,最小值min: 令m ...
- C语言中输入输出函数
1.1.1 格式化输入输出函数Turbo C2.0 标准库提供了两个控制台格式化输入. 输出函数printf() 和scanf(), 这两个函数可以在标准输入输出设备上以各种不同的格式读写数据.pri ...
- C语言 · 字符串输入输出函数
算法提高 3-2字符串输入输出函数 时间限制:1.0s 内存限制:512.0MB 描述 编写函数GetReal和GetString,在main函数中分别调用这两个函数.在读入一个实数 ...
- Go语言学习笔记(7)——函数和方法
Go语言中同时有函数和方法! 函数: go程序必须要包含一个main函数.main函数不能有任何参数和返回值! 1. 定义方法示例: func max(num1, num2 int) int { // ...
- 【Go语言学习】匿名函数与闭包
前言 入坑 Go 语言已经大半年了,却没有写过一篇像样的技术文章,每次写一半就搁笔,然后就烂尾了. 几经思考,痛定思痛,决定金盆洗手,重新做人,哦不,重新开始写技术博文. 这段时间在研究Go语言闭包的 ...
- 【Go语言学习笔记】函数做参数和闭包
函数做参数 在Go语言中,函数也是一种数据类型,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型.类似于重写(同名覆盖). 回调函数:函数有一个参数是函数类型,这个 ...
- 【C语言学习】-06 函数
函数分类.函数声明和定义.函数调用,形参实参.函数嵌套调用
- go语言基础之普通函数的调用流程
函数调用流程:先调用后返回,先进后出,函数递归,函数调用自己本分,利用此物点 1.普通函数的调用流程 package main //必须 import "fmt" func fun ...
随机推荐
- C#技术------垃圾回收机制(GC)
GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...
- dtree的使用
第一步:到官网下载下载dtree的相关包. 第二步:导入相关包 <link rel="StyleSheet" href="${ctx}/dtree/dtree. ...
- HDU 5667 Sequence 矩阵快速幂
官方题解: 观察递推式我们可以发现,所有的fi都是a的幂次,所以我们可以对fi取一个以a为底的log,gi=loga fi 那么递推式变gi=b+c∗gi−1+ ...
- ios之UI中自定义cell
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 【原】泛型-Java
泛型是Java SE5.0中新增的新特性,通过泛型使得在代码编译的时候就能检查类型安全,并且所有的强制类型转换都是自动和隐式的,从而提高代码的重用率. Note:在JavaSE7+以后的版本中,构造函 ...
- 重装mysql步骤
, 卸载MySQL 2, 删除目录 C:\Documents and Settings\All Users\Application Data\MySQL (这个真的有,原先没注意到,删掉!) 如果没有 ...
- strust1.x中formbean的原理及作用
from: http://blog.csdn.net/tuiroger/article/details/3947896 今天张老师讲了一些比较重要的strust标签,<html:link ...
- VPN两点注意事项
今天折腾了半天vpn,特记录以下两点注意事项: 1.客户端VPN连接,点右键属性=>网络选项卡=>双击Internet 协议版本 4 IPV4=>高级=>远程网络上使用默认网关 ...
- [置顶] 斗地主算法的设计与实现--项目介绍&如何定义和构造一张牌
大学期间,我在别人的基础上,写了一个简易的斗地主程序. 主要实现了面向对象设计,洗牌.发牌.判断牌型.比较牌的大小.游戏规则等算法. 通过这个斗地主小项目的练习,提高了我的面向对象设计能力,加深了对算 ...
- [二]poi实践一
1.创建时间格式的cell 2.创建不同格式的cell(字符串.布尔.数值) 3.读取遍历xls文件 4.抽取excel的内容