车大大一第一学期期末c++上机题复习题

车大大一第一学期期末c++上机题复习题

注:代码是自己手敲可能有错误欢迎指正,当然有更好的方法也要提出来呐

题目一-随机数加减法

(a) Write a program for primary school students to test the addition and subtraction within 20. Each time the program randomly generates different addition or subtraction formulas. After each formula is given, the user enters the answer from the keyboard, and then gives the next formula, generating a total of 5 test questions. After all 5 questions are answered, the score will be displayed. The output of program is as follows:

 

1: 10 + 9 = ? 19

2: 11 - 5 = ? 6

3: 19 – 3 = ? 15

4: 9 + 0 = ? 9

5: 17 - 9 = ? 8

Score: 4/5 correct, 1/5 wrong.

 

(b) (5 scores) Indicate which question has got wrong answer. The output of program is as follows:

 

1: 10 + 9 = ? 19

2: 11 - 5 = ? 6

3: 19 – 3 = ? 15

4: 9 + 0 = ? 9

5: 17 - 9 = ? 7

Score: 3/5 correct, 2/5 wrong (No.3, 5).

我的代码

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<vector>
using namespace std;
void test01()
{
	srand(time(0));
	static int counter =0;
	vector<int>array(5);
	for(int i =0;i<5;i++)
	{
		int a = rand()%20 +1;
		int b = rand()%20 +1;
		int c = rand()%2 +1;
		bool isright = false;
		int ans=0;
		switch(c)
	{
		
		case 1 :
			cout<<a<<" + "<<b<<" =";
			cin>>ans;
			if(ans==a+b) 
			{
				counter++;
				isright=true;
			}
			if(!isright) array[i]=i; //错误的话记录进array
			break;
		case 2 :
			cout<<a<<" - "<<b<<" =";
			cin>>ans;
			if(ans==a-b) 
			{
				counter++;
				isright=true;
			}
			if(!isright) array[i]=i;
			break;			
	} 

	}
	cout<<"ur score is "<< counter <<"/5"<<endl;
	cout<<"ur wrong questions :  " ;
	for(int i =0;i<5;i++)
	{
		if(array[i]!=0) cout<<array[i]+1<<" ";
	}
	
} 



int main()
{
	test01();
	system("pause");
	return 0;
}

题目二-模板排序

这边有点偷懒了,没有把用户的输入程序写上去,但感觉如果输入用模板的话int,char,double三个数组的长度不同不太好确定。问了下老师,老师说可以“在模板中定义一个非类型的形参”这个还没学到就先不用啦。

这边的程序中使用了俩个模板函数,一个是用于冒泡排序,另一个是用于打印输出数组,另外的三个test函数分别是三个类型的数组

我的代码

#include<iostream>
using namespace std;

template<class T>
void bubbysort(T array[], int len)
{
	for(int i =0;i<len-1;i++)
	 {
	 	for(int j=0;j<len-i-1;j++)
	 	 {
	 		if(array[j+1]>array[j]) swap(array[j+1],array[j]);
		 }
	 }
}


template<class T>
void printarr(T array[],int len)
{
	for(int i=0;i<len;i++)
	{
		cout<<array[i]<<" ";
	}
	cout<<endl;
}

void test01()
{
	int intarr[5]={8,1,5,7,4};
	cout<<"intarr排序前"<<endl;
	printarr(intarr,5);
	cout<<"intarr排序后"<<endl;
	bubbysort(intarr,5);
	printarr(intarr,5);
}

void test02()
{
	char chararr[18]="CDmnopEFGhijABklq"; 	//原本是17个字符,但要多一位给'\0' 
	cout<<"chararr排序前"<<endl;
	printarr(chararr,18);
	cout<<"chararr排序后"<<endl;
	bubbysort(chararr,18);
	printarr(chararr,18);
}

void test03()
{
	double doublearr[3]={1.11111, 3.33333, 2.22222};
	cout<<"doublearr排序前"<<endl;
	printarr(doublearr,3);
	cout<<"doublearr排序后"<<endl;
	bubbysort(doublearr,3);
	printarr(doublearr,3);
}

int main()
{
	char ans;
	test01();
	cout<< "do u want to continue?"<<endl;
	cout<<"Yes or No?"<<"Plz input Y or N"<<endl;
	cin>>ans;
	if(ans=='Y') 
	{
		test02();
		ans = 'N';
	}
	else cout << "Bye";
	cout<< "do u want to continue?"<<endl;
	cout<<"Yes or No?"<<"Plz input Y or N"<<endl;
	cin>>ans;
	
	if(ans=='Y') 
	{
		test03();
		ans = 'N';
	}
	else cout << "Bye";
	

	system("pause");
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值