c++上机作业 类
- 格式:docx
- 大小:472.01 KB
- 文档页数:14
C语言程序设计上机操作题C语言程序设计上机操作题生命力顽强的种子,从不对瘠土唱诅咒的歌。
以下是店铺为大家搜索整理的C语言程序设计上机操作题,希望能给大家带来帮助!一、程序填空题(30分)1、下列给定程序中,函数fun的功能是:将s所指字符串中的所有数字字符移到所有非数字字符之后,并保持数字字符串和非数字字符串原有的次序。
例如,s所指的字符串为“def35adh3kjsdtf7”,执行后结果为“defadhkjsdt3537”。
请在程序的下画线处填入正确的内容并把下画线删除,使程序得出正确的.结果。
注意:部分源程序给出如下。
不得增行或删行,也不得更改程序的结构!试题程序:二、程序改错题(共40分)2、下列给定程序中函数fun的功能是:将长整型数中各位上为奇数的数依次取出,构成一个新数放在t中。
高位仍在高位,低位仍在低位。
例如,当s中的数为87653142时,t中的数为7531 0请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!试题程序:三、程序设计题(共30分)3、某学生的记录由学号、8门课程成绩和平均分组成,学号和8门课程的成绩已在主函数中给出,请编写函数fun,其功能是:求出该学生的平均分,并放入记录的ave成员中。
例如,学生的成绩是:85.5,76,69.5,85,91,72,64.5,87.5,则他的平均分应为78.875。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun部位中填入你编写的若干语句。
试题程序:【C语言程序设计上机操作题】。
实验一(第1章实验)实验目的:1.掌握运行C语言程序的全过程。
2.熟悉编译环境。
3.初步熟悉C语言程序的语法规定。
4.了解简单函数的使用方法。
实验内容:1.编程且上机运行:求3个整数的和。
2.编程且上机运行:求2个数的和、差、积和商。
3.编程且上机运行:输入3个数,求最大值。
4.编程且上机运行:输入圆的半径,求圆的面积和周长。
5.在屏幕上输出:“hello world!”实验结果:实验二(第3章实验)1.实验目的:理解C语言的类型系统。
实验内容:写程序测试数据-2在类型char,int,unsigned int,long int,unsigned long int 中存储情况。
实验过程:实验结果:参见各种类型的存储实现描述。
2.实验目的:了解混合类型计算中类型的转换规则。
实验内容:写程序测试多种类型数据一起运算时类型的转换及表达式结果的类型。
注意unsigned int和int数据运算时类型转换的方向。
实验过程:/** 类型转换问题* 试问下面两个表达式等价吗?*/#include <stdio.h>#include <stdlib.h>int main() {unsigned int ui,uj;ui = 1;uj = 2;if (ui < uj)printf("\n%u < %u is true !\n", ui, uj);elseprintf("\n%u < %u is false !\n", ui, uj);if (ui - uj < 0)printf("\n%u - %u <0 is true !\n", ui, uj);elseprintf("\n%u - %u <0 is false !\n", ui, uj);system("pause");return 0;}实验结果:参见类型转换规则。
C语言上机练习参考标准答案C语言上机练习参考答案————————————————————————————————作者:————————————————————————————————日期:第1章C语言概述1-1编写程序,在屏幕上显示一个如下输出:---------------------------------Programming in C is fun!I love C language.---------------------------------Program#includemain(){ printf("---------------------------------\n");printf("Programming in C is fun!\n");printf("I love C language.\n");printf("---------------------------------\n");}1-2编写程序,在屏幕上显示一个如下图案:* * * ** * ** **Program (1)#includemain(){ printf("* * * *\n");printf(" * * *\n");printf(" * *\n");printf(" *\n ");}Program (2)#includemain(){ printf("%c%4c%4c%4c\n", '*', '*', '*', '*');printf("%3c%4c%4c\n", '*', '*', '*');printf("%5c%4c\n", '*', '*');printf("%7c\n ", '*');}1-3已知某个圆的半径,编写一个程序,用来计算并显示面积。
第一次课熟悉win-TC编译环境、熟悉C语言程序结构1.使用C 语言编译环境,输入下面的源程序。
将你的程序命名为hello.c,然后编译运行它。
/* program writes the words "Hello, world" to the screen *File : Hello.c* By : NJCIT* Date : 07-03-09*/#include <stdio.h>main(){printf("Hello, world\n");return(0);}2.main() /*求两数之和*/{int a,b,sum; /* 这是变量定义*/a=123;b=456;sum=a+b;printf(“sum is %d\n”,sum);}问题:1.一个C语言源程序从哪里开始执行?2. C程序的函数由几部分构成?3. ANSI C 中的注释内容是用什么符号界定?第二次课熟悉printf()函数、常见转义字符及各种数据类型的输出格式1.2. main(){int a=5,b=7,c=-1;float x=67.8564,y=-789.124;char c=‘A’;long n=1234567;unsigned u=65535;printf(“%d%d\n”,a,b);printf(“%3d%3d\n”,a,b);printf(“%f,%f\n”,x,y);printf(“%-10f%-10f\n”,x,y);printf(“%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n”,x,y,x,y,x,y);printf(“%e,%10.2e\n”,x,y);printf(“%c,%d,%o,%x\n”,c,c,c,c);printf(“%ld,%lo,%x\n”,n,n,n);printf(“%u,%o,%x,%d\n”,u,u,u,u);printf(“%s,%5.3s\n”,”COMPUTER”,”COMPUTER”);}3. 假设下面的例子都是完整程序的一部分,那么他们每一个将会输出什么?4.加载,编译并运行下面的程序。
第二章上机及作业题第二章上机题用三种存储结构(顺序表,链表,静态链表)求解josuphu问题!1:顺序表#include<stdio.h>#define max 100typedef struct{int data[max];int length;}stack,*pstack;pstack Creat() /*顺序表初始化*/{pstack p;p=(pstack)malloc(sizeof(stack));if(p)p->length=0;return p;}void Creatstack(pstack p) /*创建一个顺序表*/{int i,l,x;printf("please stack's length:\n");scanf("%d",&l);p->length=l;printf("please enter a stack:\n");for(i=0;i<l;i++){scanf("%d",&x);p->data[i]=x;}}Deletes(pstack p,int x) /*删除指定位置的元素*/{int i;for(i=x;i<=p->length;i++){p->data[i]=p->data[i+1];}p->length--;}int josephus(pstack p) /*约瑟夫循环*/{int s1,i,s,m,w;printf("please enter start adress:\n");scanf("%d",&s);printf("please enter how long to out eachother:\n");scanf("%d",&m);if(!p->length){printf("empty!\n");return 0;}s1=s-1;printf("please answer:\n");for(i=p->length;i>0;i--){s1=(s1+m-1)%i;w=p->data[s1];printf("%4d",w);Deletes(p,s1);}return 1;}int main() /*主函数*/ {pstack Q;Q=Creat();Creatstack(Q);josephus(Q);getch();}2:链表解决#include<stdio.h>typedef struct node /*定义一个链表结构体*/ {int data;struct node *next;}list,*List;void Putlist(List L) /*输入一个链表*/{int x,i,length;List s;L=NULL;printf("please enter the length:\n");scanf("%d",&length);printf("please enter the list:\n");for(i=0;i<length;i++){s=(List)malloc(sizeof(list));scanf("%d",&x);s->data=x;s->next=L;L=s;}s->next=L;}void Dislist(List L) /*输出链表*/{List p=L->next;printf("%d",L->data);while(p!=L){printf("%3d",p->data);p=p->next;}printf("\n");}int josephs(List L) /*约瑟夫循环的计算*/ {int s,m,count;List p,pre;printf("start in:\n");scanf("%d",&s);printf("how often:\n");scanf("%d",&m);if(!L){printf("the list is empty!\n");return 0;}p=L;for(count=1;count<s;count++){p=p->next;}printf("put the answer:\n");while(p!=p->next){for(count=1;count<m;count++) {pre=p;p=p->next;}printf("%2d",p->data);pre->next=p->next;free(p);p=pre->next;}printf(" %d",p->data);free(p);return 1;}void main(){int x,i,length;List L,s,r;s=L=(List)malloc(sizeof(list)); printf("please enter the length:\n"); scanf("%d",&length);printf("please enter the list:\n"); scanf("%d",&x);L->data=x;for(i=0;i<length-1;i++){r=(List)malloc(sizeof(list)); scanf("%d",&x);r->data=x;s->next=r;s=s->next;}s->next=L;josephs(L);getch();}3:静态链表#include<stdio.h>#define MAXSIZE 100struct node{int number;int next;}string[MAXSIZE];int josephus(void) /*约瑟夫循环*/ {int total;int out;int start;int i,j=1;int flag=0;int current,front;printf("The total number is:");scanf("%d",&total);printf("The out number is:");scanf("%d",&out);printf("The starting number is:");scanf("%d",&start);if(out==1) return total;for(i=0;i<total;i++){string[i].number=i+1;string[i].next=i+1;}string[i-1].next=0;front=total-1;current=0;for(i=1;i<start;i++){front=string[front].next;current =string[current].next;}while(flag<total){if(j%out==0){string[front].next=string[current].next;current=string[front].next;j=1;flag++;}else{j++;front=string[front].next;current =string[current].next;}}return(string[current].number);}void main(){int t;t=josephus();printf("The last one is %d",t);getch();}第二章作业:1.已知递增有序的两个单链表A,B分别存储了一个集合。
C语言实验报告实验1-1:hello world程序:源代码:#include<stdio.h>main(){printf("hello world!\n");system("pause");}实验1-2:完成3个数据的输入、求和并输出计算结果的程序:源代码:#include<stdio.h>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:在屏幕上输出如下图形:ABBBCCCCC源代码:#include<stdio.h>main(){printf(" A\n");printf(" BBB\n");printf(" CCCCC\n");system("pause");}实验2-1:计算由键盘输入的任何两个双精度数据的平均值源代码:#include<stdio.h>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<stdio.h>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;printf("x=%f",x);system("pause");}实验2-3:编写一个C语言程序,测试下列各表达式:i, ji + 1 , j + 1i++ , j++++i , ++ji+++++j源代码:#include<stdio.h>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 (税前),输出时保留2位小数。
第四上机作业 班级:1401032 郑成龙 学号 :14010320015 第二题:编写一个类,实现一个简单的栈元素入栈,读出栈顶元素值,
退栈判断栈顶是否为空。如果栈溢出,程序终止。成员由10个整形的数组构成。创建栈,将10入栈,将12入栈,将14入栈退出并输出栈顶元素退栈。 读出,并输出栈顶元素。 需求分析:栈作为一个常用的数据结构体,可被用于各种数据处理的函数中。实用性能比较高。
问题分析:问题即为在C++中用类结构实现一个栈的结构。定义private数组变量,编写public
功能函数:如出栈函数,入栈函数。 源代码: #include #include using namespace std; class Stack{ int a[10]; static int number; public: bool insta(int x); //入栈函数 bool outsta(); //出栈函数 void printtop(); //输出栈顶元素 }; int Stack::number=0; bool Stack::insta(int x) { number++; if(number>10) return 0; else{ a[number-1]=x; return 1; } } bool Stack::outsta() { number--; if(number<=0) { cout<<"the stack is empty" 同时实现对"<<"和">>"的重载以实现对复数类的输入和输出。要求在主函数中输入两个复数类A,B并实现A+B、A-B、12+A、A-12等操作并输出A和B。 需求分析:复数的运算在各类工程中被广泛运用,C++中并无现成的复数类及其运算。因此,用类定义一个复数类,能大大方便工程上的复数运算。 问题分析:复数相较于一般数的运算,无疑就多了虚部。因此在private中定义两个int型 表示实部与虚部。在public中定义相关的运算的函数如复数之间的加减,实数与复数的加减。在此可以通过重名不同输入量对“+”“-”符号进行重载。 源代码: #include #include using namespace std; class Complex{ int x,y; public: friend Complex operator+(const Complex &a,const Complex &b); friend Complex operator+(const Complex &a,int k); friend Complex operator+(int k,const Complex &a); friend Complex operator-(const Complex &a,const Complex &b) //此四个友缘函数实现了对复数类的加减运算的重载。 { Complex s; s.set(a.x-b.x,a.y-b.y); return s; } friend Complex operator-(const Complex &a,int k) { Complex s; s.set(a.x-k,a.y); return s; } void set(int a,int b){x=a;y=b;}//复数的读入 void print(){cout<}; Complex operator+(const Complex &a,const Complex &b) { Complex s; s.set(a.x+b.x,a.y+b.y); return s; } Complex operator+(const Complex &a,int k) { Complex s; s.set(a.x+k,a.y); return s; } Complex operator+(int k,const Complex &a) { Complex s; s.set(a.x+k,a.y); return s; } int main() { Complex A,B; int i,j,k; cout<<"please input the nuber in to A like this" 值,同时可以进行矩阵的所有操作。要求输入两个矩阵后依次执行上述操作并在每一次操作后输出矩阵结果 需求分析:矩阵作为一个数据模型,广泛应用于线性代数和工程中。现代的计算大部分需要矩阵的读入输出和相关计算。 问题分析:问题即为在C++中用类结构实现一个矩阵的结构。定义private数组变量,可用 二维整型数组来表示,编写public功能函数:矩阵的输入,和输出。矩阵的相加与相乘运算,此处可用循环结构来依歩实现。 源代码: #include #include #include using namespace std; class Matrix{ int n; int a[20][20];//矩阵的数据存入,其阶最大为20 public: friend Matrix add(const Matrix &p1,const Matrix &p2,int k); friend Matrix mul(const Matrix &p1,const Matrix &p2,int k);//上两个友原函数,实现矩阵的加与乘运算 void print() { if(n==0) cout<<"the matrix is empty"; for(int i=0;i{ for(int j=0;jcout