中山大学程序设计与实验第七次实验报告:作用域,生存期"/>
中山大学程序设计与实验第七次实验报告:作用域,生存期
一.实验目的
1. 了解变量的作用域、生存期和可见性。
2. 理解变量和函数的声明、定义、调用的区别
3. 了解递归函数
4. 了解函数重载、函数模板
二.实验原理
任何一种编程中,作用域是程序中定义的变量所存在的区域,超过该区域变量就不能被访问。
作用域是指标识符在程序中可见的部分,可以看作是标识符的一个有效范围。按其作用域范围可分为全局作用域和局部作用域。
全局作用域表现为标识符从定义处开始可以在整个程序或其他程序中进行引用
局部作用域则只能在局部范围内引用。
C++作用域分以下几类:类作用域、局部作用域、命名空间作用域、全局作用域。C++函数名称具有全局作用域,如果变量和常数的名称也在所有函数和命名空间之外声明也具有全局 作用域。在块内{ }声明的变量和常量具有局部作用域,它们在块外不可见。
函数的参数与函数最外层块中声明的局部变量具有相同的作用域。
如果嵌套块内部和外部包含具有相同名称的本地声明标识符,嵌套块内部标识符具有名称优先级,覆盖外部标识符,即就近原则
三.实验内容
实验一
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void LetterCount(string line, int count);
//Nunber of letters in line is returned in count
void LineCount(istream& file, int& count);
//Nunber of lines in file is returned in count
int main()
{ifstream inFile;inFile.open("C:\\Users\\冰镇大西瓜\\Desktop\\test.dat");//打开文件int count = 0;LineCount(inFile, count);cout << "Number of lines: " << count << endl;inFile.close();return 0;
}
void LetterCount(string line, int count)
{count = line.length();cout << " has " << count << " characters;" << endl;
}
void LineCount(istream& file, int& count)
{string line;while (1){getline(file, line);if (file)//对是否读取完成进行判断{count++;cout << "Line " << count;LetterCount(line, count);}else//若读取完成,则退出循环break;}
}
运行程序得到
程序符合预期
实验二
对局部变量和全局变量进行注释得
#include <iostream>
using namespace std;
void fn1(int x);
void fn2(int& x);
//声明全局变量
int x = 1;
int y = 2;
int main()
{//声明main()函数中的局部变量 int x = 10;int y = 20;cout << "x = " << x << ",y = " << y << endl;fn1(x);cout << "x = " << x << ",y = " << y << endl;fn2(x);cout << "x = " << x << ",y = " << y << endl;return 0;
}
void fn1(int x)
{int y = 100;//这里y为fn1()内的局部变量,作用域只在fn1中 cout << "x = " << x << ",y =" << y << endl;
}
void fn2(int& x)//这里x为引用传参,main函数中变量x的值会被改变
{int y = 100;x = 30;cout << "x = " << x << ",y =" << y << endl;
}
实验三
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void double_root(double a, double b, double c);
void single_root(double a, double b, double c);
void complex_root(double a, double b, double c);
int main()
{double a, b, c,judgement;cout << "这是一个一元二次方程求根程序,请依次输入三个系数a,b,c的值(用空格分隔):";//提示用户输入cin >> a >> b >> c;if (a != 0)//当a不为0时{judgement = sqrt(b * b - 4 * a * c);//进行判别式的计算if (judgement > 0)//有两个实数根时double_root(a, b, c);//进入double_root()函数else if (judgement == 0)//有一个实数根时single_root(a, b, c);//进入single_root()函数else//无实数根时complex_root(a, b, c);//进入complex_root()函数}else//若a为0cout << "a不可以等于0,请重新输入";
}
void double_root(double a, double b, double c)
{cout << "x1=" << fixed << setprecision(5) << (-b + sqrt(b * b - 4 * a * c)) / (2 * a) << ";x2=" << fixed << setprecision(5) << (-b - sqrt(b * b - 4 * a * c)) / (2 * a);//输出x1和x2
}
void single_root(double a, double b, double c)
{cout << "x1=x2=" << fixed << setprecision(5) << -b / (2 * a);
}
void complex_root(double a, double b, double c)
{double re, im;re = -b / (2 * a);//计算实部im = sqrt(4 * a * c - b * b) / (2 * a);//计算虚部cout << "x1=" << fixed << setprecision(5) << re << "+" << fixed << setprecision(5) << im << "i;";//进行输出cout << "x2=" << fixed << setprecision(5) << re << "-" << fixed << setprecision(5) << im << "i";
}
将特定值代入程序运行得到
符合预期输出
实验四
#include <iostream>
#include <fstream>
#include <string>
#include<Windows.h>
using namespace std;
void word_display(istream& file);
int main()
{ifstream inFile;inFile.open("C:\\Users\\冰镇大西瓜\\Desktop\\word.dat");//打开歌词所在的文本文件地址word_display(inFile);inFile.close();//关闭文件return 0;
}
void word_display(istream& file)
{string word;//定义字符串变量while (1){if (file){system("cls");//清屏getline(file, word);//读取每一行歌词cout << word;//打印歌词Sleep(1000);//间隔为1s}elsebreak;}
}
歌词文本如下
将程序运行如下
歌词逐行显示,且每次只显示一行歌词,符合输出预期
实验五
在程序的编写中,发现一个问题:在竖着输出上联和下联时,如果一个一个字符进行输出,输出结果将是空格。对该疑问查找资料显示,一个汉字一般占用两个字符,因此一个一个字符地输出无法得到想要的结果
对程序进行修改后得
#include<iostream>
#include<string>
#include<Windows.h>
using namespace std;
void up_low(string up, string low);//定义显示上联下联的函数
void top_scroll(string middle);//显示横批的函数
int main()
{string up, low, middle;//定义字符串up = "根正苗红大学生";//上联low = "社会主义好青年";//下联middle = "天天向上";//横批top_scroll(middle);up_low(up, low);return 0;
}
void top_scroll(string middle)
{int i = middle.length();//获取字符串的长度cout << " ";for (int count = 0; count < i; count+=2){cout << middle[count]<<middle[count+1];//输出字符串Sleep(1000);//间隔为1s}
}
void up_low(string up, string low)
{int i = up.length();//读取字符串长度int count = 0;while (count < i){cout << endl << up[count] << up[count + 1] << "\t\t" << low[count] << low[count + 1];//两个两个字符输出字符串Sleep(1000);count += 2;}
}
运行后得到
符合预期输出
四.实验心得
通过本次实验,我进一步加深了对单步调试运用的理解,并基本掌握了局部变量和全局变量的判定,了解变量的作用域、生存期和可见性。理解变量和函数的声明、定义、调用的区别 ,了解递归函数,初步了解函数重载、函数模板,并认识到汉字一般由两个字符组成,受益匪浅
更多推荐
中山大学程序设计与实验第七次实验报告:作用域,生存期
发布评论