C++指针作业

一、编程题(下面所有题目均需使用指针,可任选 3 道 )

  1. 利用指针统计整型数组(10个数组元素)中大于平均数的元素个数。
#include <iostream>
using namespace std;
int main() {
	int n[10],i,num, sum, avg,*p;
	for (i=0; i<10; i++) {
		cin>>*(n+i);
	}
	sum=0;
	for (i=0; i<10; i++) {
		sum+=n[i];
	}
	avg=sum/10;
	cout<<avg<<endl;
	num =0;
	p=n;
	for(i=0; i<10; i++,p++) {
		if (*p >avg) {
			num++;
		}
	}
	cout<<"数组元素大于平均数的个数是:"<<num;
	return 0;
}
  1. 从键盘输入一个字符串,然后再输入一个字符,判断该字符在字符串中出现的次数。例如字符串“Hello World”中字符“o”出现2次。
#include <iostream>
using namespace std;
int main() {
	char s[80],ch,*p;
	int count;
	cin>>s;
	cout<<"输入一个字符"<<endl;
	cin>>ch;
	p=s;
	count=0;
	while (*p!='\0') {
		if (*p== ch) {
			count++;
		}
		p++;
	}
	cout<<"字符在字符串中出现了次"<<count<<endl;
	return 0;
}
  1. 从键盘输入一个字符串,删除字符串的空格或数字字符。例如输入字符串“Hello Man!Mr.17”显示“HelloMan!Mr.”
#include <iostream>
using namespace std;
int main() {
	char a[80],b[80],*p,*q;
	cin.getline(a,80);
	p=a;
	q= b;
	while (*p!='\0') {
		if( *p!=' ' &&  (*p<'0' || *p>'9')) {
		    *q= *p;
			 q++;
		}
		p++;
	}
	*q='\0';
	cout<<"删除后的字符串:"<<b;
	return 0;	
}
  1. 将字符串转换成数字,如字符串“9527”转换成数字9527
#include <iostream>
using namespace std;
int main() {
	char s[20],*p;
	int n=0;
	cin>>s;
	p=s;
	while (*p !='\0') {

		if(*p>='0' && *p<='9') {
			n=n*10+(*p-'0');
		}
		p++;
	}
	cout<<n;

	return 0;
}
  1. 用指针实现将用户输入由数字字符和非数字字符组成的字符串中的数字提取出来,例如输入“asd123rt456;;789sf”,则产生的数字分别是123,456,789
#include <iostream>
using namespace std;
int main() {
	char s[80],*p;
	int n;
	cin>>s;
	p=s;
	n=0;
	while (*p!='0') {
		if (*p>='0' && *p<='9' ) {
			n=n*10+(*p-'0');
		}
		if ( (*p>='0' && *p<='9' ) && (*(p+1)<'0'|| *(p+1)>'9' ) ) {
			cout<<n<<endl;
			n=0;
		}
		p++;
	}
	return 0;
}
  1. 从键盘输入两个字符串,判断第二个字符串在第一个字符串中出现的次数。
#include <iostream>
using namespace std;
int main() {
	
	char str1[50],str2[10],*p1,*p2;
	int s=0;
	cout<<"please intput two string!\n";
	cin>>str1;
	cin>>str2;
	p1=str1;
	p2=str2;
	while(*p1!='\0') {

		if(*p1==*p2) {
			while(*p1==*p2 && *p2!='\0') {
				p1++;
				p2++;
			}
		}
		else
			p1++;
		if(*p2=='\0')
			s++;
		p2=str2;
	}
	cout<<"字符串&s在字符串8s中出现次数"<<s;
	return 0;
}

Python 绘图作业

一、请使用重复指令绘制如下图形(可以不考虑颜色的变化,共 36 根)。

import turtle
turtle.pensize(10)
turtle.speed(100)
cs=['orange','blue','green','red','pink']
for i in range(36):
    turtle.pencolor(cs[i % 5 ])
    turtle.fd(200)
    turtle.goto(0,0)
    turtle.left(10)
turtle.sety(-200)
turtle.pencolor('gray')
turtle.pensize(1)
turtle.circle(200)
turtle.done()

二、请使用重复指令绘制如下图形。

import turtle
turtle.speed(100)
turtle.bgcolor("black")

#红色 90 根
turtle.seth(0)
turtle.pencolor("blue")
turtle.pensize(3)
turtle.up()
turtle.sety(-80)
turtle.down()
turtle.circle(100)

turtle.up()
turtle.goto(0,0)
turtle.down()
turtle.pensize(1)

# 90根红线
turtle.pencolor("red")
for i in range(90):
    turtle.fd(300)
    turtle.dot(5)
    turtle.goto(0, 0)
    turtle.left(4)
#绿色 30 根
turtle.pencolor("green")
turtle.seth(6)
for i in range(30):
    turtle.fd(250)
    turtle.dot(5)
    turtle.goto(0, 0)
    turtle.left(12)

#白线
turtle.pencolor("white")
turtle.seth(10)
for i in range(6):
    turtle.fd(150)
    turtle.dot(5)
    turtle.goto(0, 0)
    turtle.left(60)

turtle.seth(-10)
for i in range(6):
    turtle.fd(150)
    turtle.dot(5)
    turtle.goto(0, 0)
    turtle.left(60)

turtle.done()

Python 逻辑作业

  1. 输出如下格式的九九乘法表
for i in range(9,0,-1):
    for j  in range(1,i+1):
        print(j,"X",i,"=",i*j,end="\t")
    print()
  1. 在银行存1000元钱,银行一年的利息5%,编码实现一年之后钱变成了多少?
m=1000
m=m*0.05+m
print(m)
  1. 如果你一共学习110分钟,请编码实现,你学习了多少小时多少分钟。
m=110
h=m // 60
f=m % 60
print(h,f)
  1. 有变量 a=10 和 b=20 ,编写代码交换 a 和 b 中的值。
a=10
b=20
c=a
a=b
b=c
print(a,b)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚大果壳

码文不易,晚上熬夜需要点咖啡钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值