栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

头歌C&C++ 面向过程编程综合练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

头歌C&C++ 面向过程编程综合练习

第1关:最大值函数
int Max(int a,int b)
{
    
    //找出两个参数中最大的那个并返回
    if(a>b)
    return a;
    else 
    return b;
    
}

int Max(int a,int b,int c)
{
    
    //找出三个参数中最大的那个并返回
    if(a>b)
    {
        if(a>c)
        return a;
        else 
        return c;
    }
    else 
    {
        if(b>c)
        return b;
        else
        return c;
    }
    
}
第2关:计算阶乘

fact.cpp

int Fact(int n)
{
    
    //使用递归计算 n 的阶乘
    if(n>1)
      return n*Fact(n-1);
    else 
      return 1;
    
}
fact.h
//添加定义
#ifndef FACT_H_
#define FACT_H_

int Fact(int n);

#endif

第3关:求矩阵最大元素
int max(int *b)
{
    if(b[0]>b[1])
    {
        if(b[0]>b[2])
        return b[0];
        else 
        return b[2];
    }
    else if(b[0]b[2])
        return b[1];
        else
        return b[2];
    }
}    

int MaxEle(int a[3][2],int *r,int *c)
{
    
    //返回最大元素和它所在的行、列号
    int i,j,b[3];
    for (i=0;i<3;i++)
       {
           if(a[i][0]>a[i][1])
           b[i]=a[i][0];
           else if(a[i][0]

第4关:使用链表进行学生信息管理

usr.h

#include 
using namespace std;


struct Linked
{
    
    //结构体的成员变量
    int sid;
    float sco;
    Linked *next;
    
};

Linked* Create()
{
    
    //创建并返回一个新链表
    Linked* head=new Linked;
    head->next = NULL;
    return head;
    
}

void InsertAfter(Linked *node,int num,float sc)
{
    
    //在指定节点后插入一个新节点,内容由 num,sc 参数指定
    Linked* newnode = new Linked;
    newnode->sid=num;
    newnode->sco=sc;
    newnode->next=node->next;
    node->next=newnode;
    
}

void DeleteAfter(Linked *node)
{
    
    //删除此节点之后的一个节点
    node->next = node->next->next;
    

    
}

Linked* GetByIndex(Linked *head,int index)
{
    
    //返回指定索引处的节点
    head=head->next;

    if(index==0)
    {
        return head;
    }
    else
    {
        while(index--)
        {
            head=head->next;
        }
        return head;
    }
    
}

void PrintAll(Linked *head)
{
    
    //按格式打印此链表中所有节点的成员变量
    Linked* p=head->next;
    while(p)
    {
        cout<sid<<" "<sco<next;
    }
    
    
}

run.cpp

#include "usr.h"

int main()
{
	int num;
	float score;
	cin >> num >> score ;
    Linked *lk = Create();
    InsertAfter(lk,num,score);
	cin >> num >> score ;
    InsertAfter(GetByIndex(lk,0),num,score);
	cin >> num >> score ;
    InsertAfter(GetByIndex(lk,1),num,score);
    DeleteAfter(GetByIndex(lk,0));
    PrintAll(lk);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009928.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号