c++期末考试样例(vm)

#include<iostream>
using namespace std;
int main()
{
    int x;
    cout << "输入一个数:";
    cin >> x;
    
    if(x > 0) cout  << x << "是正数" << endl;
    else if(x < 0) cout  << x << "是负数" << endl;
    else cout << x << "既不是正数,也不是负数" << endl; 

    if(x % 2) cout << x << "是奇数" << endl;
    else cout << x << "是偶数" << endl;
    return 0;
}

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	float x,y;
	cout << "输入x:";
	cin >> x;
	if(x < 5) y = -x + 3.5;
	else if(x >= 5 && x < 10) y = 20 - 3.5 * (x + 3) * (x + 3);
	else if(x >= 10) y = x / 2 - 3.5 + sin(x);
	cout << "y的值为:" << y << endl;
	return 0;
}

#include<iostream>
using namespace std;
int main()
{
	int x,m;
	float y,r;
	cout << "输入奖金:";
	cin >> x;
	m = x / 100;
	switch(m)
	{
		case 0: r = 0; y = 0;break;
		case 1: r = 2; y = r * x / 100; break;
		case 2:   
		case 3: r = 4; y = r * x / 100; break;
		case 4:
		case 5:
		case 6:
		case 7: r = 8; y = r * x / 100; break;
		default: r = 10; y = r * x / 100;
	}
	
	cout << "税率为:" << r << "%" << endl << "应交税款为:" << y << endl << "实得奖金数为:" << x - y << endl;
	return 0; 
}

#include<iostream>
using namespace std;


void printstar(void)
{
    cout << "***********\n"; 
}

void print_message(void)
{
    cout << "Hello do you do!\n";
}

int main()
{
    printstar();
    print_message();
    printstar();
}


#include<iostream>
using namespace std;
int max(int x,int y)
{
    int z;
    z = (x > y) ? x : y;
    return z;
}

int main()
{
    int a,b,c;
    cin >> a >> b;
    c = max(a,b);
    cout << "The max is " << c << endl;
}

#include<iostream>
using namespace std;

void fun(int a,int b)
{
    a  = a * 10;
    b = b + a;
    cout << a << '\t' << b << endl;
}

int main()
{
    int a = 2,b = 3;
    fun(a,b);
    cout << a << '\t' << b << endl;
}

#include<iostream>
using namespace std;

void fun(int a,int b)
{
    cout << "a=" << a << "b=" << b << endl;
}

int main()
{
    int x = 1;
    fun(x,++x);
    return 0;
}

#include<iostream>
using namespace std;

int a = 3,b = 5;

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

int main()
{
    int a = 8;
    cout << max(a,b) << endl;
}

#include<iostream>
using namespace std;
int x;
void cude(void)
{
    x = x * x * x;
}

int main()
{
    x = 5;
    cude();
    cout << x << endl;
}

#include<iostream>
using namespace std;

int fun(int a)
{
    int c;
    static int b = 3;
    c = a + b++;
    return c;
}
int main()
{
    int x = 2,y;
    y = fun(x);
    cout << y << endl;
    y = fun(x + 3);
    cout << y << endl;
}

#include<iostream>
using namespace std;

int f(int a)
{
    int b = 0;
    static int c = 3;
    b++;
    c++;
    return a + b + c;
}

int main()
{
    int a = 2,i;
    for(i = 0;i < 3;i++)
        cout << f(a) << endl;
}

#include<iostream>
using namespace std;

int q(int x)
{
    int y = 1;
    static int z = 1;
    z += z + y++;
    return x + z;
}

int main()
{
    cout << q(1) << '\t';
    cout << q(2) << '\t';
    cout << q(3) << '\t';
    cout << endl;
}

#include<iostream>
#include<time.h>
using namespace std;
int calc1(int a,int b)
{
	return a + b;
}

inline int calc2(int a,int b)
{
	return a + b;
}
int main()
{
	int x[1000],y[1000],z[1000];
	for(int i = 0;i < 1000;++i)
		for(int j = 0;j < 1000;++j)
			for(int k = 0;k < 1000;++k)
			{
				z[i] = calc1(x[j],x[k]);
				//z[i] = calc2(x[j],x[k]);
			}
}

#include<iostream>
using namespace std;
int a;
int main()
{
	extern int power(int);
	int b = 3,c,d,m;
	cin >> a >> m;
	c = a * b;
	cout << a << "*" << b <<"=" << c  << endl;
	d = power(m);
	cout << a << "**" << b <<"=" << d << endl;
 } 

extern int a;
int power(int n)
{
	int i,y = 1;
	for(i = 1;i <= n;i++)
		y *= a;
	return y;
}

#include<iostream>
using namespace std;

int fun(int m)
{
	if(m == 1)  return 1;  
	if(m == 0)  return 1;
	else 
		return fun(m - 1) * m;  //递归实现 
}

int main()
{
	int n;
	cout <<"输入一个数n:"; 
	cin >> n;
	cout << fun(n);
	return 0;
 } 


#include<iostream>
using namespace std;


void fun(int a)
{
	if(a < 10) 
	{
		cout << a;
		return;
	}
	else
	cout << a % 10;
	a /= 10;
	fun(a);
}

int main()
{
	int x;
	cout << "输入一个整数";
	cin >> x;
	fun(x);
	return 0;
}



#include<iostream>
#include<math.h>
using namespace std;

int main()
{
	float a,b,c,s;
	cout << "输入三角形的三边长:";
	cin >> a >> b >> c;
	if((a + b <= c) ||(a + c <= b) || (b + c <= a))
		cout << "无法构成三角形";
	else
	{
	s = 0.25 * sqrt((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a));
	  
	 cout << "面积为:" << s << endl;
	 }
}

#include<iostream>
#include<math.h>
using namespace std;

float px(float a,int b)
{
	if(b == 1) return a; //递归终止 
	else 
		return px(a,b - 1) + pow(-1,b + 1) * pow(a,b);  
}


int main()
{
	float x;
	int n;
	cout << "输入x和n:";
	cin >> x >> n;
	cout << px(x,n); 
	return 0;
 } 

#include<iostream>
using namespace std;

class A{
   float x,y;
   public:
      void Setxy(float a,float b) {x = a;y = b;}
      void Print(void) {cout << x << '\t' << y << endl;}
}a1,a2;

int main()
{
   A a3,a4;
}

#include<iostream>
using namespace std;

class A{
   float x,y;
   public:
      float m,n;
      void Setxy(float a,float b) {x = a;y = b;}
      void Print(void) {cout << x << '\t' << y << endl;}
};

int main()
{
   A a1,a2;  //定义对象
   a1.m = 10; a1.n = 20;  //为公有成员数据赋值
   a1.Setxy(2.0,5.0);  //为私有成员数据赋值
   a1.Print();
}


#include<iostream>
using namespace std;

class A{
   float x,y;
   public:
      float m,n;
      void Setxy(float a,float b) {x = a;y = b;}
      void Print(void) {cout << x << '\t' << y << endl;}
};

int main()
{
   A a1,a2;  //定义对象
   a1.m = 10; a1.n = 20;  //为公有成员数据赋值
   a1.x = 2,a1.y = 5;
   a1.Setxy(2.0,5.0);  //为私有成员数据赋值
   a1.Print();
}

#include<iostream>
using namespace std;

class A{
   float x,y;
   public:
      float m,n;
      void Setxy(float a,float b) {x = a;y = b;}
      void Print(void) {cout << x << '\t' << y << endl;}
};

int main()
{
   A a1,a2;
   a1.m = 10,a1.n = 20;  //为公有成员数据赋值
   a1.Setxy(2.0,5.0);
   a2 = a1;
   a1.Print();a2.Print();
}

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int i = 5,j = 10;
	cout << setw(6) << i << setw(10) << j << endl;
}

/*#include<iostream>
using namespace std;
int main()
{
	float a,b,t;
	cout << "Input 2 Real Number :\n";
	cin >> a >> b;
	if(a > b)
	{
		t = a;
		a = b;
		b = t;
	}
	cout << a << '\t' << b << endl;
}
*/
/*
#include<iostream>
using namespace std;
int main()
{
    char ch;
    while(cin.get(ch) && ch!='\n')
    switch (ch - '2')
    {
    case 0:
    case 1: cout << (char)(ch + 4);
    case 2: cout << (char)(ch + 4);break;
    case 3: cout << (char)(ch + 3);break;
    default:cout << (char)(ch + 2);
        break;
    }
    cout << endl;
}

*/

#include<iostream>
using namespace std;

int main()
{   int  i=1,sum=0;   //定义变量,初始化
     do                 //构造循环
     {     sum=sum+i;   // 循环体,多次执行
             i=i+1;
     }while (i<=100);
    cout << "sum=" << sum << endl;   //输出结果
}

#include<iostream>
using namespace std;

int main()
{
	int i;
	int result = 0;
	for(i = 1;i  <= 100;i++)
	{
		result += i;
	}
	cout << result << endl;
}

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    //声明文件流对象
    ofstream outFile("outFile.txt",ios::out);
    if(!outFile)
        //使用错误流对象输出错误的信息
        cerr << "Open file or create file error." << endl;
    else 
        //输出数据到与对象outFile关联的文件中
        outFile << "This is a test file.";
}

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    //声明文件流对象
    ofstream outFile;
    outFile.open("outFile.txt",ios::out);
    if(!outFile)
        //使用错误流对象输出错误的信息
        cerr << "Open file or create file error." << endl;
    else 
    {
        //输出数据到与对象outFile 关联的文件中
        outFile << "This is a test file2.";
        outFile.close();
    }
}

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    //往文本文件写数据
    ofstream outfile1("myfile1.txt");
    outfile1 << "Hello !....CHINA LIAONING" << endl;
    outfile1.close();

    //追加数据
    outfile1.open("myfile1.txt",ios::app);
    int x = 1212,y = 6868;
    outfile1 << x << " " << y << endl;
    outfile1.close();

    //把文本文件读数据并显示在屏幕上
    char str1[80],str2[80];
    int x2,y2;
    ifstream infile1("myfile1.txt");
    infile1 >> str1 >> str2;
    infile1 >> x2 >> y2;
    infile1.close();
    cout << "str1 = " << str1 << endl;
    cout << "str2 = " << str2 << endl;
    cout << "x2 = " << x2 << endl;
    cout << "y2 = " << y2 << endl;
}

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

int main()
{
   char str[80];
   cout<<"Input string :"<<endl;
   cin.getline (str,80,'\n');
   ofstream fout("ft.txt");
   int i=0;
   while(str[i])
	   fout.put (str[i++]);
   cout<<"len="<<i<<endl;
   fout.close ();
  
}

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
   ifstream fin("ft.txt");
   char ch;
   int i=0;
   fin.get (ch);
   while(!fin.eof ())
   {
	   cout<<ch;
	   i++;
	   fin.get (ch);	      
   }
   cout<<endl<<"len="<<i<<endl;
   fin.close ();
  
}

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
struct stu
{
	char name[20];
	int age;
	double score;
};
int main()
{
   char str[20]="Hello world!";
   stu ss={"zhang san",22,93.5};
   cout<<"WRITE to 'fb.bin'"<<endl;
   ofstream fout("fb.bin",ios::binary);
   int Len=strlen(str);
   fout.write ((char *)(&Len),sizeof(int));
   fout.write (str,Len);
   fout.write ((char *)(&ss),sizeof(ss));
   fout.close ();
   cout<<"-------------------------"<<endl;
   cout<<"READ from 'fb.bin'"<<endl;
   char str2[80];
   ifstream fin("fb.bin",ios::binary);
   fin.read ((char *)(&Len),sizeof(int));
   fin.read (str2,Len);
   str2[Len]='\0';
   fin.read  ((char *)(&ss),sizeof(ss));
   cout<<"Len="<<Len<<endl;
   cout<<"str2="<<str2<<endl;
   cout<<"ss="<<ss.name <<","<<ss.age <<","<<ss.score <<endl;
   fin.close ();  
}



#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
struct stu
{
	char name[20];
	int age;
	double score;
};
int main()
{
   int n;
   stu ss;
   cout<<"n=?";
   cin>>n;
   ofstream fout("f01.bin",ios::binary);
   for(int i=0;i<n;i++)
   {
	   cout<<"name,age,score=?";
	   cin>>ss.name >>ss.age >>ss.score ;
       fout.write ((char *)(&ss),sizeof(ss));
   }   
   fout.close ();
   cout<<"-------------------------"<<endl;
   ifstream fin("f01.bin",ios::binary);
   double ave=0;
   int n1=0;
   fin.read  ((char *)(&ss),sizeof(ss));
   while(!fin.eof ())
   {
	   ave+=ss.score ;
	   n1++;
	   fin.read  ((char *)(&ss),sizeof(ss));
   }   
   fin.close ();
   cout<<"n1="<<n1<<"  ave="<<ave/n1<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值