作业编程
#include <stdio.h>
#include<string.h>
void StrReverse(char *st)
{
char s,*p1,*p2;
p1=st;
p2=st;
while(*p2)
p2++;//将指针p2移至字符串最后一个非空元素后
p2-=1;//让指针指向最后一个非空元素
while(p1<p2)//两个指针一前一后向中间移动,交换数组元素
{
s=*p1;
*p1=*p2;
*p2=s;
p1++;
p2--;
}
}
int main()
{
char a[80];
gets(a);
StrReverse(a);
puts(a);
}
㈡ c语言作业编程问题
下面是我给的答案,有问题的联系我,再讨论。从12点多一直做到现在,别忘了给我选成推荐答案哈。累傻了我了都
1、 杨辉三角形的每一项数据正好是组合 (即s(n!/m!/(n-m)!)的值,其中n是行数(从0行开始);m是列数(从0列开始)。 请使用上述算法得到杨辉三角形每一个位置的值并按下图打印。 要求用函数f计算一个正整数的阶乘(用递归函数来实现),通过主函数调用f完成计算。
答: 下面是我的源代码,程序输出的部分在最后的注释里面
/*
* yanghui.cc
*
* Created on: 2010-6-14
* Author: LiuFeng
* Email: [email protected]
*/
#include <cstdio>
#include <iostream>
using namespace std;
int fac(int base){
if(base==1){
return 1;
}
return fac(base-1)*base;
}
int triDisplay(int** a, int row, int col){
if(row != col){
perror("must : row=col");
return (-1);
}
int sz=row;
printf("\n");
for(int i=0;i<sz;++i){
*((int*)a+i*sz+0)=1;
*((int*)a+i*sz+i)=1;
}
for(int n=2;n<sz;++n)
for(int m=1;m<n;++m){
*((int*)a+n*sz+m) = fac(n)/fac(m)/fac(n-m);
}
for(int n=0;n<sz;++n){
for(int m=0;m<=n;++m){
printf("%5d", *((int*)a+n*sz+m) );
}
printf("\n");
}
return 0;
}
int
main(void){
int data[10][10];
int size=10;
if(triDisplay((int**)data,size,size)<0){
perror("bad mtria");
exit(-2);
}
return 0;
}
/*
[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc
[Administrator@ /<7>06/14]$ ./tri.exe
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
*/
2. 编写一个函数,要求对n个学生的成绩进行排序,要求用数组名作函数参数。在数组a中存放了10个学生某门课程的成绩,调用上述函数,实现对10个学生的成绩排序。
答: 程序的源代码如下,程序的输出在最后面的注释里面
/*
* DcSort.cc
*
* Created on: 2010-6-14
* Author: LiuFeng
* Email: [email protected]
*
* g++ -g -O3 DcSort.cc -Wall -o dsort
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
typedef struct
{
char _sname[30];
double _sscore;
} stu;
int
Partition(stu * stus, int l, int h)
{
stu pivot = stus[l];
while(l<h){
while(l<h && ((stus[h]._sscore) >= (pivot._sscore))) h--;
if(l<h) stus[l++]=stus[h];
while(l<h && ((stus[l]._sscore) <= (pivot._sscore))) l++;
if(l<h) stus[h--]=stus[l];
}
stus[l]=pivot;
return l;
}
void DcSort(stu* stus, int low, int high){
int pivotpos;
if(low<high){
pivotpos=Partition(stus,low,high);
DcSort(stus,low,pivotpos-1);
DcSort(stus,pivotpos+1,high);
}
}
int main(void){
stu s[10]={
{"zhao",60},
{"qian",40},
{"sun",80},
{"li",90},
{"zhou",70},
{"wu",50},
{"zheng",80},
{"jiang",90},
{"shen",100},
{"han",80},
};
for(int i=0; i<10;++i){
printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);
}
DcSort(s,0,10);
printf("\n===================after sorted ==================\n");
for(int i=0; i<10;++i){
printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);
}
return 0;
}
/*
输出结果:
[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort
[Administrator@ ~/<1>preInterView/SortAndFind]$ clear
[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe
student: name: zhao, score: 60.000000
student: name: qian, score: 40.000000
student: name: sun, score: 80.000000
student: name: li, score: 90.000000
student: name: zhou, score: 70.000000
student: name: wu, score: 50.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: shen, score: 100.000000
student: name: han, score: 80.000000
===================after sorted ==================
student: name: qian, score: 40.000000
student: name: wu, score: 50.000000
student: name: zhao, score: 60.000000
student: name: zhou, score: 70.000000
student: name: han, score: 80.000000
student: name: sun, score: 80.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: li, score: 90.000000
student: name: shen, score: 100.000000
[Administrator@ ~/<1>preInterView/SortAndFind]$
*/
三、程序运行结果示例:
第一题:
/*
[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc
[Administrator@ /<7>06/14]$ ./tri.exe
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
*/
第二题:
/*
输出结果:
[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort
[Administrator@ ~/<1>preInterView/SortAndFind]$ clear
[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe
student: name: zhao, score: 60.000000
student: name: qian, score: 40.000000
student: name: sun, score: 80.000000
student: name: li, score: 90.000000
student: name: zhou, score: 70.000000
student: name: wu, score: 50.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: shen, score: 100.000000
student: name: han, score: 80.000000
===================after sorted ==================
student: name: qian, score: 40.000000
student: name: wu, score: 50.000000
student: name: zhao, score: 60.000000
student: name: zhou, score: 70.000000
student: name: han, score: 80.000000
student: name: sun, score: 80.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: li, score: 90.000000
student: name: shen, score: 100.000000
[Administrator@ ~/<1>preInterView/SortAndFind]$
*/
二、实验内容:按题目要求完成程序的改错、调试、填空和编写。
1、以下程序中,main函数通过调用fun()函数统计整数序列中的负数的个数以及平均值。
本题约定平均值由函数返回,负数的个数由参数返回。程序有若干错误,请先阅读程序,找出其中的错误行,
并写出出错的原因,最后上机调试该程序验证自己的预测。
#1 double aver(int a[], int n, int *p) // p所谓返回的参数之一,应该使用二级指针,或者指针的引用;
#2 { int i,sum=0 ; // 这里的*p指向了函数内部的临时变量,且该变量在函数结束时,
// 会同时被系统从栈上释放掉, 那么你在main函数中读取p指向的地址,得到的
// 就是一个不可以预期的值了,也就是“野指针”
// 使用二级指针或者指针的引用,是可以将内存在函数体内外带入带出的,但是
// 注意要在main中先申请好
#3 *p=0 ;
#4 for(i=0;i<n;i++)
#5 { sum=sum+a[i] ;
#6 if(a[i]<0) *p++;
#7 }
#8 return sum/n;
#9 }
#10 #include "stdio.h"
#11 main()
#12 { int count,x[]={0,12,33,-9,-5,27,80,0,54,63};
#13 double av;
#14 av=aver(x,10,count);
#15 printf("count: %d\naverage: %.2f\n",count,av);
#16 }
2、设有如下结构定义,且struct link为链表的结点类型,由该结构类型创建的链表中的a结点已被指针p指向(见下图),请完成下面的操作:
struct link
{ int score;
struct link *next;
}*p,*q;
(1) 写出删除b结点(包括释放其存储空间)的语句序列(允许借助于q指针),但要求链表的连续结构不能破坏,不能移动p指针。
答:
void
deleteNode(link* p, link* q, int b)
{
q = p;
int len=0;
while(q->next)++len;
if(len < b) {perror("You input a overflow position"); exit(-1);}
q=p;
while(--b){ // move to b
q=q->next;
}
while(q->next) { // overwrite the prenode,one by one
q->score=q->>next->score;
}
free(q) // q point to the last node;
}
(2) 阅读下面程序说明,按注释提示,在划线处补充细节,使程序达到预期功能。 [程序说明]以下程序中,函数create的功能是创建一个结点类型为struct link的学生成绩链表,main函数中,首先调用create函数创建一个包含N个结点的成绩链表,然后调用问题(1)的算法,将链表的第2个结点删除掉,要求输出结点删除前、后链表的内容,以验证问题(1)算法的正确性。
#include <stdio.h>
#define N 5
#define L sizeof(struct link)
struct link
{ int score; /* 成绩 */
(1);/* 定义结点的指针域next */
};
struct link *create(void)
{
struct link *head,*p;
int i;
head=NULL;
printf("Input %d records\n",N);
for(i=1;i<=N;i++)
{
p=(2) ; /* 创建一个动态结点 */
scanf("%d",&p->score);
(3); /* 新结点进栈 */
head=p;
}
return (4) ; /* 返回所创建链表的头指针 */
}
void printlist(struct link *head) /* 输出链表 */
{ struct link *p;
p=head;
while(p!=NULL)
{ printf("%d,",p->score);
(5) ; /* 使p后移一个结点 */
}
printf("\n");
}
int
main( void)
{ struct link *base,*new;
(6) ; /* 调用create函数,创建链表 */
printlist(base); /* 输出原始链表 */
/* 借助于new指针删除第2个结点 */
(8) ; /* 输出结点删除以后的链表内容 */
printf("ok!\n");
}
(2)答:
【 (1) link * next; 】
【 (2) p= (link*)malloc(sizeof(struct link)); 】
【 (3) p->next = head 】
【 (4) head 】
【 (5) p=p->next 】
【 (6) base = create(); 】
【 (7) deleteNode(base, new, int b); // 利用我们上一问中,写好的函数 】
【 (8) printlist(base);】
㈢ C语言编程作业,急!!!!
#include"stdio.h"
#include<string.h>
structtel{
charname[11],num[11];
};
intmain(intargc,char*argv[]){
structtels[50];
intn,i;
chart[]="############";
printf("Pleaseentern(int0<n<51)... ");
if(scanf("%d",&n)!=1||n<1||n>50){
printf("Inputerror,exit... ");
return0;
}
printf("Entersomenameandtel-number(by'','Enter'end)... ");
for(i=0;i<n;i++)
scanf("%10s%*[^0-9]%10s",s[i].name,s[i].num);
printf("------------------------ ");
for(i=0;i<n;i++)
printf("%.*s%s%.*s%s ",12-strlen(s[i].name),t,s[i].name,12-strlen(s[i].num),t,s[i].num);
return0;
}
运行样例:
㈣ C++编程作业
struct STRU_SDTMSG
{
cstring sno;
cstring sname;
bool sgender;
int syear;
STRU_SDTMSG()
{
sno = "";
sname = "";
sgender = true;
syear = 0;
}
}
cstring 这种写法可以换成 char sno[15];
构造函数不要也可以;
㈤ C语言编程作业,求解答
作业1:result(int)= c (char) * i (int) + f (float) / d (double) -(f + i);
第一步:f+i, 一个float和int相加,按精度高的float进行计算,结果为float
result = char * int + float/double - float;
第二步:char * int, 他们都是整数相加为int
result =int + float/double - float;
第三步:float/double,按精度高的double进行计算结果为double
result =int + double - float;
第四步:int + double 结果为double
result =double - float;
第五步:double - float 结果为double,result为整形,赋值给整形会强制把double转化成int,保留整数。
作业二:
a=3, b=5
原因,swap的形参是值传递,实参传值给形参,子函数无法改变实参的值;要改变得传址。
作业三:
顺序查找法适应性好,可以适用在无序和有序数组查找;
折半查找法只适用于有序数组,无序查找会失败;但是在有序数组查找时查找效率高于顺序查找。
㈥ 大一C语言作业. 编写程序输入两个整数,输出它们的商和余数。
#include<stdio.h>
int main()
{
int a,b,c,d=0;
scanf("%d %d",&a,&b);
c=a/b;
d=a%b;
printf("商是:%d ",c);
printf("余数是:%d ",d);
return 0;
}
运行可用,输入用空格分分开两个数
比如输入:5 3后回车
输出:
商是:1
余数是:2
㈦ 简单的编程作业!急!!!
#include<stdio.h>
#include<time.h>
intmain()
{
intget_age(intbirth[]);
charid[19];
intbirth[8],age=0;
printf("输入身份证:");
scanf("%s",id);
for(inti=0;i<8;i++){
birth[i]=id[6+i]-'0';
}
printf("年龄为:%d",get_age(birth));
return0;
}
intget_age(intbirth[]){
intage=0;
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
age+=1900+p->tm_year-(birth[0]*1000+birth[1]*100+birth[2]*10+birth[3]);
inttmp=1+p->tm_mon-(birth[4]*10+birth[5]);
if(tmp==0){
if((p->tm_mday-(birth[6]*10+birth[7]))<0){
age--;
}
}elseif(tmp<0){
age--;
}
returnage;
}
㈧ C语言作业,编程实现两个整数的算数运算(加减乘除余)
#include<stdio.h>
main()
{
int a,b;
printf("Please input data a,b");
scanf("%d%d",&a,&b);
printf("\na+b=%d",a+b);
printf("\na-b=%d",a-b);
printf("\na*b=%d",a*b);
printf("\na/b=%d",a/b);
printf("\na%b=%d",a%b);
getch();
}
㈨ 编程作业怎么做!
不知道你问的是哪一题
假设是其中最清楚的那一题,如555555最大的三位数
其实这题很简单,如果不考虑性能的话,直接从100开始遍历,定义两个局部变量
假设是int a与int b
弄一个循环,a从100开始,然后b从1开始
a×b如果等于55555,就记录在一个temp变量一面,如果遇到下一个a×b等于555555的组合,就跟temp比,如果比temp记录的数大,就用大的替换掉temp里面小的那一个。
如果a×b大于55555,立即结束本次循环,a+1,跳入下一次循环。
其实还有更简便的方法。。。不过用到一些复杂的数学公式就不说了。
如果要简单粗暴的话,直接开多线程去找,一个线程负责一个区间,比方说100-200,200-300一直到5555500到555555,找到满足a×b=555555的数,而且每次a+5,(因为555555末尾是5,其中一个乘数必定是5,所以以5作为每次循环的步长应该能提升5倍性能)
总之怎么简单粗暴怎么来。
㈩ VB作业 编程
一
Private Sub form_Click()
Dim b, c, d, sxh As Integer
For i = 100 To 999
b = i \ 100
c = (i - b * 100) \ 10
d = i - b * 100 - c * 10
sxh = b ^ 3 + c ^ 3 + d ^ 3
If sxh = i Then
Print sxh;
End If
Next i
End Sub
二、不知道你要求的是什么的值!
三
Private Sub form_Click()
Dim n, s As Long
n = 1: s = 0
For i = 1 To 10
n = n * i
s = s + n
Next i
Print s
End Sub