c语言帮帮忙 学生成绩管理 要用流程图描述实现算法,根据算法进行代码的编写 C语言编程题,求5!+16!+27!,定义函数求n! 用程序...
// biaobiao.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<math.h>
#include <string.h>
#define MaxSize 100
typedef struct stud
{
char name[10];
int num;
float c;
float math;
float english;
float zf;
int pnum;
int pzf;
}Stud;
void ReadFile(Stud stud[],int &n)
{
FILE *fp;
long length;
int i;
if((fp=fopen("stud.dat","rb"))==NULL){
n=0;
return;
}
fseek(fp,0,2);
length=ftell(fp);
rewind(fp);
n=length/sizeof(Stud);
if(n!=0)
n--;
else
stud[0].pnum=stud[0].pzf=-1;
for(i=0;i<=n;i++)
fread(&stud[i],sizeof(Stud),1,fp);
fclose(fp);
}
void Clearup(Stud stud[],int &n)
{
FILE *fp;
if((fp=fopen("stud.dat","wb"))==NULL){
printf(">> 文件不能打开
");
return;
}
stud[0].pnum=stud[0].pzf=-1;
n=0;
fclose(fp);
printf(" ============================================================================
");
}
void SaveFile(Stud stud[],int &n)
{
int i;
FILE *fp;
if((fp=fopen("stud.dat","wb"))==NULL){
printf(">> 文件不能打开
");
return;
}
if(n>1)
for(i=0;i<=n;i++)
fwrite(&stud[i],sizeof(Stud),1,fp);
fclose(fp);
printf(" ============================================================================
");
}
void Input(Stud stud[],int &n)
{
int i;
int flag=0;
printf(">>请输入登记学生 姓名 学号 C语言成绩 数学成绩 英语成绩:
");
n++;
scanf("%s%d%f%f%f",&stud[n].name,&stud[n].num,&stud[n].c,&stud[n].math,&stud[n].english);
stud[n].zf=stud[n].c+stud[n].math+stud[n].english;
for(i=0;i<=n-2;i++)
{
if(stud[i].num==stud[n].num)
{
flag=flag+1;
}
}
if(flag!=0)
printf("学号输入重复,请检查!!
");
stud[n].pnum=stud[n].pzf=0;
printf(" ============================================================================
");
}
void OutputFile(Stud stud[],int n)
{
int i;
if(n<1)
{
printf(">>没任何记录
");
return;
}
printf(" -----------------------------------------------------------------
");
printf("| 姓名 | 学号 | C语言成绩 | 数学成绩 | 英语成绩 | 总分 |
");
printf(" -----------------------------------------------------------------
");
for(i=1;i<=n;i++)
{
printf("|%10s | %3d | %6.2f | %6.2f | %6.2f | %6.2f |
",stud[i].name,stud[i].num,stud[i].c,stud[i].math,stud[i].english,stud[i].zf);
printf(" -----------------------------------------------------------------
");
}
}
void Dispnum(Stud stud[],int n)
{ int i=2,j;
int k=stud[0].pnum;
if(n<1)
{
printf(">>没有这种记录,不能进行排序
");
return;
}
stud[0].pnum=1;stud[1].pnum=-1;
while(i<=n)
{
j=0;
while(stud[j].pnum!=-1 && stud[stud[j].pnum].num<stud[i].num)
j=stud[j].pnum;
stud[i].pnum=stud[j].pnum;
stud[j].pnum=i;
i++;
}
printf(" -----------------------------------------------------------------
");
printf("| 姓名 | 学号 | C语言成绩 | 数学成绩 | 英语成绩 | 总分 |
");
printf(" -----------------------------------------------------------------
");
while(k!=-1)
{
printf("|%10s | %3d | %6.2f | %6.2f | %6.2f | %6.2f |
",stud[k].name,stud[k].num,stud[k].c,stud[k].math,stud[k].english,stud[k].zf);
printf(" -----------------------------------------------------------------
");
k=stud[k].pnum;
}
printf(" ============================================================================
");
}
void Dispzf(Stud stud[],int n)
{ int i=2,j;
int k=stud[0].pzf;
if(n<1)
{
printf(">>没有记录,不能进行排序
");
return;
}
stud[0].pzf=1;stud[1].pzf=-1;
while(i<=n)
{
j=0;
while(stud[j].pzf!=-1 && stud[stud[j].pzf].zf<stud[i].zf)
j=stud[j].pzf;
stud[i].pzf=stud[j].pzf;
stud[j].pzf=i;
i++;
}
printf(" -----------------------------------------------------------------
");
printf("| 姓名 | 学号 | C语言成绩 | 数学成绩 | 英语成绩 | 总分 |
");
printf(" -----------------------------------------------------------------
");
while(k!=-1)
{
printf("|%10s | %3d | %6.2f | %6.2f | %6.2f | %6.2f |
",stud[k].name,stud[k].num,stud[k].c,stud[k].math,stud[k].english,stud[k].zf);
printf(" -----------------------------------------------------------------
");
k=stud[k].pzf;
}
printf(" ============================================================================
");
}
void xuehao(Stud stud[],int n)
{
int num,i,flag=0;
printf("请输入你要查询学生的学号:
");
scanf("%d",&num);
for(i=0;i<=n;i++)
{
if(stud[i].num==num)
{
printf(" -----------------------------------------------------------------
");
printf("| 姓名 | 学号 | C语言成绩 | 数学成绩 | 英语成绩 | 总分 |
");
printf(" -----------------------------------------------------------------
");
printf("|%10s | %3d | %6.2f | %6.2f | %6.2f | %6.2f |
",stud[i].name,stud[i].num,stud[i].c,stud[i].math,stud[i].english,stud[i].zf);
printf(" -----------------------------------------------------------------
");
flag++;
}
}
if(flag==0)printf("查询操作失败,没有你要查询的学生!!!
");
else printf("查询操作成功!!!
");
printf(" ============================================================================
");
}
void xingming(Stud stud[],int n)
{
char name[10];
int i,flag=0;
printf("请输入你要查询的学生的姓名:
");
scanf("%s",&name);
for(i=0;i<=n;i++)
{
if(strcmp(stud[i].name,name)==0)
{
printf(" -----------------------------------------------------------------
");
printf("| 姓名 | 学号 | C语言成绩 | 数学成绩 | 英语成绩 | 总分 |
");
printf(" -----------------------------------------------------------------
");
printf("|%10s | %3d | %6.2f | %6.2f | %6.2f | %6.2f |
",stud[i].name,stud[i].num,stud[i].c,stud[i].math,stud[i].english,stud[i].zf);
printf(" -----------------------------------------------------------------
");
flag++;
}
}
if(flag==0)printf("查询操作失败,没有你要查询的学生!!!
");
else printf("查询操作成功!!!
");
printf(" ============================================================================
");
}
void Modify(Stud stud[],int n)
{
int NO,j;
int flag=0;
float c,math,english;
printf("请输入你要修改学生的学号:
");
scanf("%d",&NO);
for(j=0;j<=n;j++)
{
if(stud[j].num==NO)
{
printf("请输入你要修改的学生的 C语言成绩 数学成绩 英语成绩:
");
scanf("%f%f%f",&c,&math,&english);
stud[j].c =c;stud[j].math=math;stud[j].english=english;
stud[j].zf=stud[j].c+stud[j].math+stud[j].english;
flag++;
}
}
if(flag==0)printf("修改操作失败,该学号不存在!!!
");
else printf("修改操作成功!!!
");
printf(" ============================================================================
");
}
void Delete(Stud stud[],int &n)
{
int NO,i,j;
int flag=0;
printf("请输入你要删除的学生的学号:
");
scanf("%d",&NO);
for(i=0;i<=n;i++)
{
if(stud[i].num==NO)
{
for(j=i;j<n;j++)
stud[j]=stud[j+1];
flag++;
}
}
if(flag==0)printf("删除操作失败,该学号不存在!!!
");
else {n--;
printf("删除操作成功!!!
");
}
printf(" ============================================================================
");
}
void main()
{ double dummy=sin(0.0);
Stud stud[MaxSize];
int k, n;
printf(" 欢迎使用学生成绩管理系统!!!
");
printf(" ============================================================================
");
ReadFile(stud,n);
do{
printf(" 1:学生登记 2:显示学生信息
");
printf(" 3:清空记录 4:删除记录
");
printf(" 5: 按学号排序输出 6:按总分排序输出
");
printf(" 7:学号查询 8:姓名查询
");
printf(" 9:修改 0:储存退出
");
printf("请选择相应的功能:");
scanf("%d",&k);
if(k>9&&k<0)printf("操作不当,请重新输入:
");
switch(k)
{
case 1:Input(stud,n);
break;
case 2:OutputFile(stud,n);
break;
case 3:Clearup(stud,n);
break;
case 4:Delete(stud,n);
break;
case 5:Dispnum(stud,n);
break;
case 6:Dispzf(stud,n);
break;
case 7:xuehao(stud,n);
break;
case 8:xingming(stud,n);
break;
case 9:Modify(stud,n);
break;
case 0:SaveFile(stud,n);
break;
}
}while(k!=0);
}
效果图:
(C语言)某同学想计算篮球的体积,请你用流程图描述其算法并根据流程图编写程序~
测量篮球的大圆周长。
定义圆周率pi。
输入测量到的周长。
根据周长求出篮球体积。
代码如下——
#include "stdio.h"#define pi 3.1415926int main(int argc,char *argv[]){double l;printf("Enter the l(R: l>0)...
l=");if(scanf("%lf",&l)!=1 || l<=0){printf("Input error, exit...
");return 0;}printf("The volume of the basketball is %f
",l*l*l/pi/pi/6);return 0;}运行样例:
如果需要精确计算16和27的阶乘需要编写大整数计算,至少要实现大整数加法,大整数乘以短整数,大整数除以短整数,大整数转换为字符串用于显示结果。当然如果不需要精确计算可以使用浮点数来计算。
#include #include #include #include typedef int BOOL;typedef unsigned short LINT_B;typedef unsigned long LINT_B2;typedef LINT_B * LINT;typedef const LINT_B * CLINT;#define TRUE 1#define FALSE 0#define BASE 0x10000ul#define BASEDIV2 0x8000ul#define BASEMINONE 0xFFFFul#define DIGITLEN 64 // 当 DIGITLEN = 64 时可以实现200以内的阶乘#define Max(a, b) ((a) > sBitperdgt));// 处理 A 比 B 多出的长度while (al = BASE) *ch++ = (LINT_B)(carry = (LINT_B2)*al++ + (LINT_B2)(LINT_B)(carry >> sBitperdgt));else *ch++ = *al++;}// 处理进位if(carry & BASE) *ch++ = (LINT_B)1;return ch;}// 短乘法 C = A * bLINT umul(CLINT al, CLINT ah, LINT_B b, LINT cl){LINT ch;LINT_B2 *pc;CLINT pa;LINT_B2 carry, bv;if(b == 0 || isZero(al, ah)) {if(cl) *cl = 0; return cl;}if(ah - al == 1) // A 的长度只有一个数字{pc = (LINT_B2 *)cl;*pc = (LINT_B2)*al * (LINT_B2)b;ch = cl + 2;Rmldzrs(cl, ch);return ch;}ch = cl;pa = al;carry = (LINT_B2)0, bv = (LINT_B2)b;// 循环计算while(pa > sBitperdgt));// 处理进位*ch++ = (LINT_B)(carry >> sBitperdgt);Rmldzrs(cl, ch);return ch;}// 短除 A = b * C + d// cl 或 d 可以是空指针LINT udiv(CLINT al, CLINT ah, LINT_B b, LINT cl, LINT d){int lenA, lenC;LINT bufC = NULL, ch, pch;LINT_B c;CLINT pah;LINT_B2 rdach, bv, rv;// 处理特殊情况if(b == 0){printf("带余短除出现除以0错误");if(d) *d = 0;if(cl) *cl = 0; return cl;}lenA = ah - al;if(lenA == 0) {if(d) *d = 0; if(cl) *cl = 0; return cl;} // 被除数等于0,结果商等于0,余数等于0if(lenA == 1) // 被除数也只有1位{if(*al < b) {if(d) *d = *al; if(cl) *cl = 0;} // 被除数小于除数,商等于0,余数等于被除数else{c = *al / b;if(cl) *cl++ = c;if(d) *d = *al - c * b;}return cl;}lenC = lenA - 1; // 计算长度if(cl == NULL){bufC = (LINT)malloc((lenC + 1) * sizeof(LINT_B)); // 申请内存,保证cl不为NULLif(bufC == NULL){printf("带余短除申请临时内存发生错误!
");if(d) *d = 0;return NULL;}cl = bufC;}ch = cl + lenC;// 开始计算pah = ah - 1;pch = ch;bv = b, rv = 0;while(al <= pah){*pch = (LINT_B)((rdach = ((rv << sBitperdgt) + (LINT_B2)*pah--)) / bv);rv = (rdach - bv * (LINT_B2)*pch--);}if(d) *d = (LINT_B)rv;if(bufC){free(bufC);return NULL;}return (*ch == 0) ? ch : ++ch;}// 大整数变成数字串const char * LInt2Str(char * o, CLINT l, CLINT h, LINT_B base, BOOL uppercase){const char * sVecUpper = "0123456789ABCDEF";const char * sVecLower = "0123456789abcdef", *Vec;char *po;LINT_B x, t[DIGITLEN], ii[DIGITLEN];LINT th, tl, il, ih;CLINT xl, xh;BOOL flag = TRUE;po = o;il = ii;ih = copy(l, h, il);Rmldzrs(il, ih);if(isZero(il, ih)){*o++ = '0';*o++ = 0;return po;}Vec = uppercase ? sVecUpper : sVecLower;xl = &x;xh = xl + 1;th = tl = t;switch(base){case 2: case 8: case 10: case 16: break;default: base = 10;}while(1){if(flag){th = udiv(il, ih, base, tl, &x);flag = FALSE;}else{ih = udiv(tl, th, base, il, &x);flag = TRUE;}*o++ = Vec[x];if(th == tl || ih == il) break;}*o++ = 0;reverse(po);return po;}// 阶乘,可以精确计算200以内的阶乘,定义更大的DIGITLEN可以实现更大数字的阶乘LINT jiecheng(LINT l, LINT_B x){LINT h = l + 1, tl, th;LINT_B i = 1, t[DIGITLEN];BOOL flag = TRUE;*l = 1;th = tl = t;while(i <= x){if(flag) {flag = FALSE; th = umul( l, h, i++, tl);}else {flag = TRUE; h = umul(tl, th, i++, l);}}if(!flag) h = copy(tl, th, l);return h;}int main(){char buf[512];LINT_B a[DIGITLEN], b[DIGITLEN], c[DIGITLEN], i = 5;LINT al, ah, bl, bh, cl, ch;ah = al = a, bh = bl = b, ch = cl = c;while(i < 30){printf("%d! ", i);ch = add(al, ah, bl, jiecheng(bl, i), cl);ah = copy(cl, ch, al);i += 11;if(i < 30) printf("+ ");}printf("= %s
", LInt2Str(buf, al, ah, 10, FALSE));return 0;}