C语言用三种方式打印数组
#include<stdio.h>
void pointarray2(int a[]){//用指针变量指向数组元素
int *point;
int i;
for(i=0;i<10;i++){
point=&a[i];
printf("%d",*point);
}
printf("
");
}
void pointarray(int a[]){//通过数组名计算数组的元素地址,找出元素的值
int *point;
int i;
point=a;
for(i=0;i<10;i++){
printf("%d",*(point+i));
}
printf("
");
}
int main(){
int a[10]={9,8,7,6,5,4,3,2,1,0};
int i;
for(i=0;i<10;i++){//下标法
printf("%d",a[i]);
}
printf("
");
pointarray(a);
pointarray2(a);
return 0;
}
实现数组倒序存储
#include<stdio.h>
void swap(int *p1,int *p2){
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
void reverse(int a[],int n){
int i;
int *low,*high;
for(i=0;i<=(n-1)/2;i++){
low=&a[i];
high=&a[n-1-i];//n-1-i指向对应的元素
swap(low,high);
}
}
int main(){
int a[10]={3,4,6,2,3,1,4,5,6,0};
int n=10,i;
for(i=0;i<n;i++){
printf("%d",a[i]);
}
printf("
");
printf("reverse later---------
");
reverse(a,10);//将数组倒叙排放
for(i=0;i<n;i++){
printf("%d",a[i]);
}
printf("
");
}
max min search
#include<stdio.h>
int *max,*min;
void Max_Min_Search(int *a,int n){
int i=0;
max=a;
min=a;
for(;i<n;i++){
if(a[i]>*max){
max=&a[i];
}
if(a[i]<*min){
min=&a[i];
}
}
}
int main(){
int a[10]={24,10,23,5,2,540,26,30,34,35};
int i=0;
for(;i<10;i++){
printf("%d ",a[i]);
}
printf("
");
printf("---------------Searching------------------
");
Max_Min_Search(a,10);
printf("max=%d min=%d
",*max,*min);
}
打印二维数组
#include<stdio.h>
void PrintTwoArray(int (*a)[4],int row,int len){//(*a)[4]必须要传递列值,数组指针变量
int i,j;
for(i=0;i<row;i++){
for(j=0;j<len;j++){
printf("%d ",*(*(a+i)+j));
}
printf("
");
}
}
int main(){
int a[3][4]={5,6,7,8,9,3,0,4,5,6,7,3};
printf("a的地址:%d
",a);
printf("a: %d
",&a);
printf("a[0]:%d
",a[0]);
printf("*(*(a+1)):%d
",*(*(a+1)));
printf("*(*(a+1)+2):%d
",*(*(a+1)+2));
printf("&a[1][0]:%d
",&a[1][0]);
printf("--------------------
");
PrintTwoArray(a,3,4);
}
copy字符串
#include<stdio.h>
void print_string(char *string){
printf("----------------string b is------
");
for(;*string!=' ';string++){
printf("%c",*string);
}
printf("
");
}
void copy_string5(char *a,char *b){
char *p;
p=b;
while(*p++=*a++){}
p=b;
print_string(p);
}
void copy_string4(char *a,char *b){
char *p;
p=b;
while(*a!=' '){
*p++=*a++;
}
p=b;
print_string(p);
}
void copy_string3(char *a,char *b){
char *p;
p=b;
while((*p++=*a++)!=' '){
}
p=b;
print_string(p);
}
void copy_string2(char *a,char *b){
char *p;
p=b;
while((*p=*a)!=' '){
p++;
a++;
}
p=b;
print_string(p);
}
void copy_string1(char *a,char *b){
char *p1,*p2;
p2=b;
p1=a;
for(;*p1!=' ';p1++,p2++){
*p2=*p1;
}
*p2=' ';
p2=b;
printf("-------------------------
");
print_string(p2);
}
int main(){
char string[]="hello world";
char *a="i love you",b[]="hello world ",*p1,*p2;//此处应该定义为char b[] 而不能用char *b;因为char *b放在了常量区,不能被修改.
char *s="hello ";
int i;
printf("%s
",string);
printf("%s
",s);
p1=a;
p2=b;
for(;*p1!=' ';p1++,p2++){
*p2=*p1;
}
*p2=' ';
printf("----------------string b is-------
");
for(i=0;b[i]!=' ';i++){
printf("%c",b[i]);
}
printf("---------------------
");
copy_string1(a,b);
copy_string2(a,b);
copy_string3(a,b);
copy_string4(a,b);
copy_string5(a,b);
}
指针函数传递参数
#include<stdio.h>
int add(int x,int y){
return x+y;
}
int min(int x,int y){
if(x>y){
return y;
}
return x;
}
int max(int x,int y){
if(x>y){
return x;
}else{
return y;
}
}
void prosess(int x,int y,int (*fun)()){///////////////////
printf("%d
",fun(x,y));
}
int main(){
int min(int,int);
int max(int,int);
int add(int,int);
void prosess(int ,int ,int (*fun)());
int a,b;
scanf("%d %d",&a,&b);
printf("max=");
prosess(a,b,max);
printf("min=");
prosess(a,b,min);
printf("sum=");
prosess(a,b,add);
}
输入序号打印学生各科成绩
#include<stdio.h>
int *search(int (*score)[4],int id){
return *(score+id);
}
int main(){
int score[][4]={12,3,34,54,56,25,63,90,12,57,63,27};
int id,i;
int *p;
scanf("%d",&id);
if(id-1>0&&id-1<3){
p=search(score,id-1);
for(i=0;i<4;i++){
printf("%d ",*(p+i));
}
printf("
");
}
}
判断不及格的学生
#include<stdio.h>
int *search(int (*score)[4],int id){
return *(score+id);
}
int print_score1(int (*score)[4],int i){
int j;
for(j=0;j<4;j++){
if(*(*(score+i)+j)<60){
return i+1;
}
}
return -1;
}
void print_score(int *p){
int i=0;
for(;i<4;i++){
printf("%d ",*(p+i));
}
printf("
");
}
int main(){
int score[][4]={12,3,34,54,70,89,63,90,12,57,63,27};
int id,i;
int *p1;
scanf("%d",&id);
if(id-1>=0&&id-1<3){
p1=search(score,id-1);
print_score(p1);
}
for(i=0;i<3;i++){
if(print_score1(score,i)>0){
printf("%d号不及格 ",print_score1(score,i));
}else{
printf("及格 ");
}
}
}
字符串排序
#include<stdio.h>
#include<string.h>
void sort(char *name[],int n){
int i,j;
char *p;
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(strcmp(name[i],name[j])>0){
p=name[i];
name[i]=name[j];
name[j]=p;
}
}
}
}
void print(char *name[] , int n){
int i;
for(i=0;i<n;i++){
printf("%s ",name[i]);
}
printf("
");
}
int main(){//指针数组
char *name[]={"php","go","c++","java","c"};
int n=5;
sort(name,n);
print(name,n);
}
char *name[]={"php","go","c++","java","c"};
char **p;
int i;
int n=5;
for(i=0;i<n;i++){
p=name+i;
printf("%5s",*p);
}
void test(){
int name[]={1,2,4,53,3};
int *p1,*p2;
p1=&name[1];
p2=&name[4];
printf("%d",p2-p1);//3
}
带参数的宏定义
#include<stdio.h>
#define M(a,b) (a>b)?a:b
int main(){
int a,b;
scanf("%d %d",&a,&b);
printf("max=%d",M(a,b));
}
结构体
#include<stdio.h>
struct date{
int year;
int month;
int day;
};
struct{
int id;
char *name;
char gender;
int age;
float score;
char *addr;
struct date birthday;
}student1,student2={1,"hanmeimei",'m',45,22.2f,"hubei",2019,12,12};
int main(){
student1=student2;
printf("number=%d
name=%s
gender=%c
age=%d
score=%f
addr=%s
birthday=%d-%d-%d
",student1.id,student1.name,student1.gender,student1.age,student1.score,student1.addr,student1.birthday.year,student1.birthday.month,student1.birthday.day);
}
实现投票
#include<stdio.h>
#include<string.h>
struct PushTicket{
char *name;
int ticket;
}*point,ticket[5]={{"hanmeimei",0},{"lilei",0},{"lili",0},{"c",0},{"java",0}};
void PrintTicket(struct PushTicket *point){
int i;
point=ticket;
printf("------------候选人的票-----------
");
for(i=0;i<5;i++,point++){
printf("%s-----:%d
",point->name,point->ticket);
}
}
void compare(char *input){
int i;
point=ticket;
for(i=0;i<5;i++,point++){
if(strcmp(input,point->name)==0){
(point->ticket)++;
}
}
}
int main(){
int i;
char input[20];
point=ticket;
printf("------------候选人-----------
");
for(i=0;i<5;i++,point++){
printf("%s ",point->name);
}
printf("
");
printf("---------------
");
while(1){
printf("请输入候选人名称投票:");
scanf("%s",&input);
compare(input);
if(strcmp(input,"-1")==0){
break;
}
}
PrintTicket(point);
}
动态数组
链表
#include<stdio.h>
struct student{
int num;
float score;
struct student *next;
};
void PrintLink(struct student *p){
while(p!=NULL){
printf("num=%d,score=%f",p->num,p->score);
p=p->next;
}
}
int main(){
struct student a,b,c,*head;
a.num=1;
a.score=89.0f;
a.next=&b;
b.num=2;
b.score=90;
b.next=&c;
c.num=3;
c.score=100;
c.next=NULL;
head=&a;
PrintLink(head);
}
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student{//定义链表
int num;
float score;
struct student *next;
};
void print(struct student *head){//打印链表
while(head!=NULL){
printf("num=%d,score=%f
",head->num,head->score);
head=head->next;
}
}
struct student *DeleteNode(int input,struct student *head){
struct student *p1,*p2;
if(head==NULL){
printf("空链表
");
}
p1=head;
while(p1->num!=input&&p1->next!=NULL){//当输入的shu和链表的书不相等时.
p2=p1;
p1=p1->next;
}
if(p1->num==input){
if(p1==head){
head=p1->next;
}else{
p2->next=p1->next;
}
}
return head;
}
struct student *insert(struct student *stu,struct student *head){//有bug
struct student *p1,*p2,*p0;
p0=stu;
if(head==NULL){
printf("NUll linktable
");
}
p1=head;
while(p0->num>p1->num&&(p1->next!=NULL)){
p2=p1;
p1=p1->next;
if(p0->num<=p1->num){
if(head==p1){
head=p0;
}else{
p2->next=p0;
p0->next=p1;
}
}else{
p1->next=p0;
p0->next=NULL;
}
}
p1=head;
p2=p1;
return head;
}
struct student *create(){//创建链表
int n=0;
struct student *head,*p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("input num-------------:");
scanf("%d",&p1->num);
printf("input score-----------:");
scanf("%f",&p1->score);
head=NULL;
while(p1->num!=0){
n++;
if(n==1){
head=p1;
}else{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("input num-------------:");
scanf("%d",&p1->num);
printf("input score-----------:");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
int main(){
int input;
struct student *stu,*p,stu1;
stu=create();
p=stu;
print(p);
printf("
");
printf("删除节点-----:");
scanf("%d",&input);
print(DeleteNode(input,p));
printf("
");
printf("插入节点");
printf("input num-------------:");
scanf("%d",&stu1.num);
printf("input score-----------:");
scanf("%f",&stu1.score);
print(insert(&stu1,stu));
printf("
");
printf("插入节点");
printf("input num-------------:");
scanf("%d",&stu1.num);
printf("input score-----------:");
scanf("%f",&stu1.score);
print(insert(&stu1,stu));
printf("
");
}
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *file;
if(!(file=fopen("d:\hello.txt","w"))){
printf("error");
}else{
printf("success!!!
");
}
}
用位运算实现大小写转换
#include<stdio.h>
int main(){
char ch;
printf("please input char:");
ch=getchar();
while(!(ch>'A'&&ch<'z')||(ch>'Z'&&ch<'a')){
printf("error please input:
");
ch=getchar();
}
if(ch & 32){//32的二进制位是100000 ch&100000得到第五位数是0,然后就换转大写
ch=ch&223;
}else{
ch=ch|255;//得到第五位数是1 转小写.
}
putchar(ch);
ch=getchar();//打印回车
putchar(ch);
}
struct与malloc
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student{
int id;
char *name;
int score;
};
void fun(struct student *p){
(*p).id=12;
p->name="hello";
p->score=90;
}
void show(struct student *p){
printf("%d %s %d
",p->id,p->name,p->score);
}
int main(){
struct student *stu;
stu=(struct student *)malloc(LEN);
fun(stu);
//printf("%d %s %d",stu.id,stu.name,stu.score);
show(stu);
}
动态数组的增删排
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct Arr{
int *pBase;//数组的首地址
int len;//数组的长度
int cnt;//当前有效元素的个数
};
void init_arr(struct Arr *,int length);
int append_arr(struct Arr *,int val);//追加
int insert_arr(struct Arr *,int pos,int val);
int delete_arr(struct Arr *,int pos,int *val);
//int get();
int is_empty(struct Arr *p);
int is_full(struct Arr *p);
int sort_arr(struct Arr *p);
void show_arr(struct Arr *p);
int reverse_arr(struct Arr *p);
int main(){
struct Arr arr;
int val;
init_arr(&arr,6);
append_arr(&arr,100);
append_arr(&arr,99);
append_arr(&arr,80);
append_arr(&arr,23);
if(append_arr(&arr,45)){
printf("append success
");
}
insert_arr(&arr,7,555);
if(append_arr(&arr,567)){
printf("append success
");
}
if(append_arr(&arr,1000)==1){
printf("append success
");
}
printf("%d
",arr.cnt);
show_arr(&arr);
printf("----------删除元素-------
");
delete_arr(&arr,3,&val);
printf("--------删除的元素是:%d
",val);
show_arr(&arr);
printf("----------倒置元素--------
");
reverse_arr(&arr);
show_arr(&arr);
printf("----------排序-------------
");
sort_arr(&arr);
show_arr(&arr);
printf("%d
",arr.cnt);
}
void init_arr(struct Arr *p,int length){
p->pBase=(int *)malloc(sizeof(int)*length);
if(NULL==p->pBase){
printf("error
");
exit(-1);
}
p->len=length;
p->cnt=0;
return;
}
int is_empty(struct Arr *p){
if(p->cnt==0){
return 1;
}
return 0;
}
void show_arr(struct Arr *p){
int i;
if(is_empty(p)==1){
printf("数组为空
");
}
for(i=0;i<p->cnt;i++){
printf("%d ",*(p->pBase+i));
}
printf("
");
}
int is_full(struct Arr *p){
if(p->cnt==p->len){
return 1;
}
return 0;
}
int append_arr(struct Arr *p,int val){
if(is_full(p)==1){
printf("数组已满
");
return 0;
}
//不满时
p->pBase[p->cnt]=val;
(p->cnt)++;
return 1;
}
int insert_arr(struct Arr *p,int pos,int val){
int i=(p->cnt)-1;
if(pos<1||pos>(p->cnt)+1){//<1或者大于当前有效元素
return 0;
}
if(is_full(p)==1){
printf("数组已满
");
return 0;
}
//pos的值从1开始,且从pos的位置之前插入
for(;i>=pos-1;i--){
p->pBase[i+1]=p->pBase[i];
}
p->pBase[pos-1]=val;
(p->cnt)++;
return 1;
}
int delete_arr(struct Arr *p,int pos,int *val){
int i;
if(is_empty(p)==1){
printf("数组为空
");
return 0;
}
if(pos<1||pos>p->cnt){
return 0;
}
*val=p->pBase[pos-1];
for(i=pos;i<=p->cnt;i++){
p->pBase[i-1]=p->pBase[i];
}
(p->cnt)--;
return 1;
}
int reverse_arr(struct Arr *p){
int i=0,temp,j=p->cnt-1;
//int *low,*high;
if(is_empty(p)==1){
printf("数组为空
");
return 0;
}
while(i<j){
temp=p->pBase[i];
p->pBase[i]=p->pBase[j];
p->pBase[j]=temp;
i++;
j--;
}
/*low=&(p->pBase[0]);
high=&(p->pBase[p->cnt]);
for(i=0;i<=(high-low)/4;i++){
low=&(p->pBase[i]);
high=&(p->pBase[p->cnt-1-i]);
temp=*low;
*low=*high;
*high=temp;
}*/
return 1;
}
int sort_arr(struct Arr *p){
int i,min,j,temp;
for(j=0;j<p->cnt;j++){
min=p->pBase[j];
for(i=j+1;i<p->cnt;i++){
if(p->pBase[i]<min){
temp=p->pBase[i];
p->pBase[i]=min;
min=temp;
}
}
p->pBase[j]=min;
}
return 1;
}