文档库 最新最全的文档下载
当前位置:文档库 › C#上机实验答案

C#上机实验答案

C#上机实验答案
C#上机实验答案

实验二

(1)编写一个控制台应用程序Exp02_01,根据用户输入的两个整数,分别输出这两个整数的和、差、积和商。(提示:用

Convert.ToInt32(Console.ReadLine())把用户从键盘上的输入转换成整数)using System;

using System.Collections.Generic;

using System.Text;

namespace Exp02_01

{

class Program

{

static void Main(string[] args)

{

int a = Convert.ToInt32(Console.ReadLine());

//Console.WriteLine("{0}", a);

int b=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("a={0},b={1},a+b={2},a-b={3},a*b={4},a/b={5}", a, b, a + b, a - b, a * b, a / b);

}

}

}

(2)创建一个控制台应用程序Exp02_02,求1到100之和。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp02_02

{

class Program

{

static void Main(string[] args)

{

int Sum, i;

Sum = 0;

for (i = 1; i <= 100; i++)

Sum += i;

Console.WriteLine("Sum is " + Sum);

Sum = 0;

for (i = 100; i > 0; i--) // i也可以每次减1

Sum += i;

Console.WriteLine("Sum is " + Sum);

}

}

}

(3)创建一个控制台应用程序Exp02_03,求半径为从键盘上输入的任意一个双精度值的圆的面积。(提示:用Convert.ToDouble(Console.ReadLine())把用户从键盘上的输入转换成双精度数)

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp02_03

{

class Program

{

static void Main(string[] args)

{

// double b = 3.1415926;

double b = System.Math.PI;

//Console.WriteLine(System.Math.PI);

double a;

a = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("s={0},r={1}", a * a * b, a);

}

}

}

实验三

(1)创建一个控制台应用程序,

a.创建新项目,项目类型:Visual C# ,模板:控制台应用程序,名称:

Exp03_01。

b.命名空间Exp03_01中已有一个类Program,现在在命名空间Exp03_01

中添加一个类MyClass,在该类中声明两个公共的整型字段num1和num2,再声明一个公共的返回值为整型的方法GetSum(),该方法体内包含一条

语句return num1+num2;用来返回两数的和。

c.在Program类的Main方法中,创建一个MyClass类的实例变量mc,分

别给mc的num1和num2成员赋值,然后调用mc的方法GetSum()求得

两数之和,并显示在控制台上。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp03_01

{

class MyClass

{

public int num1, num2;

public int GetSum()

{

return num1 + num2;

}

}

class Program

{

static void Main(string[] args)

{

MyClass mc = new MyClass();

mc.num1 = Convert.ToInt32(Console.ReadLine());

mc.num2 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("{0}和{1}的和{2}",mc.num1,mc.num2,mc.GetSum());

}

}

}

(2)创建一个控制台应用程序,输出九九乘法表。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp03_02

{

class Program

{

static void Main(string[] args)

{

int i, j, sum = 0;

for (i = 1; i < 10; i++)

{

for (j = 1; j <= i; j++)

{

sum = i * j;

Console.Write("{0,2:d}*{1,2:d}={2,-2:d}", j, i, sum);

if (i == j)

Console.Write("\n");

}

}

}

}

}

(3)创建一个控制台应用程序,输出所有的水仙花数。(水仙花数为一个三位数,它们各个位上的立方和等于该数本身)。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp03_03

{

class Program

{

static void sxh()

{

int a, b, c, n;

for (a = 1; a <= 9; a++)

for (b = 0; b <= 9; b++)

for (c = 0; c <= 9; c++)

{

n = a * 100 + b * 10 + c;

if (n == a * a * a + b * b * b + c * c * c)

Console.WriteLine(n);

}

}

static void Main(string[] args)

{

int a, b, c;

Console.WriteLine("水仙花数数为:");

for (int n = 100; n <= 999; n++)

{

a = n / 100;

b = (n - a * 100) / 10;

c = n % 10;

if (n == a * a * a + b * b * b + c * c * c)

Console.WriteLine(n);

}

Console.WriteLine("****");

sxh();

}

}

}

实验四

(1)创建一个控制台应用程序Exp04_01,求1!+2!+……+10!。编写一个

函数来求一个数的阶乘。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp04_01

{

class Program

{

static int fac(int i)

{

int mm = 1;

for (int j = 1; j <= i; j++)

mm = mm * j;

return mm;

}

static void Main(string[] args)

{

int sum = 0;

for (int i = 1; i <= 10; i++)

sum = sum + fac(i);

Console.WriteLine("1!+2!+.....+10!={0}", sum);

Console.ReadKey();

}

}

}

(2)创建一个控制台应用程序Exp04_02,编写一个函数用来求任意多个整数中的最大值。(提示:用参数数组)

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp04_02

{

class Program

{

static int MaxV alue(params int[] arr)

{

int max = arr[0];

for (int i = 1; i < arr.Length; i++)

{

if (max < arr[i])

max = arr[i];

}

return max;

}

static void Main(string[] args)

{

//int maxval = MaxValue(3, 54, 6, 8, 23);

//int[] a ={ 3, 6, 8, 23, 45 };

// int[] a=new int[]{ 3, 6, 8, 23, 45 };

int n;

Console.WriteLine("请输入数组的元素个数:");

n = Convert.ToInt32(Console.ReadLine());

int[] a = new int[n];

for (int i = 0; i < a.Length; i++)

{

Console.WriteLine("请输入数组元素a[{0}]:", i);

a[i] = Convert.ToInt32(Console.ReadLine());

}

int maxval = MaxValue(a);

Console.WriteLine("最大值是:{0}", maxval);

Console.ReadKey();

//int maxval = MaxValue(3, 54,987);

//Console.WriteLine("最大值是:{0}", maxval);

//Console.ReadKey();

}

}

}

(3)创建一个控制台应用程序Exp04_03,在该程序中定义一个矩形类Rectangular,该类包含长和宽两个字段(这两个字段初始化值分别为3和4),同时该类包含计算矩形的面积、矩形的周长方法。然后在主方法Main中调用这两个方法。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp03_03

{

class Program

{

static void sort(int[] arr)

{

int temp;

for (int i = 0; i < arr.Length - 1; i++)

{

for (int j = i + 1; j < arr.Length; j++)

{

if (arr[i] > arr[j])

{

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

static void Main(string[] args)

{

int[] myarr = new int[10];

for (int i = 0; i < myarr.Length; i++)

{

Console.WriteLine("请输入一个整数:");

myarr[i] = Convert.ToInt32(Console.ReadLine());

}

sort(myarr);

Console.WriteLine("排序后的结果为:");

for (int i = 0; i < myarr.Length; i++)

{

Console.WriteLine(myarr[i]);

}

Console.ReadKey();

}

}

}

实验五

(1)创建一个控制台应用程序,求出1-1000之间的所有能被7整除的数,并计

算和输出每5个的和。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp05_01

{

class Program

{

//求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。

static void Main(string[] args)

{

int i, sum = 0, m = 0;

for (i = 1; i <= 1000; i++)

if (i % 7 == 0)

{

Console.Write("{0,5:d}", i);

sum = sum + i;

m++;

if (m % 5 == 0)

{

Console.WriteLine("\n 和为{0}", sum);

sum = 0;

}

}

}

}

}

(2)创建一个控制台应用程序,从键盘输入一个正整数,按数字的相反顺序输出。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp05_02

{

class Program

{

static void Main(string[] args)

{

//int m, n;

//Console.WriteLine("输入一个正整数:");

//int a = Convert.ToInt32(Console.ReadLine());

//while (a >= 10)

//{

// m = a % 10;

// Console.Write(m);

// a = a / 10;

//}

//Console.Write(a);

//// 或

Console.WriteLine("请输入一个正整数:");

string number = Console.ReadLine();

for (int i = number.Length - 1; i >= 0; i--)

{

Console.Write(number[i]);

}

Console.Read();

}

}

}

(3)创建一个控制台应用程序,编写一个函数用来输出前20项斐波拉基数列。该数列的生成方法为:F1=1,F2=1,Fn=Fn-1+Fn-2(n>=3)。

using System;

using System.Collections.Generic;

using System.Text;

namespace Exp05_03

{

class Program

{

public static int Fi(int n)

{

if (n == 1 || n == 2)

return 1;

else

return Fi(n - 1) + Fi(n - 2);

}

static void Main(string[] args)

{

for (int j = 1; j <= 20; j++)

Console.Write("{0},", Fi(j));

}

}

}

实验六

(1)创建一个控制台应用程序,在该程序中创建一个静态方法MaxMinAvgArray

(),该方法可以求出一个整型数组中的最大值、最小值和平均值。(考虑一下该方法包含几个参数,分别定义为何种类型的参数)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp06_01

{

class Program

{

public static void MaxMinArray(int[] a, out int max, out int min, out double avg)

{

int sum;

sum = max = min = a[0];

for (int i = 1; i < a.Length; i++)

{

if (a[i] > max) max = a[i];

if (a[i] < min) min = a[i];

sum += a[i];

}

avg = sum / a.Length;

}

static void Main(string[] args)

{

int[] score = { 87, 89, 56, 90, 100, 75, 64, 45, 80, 84 };

int smax, smin;

double savg;

MaxMinArray(score, out smax, out smin, out savg);

Console.Write("Max={0}, Min={1}, Avg={2} ", smax, smin, savg);

Console.Read();

}

}

}

(2)创建一个控制台应用程序,在该程序中定义一个学生类Student,该类能够记录学生姓名、班级和学号信息,并能够输出和修改这些信息。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp06_02

{

class Student

{

string name;

string bj;

string xh;

public string Name

{

set { name = value; }

get { return name; }

}

public string Bj

{

set { bj = value; }

get { return bj; }

}

public string Xh

{

set { xh = value; }

get { return xh; }

}

public Student(string s1, string s2, string s3)

{

name = s1;

bj = s2;

xh = s3;

}

}

class Program

{

static void Main(string[] args)

{

Student stud1 = new Student("李红", "07级三班", "0712001");

Student stud2 = new Student("王刚", "07级三班", "0712005");

stud1.Bj = "07级二班";

stud2.Xh = "0712009";

Console.WriteLine("stud1的姓名:{0},班级:{1},学号:{2}.", https://www.wendangku.net/doc/6b14103488.html,, stud1.Bj, stud1.Xh);

Console.WriteLine("stud2的姓名:{0},班级:{1},学号:{2}.", https://www.wendangku.net/doc/6b14103488.html,, stud2.Bj, stud2.Xh);

}

}

}

(3)创建一个控制台应用程序,在该程序中定义一个类Lion,在该类中包含两个私有的字段:字符串型的name和整型的age,这两个私有字段分别通过两个公共的读写属性Name和Age来对它们进行读写。该类还包含一个带两个参数的公共构造函数,两个参数分别为字符串型和整型,在该构造函数中分别用这两个参数给相应的字段name和age赋值。在Program类的Main方法中调用Lion 的构造函数实例化一个对象,然后输出它的属性Name和Age。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp06_03

{

class Lion

{

string name;

int age;

public string Name

{

get { return name; }

set { name = value; }

}

public int Age

{

get { return age; }

set { age = value; }

}

public Lion(string name1, int age1)

{

name = name1;

age = age1;

}

}

class Program

{

static void Main(string[] args)

{

Lion lion1 = new Lion("Jack", 8);

Console.WriteLine("The lion's nameis {0},the lion's age is {1}:", https://www.wendangku.net/doc/6b14103488.html,,

lion1.Age);

Console.ReadKey();

}

}

}

实验七

(1)创建一个控制台应用程序,从键盘输入一个字符串,分别统计数字、字母

和其它字符的个数。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp07_01

{

class Program

{

static void Main(string[] args)

{

int letter = 0, number = 0, others = 0;

string s;

s = Console.ReadLine();

for (int i = 0; i < s.Length; i++)

{

if (s[i] >= 'A' && s[i] <= 'Z' || s[i] >= 'a' && s[i] <= 'z')

letter++;

else if (s[i] >= '0' && s[i] <= '9')

number++;

else

others++;

}

Console.WriteLine("字母个数为:{0},数字个数为:{1},其它字符为:{2}。", letter, number, others);

}

}

}

(2)创建一个控制台应用程序,在该程序中定义一个类MyClass,在该类中包含max方法的四个不同的版本,它们具有不同的参数列表,一个返回两个整型数中的较大值,一个返回两个双精度数中的较大值,一个返回三个整型数中的最大值,一个返回三个双精度数中的最大值。在Program类的Main()方法中给max 方法传递不同的实参来调用不同的方法。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp07_02

{

class MyClass

{

public int max(int x, int y)

{

return x > y ? x : y;

}

public double max(double x, double y)

{

return x > y ? x : y;

}

public int max(int x, int y, int z)

{

return max(max(x, y), z);

}

public double max(double x, double y, double z)

{

return max(max(x, y), z);

}

}

class Program

{

static void Main(string[] args)

{

MyClass sc = new MyClass();

Console.WriteLine("求3和5的较大值:{0}.", sc.max(3, 5));

Console.WriteLine("求3.8和5.9的较大值:{0}.", sc.max(3.8, 5.9));

Console.WriteLine("求3、5和9的较大值:{0}.", sc.max(3, 5, 9));

Console.WriteLine("求3.8、5.9和7.8的较大值:{0}.", sc.max(3.8, 5.9, 7.8));

Console.ReadKey();

}

}

}

(3)创建一个控制台应用程序,在该程序中定义一个School类,该类中有一个私有的整型数组grade,用来存储学生的成绩,还有一个公共的整型索引器,用来读写grade数组中的各元素,该类中还有一个公共的构造函数,给grade数组初始化为有10个元素。在Program类的Main()方法中声明一个School类实例,然后利用索引来对grade数组中的元素进行赋值和显示。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp07_03

{

class School

{

int[] grade;

public School()

{

grade = new int[10];

}

public int this[int index]

{

set

{

grade[index] = value;

}

get

{

return grade[index];

}

}

}

class Program

{

static void Main(string[] args)

{

School sc = new School();

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

{

Console.Write("请输入第{0}个整数。", i + 1);

sc[i] = Convert.ToInt32(Console.ReadLine());

Console.WriteLine();

}

Console.WriteLine("请输出学生的成绩。");

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

{

Console.WriteLine("第{0}个学生的成绩是:{1}。", i + 1, sc[i]);

}

Console.ReadKey();

}

}

}

实验八

(1)创建一个控制台应用程序,输出2-200之间的所有的素数。素数就是只能被1和它本身整除的数,判断一个数是否为素数,要求用一函数来实现。每个素数占6个字符左对齐显示。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp08_01

{

class Program

{

public static bool prime(int m)

{

for (int i = 2; i < m; i++)

if (m % i == 0)

return false; // 返回给调用者

return true;

}

public static void Main()

{

int m, n = 1;

Console.Write("{0,-6}", 2);

for (m = 3; m < 200; m += 2)

{

if (prime(m)) // 调用方法prime

{

Console.Write("{0,-6}", m);

if (++n % 10 == 0)

Console.WriteLine("\n");

}

}

Console.Read();

}

}

}

(2)创建一个控制台应用程序,在该程序中定义一个矩形类Rectangular,要求能够计算矩形的面积、矩形的周长、比较两个矩形的大小。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp08_02

{

class Rectangular

{

double width;

double height;

public Rectangular(double num1, double num2)

{

width = num1;

height = num2;

}

public double GetArea()

{

return width * height;

}

public double GetGirth()

{

return (width + height) * 2;

}

public static void CompareArea(Rectangular r1, Rectangular r2)

{

if (r1.GetArea() > r2.GetArea())

Console.WriteLine("第一个矩形比第二个矩形大。");

else if (r1.GetArea() == r2.GetArea())

Console.WriteLine("两个矩形一样大。");

else

Console.WriteLine("第二个矩形比第一个矩形大。");

}

public void Compare(Rectangular r)

{

if (this.GetArea() > r.GetArea())

Console.WriteLine("第一个矩形比第二个矩形大。");

else if (this.GetArea() == r.GetArea())

Console.WriteLine("两个矩形一样大。");

else

Console.WriteLine("第二个矩形比第一个矩形大。");

}

}

class Program

{

static void Main(string[] args)

{

Rectangular r1 = new Rectangular(3, 5);

Rectangular r2 = new Rectangular(4, 7);

Console.WriteLine("第一个矩形的面积={0},周长={1}。", r1.GetArea(), r1.GetGirth());

Console.WriteLine("第二个矩形的面积={0},周长={1}。", r2.GetArea(), r2.GetGirth());

https://www.wendangku.net/doc/6b14103488.html,pareArea(r1, r2);

https://www.wendangku.net/doc/6b14103488.html,pare(r2);

}

}

}

(3)创建一个控制台应用程序Exp08_03,

a.在该应用程序中创建一个公共的类Shape,该类中包含一个受保护的字符串字段Color,两个公共的构造函数,一个无参数,无实现代码;另一个含一个字符串参数,在该构造函数中,把参数赋给字段Color。有一个公共的方法GetColor(),该方法用来返回Color字段的值。还有一个公共的返回值为double型的虚方法GetArea(),该方法的返回值为0.0

b. 该应用程序中还包含一个公共的Circle类,该类从Shape类中派生,该类含一个私有的double型的字段Radius,一个公共的构造函数,该构造函数有两个参数,其中一个给Color赋值,另一个给Radius赋值。对基类中的虚方法GetArea()进行重写,用来返回圆的面积。

c. 该应用程序中还包含一个公共的Rectangular类,该类从Shape类中派生,该类含两个受保护的double型的字段Width和Length,两个公共的构造函数,一个无参数的,用来给Width和Length都赋0;另一个构造函数有三个参数,其中一个给Color赋值,一个给Width赋值。还有一个给Length赋值。另外,该类中也要对基类中的虚方法GetArea()进行重写,用来返回长方形的面积。还存在一个公共的方法GetPerimeter(),用来返回长方形的周长。

d.该应用程序中还包含一个公共的Square类,该类从Rectangular类中派生,该类含一个公共的构造函数,该构造函数有两个参数,其中一个给Color赋值,另一个给Width和Length赋相同的值。

e.在Program类的Main()方法中定义这几种形状的实例,然后输出它们的面积及周长(除圆外)。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp08_03

{

public class Shape

{

protected string Color;

public Shape(){ ;}

public Shape(string Color)

{

this.Color = Color;

}

public string GetColor()

{

return Color;

}

public virtual double GetArea()

{

return 0.0;

}

}

//定义Circle类,从Shape类中派生

public class Circle : Shape

{

private double Radius;

public Circle(string Color, double Radius)

{

this.Color = Color;

this.Radius = Radius;

}

public override double GetArea()

{

return System.Math.PI * Radius * Radius;

}

}

// 派生类Rectangular,从Shape类中派生

public class Rectangular : Shape

{

protected double Length, Width;

public Rectangular()

{

Length = Width = 0;

}

public Rectangular(string Color, double Length, double Width)

{

this.Color = Color;

this.Length = Length;

this.Width = Width;

}

public override double GetArea()

{

return Length * Width;

}

public double GetPerimeter()//周长

{

return (2 * (Length + Width));

}

}

// 派生类Square,从Rectangular类中派生

public class Square : Rectangular

{

public Square(string Color, double Side)

{

this.Color = Color;

this.Length = this.Width = Side;

}

}

public class Program

{

public static void Main()

{

Circle Cir = new Circle("orange", 3.0);

Console.WriteLine("Circle color is {0},Circle area is {1}", Cir.GetColor(), Cir.GetArea());

Rectangular Rect = new Rectangular("red", 13.0, 2.0);

Console.WriteLine("Rectangualr color is {0},Rectangualr area is {1}, Rectangular perimeter is {2}", Rect.GetColor(), Rect.GetArea(), Rect.GetPerimeter());

Square Squ = new Square("green", 5.0);

Console.WriteLine("Square color is {0},Square Area is {1}, Square perimeter is {2}", Squ.GetColor(), Squ.GetArea(), Squ.GetPerimeter());

}

}

}

实验九

1、创建一个控制台应用程序,在程序中定义一个基类MyClass,其中包含公共

虚拟方法GetString().这个方法应返回存储在受保护字段myString中的字符串。该字段可以通过只写公共属性containedString来访问。该类还存在一个公共抽象方法PrintString()。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp09_01

{

abstract class MyClass

{

protected string myString;

public string containedString

{

set { myString = value; }

}

public virtual string GetString()

{

return myString;

}

public abstract void PrintString();

}

class Program

{

static void Main(string[] args)

{

}

}

}

2、创建一个控制台应用程序,在上一题的基础上,从类MyClass中派生一个类MyDerivedClass。重写GetString(),使用该方法的基类执行代码从基类返回一个字符串,但在返回的字符串中添加文本”(out put from derived class)”。重写基类中的抽象方法PrintString(),向控制台输出字符串“Hello World!”。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Exp09_02

{

abstract class MyClass

{

protected string myString;

public string containedString

{

C语言上机报告答案

2010C语言实验报告参考答案 实验一熟悉C语言程序开发环境及数据描述 四、程序清单 1.编写程序实现在屏幕上显示以下结果: The dress is long The shoes are big The trousers are black 答案: #include main() { printf("The dress is long\n"); printf("The shoes are big\n"); printf("The trousers are black\n"); } 2.改错题(将正确程序写在指定位置) 正确的程序为: #include main() { printf("商品名称价格\n"); printf("TCL电视机¥7600\n"); printf("美的空调¥2000\n"); printf("SunRose键盘¥50.5\n"); } 2.编写程序: a=150,b=20,c=45,编写求a/b、a/c(商)和a%b、a%c(余数)的程序。答案: #include main() { int a,b,c,x,y; a=150; b=20; c=45; x=a/b; y=a/c; printf("a/b的商=%d\n",x); printf("a/c的商=%d\n",y);

x=a%b; y=a%c; printf("a/b的余数=%d\n",x); printf("a/c的余数=%d\n",y); } 4. 设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将a 赋给c。(提示:用条件运算符) 答案: #include main() { int a,b,c; a=0; b=-10; c= (a>b) ? b:a; printf("c = %d\n",c); } 五、调试和测试结果 1.编译、连接无错,运行后屏幕上显示以下结果: The dress is long The shoes are big The trousers are black 3、编译、连接无错,运行后屏幕上显示以下结果: a/b的商=7 a/c的商=3 a/b的余数=10 a/c的余数=15 4. 编译、连接无错,运行后屏幕上显示以下结果: c =-10 实验二顺序结构程序设计 四、程序清单 1.键盘输入与屏幕输出练习 问题1 D 。 问题2 改printf("%c,%c,%d\n",a,b,c);这条语句 改成:printf("%c %c %d\n",a,b,c);

数据库上机实验题目和答案

试用SQL的查询语句表达下列查询: 1.检索王丽同学所学课程的课程号和课程名。 select Cno ,Cname from c where Cno in (select cno from sc where sno in (select sno from s where sname='王丽' )) 2.检索年龄大于23岁的男学生的学号和姓名。 select sno,sname from s where sex='男' and age>23 3.检索‘c01’课程中一门课程的女学生姓名 select sname from s where sex='女' and sno in (select sno from sc where cno='c01') 4.检索s01同学不学的课程的课程号。 select cno from c where cno not in (select cno from sc where sno ='s01') 5.检索至少选修两门课程的学生学号。 select sc.sno from s,sc where s.sno=sc.sno group by sc.sno having count(https://www.wendangku.net/doc/6b14103488.html,o)>=2 6.每个学生选修的课程门数。 解法一: select so.sno sno,https://www.wendangku.net/doc/6b14103488.html,ount,s.sname from(select sc.sno sno,count(sc.sno) ccount from sc,s where s.sno=sc.sno group by sc.sno ) so,s where s.sno=so.sno 解法二: select sc.sno sno,s.sname,count(sc.sno) ccount from sc,s where s.sno=sc.sno group by sc.sno,sname

上机实验 11 参考答案

上机实验11 指针与数组 一.实验目的 1. 掌握用下标、数组名或指针等不同方式引用数组元素; 2. 掌握数组名作函数参数的方法; 3.掌握常用的字符串处理函数和字符处理函数; 4.掌握用指针处理字符串的方法; 二.实验内容 【实验题1】程序填空:自定义函数del(s), 功能是删除字符串s中的数字字符, 要求使用字符处理函数isdigit()和字符串处理函数strcpy()。在主函数中输入1个字符串,然后调用函数del(), 用于删除其中的数字字符,并输出处理后的字符串。 提示:从字符串s的首字符开始, 到结束符’\0’之前为止,逐个检查第i个字符是否是数字字符,是则删除该字符——使用字符判别函数isdigit(s[i]), 如果s[i]是数字字符,该函数返回1,否则返回0. 难点:如何删除s的第i个字符?——使用字符串复制函数strcpy(), 将子串s+i+1(从字符s[i+1]开始的子串)复制到给s+i (从字符s[i]开始的子串),即strcpy(s+i, s+i+1)。 #include #include < ctype.h > #include void del( char *s); //line 4 函数声明 void main() { char str[80]; gets( str); //输入字符串str del(str ); //调用函数del(),删除str中的数字 puts(str); //输出字符串str } void del(char *s) //line 12 函数定义 { int i=0; while(s[i]!='\0') if(isdigit(s[i]) ) strcpy( s+i, s+i+1); // 如果字符s[i]是数字,用函数strcpy删除它 else i++; // 否则,继续查看下一个字符 } 运行程序,输入字符串"a1b2 #include

大一C语言上机实验试题和答案

实验一上机操作初步(2学时) 一、实验方式:一人一机 二、实验目的: 1、熟悉VC++语言的上机环境及上机操作过程。 2、了解如何编辑、编译、连接和运行一个C程序。 3、初步了解C程序的特点。 三、实验内容: 说明:前三题为必做题目,后两题为选做题目。 1、输出入下信息:(实验指导书P79) ************************* Very Good ************************* 2、计算两个整数的和与积。(实验指导书P81) 3、从键盘输入一个角度的弧度值x,计算该角度的余弦值,将计算结果输出到屏幕。(书 P3) 4、在屏幕上显示一个文字菜单模样的图案: ================================= 1 输入数据 2 修改数据 3 查询数据 4 打印数据 ================================= 5、从键盘上输入两个整数,交换这两个整数。 四、实验步骤与过程: 五、实验调试记录: 六、参考答案: 1、#include void main( ) { printf(“********************\n”); printf(“ Very Good\n”); printf(“********************\n”); } 2、#include void main( ) { int a,b,c,d; printf(“Please enter a,b:”); scanf(“%d,%d”,&a,&b); c=a+b; d=a*b; printf(“%d+%d=%d\n”,a,b,c); printf(“%d*%d=%d\n”,a,b,d);

MATLAB上机实验(答案)

MATLAB工具软件实验(1) (1)生成一个4×4的随机矩阵,求该矩阵的特征值和特征向量。程序: A=rand(4) [L,D]=eig(A) 结果: A = 0.9501 0.8913 0.8214 0.9218 0.2311 0.7621 0.4447 0.7382 0.6068 0.4565 0.6154 0.1763 0.4860 0.0185 0.7919 0.4057 L = -0.7412 -0.2729 - 0.1338i -0.2729 + 0.1338i -0.5413 -0.3955 -0.2609 - 0.4421i -0.2609 + 0.4421i 0.5416 -0.4062 -0.0833 + 0.4672i -0.0833 - 0.4672i 0.4276 -0.3595 0.6472 0.6472 -0.4804 D = 2.3230 0 0 0 0 0.0914 + 0.4586i 0 0 0 0 0.0914 - 0.4586i 0 0 0 0 0.2275 (2)给出一系列的a值,采用函数 22 22 1 25 x y a a += - 画一组椭圆。 程序: a=0.5:0.5:4.5; % a的绝对值不能大于5 t=[0:pi/50:2*pi]'; % 用参数t表示椭圆方程 X=cos(t)*a; Y=sin(t)*sqrt(25-a.^2); plot(X,Y) 结果: (3)X=[9,2,-3,-6,7,-2,1,7,4,-6,8,4,0,-2], (a)写出计算其负元素个数的程序。程序: X=[9,2,-3,-6,7,-2,1,7,4,-6,8,4,0,-2]; L=X<0; A=sum(L) 结果: A =

C语言上机综合实验一及答案

1、编制程序,输入n 个整数(n 从键盘输入,n>0),输出它们的偶数和。 2、 编程,输入n 后:输入n 个数,根据下式计算并输出y 值。 3、输入一行字符,统计并输出其中英文字母、数字字符和其他字符的个数。 4、编写程序,输入一个正整数n ,计算并输出下列算式的值。要求定义和调用函数total(n)计算1+1/2+1/3+……+1/n ,函数返回值的类型是double 。 5、输入一个正整数n ,求1+1/2!+1/3!+……1/n!的值,要求定义并调用函数fact(n)计算n 的阶乘,函数返回值的类型是单精度浮点型。 答案: 程序1、 #include void main () { int n,i,x,sum=0; while(scanf("%d",&n),n<=0); printf ("请输入%d 个数:", n); for (i=1; i<=n ;i++) { scanf("%d",&x); if(x%2==0) sum+=x; } printf ("sum=%d", sum) ; } 程序2、 #include #include void main( ) { int i,n; float x,y; scanf(“%d”,&n); for(i=1;i<=n;i++){ scanf(“%f”,&x); if(x<-2) y=x*x-sin(x); else if(x<=2) y=pow(2,x)+x; else y=sqrt(x*x+x+1); printf(“%f \n”,y); } } 程序3、 # include void main( ) { int letter,digit,other; ?????>++≤≤-+-<-=2 12222sin 22x x x x x x x x y x 111111...23n k s k n ===++++∑

C语言上机实验[1]

实验四循环结构程序设计(4学时) 一、实验方式:一人一机 二、实验目的: 1、熟练掌握while语句、do-while语句和for语句。 2、练习并掌握循环结构的嵌套形式。 3、掌握循环结构的程序设计方法。 三、实验内容:说明:前四题为必做题目,后两题为选做题目。 1、从键盘上输入若干个学生的成绩,统计并输出最高成绩和最低成绩,当输入负数时结 束输入。(实验指导书P41) 2、求所有的水仙花数。水仙花数是一个3位数的自然数,该数各位数的立方和等于该数 本身。(实验指导书P42) 3、判断输入的某个数是否为素数。若是,输出YES,否则输出NO。(实验指导书P167) 4、计算π的近似值。公式如下:π/4=1-1/3+1/5-1/7+……,直到最后一项的绝对值小 于10-6为止。(实验指导书P169) 5、计算1!+2!+……+n! 的值,n值由键盘输入。(实验指导书P176) 6、输入10个整数,统计并输出其中正数、负数和零的个数。 四、实验答案:(代码+运行结果截屏) 实验五综合实验1:结构化程序设计(2学时) 一、实验方式:一人一机 二、实验目的: 1、进一步掌握选择结构、循环结构的编程特点。 2、掌握C语言的结构化程序设计思想。 3、学习利用循环结构实现的一些常用算法(如穷举、迭代、递推等)。 三、实验内容:说明:前两题为必做题目,后两题为选做题目。 1、编一程序,对于给定的一个百分制成绩,输出对应A,B,C,D,E表示的的等级成绩。设: 90分以上为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E。(实验指导书P162) 2、百马百担问题。(实验指导书P45) 3、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。 4、输出如下图案: * *** ***** ******* ***** *** * 四、实验答案:(代码+运行结果截屏)

华南农业大学C语言实验上机实验第四版参考答案

C语言程序设计上机实验指导与习题 参考答案(第四版) (学生改编) 实验 1 C语言程序初步 一、实验目的 (1)了解所用的计算机系统的基本操作方法,学会独立使用该系统。 (2)了解在该系统上如何编辑、编译、连接和运行一个C程序。 (3)通过运行简单的C程序,初步了解C程序的特点。 (4)在教师的指导下,学会使用在线评判系统。 二、实验内容 1. 运行第一个C程序 [题目:The first C Program] 将下列程序输入visual c++ ,编译、连接和运行该程序。 #include"stdio.h" main() { printf("The first C Program\n"); } [具体操作步骤] (1)在编辑窗口中输入程序。 (2)保存程序,取名为 a1.c。 (3)按照第一章中介绍的方法,编译、连接和运行程序。 (4)按照第三章介绍的方法,将代码提交到在线评判系统,系统返回“通过”,则该题完成。

2. 在在线评判系统中提交实现了计算a+b功能的程序 [题目1001:计算a+b] 由键盘输入两个整数,计算并输出两个整数的和。实现该功能的程序如下, #include "stdio.h" main() { int a, b; scanf("%d%d", &a, &b); printf("%d", a + b); } (1)在程序编辑窗口中输入程序。 (2)保存程序,取名为 a2.c。 (3)按照前二章中介绍的方法,编译、连接和运行程序。 (4)在程序运行过程中,输入 15 30↙ (↙表示输入回车符) (5)如果看到如下输出结果,则表明15+30 的结果正确,如果得不到如下结果,则需检查并更正程序。 45 (6)按照第三章中介绍的方法进入在线评判系统。 (7)显示题目列表,点击题号为1001,题名为“计算a+b”的题目。 (8)查看完题目要求后,点击页面下端的“sumbit”,参照第二章提交程序的方法提交程序a2.c。 (9)查看评判结果,如果得到“accepted”则该题通过,否则返回第一步检查程序是否正确。 3 实验 2 基本数据类型、运算和表达式 一、实验目的 (1)掌握C语言数据类型,熟悉如何定义一个整型和实型的变量,以及对它们赋值的方法。(2)掌握不同的类型数据之间赋值的规律。 (3)学会使用C的有关算术运算符,以及包含这些运算符的表达式,特别是自加(++)和自减(--)运 算符的使用。 (4)进一步熟悉C程序的编辑、编译、连接和运行的过程。 二、实验内容 1. 变量的定义 [题目 1117:变量定义,按要求完成程序] 下面给出一个可以运行的程序,但是缺少部分语句,请按右边的提示补充完整缺少的语句。#include "stdio.h" main() { int a, b; /*定义整型变量a和b*/

c语言上机实验完整标准答案

c语言上机实验完整答案

————————————————————————————————作者:————————————————————————————————日期:

实验一自测练习1 程序代码 #include"stdio.h" void main() {int x; scanf("%d",&x); if (x%2 !=0) printf("%d is an odd\n",x); else printf("%d is an even\n",x); } 运行结果 自测练习2 程序代码 #include"stdio.h" void main() {int i,sum; i=1;sum=0; while (i<=100) {sum=sum+i; i++; }

printf("sum=%d\n",sum); } 运行结果 自测练习3 程序代码 #include"stdio.h" void main() {int i,n; long p; p=1; printf("Enter n:"); scanf("%d",&n); for (i=1;i<=n;i++) p=p*i; printf("p=%ld\n",p); } 运行结果

自测练习4 程序代码 #include"stdio.h" int max(int x,int y) {int z; if (x>y) z=x;else z=y; return(z); } void main() {int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("max=%d\n",c); } 运行结果

Matlab上机实验答案 (1)

Matlab上机实验答案 实验一MATLAB运算基础 1. 先求下列表达式的值,然后显示MATLAB工作空间的使用情况并保存全部变量。 >> z1=2*sin(85*pi/200)/(1+exp(2)) z1 = 0.2375 >> x=[2 1+2i;-0.45 5]; >> z2=1/2*log(x+sqrt(1+x^2)) z2 = 0.7120 - 0.0253i 0.8968 + 0.3658i 0.2209 + 0.9343i 1.2041 - 0.0044i 2.9,,2.9, 3.0

>> a=-3.0:0.1:3.0; >> z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2) (>> z33=(exp(0.3*a)-exp(-0.3*a))/2.*sin(a+0.3)+log((0.3+a)/2)可以验证z3==z33,是否都为1) z3 = Columns 1 through 5 0.7388 + 3.2020i 0.7696 + 3.2020i 0.7871 + 3.2020i 0.7920 + 3.2020i 0.7822 + 3.2020i Columns 6 through 10 0.7602 + 3.2020i 0.7254 + 3.2020i 0.6784 + 3.2020i 0.6206 + 3.2020i 0.5496 + 3.2020i Columns 11 through 20 0.4688 + 3.2020i 0.3780 + 3.2020i 0.2775 + 3.2020i 0.2080 + 3.2020i 0.0497 + 3.2020i

C语言上机实验标准答案.doc

实验一上机操作初步 (2 学时 ) 一、实验方式:一人一机 二、实验目的: 1、熟悉 VC++语言的上机环境及上机操作过程。 2、了解如何编辑、编译、连接和运行一个 C 程序。 3、初步了解 C程序的特点。 三、实验内容: 说明:前三题为必做题目,后两题为选做题目。 1、输出入下信息: ( 实验指导书 P79) ************************* Very Good ************************* 2、计算两个整数的和与积。( 实验指导书 P81) 3、从键盘输入一个角度的弧度值x,计算该角度的余弦值,将计算结果输出到屏幕。 ( 书 P3) 4、在屏幕上显示一个文字菜单模样的图案: ================================= 1 输入数据 2 修改数据 3 查询数据 4 打印数据 ================================= 5、从键盘上输入两个整数,交换这两个整数。 四、实验步骤与过程: 五、实验调试记录: 六、参考答案: 1、#include <> void main( ) {printf( printf( printf( “ ********************\n “Very Good\n” ); “ ********************\n ” ); ” ); } 2、#include <> void main( ) {int a,b,c,d; printf( “ Please enter a,b: ”);

scanf( “%d,%d” ,&a,&b); c=a+b; d=a*b; printf( “ %d+%d=%d\n” ,a,b,c); printf( “ %d*%d=%d\n” ,a,b,d); } 3、#include <> #include <> void main( ) { double x,s; printf( “ Please input value of x: ”); scanf( “%lf ” ,&x); s=cos(x); printf( “ cos(%lf)=%lf\n ”,x,s); } 4、#include <> void main( ) { printf( “ ==================================\n”); printf( “ 1 输入数据 2 修改数据 \n ”); printf( “ 3 查询数据 4 打印数据 \n ”); printf( “ ===================================\n”); } 5、#include <> void main( ) { int x,y,t; printf( “ Please enter x and y: ”); scanf( “%d%d”,&x,&y); t=x; x=y; y=t; printf( “ After swap:x=%d,y=%d\n ” ,x,y); } 实验二简单的 C程序设计 (4 学时 ) 一、实验方式:一人一机 二、实验目的: 1、掌握 C语言的数据类型。 2、学会使用 C语言的运算符及表达式。 3、掌握不同数据类型的输入输出方法。 三、实验内容: 说明:前四题为必做题目,后两题为选做题目。

数据结构上机实验答案

《数据结构实验指导书》答案 实验一: 1、请编写函数int fun(int *a, int *b),函数的功能是判断两个指针a和b所指存储单元的值 的符号是否相同;若相同函数返回1,否则返回0。这两个存储单元中的值都不为0。在主函数中输入2个整数、调用函数fun、输出结果。 #include int fun(int *a, int *b) { if (*a*(*b)>0) return(1); else return(0); } main() { int x,y; scanf("%d%d",&x,&y); if (fun(&x,&y)) printf("yes\n"); else printf("no"); } 2、计算1+2+3+……+100,要求用指针进行设计。即设计函数int fun(int *n)实现求 1+2+3+……+*n,在主函数中输入、调用、输出结果。 #include int fun(int *n) { int i,sum=0; for (i=1;i<=*n;i++) sum+=i; return(sum); } main() { int x,sum; scanf("%d",&x); printf("the sum is %d\n",fun(&x)); } 3、函数的功能是求数组a中最大数的位置(位序号)。在主函数中输入10个整数、调用函

数fun、输出结果。 #define N 10 #include void input(int *a,int n) { int i; for (i=0;i*max) max=a+i; return(max-a); } main() {int a[N],maxi; input(a,N); maxi=fun(a,N); printf("\n the max position is %d\n",maxi); } 4、请编写函数fun(int *a,int n, int *odd, int *even),函数的功能是分别求出数组a中所有奇数之和和所有偶数之和。形参n给出数组中数据的个数;利用指针odd和even分别返回奇数之和和偶数之和。在主函数中输入10个整数、调用函数fun、输出结果。 #define N 10 #include void input(int *a,int n) { int i; for (i=0;i

C#上机实验题目和答案8

(1)创建一个控制台应用程序,在程序中定义一个公共接口IMyInterface,该接口中包含两个方法,一个是DoSomething(),另一个是DoSomethingElse();另外再定义一个类MyClass,该类实现了接口IMyInterface,在DoSomething()方法中向控制台输出“Do something.”,在DoSomethingElse()方法中向控制台输出“Do something else.”在Program类中的Main()方法中实例化MyClass 的对象和定义一个接口变量,通过对象和接口变量来访问这两个方法。 (2)创建一个控制台应用程序,在程序中定义了一个接口IIfc1,该接口包含一个无返回值,且带一个字符串类型的参数的方法PrintOut();在程序中定义了另一个接口IIfc2,该接口中也包含一个无返回值,且带一个字符串类型的参数的方法PrintOut();程序中还定义了一个类MyClass,该类以类级别和显式接口成员两种方式实现了这两个接口。在Program类的Main()方法中分别以类对象的引用和两个接口对象的引用来调用PrintOut()方法。 (3)创建一个控制台应用程序,求一个方阵的对角元之和。 1. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { public interface IMyInterface { void DoSomething(); void DoSomethingElse(); } class MyClass : IMyInterface { public void DoSomething() { Console.WriteLine("Do Something."); } public void DoSomethingElse() { Console.WriteLine("Do Something Else."); } } class Program { static void Main(string[] args) { MyClass MC = new MyClass(); MC.DoSomething();

河南城建学院MATLAB上机实验答案

一熟悉Matlab工作环境 1、熟悉Matlab的5个基本窗口 思考题: (1)变量如何声明,变量名须遵守什么规则、是否区分大小写。 答:变量一般不需事先对变量的数据类型进行声明,系统会依据变量被赋值的类型自动进行类型识别,也就是说变量可以直接赋值而不用提前声明。变量名要遵守以下几条规则:?变量名必须以字母开头,只能由字母、数字或下划线组成。 ?变量名区分大小写。 ?变量名不能超过63个字符。 ?关键字不能作为变量名。 ?最好不要用特殊常量作为变量名。 (2)试说明分号、逗号、冒号的用法。 分号:分隔不想显示计算结果的各语句;矩阵行与行的分隔符。 逗号:分隔欲显示计算结果的各语句;变量分隔符;矩阵一行中各元素间的分隔符。 冒号:用于生成一维数值数组;表示一维数组的全部元素或多维数组某一维的全部元素。 (3)linspace()称为“线性等分”函数,说明它的用法。 LINSPACE Linearly spaced vector. 线性等分函数 LINSPACE(X1, X2) generates a row vector of 100 linearly equally spaced points between X1 and X2. 以X1为首元素,X2为末元素平均生成100个元素的行向量。 LINSPACE(X1, X2, N) generates N points between X1 and X2. For N < 2, LINSPACE returns X2. 以X1为首元素,X2为末元素平均生成n个元素的行向量。如果n<2,返回X2。 Class support for inputs X1,X2: float: double, single 数据类型:单精度、双精度浮点型。 (4)说明函数ones()、zeros()、eye()的用法。 ones()生成全1矩阵。 zeros()生成全0矩阵。 eye()生成单位矩阵。 2、Matlab的数值显示格式

华南农业大学C语言实验上机实验第四版参考答案

华南农业大学C语言实验上机实验第四版参考答案 (4) C语言程序设计计算机实验教学与练习 参考答案(第4版) 实验1 C语言程序初步1、实验目的 (1)了解所用计算机系统的基本操作方法并学会独立使用该系统(2)了解如何在系统上编辑、编译、连接和运行C程序(3)通过运行一个简单的C程序,初步了解C程序的特点。(4)在教师的指导下,学会使用在线评价系统 2,实验内容 1。运行第一个C程序 [标题:第一个C程序] 将下列程序输入visual c++,编译、连接并运行该程序# include \ main(){ printf(\ } [具体操作步骤] (1)在编辑窗口中输入程序(2)保存程序,命名为a1.c (3)根据第1章中描述的方法编译、连接和运行程序。 (4)按照第3章描述的方法将代码提交到在线评估系统,系统返回“通过”,问题完成

2。在线测评系统中提交了一个程序 [话题1001:计算a+b] ,实现了计算a+b的功能。键盘输入两个整数,计算并输出两个整数的和。实现该功能的程序如下,#include \main() { int a,b; scanf(\ printf(\ } (1))在程序编辑窗口中输入程序(2)保存程序,命名为a2.c (3)根据前两章描述的方法编译、连接和运行程序(4)在程序操作过程中,输入1530 ↙ (?表示输入回车) (5)如果看到以下输出结果,则表明15+30的结果是正确的。如果您无法获得以下结果,您需要检查并更正程序45 (6)根据第3章介绍的方法进入在线评估系统。 (7)显示主题列表,点击标题为1001且标题为“计算a+b”的主题 (8)查看主题要求后,点击页面底部的“sumbit”,参照第二章提交程序的方法提交程序a2.c。 (9)检查判断结果,如果获得”接受”,则问题通过,否则返回第一步检查程序是否正确。3 实验2基本数据类型、运算和表达式1、实验目的 (1)掌握c语言数据类型,熟悉如何定义整数和实变量,以及如何给它们赋值(2)掌握不同类型数据之间的分配规律 (3)学会使用C的相关算术运算符和包含这些运算符的表达式,尤

C语言实验报告参考答案

《C语言程序设计》 实 验 手 册

《C语言程序设计》实验课程简介 课程名称:C语言程序设计实验 课程性质:专业必修课 课程属性:专业必修课 学时学分:学时32 学分1 开课实验室:软件实验室 面向专业:网络工程、软件工程、计算机科学与技术 一、课程的任务和基本要求 C语言程序设计实验是面向计算机相关专业学生开设的《C语言程序设计》实验课,是配合《C语言程序设计》课程而开设的实验性教育环节。本课程的主要任务是让学生充分掌握C 语言程序设计的基本概念、各种数据类型的使用技巧、模块化程序设计的方法等。C语言程序设计实验对课程中所涉及的知识进行验证,同时也是学生很好地学习课程的辅助手段。通过C语言上机实验的教学活动,使学生真正全面掌握C语言的基础知识,培养和提高学生的程序开发能力。 二、实验项目 【实验一】最简单的C程序---顺序程序设计 【实验二】逻辑运算和判断选取控制 【实验三】循环结构程序设计(一) 【实验四】循环结构程序设计(二) 【实验五】函数 【实验六】数组(一) 【实验七】数组(二) 【实验八】指针 【实验九】结构体、共用体和文件 【实验十】C程序综合性实验 三、有关说明 1、与其它课程和教学环节的联系: 先修课程:计算机文化 后续课程:面向对象程序设计、Java程序设计、数据结构、软件工程 2、教材和主要参考书目: (1)教材: 《C程序设计习题解答与上机指导》,谭浩强吴伟民著,北京:清华大学出版社,2003年。(2)主要参考书目: 《C语言程序设计》谭浩强主编,清华大学出版社,2003年。

三、实验内容 实验一最简单的C程序---顺序程序设计 (验证性实验 2学时) (一)、实验目的 1.熟悉win-tc程序运行环境 2.掌握运行一个C程序的步骤,理解并学会C程序的编辑、编译、链接方法 3.掌握C语言中使用最多的一种语句——赋值语句 4.掌握数据的输入输出方法,能正确使用各种格式控制符 (二)、实验内容 1.写出下列程序的运行结果 (1)#include void main() { printf(“*****************\n”); printf(“This is a c program. \n”); printf(“****************\n”); } 运行结果及分析:运行结果为: Printf函数语句表示输出引号内的字符串,最后的\n表示换行, 将程序中的\n去掉后,运行结果及分析:运行结果为: 去掉\n后不换行连续显示 (2)#include void main() { int a=100,b=20,sum,sb; sum=a+b; sb=a/b; printf("sum=%d,sb=%d",sum,sb); } 运行结果及分析: sum=100+20=120;sb=100/20=5. (3)#include void main( )

java上机实验答案与解析

JAVA上机实验题答案与解析 实验一 Java程序编程 1.编写一个Java应用程序,输出容为Hello!。 注:文件位置位于e:\2:\Hello.java 编译:(1)e:(2)cd 2 (3)javac Hello.java(4)java Hello 2.编写一个Java小应用程序,输出容为我一边听音乐,一边学Java。 第一步编写 import java.awt.*; import java.applet.*; public class MyApplet extends Applet{ public void paint(Graphics g){ g.drawString("我一边听音乐,我一边做java",25,25); } } 第二步在DOS环境中编译(....javac MyApplet.java) 第三步使用记事本编写 第四步将记事本文件名命名为MyApplet.html 第五步打开MyApplet.html 实验二类的定义 1.编写Java应用程序,自定义Point类,类中有两个描述坐标位置的double 变量x,y,利用构造方法,实现对Point 对象p1,p2初始化,p1和p2对应坐标分别为(15,20),(10,30);定义方法getX(),getY()分别获得点的横坐标和纵坐标;定义方法setX(),setY()分别获得点的横坐标和纵坐标;并且把p1和p2输出; public class Point { double x,y; Point(double x,double y){ this.x=x; this.y=y; } double getX(){ return x; } double getY(){ return y; } void setX(double x){ this.x=x;

大学大一c语言程序设计实验室上机题全部代码答案实验报告

C语言实验报告 实验1-1: hello world程序: 源代码: #include main() { printf(hello world!\n); system(pause); } 实验1-2: 完成3个数据的输入、求和并输出计算结果的程序:源代码: #include main() { int i,j,k,sum; scanf(%d%d%d,&i,&j,&k); sum=i+j+k; printf(sum=%d,sum); system(pause); 实验1-3: 在屏幕上输出如下图形: A BBB CCCCC 源代码: #include main() { printf( A\n); printf( BBB\n); printf( CCCCC\n); system(pause); } 实验2-1: 计算由键盘输入的任何两个双精度数据的平均值

源代码: #include main() { double a,b; scanf(%lf%lf,&a,&b); printf(%.1lf\n,(a+b)/2); system(pause); } 实验2-2: 写一个输入7个数据的程序,把输入的数据代入a + b * (c – d ) / e * f –g 表达式进行运算源代码: #include main() { float a,b,c,d,e,f,g,x; scanf(%f%f%f%f%f%f%f,&a,&b,&c,&d,&e,&f,&g); x=a + b * (c - d ) / e * f - g; 牰湩晴尨?春??※ system(pause); } 实验2-3: 编写一个C语言程序,测试下列各表达式: i, j i + 1 , j + 1 i++ , j++ ++i , ++j i+++++j 源代码: #include main() { int i=1,j=1; printf(%d %d\n,i+1,j+1); printf(%d %d\n,i++,j++); printf(%d %d\n,++i,++j); printf(%d\n,(i++)+(++j)); system(pause); } 实验2-4: 输入存款金额money,存期year和年利率rate,根据下列公式计算存款到期时的利息interest(税

c语言上机实验完整答案

%c 字符形式输出, %d 整数形式输出, 实验一 自测练习1 程序代码 #include void main() { int x; scanf("%d",&x); //%d十进制整型,&指x在内 存中的地址。上面 scanf的作用是:按照 x在内存的地址将x 的值存进去, if (x%2 !=0) printf("%d is an odd\n",x); else printf("%d is an even\n",x); }

运行结果 自测练习2 程序代码 #include void main() { int i, sum; i=1 ; sum=0;

while (i<=100) { sum=sum+i; i++; } printf("sum=%d\n",sum); } 运行结果 自测练习3 程序代码 #include void main( ) { int i, n; long p;

p=1; printf("Enter n:"); scanf("%d",&n); for (i=1; i<=n; i++) p=p*i; printf(" p=%ld\n", p); } 运行结果 自测练习4 程序代码 #include"stdio.h" int max(int x,int y) {int z; if (x>y) z=x;else z=y; return(z); } void main() {int a,b,c;

scanf("%d,%d",&a,&b); c=max(a,b); printf("max=%d\n",c); } 运行结果 自测练习5 程序代码 #include"stdio.h" void main() {int x,y; for(x=0;x<=25;x++) for(y=0;y<=50;y++) if(4*x+y*2==100) printf("兔=%d,鸡=%\n",x,y); } 运行结果

相关文档
相关文档 最新文档