❶ 高手快來!需要c語言代碼一份!謝謝~用C++創建一個類族
class Vehicle
{
public:
double speed() //1.展示對象當前狀態信息(輸出其數據成員值);
{
cout<<"Speed is : "<<this.d_speed<<endl;
return d_speed;
}
/*你在超速會爆炸的類里實現該函數,參數:sp用來判斷是否引起爆炸*/
virtual double xxx(const double sp); //2.可以對交通工具加速、減速。
/*
根據入參sp 、a (這里肯定不夠用,需要的參數自己添加)計算耗油量
*/
virtual double x(const double sp,const int a);//
/*
根據入參計算行駛時間,在第三層類中各自實現
*/
virtual double x(const double sp,const int a);//
private:
double m_speed;
};
class GroundVehicle : public Vehicle
{
};
class TransatmosphericVehicle : public Vehicle
{
public:
/*
該方法分別在 飛機、直升機的類 中實現,它們的墜機速度肯定不同,分別且必須實現
*/
virtual double xx();//2)空中交通工具中,airplane速度需要有下限,低於下限則墜機
};
class bicycle :public GroundVehicle
{
};
class car : public GroundVehicle
{
};
class motobike : public GroundVehicle
{
};
class airplane :public TransatmosphericVehicle
{
};
class Helicopter : public TransatmosphericVehicle
{
};
寫了個架子,你可以在這個基礎上實現你的功能
❷ C語言 建立一個結構體類型,包括了學生姓名和兩門課的成績,用這個類型定義結構體數組,保存多個學生信息
綜合下面兩個你再稍微改一下吧,現在沒時間專門弄個完全扣題的了,見諒
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu
{
int num;
int wuli;
int huaxue;
int math;
};
FILE *fp;
//添加學生信息,可以排除與已存的沖突;
void add()
{
int a,b,c,d,n=0;
struct stu student;
fp=fopen("score.txt","r+");
printf("請輸入要添加學生的學號、物理成績、化學成績及數學成績\n");
scanf("%d%d%d%d",&student.num,&student.wuli,&student.huaxue,&student.math);
while(!feof(fp))
{
fscanf(fp,"%d%d%d%d",&a,&b,&c,&d);
if(student.num==a)
{
printf("該學生信息已存在,不需要添加\n");
fclose(fp);
n=1;
break;
}
}
if(n==0)
{
printf("信息庫中沒有該學生信息,可以添加\n");
fseek(fp,0,2);//文件末尾添加;
fprintf(fp,"%d\t%d\t%d\t%d\n",student.num,student.wuli,student.huaxue,student.math);
fclose(fp);
}
}
//修改學生信息;
void modify()
{
int n=0,number,a,b,c,d,offset;
struct stu student;
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息庫為空\n");
exit(0);
}
printf("請輸入要修改學生的學號\n");
scanf("%d",&number);
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%d%d%d%d",&a,&b,&c,&d);
if(number==a)
{
printf("該學生信息為:\n");
printf("%d\t%d\t%d\t%d\n",a,b,c,d);
printf("請輸入要修改的信息\n");
scanf("%d%d%d%d",&student.num,&student.wuli,&student.huaxue,&student.math);
fseek(fp,offset,0);
fprintf(fp,"%d\t%d\t%d\t%d\n",student.num,student.wuli,student.huaxue,student.math);
n=1;
break;
}
}
if(n==0)
{
printf("該學生信息不存在\n");
}
fclose(fp);
}
//刪除學生信息;
void del()
{
int n=0,number,a,b,c,d,m,offset;
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息庫為空\n");
exit(0);
}
printf("請輸入刪除學生的學號\n");
scanf("%d",&number);
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%d%d%d%d",&a,&b,&c,&d);
if(number==a)
{
printf("該學生信息為:\n");
printf("%d\t%d\t%d\t%d\n",a,b,c,d);
printf("是否要刪除該學生信息,是:1 ;否:0\n");
scanf("%d",&m);
if(m)
{
fseek(fp,offset,0);
fprintf(fp,"%s\t%s\t%s\t%s"," "," "," "," ");
printf("\n");
printf("該學生信息已刪除\n");
}
else printf("您已放棄刪除該學生信息\n");
n=1;
break;
}
}
if(n==0)
{
printf("該學生信息不存在\n");
}
fclose(fp);
}
//查找學生信息;
void search()
{
int n=0,number,a,b,c,d,m;
char s[10];
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息庫為空\n");
exit(0);
}
printf("請選擇查找方式:1--按學號查找;0--按學科查找\n");
scanf("%d",&m);
if(m)
{
printf("請輸入要查找學生的學號\n");
scanf("%d",&number);
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
if(number==a)
{
printf("已找到,該學生信息為:\n");
printf("%s %s %s %s\n","學號","物理成績","化學成績","數學成績");
printf("%d\t %d\t %d\t %d\n",a,b,c,d);
n=1;
break;
}
}
if(n==0)
{
printf("該學生信息不存在\n");
}
fclose(fp);
}
else
{
printf("請輸入要查找的學科\n");
scanf("%s",s);
if(strcmp(s,"wuli")==0)
{
printf("學號\t物理成績\n");
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
printf("%d\t%d\n",a,b);
fclose(fp);
}
if(strcmp(s,"huaxue")==0)
{
printf("學號\t化學成績\n");
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
printf("%d\t%d\n",a,c);
fclose(fp);
}
if(strcmp(s,"math")==0)
{
printf("學號\t數學成績\n");
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
printf("%d\t%d\n",a,d);
fclose(fp);
}
if(strcmp(s,"wuli")&&strcmp(s,"huaxue")&&strcmp(s,"math") )
{
printf("錯誤,要查找的學科不存在\n");
fclose(fp);
}
}
}
//統計學生信息;
void tongji()
{
int n=0,a,b,c,d,m,k,x[100],i,j,l=1;
float sum=0,h[100],k1;
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息庫為空\n");
exit(0);
}
printf("0--統計單科平均成績\n");
printf("1--統計單個學生的平均成績和總成績,對學生排名\n");
printf("請選擇統計類型\n");
scanf("%d",&m);
if(!m)
{
printf("1--統計物理的平均成績\n");
printf("2--統計化學的平均成績\n");
printf("3--統計數學的平均成績\n");
printf("請選擇要統計的學科\n");
scanf("%d",&m);
if(m==1)
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
sum=sum+b;
n++;
}
printf("物理的平均成績為 %2.2f\n",sum/n);
fclose(fp);
}
if(m==2)
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
sum=sum+c;
n++;
}
printf("化學的平均成績為 %.2f\n",sum/n);
fclose(fp);
}
if(m==3)
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
sum=sum+d;
n++;
}
printf("數學的平均成績為 %.2f\n",sum/n);
fclose(fp);
}
}
else
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
h[n]=(float)(b+c+d);
x[n]=a;
n++;
}
for(i=0;l==1&&i<n-1;i++)
{
l=0;
for(j=0;j<n-1-i;j++)
{
if(h[j]>h[j+1])
{
k1=h[j];
h[j]=h[j+1];
h[j+1]=k1;
k=x[j];
x[j]=x[j+1];
x[j+1]=k;
l=1;
}
}
}
printf("排序後\n");
printf("學號\t總成績\t平均成績\n");
for(i=0;i<n;i++)
printf("%d\t%3.0f\t%.2f\n",x[i],h[i],h[i]/3.0);
}
fclose(fp);
}
void main()
{
int m;
/*fp=fopen("score.txt","w");
fprintf(fp,"%s\t%s\t%s\t%s\n","學號","物理成績","化學成績","數學成績");
fclose(fp);*/
while(1)
{
printf("\n\t\t*********歡迎使用學生信息管理系統*********\n\n");
printf("\n\t\t添加,請按1");
printf("\n\t\t修改,請按2");
printf("\n\t\t刪除,請按3");
printf("\n\t\t查詢,請按4");
printf("\n\t\t統計,請按5");
printf("\n\t\t退出,請按0\n");
printf("\n\t\t********************************************\n\n");
printf("Please select(0--5):");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: add();
break;
case 2: modify();
break;
case 3: del();
break;
case 4: search();
break;
case 5: tongji();
break;
case 0: exit(0);
}
printf("\n\n操作完畢,請再次選擇!");
}
else
printf("\n\n選擇錯誤,請再次選擇!");
}
}
下面是個輸出不及格成績的
#include<stdio.h>
float *search(float (*pointer)[4])
{
float *p;
int i;
p=*(pointer+2);
for(i=0;i<4;i++)
{
if(*(*pointer+i)<60)p=*pointer;
}
return p;
}
void main()
{
float score[][4]={{60,70,80,90},{56,89,67,89},{34,78,90,66}};
float *p;
int i,j;
for(i=0;i<3;i++)
{
p=search(score+i);
if(p==*(score+i))
{
printf("NO.%d's score:",i+1);
for(j=0;j<4;j++)
printf("%4.2f ",*(p+j));
}
printf("\n");
}
}
❸ c語言定義一個類,圖書館(數量,書名,分類)(分類包括小說,教材,哲學)
struct type
{
char xiaoshuo[20];
char jiaocai[20];
char zhexue[20];
};
struct tushu
{
int num;
char name[20];
struct type leibie;
};
英語不太好 只有拼音代替了
❹ 怎麼把一個c語言寫得函數封裝成一個類
比如你要寫一個求最大值的類。
class A{
MAX(int a,int b){return a>b?a:b;}
};
int main()
{
A a;
printf("%d\n",a.MAX(3,4)); //求3,4的最大值
}
❺ 如何用C語言或C++實現一個List類
C語言沒有類的概念。C++有現成的List類, #include<list>即可。
如果要自己實現可以參考C++數據結構的書籍,是最基本的練習。
這里實現一個簡單的常式,請參考:
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
usingnamespacestd;
#include<stdio.h>
#include<string>
#include"math.h"
template<classT>classList{
public:
List()//構造函數
{
pFirst=NULL;
}
voidAdd(T&t)//在Link表頭添加新結點
{
if(pFirst==NULL)
{
pFirst=newNode;
*(pFirst->pT)=t;
}
else
{
Node*pNewNode=newNode;
*(pNewNode->pT)=t;
pNewNode->pNext=pFirst;
pFirst=pNewNode;
}
}
voidRemove(T&t)//在Link中刪除含有特定值的元素
{
Node*pNode=pFirst;
if(*(pNode->pT)==t)
{
pFirst=pFirst->pNext;
deletepNode;
return;
}
while(pNode!=NULL)
{
Node*pNextNode=pNode->pNext;
if(pNextNode!=NULL)
{
if(*(pNextNode->pT)==t)
{
pNode->pNext=pNextNode->pNext;
deletepNextNode;
return;
}
}
else
return;//沒有相同的
pNode=pNode->pNext;
}
}
T*Find(T&t)//查找含有特定值的結點
{
Node*pNode=pFirst;
while(pNode!=NULL)
{
if(*(pNode->pT)==t)
{
returnpNode->pT;
}
pNode=pNode->pNext;
}
returnNULL;
}
voidPrintList()//列印輸出整個鏈表
{
if(pFirst==NULL)
{
cout<<"列表為空列表!"<<endl;
return;
}
Node*pNode=pFirst;
while(pNode!=NULL)
{
cout<<*(pNode->pT)<<endl;
pNode=pNode->pNext;
}
}
~List()
{
Node*pNode=pFirst;
while(pNode!=NULL)
{
Node*pNextNode=pNode->pNext;
deletepNode;
pNode=pNextNode;
}
}
protected:
structNode{
Node*pNext;
T*pT;
Node()
{
pNext=NULL;
pT=newT;
}
~Node()
{
deletepT;
}
};
Node*pFirst;//鏈首結點指針
};
classStudent
{
public:
charid[20];//學號
charname[20];//姓名
intage;//年齡
Student()
{
}
~Student()
{
}
Student(constchar*pid,constchar*pname,int_age)
{
strcpy(id,pid);
strcpy(name,pname);
age=_age;
}
booloperator==(constStudent&stu)
{
returnstrcmp(id,stu.id)==0&&strcmp(id,stu.id)==0&&age==stu.age;
}
Student&operator=(constStudent&stu)
{
strcpy(id,stu.id);
strcpy(name,stu.name);
age=stu.age;
}
friendostream&operator<<(ostream&out,constStudent&stu);
};
ostream&operator<<(ostream&out,constStudent&stu)
{
out<<"id:"<<stu.id<<" name:"<<stu.name<<" age:"<<stu.age<<endl;
}
intmain()
{
List<Student>stuList;
cout<<"添加學生前:"<<endl;
stuList.PrintList();
Studentstu1("1","張三",18);
Studentstu2("2","李四",18);
Studentstu3("3","王五",18);
Studentstu4("4","至尊寶",18);
Studentstu5("5","豬八戒",18);
Studentstu6("6","唐僧",18);
Studentstu7("7","沙和尚",18);
Studentstu8("8","觀音",18);
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
stuList.Add(stu4);
stuList.Add(stu5);
stuList.Add(stu6);
stuList.Add(stu7);
stuList.Add(stu8);
cout<<"添加學生後:"<<endl;
stuList.PrintList();
Studentstu11("1","張三",18);
Student*pStu=stuList.Find(stu11);
cout<<"查找到的同學是:"<<*pStu;
stuList.Remove(stu11);
cout<<" 刪除第一個後:"<<endl;
stuList.PrintList();
return0;
}
❻ 用慣了C語言現在用C++怎樣才能合理的抽象出一個類呀怎樣用繼承好不習慣
讓你好好看書認真學習的廢話就不說,自己敲點個人經驗吧。
第一,個人認為c++中的類和c中的結構體差別僅僅在於其中定義的變數和函數有其他屬性,而結構體全部是public;
第二,如何合理抽象一個類,其實和建立結構體一樣。關鍵要劃分好屬性。
第三,那麼如何抽象出一個類呢?比如你定義人這個類,就把人的共同屬性列出來。而不要把學生的屬性加進去。定義學生類的時候,可以從人類繼承。
第四,繼承就是使子類具有父類屬性,再加進去自己的屬性。
❼ 如何在C語言通訊錄里加(a.辦公類b.個人類c.商務類)
言是一門通用計算機編程語言,廣泛應用於底層開發。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國