ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c++期末实验题

2021-12-20 12:06:06  阅读:151  来源: 互联网

标签:std cout int c++ stu 实验 using 期末 include


一、程序控制结构

1、用cin语句输入x,y的值,分别利用if和swich两种分支语句完成;

//if结构
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double x;
	cout << "输入具体x的值: ";
	cin >> x;
	if (x < 0) cout << x;
	else if (x >= 0 && x < 10) cout << 2 * x - 1;
	else cout << 3 * x - 1;
	return 0;
}

//switch结构
#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <math.h>
int main()
{
    int x, t;
    int y;
    scanf("%d", &x);
    t = (x < 0) + (x < 10) ;   //括号中的关系表达式有几个为真(1),确定了t的值,实则反映的是x的范围
    switch (t)
    {
    case 2: //(x<0) 、 (x<10) 为真有2,自然x>=0,且x<10
        y =x;
        break;
    case 1: //(x<0),(x<10) 为真有1,自然……
        y = 2 * x -1 ;
        break;
    case 0://只有(x<0) , (x<10) 全0了.也可写作default:
        y = 3*x-1;
    }
    printf("%d\n", y);
    return 0;
}

2、为顾客找零钱时,希望选用的纸币张数最少。例如,73 元,希望零钱的面值为 50 元一张,20 元一张,1 元三张。设零钱面值有 50 元、20 元、10 元.5 元和 1元,请编写程序,用户输入100 以下的数,计算找给顾客的各面值的纸币张数,数 据间以空格隔开。

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    int c, wushi, ershi, shi, wu, yi;
    cin >> c;
    wushi = c > 50 ? c / 50 : 0;
    cout << wushi;
    ershi = c - 50;
    ershi = (ershi > 0) ? ershi / 20 : 0;
    cout << " " << ershi;
    shi = c - wushi * 50 - ershi * 20;
    shi = (shi > 0) ? shi / 10 : 0;
    cout << " " << shi;
    wu = c - 50 - ershi * 20 - shi * 10;
    wu = (wu > 0) ? wu / 5 : 0;
    cout << " " << wu;
    yi = c - 50 - ershi * 20 - shi * 10 - wu * 5;
    yi = (yi > 0) ? yi : 0;
    cout << " " << yi << endl;

    return 0;
}

二、数组

1、用冒泡法对输入的任意十个整数按由小到大的顺序排列输出。 冒泡法原理:将相邻两个数进行比较,把小数调到前边,大数逐渐“下沉”,如此进 行一轮后,就把最大的数互换到最后,再进行一轮,则会把第二大数排在倒数第二 的位置上,进行 N-1轮后,整个数列即可排好。在这种排序过程中,小数如同气泡 一样逐层上浮,而大数逐个下沉,因此,被形象地喻为“冒泡”。

#include<iostream>
using namespace std;
int main()
{
	//对冒泡排序进行升序排序 
	int arr[10] = { 4, 1, 2, 5, 3, 7, 6, 9, 8, 10 };
	int b = sizeof(arr) / sizeof(arr[0]);//定义数组中元素个数;
	cout << "原数组为:" << endl;
	for (int i = 0; i < b; i++)
	{
		cout << arr[i] << " ";
	}
	//冒泡排序
	for (int j = 1; j < b; j++)
	{
		for (int i = 0; i < b - j; i++)
		{
			if (arr[i] > arr[i + 1])//交换数组元素
			{
				int temp = arr[i];
				arr[i] = arr[i + 1];
				arr[i + 1] = temp;
			}
		}
	}
	cout << endl << "排序后的数组为:" << endl;
	for (int i = 0; i < b; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

2、有十个数按从大到小的顺序存放在一个数组中,输入一个数,要求找出该数是数组中 第几个元素的值,如果该数不在数组中,则打印出“无此数”。

#include <iostream>
using namespace std;
int BinarySearch(int myarray[], int n, int key)
{
    int low = 0, high = n - 1;
    int mid = 0;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (myarray[mid] == key)
            return mid;
        if (myarray[mid] < key)
            low = mid + 1;

        else
            high = mid - 1;

    }
    return -1;
}


int main()
{
    int myarray[] = { 1,3,4,6,8,7 };
    int index = BinarySearch(myarray, 6, 8);
    if (index != -1)
    {
        cout << "Find it!" << endl;
        cout << "index is:" << index << endl;
    }
    else
        cout << "Not Found!" << endl;

    return 0;
}

3、有一个 3×4 的矩阵,编写程序求出其中最大元素的值和最小元素的值,以及它们所在 的行号和列号。


#include<stdio.h>
void main()
{
    int i, j, colum = 0, row = 0, max;
    int a[3][4] = { {1,2,3,4},{9,8,7,6},{-10,10,-5,2} };
    max = a[0][0];
    for (i = 0; i <= 2; i++)
    {
        for (j = 0; j <= 3; j++)
        {
            if (a[i][j] > max)
            {
                max = a[i][j];
                colum = j;
                row = i;
            }
        }
    }
    printf("max=%d,row=%d,colum=%d", max, colum, row);
    printf("\n");
}

三、函数

1、编写求阶乘的函数,在主函数中调用该函数,求 1!+2!+3!+…+n!并输出结果,n 从键盘上输入(n<10)。

#include<iostream>
using namespace std;
double FA(int n)
{
	double m = 1;
	for (int i = 1; i <= n; i++)
		m *= i;
	return m;
}

int main()
{
	double ans = 0, n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		ans += FA(i);
	cout << ans;
	return 0;
}

2、编写一组求数组中最大最小元素的函数。 int imax(int array[], int count);//求整型数组的最大元素 int imin(int array[], int count);//求整型数组的最小元素 其中,参数count为数组中的元素个数,函数的返回值即为求得的最大或最小 元素之值,要求同时编写出主函数进行验证。

#include<iostream>
using namespace std;
int imax(int array[], int count) {
	int max = array[0];
	for (int i = 1;i < count ; i++)
	{
		if (array[i] > max) {
			max = array[i];
		}
	}
	return max;
}
int imin(int array[], int count) {
	int min = array[0];
	for (int i = 1; i < count; i++)
	{
		if (array[i] < min) {
			min = array[i];
		}
	}
	return min;
}   
int main() {
	int count;
	int a[100];
	cin >> count;
	for (int i = 0; i < count; i++) 
	{
		cin >> a[i];
	}
	int max = imax(a, count);
	int min = imin(a, count);
	cout << max << endl;
	cout << min << endl;
	return 0;
}

3、编写函数 int isprime(int a),用来判断整数 a 是否为素数,若是素数, 函数返回 1,否则返回0。调用该函数找出任意给定的n个整数中的素数。 注意,1 不是素数。


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int i, j ;
	int k = 0;
	for (i = 100; i <= 200; i++) {
		for (j = 2; j < i; j++)
			if (i % j == 0)break;
		if (j == i)
		{
			cout << '\t' << i;
			k++;
			if (k % 5 == 0)cout << endl;
		}
	}
	system("pause");
	return 0;
}

四、指针与结构体

1、上机验证程序;

#include<iostream>
using namespace std;
int main()
{
	char str1[] = "I love CHINA!", str2[20], * p1, * p2;
	p1 = str1; p2 = str2;
	for (; *p1!= '\0'; p1++, p2++)
		*p2 = *p1;
	*p2 = '\0';
	p1 = str1;
	p2 = str2;
	cout << "str1 is:" << p1 << endl;
	cout << "str2 is:" << p2 << endl;
	return 0;
}

2、阅读程序,写出运行结果;

#include<iostream>
using namespace std;
void main()
{
	char a[10] = "fdadegda";
int i, j;
for (i = j = 0; a[i] != '\0'; i++)
if (a[i] != 'a')
a[j++] = a[i];
a[j ]= '\0';
cout << a << endl;
}




3、定义一个学生结构体,包括学号、性别,数学,英语四条信息,输入五名学生信息,分别输出数 学和英语最高分的学生。 要求:性别使用枚举类型定义,但输出要用男或女。

#include <iostream>
using namespace std;
struct Student
{
    char num[13]; 
    char name[10]; 
    int cpp;
    int math;
    int english;
    int grade;
    double average;
};
const int N = 5;
int main()
{
    int i, j, k;
    Student stu[N];
    for (i = 0; i < N; i++)
    {
        cin >> stu[i].num >> stu[i].name >> stu[i].cpp >> stu[i].math >> stu[i].english;
        stu[i].grade = stu[i].cpp + stu[i].math + stu[i].english;
        stu[i].average = stu[i].grade / 3.0;
    }
    cout << "学号\t姓名\t总分\t均分" << endl;
    for (i = 0; i < N; i++)
        cout << stu[i].num << '\t' << stu[i].name << '\t' << stu[i].grade << '\t' << stu[i].average << endl;
    return 0;
}

4、编写函数重置两个变量的值,该函数的原型为“void reset(int *a, int *b);”。 函数内部将两个值重置为两个变量原值的平均数(出现小数则四舍五入);

#include<iostream>
using namespace std;
void reset(int* a, int* b)
{
	if ((*a + *b) % 2 != 0)
		*a = *b = (*a + *b) / 2 + 1;
	else
		*a = *b = (*a + *b) / 2;
}
int main()
{
	int x, y;
	cin >> x >> y;
	reset(&x, &y);
	cout << x << " " << y << endl;
	return 0;
}

5、定义一个结构体变量(包括年、月、日),要求输入年、月、日,程序能计算本日在本年中 是第几天,注意闰年问题。

#include <iostream>
using namespace std;
struct Student
{
    char num[13]; 
    char name[10]; 
    int cpp;
    int math;
    int english;
    int grade;
    double average;
};
const int N = 5;
int main()
{
    int i, j, k;
    Student stu[N];
    for (i = 0; i < N; i++)
    {
        cin >> stu[i].num >> stu[i].name >> stu[i].cpp >> stu[i].math >> stu[i].english;
        stu[i].grade = stu[i].cpp + stu[i].math + stu[i].english;
        stu[i].average = stu[i].grade / 3.0;
    }
    cout << "学号\t姓名\t总分\t均分" << endl;
    for (i = 0; i < N; i++)
        cout << stu[i].num << '\t' << stu[i].name << '\t' << stu[i].grade << '\t' << stu[i].average << endl;
    return 0;
}

标签:std,cout,int,c++,stu,实验,using,期末,include
来源: https://blog.csdn.net/weixin_50132000/article/details/122037324

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有