您的位置:首页 > 编程语言 > C语言/C++

实验一 C++基础练习(附答案)

2013-09-14 10:01 316 查看
实验一 C++基础练习
实验目的和要求

1.掌握C++程序的基本格式与规范,学会编写简单的C++程序。

2.理解C++程序结构的特点。

3.熟悉C++程序基本的输入输出操作。

4.掌握C++数据类型常量和变量以及修饰符的使用方法。

5.掌握函数定义、函数调用和函数说明的方法。

6.熟悉作用域运算符的功能和基本使用方法。

7.掌握C++内存的动态分配与释放方法。

8.理解引用的概念,掌握引用的使用方法。

实验内容

1.编写一个程序,从键盘上输入摄氏温度,通过转换输出华氏温度,转换公式为。例如,输入摄氏温度为10时,输出华氏温度为50;输入摄氏温度为37时,输出华氏温度为98.6。要求输入输出时有提示。

2.定义一个结构体数据类型用于保存自己的个人信息,例如可以包含姓名、性别、年龄、几门课程的成绩等(根据自己的需要定义成员)。在程序中输入个人信息,然后按一定的格式输出个人信息。要求输入输出时有提示。

3.编写函数求2个和3个正整数中的最大值,并在主函数中进行调用验证。要求:

(1)利用函数重载实现;

(2)用参数带默认值的函数实现。

4.编写一个程序,动态定义一个整型数组(数组的大小通过键盘输入),给数组元素赋值。求数组所有元素的和,求和的结果使用全局变量sum存储,同时对数组中的奇数求和,结果使用局部变量sum存储,将两个结果输出。本题要求体会和理解作用域运算符的概念与基本使用方法,同时掌握动态内存分配和释放。

5.编写一个函数建立一个单链表。链表结构如下:



链表中节点的结构为:

typedef struct Node

{

int data;

Node *next;

} *LinkList;

建立单链表函数的原型为:

int CreateList(LinkList &L,int n)

其中L为单链表的头指针,创建的链表通过L返回;n为链表中节点的个数。函数中通过new运算符给每个节点分配存储空间,并通过cin输入节点中保存的数据,最后将节点插入到链表末尾。

再编写一个输出链表数据的函数,函数原型为:

int PrintList(LinkList L)

最后编写主函数进行测试。

6.阅读下列程序,指出错误的语句以及出错的原因。然后上机调试改正错误。

(1)

include <iostream.h>

void main()

{

cin>>x;

int y=x*x;

cout<<"y=<<y<<\n";

return 0;

}

(2)

#include <iostream.h>

void main()

{

int a,b;

a=7;

int s=a+b;

cout<<"a+b="<<s<<endl;

}

7.调试下列程序,写出输出结果,并解释输出结果

(1)

#include <iostream.h>

void main()

{

double dd=3.9,de=1.3;

double &rdd=dd,&rde=de;

cout<<rdd+rde<<','<<dd+de<<endl;

rdd=2.6;

cout<<rdd<<','<<dd<<endl;

de=2.5;

cout<<rde<<','<<de<<endl;

}

(2)

#include <iostream.h>

void main()

{

void fun(int ,int &);

int a,b;

fun(2,a);

fun(3,b);

cout<<"a+b="<<a+b<<endl;

}

void fun(int m,int &n)

{

n=m*4;

}

(3)

#include <iostream.h>

int &fun(int);

int aa[5];

void main()

{

int a=5;

for(int i=0;i<5;i++)

fun(i)=a+i;

for(i=0;i<5;i++)

cout<<aa[i]<<" ";

cout<<endl;

}

int &fun(int a)

{

return aa[a];

}



参考答案(非权威,仅仅是我自己的理解,如有错误,请批评指正!)

第一题:



#include <iostream.h>



void main()

{

float x;

cout<<"请输入一个摄氏温度:";

cin>>x;

cout<<"它的华氏温度为:"<<9*x/5+32<<endl;

}





第二题:



#include <iostream.h>

#define N 1



struct student //定义并声明结构变量

{

int number;

char name[20];

char sex[10];

int age;

float score[2];

}stu
;



void main()

{

cout<<"请输入学号:";

cin>>stu[0].number;



cout<<"请输入姓名:";

cin>>stu[0].name;



cout<<"请输入性别:";

cin>>stu[0].sex;



cout<<"请输入年龄:";

cin>>stu[0].age;



cout<<"请输入A课程成绩:";

cin>>stu[0].score[0];



cout<<"请输入B课程成绩:";

cin>>stu[0].score[1];



cout<<endl;

cout<<endl;



cout<<"你的学号为:"<<stu[0].number<<endl;

cout<<"你的姓名为:"<<stu[0].name<<endl;

cout<<"你的性别为:"<<stu[0].sex<<endl;

cout<<"你的年龄为:"<<stu[0].age<<endl;

cout<<"你的A课成绩为:"<<stu[0].score[0]<<endl;

cout<<"你的B课成绩为:"<<stu[0].score[1]<<endl;

}





第三题:



#include<iostream>

using namespace std;





int MAX(int x,int y) {return x>y?x:y;}

int MAX(int a,int b,int c) {return (a>b?a:(b>c?b:c));}





void main()

{

int x,y,a,b,c;



cout<<"请输入x:";

cin>>x;

cout<<"请输入y:";

cin>>y;



cout<<endl;



cout<<"请输入a:";

cin>>a;

cout<<"请输入b:";

cin>>b;

cout<<"请输入c:";

cin>>c;



cout<<endl;

cout<<endl;



cout<<"MAX(x,y)="<<MAX(x,y)<<endl;

cout<<"MAX(a,b,c)="<<MAX(a,b,c)<<endl;

}





第四题:



#include<iostream>

using namespace std;

int sum=0;



void main()

{

int N,sum=0;

cout<<"请确定数组大小N的值:";

cin>>N;

int *p = new int
;



cout<<"请输入N个整数到数组p
中:"<<endl;

for(int i=0;i<N;i++) cin>> p[i];

for(int k=0;k<N;k++)

{

::sum += p[k];

if( p[k]%2!=0) sum += p[k];

}

cout<<endl<<endl;

cout<<"数组p
的总和为:";

cout<<::sum<<endl;



cout<<endl<<endl;



cout<<"数组p
的奇数和为:";

cout<<sum<<endl;



delete []p;

}





第五题:



#include <iostream.h>



typedef struct Node

{

int data;

Node *next;

} *LinkList;



LinkList CreateList(LinkList &L,int n)

{

LinkList p = NULL;

LinkList temp = NULL;

int data;

cout << "请任意输入n个正整数:" << endl;

cin >> data;

while (n > 0)

{

p = new struct Node;

p->data = data;

p->next = NULL;

if (!L) L = temp = p;

else

{

temp->next = p;

temp = p;

}

cin >> data;

n--;

}

return L;

}



int PrintList(LinkList L)

{

LinkList p = L;

cout << "链表的全部数据如下:" << endl;

while (p)

{

cout << p->data << " ";

p = p->next;

}

cout << endl;

return 0;

}



void freeLink(LinkList L)

{

LinkList p = NULL;

while (L)

{

p = L;

p->next = NULL;

L = L->next;

delete p;

}

}



void main()

{

LinkList L = NULL;

L = CreateList(L,3); //输入链表数据

PrintList( L ); //输出链表数据

freeLink( L ); //释放单链表的头指针

}





第六题:

(1)



修改前的程序:

include <iostream.h> //头文件应定义应加上#号

void main()

{

cin>>x; //使用x之前应先申明x的内型

int y=x*x;

cout<<"y=<<y<<\n"; //引号的位置错误,应改为"y=",且换行应该使用endl

return 0; //main函数定义为void类型,因此不应该有返回值,故去掉这句话。

}



修改后的程序:



#include <iostream.h>

void main()

{

int x;

cin>>x;

int y=x*x;

cout<<"y="<<y<<endl;

}



(2)

修改前的程序:



#include <iostream.h>

void main()

{

int a,b; //整个过程中b都没有赋值,因此a+b为一个不确定的数。

a=7;

int s=a+b;

cout<<"a+b="<<s<<endl;

}



修改后的程序:



#include <iostream.h>

void main()

{

int a,b;

a=7;

b=0;

int s=a+b;

cout<<"a+b="<<s<<endl;

}





第七题:

(1)



#include <iostream.h>

void main()

{

double dd=3.9,de=1.3; //定义两个double类型的数据

double &rdd=dd,&rde=de; //分别对dd和de进行引用声明为rdd和rde



//由于引用与其所引用的变量的值永远保持相同。故rdd+rde=dd+de=3.9+1.3=5.2

//因此输出为:5.2,5.2

cout<<rdd+rde<<','<<dd+de<<endl;

rdd=2.6; //rdd=dd被赋值为2.6

cout<<rdd<<','<<dd<<endl; //rdd=dd=2.6因此输出为:2.6,2.6

de=2.5; //de=rde被赋值为2.5

cout<<rde<<','<<de<<endl; //de=rde=2.5 因此输出为:2.5,2.5

}



(2)



#include <iostream.h>

void main()

{

void fun(int ,int &); //函数原型说明

int a,b; //声明两个int类型的数据a,b

fun(2,a); //调用函数fun,使引用变量a=2*4=8

fun(3,b); //调用函数fun,使引用变量b=3*4=12

cout<<"a+b="<<a+b<<endl; //输出a+b的值20

}



void fun(int m,int &n)

{

n=m*4;

}



(3)



#include <iostream.h>

int &fun(int); //声明一个函数来引用数组aa[5]

int aa[5]; //声明一个数组



void main()

{

int a=5;

for(int i=0;i<5;i++)

//通过for循环,调用函数fun();为其建立引用,对数组aa[5]分别赋值

fun(i)=a+i;

for(i=0;i<5;i++) //通过for循环,分别输出显示数组aa[5]的值:5 6 7 8 9

cout<<aa[i]<<" ";

cout<<endl; //换行

}



int &fun(int a)

{

return aa[a];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: