c语言------期末考试编程练习题

判断是否是正确的ip地址

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int tran(char * a){
	int n;
	int i;
	int sum;
	int count;
	sum=0;
	count=1;
	n=strlen(a);
	for(i=n-1;i>=0;i--){
		sum=sum+(a[i]-'0')*count;
		count=count*10;
	}
	return sum;
}
int main(){
	char s[100]="192.41.6.20";
	char *ret;
	int i=0,j=0;
	char a[100][100];
	int n;
	memset(a,'\0',sizeof(a));
	for(i=0;i<strlen(s);i++){
		//if(!isdigit((int)s[i]))
		if(!(s[i]>='0'&&s[i]<='9')&&s[i]!='.')
		{
			puts("含有非数字");
			putchar(s[i]);
			return 0;
		}
	}
	i=0;
	ret=strtok(s,".");
	while(ret!=NULL){
		strcpy(a[i++],ret);
		ret=strtok(NULL,".");	
	}

	
	if(i>4){printf("个数多了");return 0;}
	for(j=0;j<i;j++){
			if(tran(a[j])<0||tran(a[j])>256){
				puts("不在0-255之间");
				//puts(a[j]);
				return 0;
		}
		
	}
	for(j=0;j<i;j++){
		printf("%0X",tran(a[j]));
	}
	
	return 0;


}

字符串数字转为int类型数字

#include <stdio.h>
#include <string.h>
#include <ctype.h>
//字符串数字转为int类型数字
void trans(){
	char s1[]="192";
	int i;
	int j;
	int c,tmp;
	int n=strlen(s1);
	c=0;
	j=1;
	for(i=n-1;i>=0;i--){
		tmp=s1[i]-'0';
		c=j*tmp+c;
		j=j*10;
	}
	printf("%d",c);
}
int main()
{
	trans();
	return 0;
}

删除子串

先分为奇数和偶数组,然后排序输出

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
//删除子串
void zichuan(){
	char s1[]="aaaBCDeeebcdfff";
	char s2[]="bcd";
	char *p;
	int n=strlen(s2);
	int i;
	for(i=0;i<strlen(s1);i++){
		s1[i]=tolower(s1[i]);	
	}
	while(strstr(s1,s2)!=NULL){
		p=strstr(s1,s2);
		while(*p!='\0'){
			*p=*(p+n);
			p++;	
		}	
	}
	puts("删除子串");
	puts(s1);
}
//先分为奇数和偶数组,然后排序输出
void sort(){
	int n;
	int i,j,tmp,q;
	int a[1000];
	int b[1000],c[1000];
	scanf_s("%d ",&n);
	j=0;
	q=0;
	for(i=0;i<n;i++){
		scanf_s("%d",&a[i]);
		if(a[i]%2==0){
			b[j++]=a[i];
		}
		else{
			c[q++]=a[i];
		}
	}
	puts("\n");
	puts("b:");
	for(i=0;i<j;i++){
		printf("%d ",b[i]);
	}
	puts("c:");
	for(i=0;i<q;i++){
		printf("%d ",a[i]);
	}
	puts("\n");
	for(i=1;i<n;i++){
		if(a[i]<a[i-1]){
			tmp=a[i];
			for(j=i-1;a[j]>tmp;j--){
				a[j+1]=a[j];
			}
			a[j+1]=tmp;
		}
	
	}

	for(i=0;i<n;i++){
		printf("%d ",a[i]);
	}
}


int main(){

	//zichuan();
	//sort();
}

反向输出—c++

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
	string str;
	cin >> str;
	reverse(str.begin(),str.end());
	cout << str;
	return 0;
}

反向输出—c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
int main(){
	char A[MAXSIZE];
	int i;`在这里插入代码片`
	scanf("%s",A);
	for(i=strlen(A)-1;i>=0;i--){
		printf("%c",A[i]);
	}
}

输出能同时被整除的数–c

输出100-1000之间能被5,6 整除的数字,每10个位一行,相邻两个数用空格隔开,每一行末尾没有空格

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
int main(){
	int i=100;
	int c=0;
	for(i=100;i<1000;i++)
		if(i%5==0&&i%6==0){
			if(c<9){
				printf("%d ",i);
				c++;
			}
			else{
				printf("%d\n",i);
				c=0;
			}
		}
}

十进制转为2进制,并计算1,0个数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
int main(){
	int n,c,d;
	int A[MAXSIZE];
	int i=0;
	n=5;
	c=0;
	d=0;
	scanf_s("%d",&n);
	while(n){
		if(n%2==1){
			c++;
		}
		else{
			d++;
		}
		n=n/2;		
	}
	printf("1:%d,0:%d\n",c,d);
}

输入5个数,插入链表,排序,输出

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAXSIZE 100

struct	Node{
	int data;
	struct Node * next;
};

void create(struct Node *h,int *a){
	struct Node *p,*q;
	int i;
	q=h;
	for(i=0;i<5;i++){
		p=(struct Node *)malloc(sizeof(struct Node));
		p->data=a[i];
		q->next=p;
		q=p;
	}
	p->next=NULL;

	return ;
}
void sort(int *a,int n){
	
	int tmp;
	int i,j;
	for(i=1;i<n;i++){
		if(a[i]<a[i-1]){
			tmp=a[i];
			for(j=i-1;a[j]>tmp;j--){
				a[j+1]=a[j];
			}
			a[j+1]=tmp;
		}
	}

}
int main(){
	struct Node *h,*p,*q;
	int i;
	int a[MAXSIZE];
	int n=5;
	for(i=0;i<n;i++){
		scanf_s("%d",&a[i]);
	}
	
	sort(a,n);
	h=(struct Node *)malloc(sizeof(struct Node));
	
	create(h,a);
	p=h->next;
	for(i=0;i<5;i++){
		printf("%d ",p->data);
		p=p->next;
	}	
	return 0;
}

删除最小值

#include<stdio.h>
#include<string.h>
void dele(int *a,int i,int n){
	int j;
	printf("delete a[i]:%d\n",a[i]);
	for(j=i+1;j<n;j++){
		a[j-1]=a[j];
	}

}
int find_min(int *a,int n){
	int min,min_i,i;
	min=a[0];
	min_i=0;
	for(i=0;i<n;i++){
		if(a[i]<min){
			min=a[i];
			min_i=i;
		}
	}
	return min_i;
}
void sort(int *a,int n){
	int i,j,tmp;
	for(i=1;i<n;i++){
		if(a[i]<a[i-1]){
			tmp=a[i];
			for(j=i-1;a[j]>tmp;j--){
				a[j+1]=a[j];
			}
			a[j+1]=tmp;		
		}
	
	}

}
int main()
{	
	int i;
	int n=5;
	int a[]={5,4,3,2,1};
	//sort(a,n);
	i=find_min(a,n);
	dele(a,i,n);
	for(i=0;i<n;i++){
		printf("%d ",a[i]);
	}
}

字符数组排序,字符串排序,数组排序

#include <stdio.h>
#include <string.h>
//字符数组的排序
void sort(){
	char s[]={'e','d','a','c','b'};
	int i,j;
	char tmp;
	int n;
	n=sizeof(s)/sizeof(char);
	for(i=0;i<n;i++){
		for(j=i+1;j<n;j++){
			if(s[j]<s[i]){
				tmp=s[i];
				s[i]=s[j];
				s[j]=tmp;			
			}
		}
	}
	for(i=0;i<n;i++){
		printf("%c",s[i]);
	}
}
//字符串的排序
void sort1(){
	char s[][100]={"aaaa","aaab","aabb","bbbb"};
	int i,j;
	char tmp[100];
	int n;
	n=sizeof(s)/sizeof(s[0]);
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(strcmp(s[i],s[j])<0){
				strcpy(tmp,s[i]);
				strcpy(s[i],s[j]);
				strcpy(s[j],tmp);
			
			}
		
		}
	}
	for(i=0;i<n;i++){
		puts(s[i]);
	}
}
//数组排序---交换排序
void sort2(){
	int i,j,tmp;
	int s[]={2,1,3,5,4};
	int n;
	n=sizeof(s)/sizeof(int);
	for(i=0;i<n;i++){
		for(j=i+1;j<n;j++){
			if(s[i]>s[j]){
				tmp=s[i];
				s[i]=s[j];
				s[j]=tmp;
			}
		}
	}
	for(i=0;i<n;i++){
		printf("%d",s[i]);
	}
}
//数组排序---顺序插入排序
void sort3(){
	int i,j,tmp;
	int s[]={2,1,3,5,4};
	int n;
	n=sizeof(s)/sizeof(int);
	for(i=1;i<n;i++){
		if(s[i]<s[i-1]){
			tmp=s[i];
			for(j=i-1;s[j]>tmp;j--){
				s[j+1]=s[j];
			}
			s[j+1]=tmp;
		}
	}
	for(i=0;i<n;i++){
		printf("%d",s[i]);
	}
}
int main(){
	sort3();
}

字符串删除从n-k

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
void del(){
	char s[]="12345678";
	//删除从3-5的字符
	int k=3,n=5;
	int i;
	for(i=n;i<strlen(s);i++){
		s[i-(n-k+1)]=s[i];
	}
	s[i-(n-k+1)]='\0';
	puts(s);
}
int main(){
	del();
}

重点:是否存在环

int cycle(struct node *h){
	struct node *fast;
	struct node *slow;
	if(h==NULL||h->next==NULL){
		return 0;}
	slow=h;
	fast=h->next;
	while(slow!=fast){
		if(fast==NULL||fast->next==NULL){	return 0;}
		slow=slow->next;
		fast=fast->next->next;
	}
	return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
struct node{
	int data;
	struct node *next;
};
//后插法
struct node * create(int a[],int n){
	struct node *h,*p,*q;
	int i;
	h=(struct node *)malloc(sizeof(struct node));
	q=h;
	for(i=0;i<n;i++){
		p=(struct node *)malloc(sizeof(struct node));
		p->data=a[i];
		q->next=p;
		q=p;
	}
	q->next=NULL;

	return h;
}
//头插法
struct node * create1(int a[],int n){
	struct node *h,*p,*q;
	int i;
	h=(struct node *)malloc(sizeof(struct node));
	h->next=NULL;
	for(i=0;i<n;i++){
		p=(struct node *)malloc(sizeof(struct node));
		p->data=a[i];
		p->next=h->next;
		h->next=p;
	}

	return h;
}
void out(struct node *h,int n){
	struct node *p;
	p=h->next;
	while(n--){
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}

void del(struct node *q){
	struct node *p;
	p=q->next;
	q->next=p->next;
	free(p);
}

int cycle(struct node *h){
	struct node *fast;
	struct node *slow;
	if(h==NULL||h->next==NULL){
		return 0;}
	slow=h;
	fast=h->next;
	while(slow!=fast){
		if(fast==NULL||fast->next==NULL){	return 0;}
		slow=slow->next;
		fast=fast->next->next;
	}
	return 1;
}


int main(){
	struct node *h,*q,*p;
	int n;
	int j;
	int a[]={1,2,3,4,5};
	n=5;
	h=create(a,n);
	out(h,n);
	
	p=h;
	while(p->next!=NULL){
		p=p->next;
	}
	p->next=h->next;
	printf("%d\n",cycle(h));
	p=h;
	while(--n){
		p=p->next->next;
		del(p);
		//out(p,n);
	}
	return 0;
}

重点:击鼓传花

#include<stdio.h>
#include<string.h>
#include<ctype.h>
typedef int elemtype;
void outa(elemtype *a,int n){
	int i;
	for(i=0;i<n;i++){
		printf("%d ",a[i]);
	}
	printf("\n");
}
void sort(elemtype *a,int n){
	int i,j,tmp;
	for(i=1;i<n;i++){
		if(a[i-1]>a[i]){
			tmp=a[i];
			for(j=i-1;a[j]>tmp;j--){
				a[j+1]=a[j];
			}
			a[j+1]=tmp;
		}
	}

}
int dele(elemtype *a,int i,int n){
	int j;
	for(j=i+1;j<n;j++){
		a[j-1]=a[j];
	}
	return n-1;
}
int main(){
	elemtype s1[]={1,2,3,4,5};
	int i=0,j=0;
	int n=5;

	while(n>0){
		j=0;
		while(j<2){
			j++;
			i=(i+1)%n;
		}
		n=dele(s1,i,n);
		outa(s1,n);
	}	
}

重点:构造节点

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAXSIZE 100

struct	Node{
	int data;
	struct Node * next;
};
int main(){
	struct Node *h;
	h=(struct Node *)malloc(sizeof(struct Node));
	h->next=NULL;
	return 0;
}

struct Node{
int data;
struct Node * next;
};最后的一个括号外面的分号特别容易漏掉,会报错哟
h->next=NULL;NULL需要大写

重点:生成随机数1-20

头文件仍然是:<stdlib.h>
如要产生[m,n]范围内的随机数num,可用:

int num=rand()%(n-m+1)+m;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAXSIZE 100
int main(){
	int n,c,d;
	int i;
	int A[MAXSIZE];
	for(i=0;i<5;i++){
		n=rand()%20+1;
		printf("%d ",n);
	}
}

重点::排序–c

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
void sort(int *A,int n){
	int i,j,tmp;
	for(i=1;i<n;i++){
		if(A[i]<A[i-1]){
			tmp=A[i];
			for(j=i-1;A[j]>tmp;j--){
				A[j+1]=A[j];
			}
			A[j+1]=tmp;
		}
	}
}
int main(){
	int n=5;
	int A[]={5,4,3,2,1};
	int i;

	sort(A,5);
	for(i=0;i<n;i++){
		printf("%d ",A[i]);
	}
}

先奇数偶数排序,然后再进行从小到大排序

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
void sort(int *A,int n){
	int i,j,tmp;
	for(i=1;i<n;i++){
		if(A[i]<A[i-1]){
			tmp=A[i];
			for(j=i-1;A[j]>tmp;j--){
				A[j+1]=A[j];
			}
			A[j+1]=tmp;
		}
	}
}
int main(){
	int n=5;
	int A[]={5,4,3,2,1};
	int B[MAXSIZE],C[MAXSIZE];
	int i,c,d;


	printf("\n");
	c=0;
	d=0;
	for(i=0;i<n;i++){
		if(A[i]%2==1){
			B[c++]=A[i];
		}
		else{
		    C[d++]=A[i];
		}
	}
	sort(B,c);
	sort(C,d);
	for(i=0;i<c;i++){
		printf("%d ",B[i]);
	}

	for(i=0;i<d;i++){
		printf("%d ",C[i]);
	}
	printf("\n");
}

重点:常用字符串函数

#include <ctype.h>
int tolower(int c) //把给定的字母转换为小写字母

int toupper(int c)
//该函数把小写字母转换为大写字母。
<string.h>
   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");
 
   ret = strcmp(str1, str2);
 
   if(ret < 0)
   {
      printf("str1 小于 str2");
<string.h>
void *memset(void *str, int c, size_t n) 
复制字符 c(一个无符号字符)到参数 str 所指向的字符串的前 n 个字符。

重点:删除所有子串

思路:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int main(){
	char s1[]="aaaacdeaaacde";
	char s2[]="cde";
	char *p;
	char *p2;
	p=strstr(s1,s2);
	printf("strstr(s1,s2)得到第一次出现子串的首个位置:%s\n",p);
	p2=p+strlen(s2);
	printf("指针p2表示第一次出现子串的末尾位置%s的下一位\n",p2);
	*p=*p2; 
	printf("用后面的字符覆盖第一次出现的子串:%s\n",s1);
}

完整求解代码:

#include <string.h>
#include <stdio.h>
int main(){
	char s1[]="aaabcdeeebcd";
	char s2[]="bcd";
	int len_s2;
	char *place;
	int i;
	len_s2=strlen(s2);
	
	while(strstr(s1,s2)!=NULL){//判断s2是否在s1中出现
		place=strstr(s1,s2);//place 指向子串第一次出现的位置
		while(*place!='\0'){//用后面的覆盖出现的子串,依次往前挪动
			*place=*(place+len_s2);
			place++;
		}
		printf("本次处理结果为%s\n",s1);
	}
	
}
#include<stdio.h>
#include<string.h>
int main()
{
	char str1[100],str2[100],t[100];
	char *p;
	gets(str1);
	gets(str2);
	
	while((p=strstr(str1,str2))!=NULL)
	{
		strcpy(t,p+strlen(str2));
		*p='\0';
		strcat(str1,t);
	}
	puts(str1);
}

重点:打印杨辉三角

#include<stdio.h>
int main()
{
    int n,a[14][14],i,j,k;
    scanf("%d",&n);
    while(n<=0||n>=13)
    {
        scanf("%d",&n);
    }
    for(i=1;i<=n;i++){
        a[i][1]=a[i][i]=1;
    }
    for(i=3;i<=n;i++){
        for(j=2;j<=i-1;j++){
            a[i][j]=a[i-1][j-1]+a[i-1][j];
        }
    }
    for(i=1;i<=n;i++){
        for(k=1;k<=n-i;k++)
            printf("   ");
        for(j=1;j<=i;j++)
            printf("%6d",a[i][j]);
        printf("\n");
    }return 0;
}

重点:二维字符串数组的行数

重点:字符串排序

#include <stdio.h>
#include <string.h>
#include <ctype.h>
//二维字符串数组的行数
void count(){
 char str[][6] = {"abc", "ABC", "abc"};
     
     
    printf("str行内元素%d\n", sizeof(*str));//这个会显示出二维数组中每个行的元素个数 (这里是指可容纳的个数)
    printf("str总%d\n", sizeof(str));//这个会显示出二维数组中元素的个数(这里是指可容纳的个数) 
 
    //其实就是利用了sizeof函数来判断 我们都知道sizeof()用来判断一个类型所占用的字节数的
    //所以:你问‘C语言二维字符串数组的行数怎么求啊?’ 你应该是说在定义数组时候没有显示的写出行数,可是此时需要求出行数是吧?
    //那么    行数 = 可容纳元素总数(这里是指可容纳的个数) / 行中元素个数(这里是指可容纳的个数)
 
    printf("行数=%d\n", sizeof(str)/sizeof(*str));
 
}
//字符串排序
void sort(){
	char c[][10]={"aaa","eee","bbb","ddd","qqq"};
	int i,j,tmp;
	char t[10];
	int n;
	n=sizeof(c)/sizeof(*c);
	for(i=0;i<n;i++){
		for(j=i+1;j<n;j++){
			if(strcmp(c[i],c[j])>0){
				strcpy(t,c[i]);
				strcpy(c[i],c[j]);
				strcpy(c[j],t);
			}
		}
	}
	
	for(i=0;i<n;i++){
		puts(c[i]);
	}
	//printf("%d",strlen(c));
}
int main(){
	sort();
	}

重点:常用函数

math.h

abs() fabs(),注意区分
在这里插入图片描述

<ctype.h>字符函数

在这里插入图片描述

#include <ctype.h>
int main(){
	printf("%d\n",isdigit('c'));
	printf("%d\n",isdigit('0'));

	printf("%d\n",isalpha('c'));
	printf("%d\n",isalpha('0'));

	printf("%d\n",isalnum('c'));
	printf("%d\n",isalnum('0'));
	printf("%d\n",isalnum('+'));

}

<string.h>

在这里插入图片描述

<stdlib.h>

在这里插入图片描述

stdio.h

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值