c++程序(请各位大侠帮忙看看,有错请指正)

(1)定义一种表示学生的姓名,课程名,期中和期末成绩的结构,然后输入一个学生的期中和期末成绩,计算平均成绩并输出。#include<iostream.h>structst... (1)定义一种表示学生的姓名,课程名,期中和期末成绩的结构,然后输入一个学生的期中和期末成绩,计算平均成绩并输出。
#include<iostream.h>
struct student
{
char *name;
char *course;
int midscore;
int finalscore;
}stu[1]={"LiPing","语文",80,85};
float average
aver=[midscore+finalscore]/2
cout<<average<<endl;
}

(2)某书店出售下列计算机书籍,其书名,其数量和单价如下所示:
Pascal 40 21.5
Foxpro 50 20.0
C 80 25.0
按下列要求编程:
显示各种书名,数量和单价
按书名查找各种书籍的数量
给定书名和数量计算应付金额
增添新的书名,数量和单价。例如,
Office97 60 24.0

struct book
{
char *name;
int *number;
double *price;
};
#include<iostream.h>
#include<string.h>
void main()
{ boo[4]={{"Pascal",40,21.5},{"Foxpro",50,20.0},{"C",80,25.0},{"Office97",60,24.0}};
struct book *b1,*find(struct book b[]);
b1=find(boo);
if(b1==0) cout<<"error\n";
else
cout<<b1->name<<":"<<b1->number<<","<<b1->price<<endl;
}

struct book *find(struct book b[])
{ char name 1 [20];
cout<<"Input book's name; ";
cin>>name 1;
for(int i=0;i<4;i++)
if(strcmp9name 1,s[i].name)==0)
return s+i;
return 0;
}
展开
 我来答
百度网友036f89e
推荐于2016-05-11 · TA获得超过137个赞
知道答主
回答量:102
采纳率:0%
帮助的人:104万
展开全部
第一题修改如下:参考
#include<iostream>
using namespace std;
struct student
{
char *name;
char *course;
float midscore;
float finalscore;
}stu={"LiPing","语文",80,85};
int main()
{
float average;
average=(stu.midscore+stu.finalscore)/2 ;
cout<<average<<endl;
system("pause");
}
1.语句后面一定要有 ;2.对结构体的引用一定要用结构体变量名来指定。
3.用到cout和cin输入和输出时要加using namespace std;命名空间。
4.指定用#include<iostream>头文件时不应用.h

第二题:
有些功能帮你实现了,参考:
struct book
{
char name[20];
int number;
double price;
}book[4]={
{"Pascal",40,21.5},{"Foxpro",50,20.0},{"C",80,25.0},{}
};
#include<iostream>
#include<string.h>
using namespace std;
void print(struct book b[],int n)//实现第一个功能
{
int i;
cout<<"显示各种书名,数量和单价:"<<endl;
for(i=0;i<n;i++)
{
cout<<b[i].name<<" "<<b[i].number<<" "<<b[i].price<<endl;
cout<<endl;
}
}
//实现第二个功能
void print1(char *name,int m)
{
int i;
for(i=0;i<3;i++)
{
if(strcmp(book[i].name,name)==0)
cout<<"应付金额为:"<<m*book[i].price<<endl;
}
}
//实现第四个功能
void addbook(char *addname,int addnum,float addprice)
{
int i;
struct book b[4];
for(i=0;i<3;i++)
{
strcpy(b[i].name,book[i].name);
b[i].number=book[i].number;
b[i].price=book[i].price;
}
strcpy(b[3].name,addname);
b[3].number=addnum;
b[3].price=addprice;
cout<<"添加书后的情况为:"<<endl;
cout<<"书名---数量----价格"<<endl;
for(i=0;i<4;i++)
{
cout<<b[i].name<<" "<<b[i].number<<" "<<b[i].price<<endl;
cout<<endl;
}
}
int main()
{
char bname[20],addnam[20];
int n,addnu;
float addpric;
struct book *b1,*find(struct book b[]);
print(book,3);
b1=find(book);
if(b1==0)
cout<<"error\n";
else
cout<<b1->name<<","<<b1->number<<","<<b1->price<<endl;
cout<<"给定书名和数量:"<<endl;
cin>>bname>>n;
print1(bname,n);
cout<<"输入要添加的书名--数量--价格:"<<endl;
cin>>addnam>>addnu>>addpric;
addbook(addnam,addnu,addpric);
system("pause");
}
//实现第三个功能
struct book *find(struct book book[])
{
char name1[20];
cout<<"输入书的名字; ";
cin>>name1;
for(int i=0;i<3;i++)
if(strcmp(name1,book[i].name)==0)
return book+i;
}
建议:用函数调用时最好包函数体写在main()之前,这样就不需要在main()函数中声明。
一个函数中只有一个 return 有效
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式