A. 關於c語言課題設計(學生信息管理系統)
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"conio.h"
typedef struct Student
{char name[20];
char num[20];
int score;
Student *next;
}student;
student *creat()
{student *head;student *p,*s;int flag=1;
head=(student*)malloc(sizeof(student));p=(student*)malloc(sizeof(student));
head->next=p;
if(!p){printf("失敗");return 0;}
printf("輸入姓名:");gets(p->name);
printf("輸入學號:");scanf("%s",p->num);
printf("輸入成績:");scanf("%d",&p->score);
while(flag)
{s=(student*)malloc(sizeof(student));
getchar();
printf("輸盯肢入姓名:(輸入0退出):");gets(s->name);
if(!strcmp(s->name,"0")){flag=0;break;}
printf("輸入學號:");scanf("%s",s->num);
printf("輸入成績:");scanf("%d",&s->score);
p->next=s;
p=s;
}
p->帶滑next=NULL;
return head;
}
void listprint(student *head)
{student *p;
p=head->next;
while(p)
{printf("姓名:%s\n學號:%s\n成績:%d\n",p->name,p->num,p->score);
p=p->next;
}
}
void listfind(student *head,char str[])
{student *p;p=head->next;
if(p)
{
while(p&&(strcmp(p->name,str)&&strcmp(p->num ,str)))
p=p->next;
if(p){ printf("姓名:%s\n學號:%s\n成績:%d\n",p->凱行世name,p->num,p->score);}
else printf("沒有該學生信息!\n");
}
}
void listadd(student *head)
{student *p,*s;int flag=1;
p=head->next;
while(p->next!=NULL)p=p->next;
while(flag)
{s=(student*)malloc(sizeof(student));
printf("輸入姓名:(輸入0退出):");gets(s->name);
if(!strcmp(s->name,"0")){ flag=0;break;}
printf("輸入學號:");scanf("%s",s->num);
printf("輸入成績:");scanf("%d",&s->score);
p->next=s;
p=s;
getchar();
}
p->next=NULL;
}
void listdel(student *head,char str[])
{student *p1,*p2;p2=head;
p1=head->next;
while(p1&&(strcmp(p1->name,str)&&strcmp(p1->num ,str)))
{p2=p1;p1=p1->next;}
if(!p1)printf("\n\n\n\n\t 沒有該學生的信息\n");
else {p2->next=p1->next;printf("\n\n\n\n\n\t\t\t\t 刪除成功!\n\n");free(p1);}
}
void listsort(student *head)
{student *p1,*p2,*p3,*p4,*temp;temp=(student *)malloc(sizeof(student));
p1=p2=head->next;
for(;p1!=NULL;p1=p1->next)
{p2=p3=p1;p4=p3;
for(;p3!=NULL;p3=p3->next)
{
if(p3->score>p4->score){p4=p3;}
}strcpy(temp->name,p2->name);strcpy(temp->num,p2->num);temp->score=p2->score;
strcpy(p2->name,p4->name);strcpy(p2->num,p4->num);p2->score=p4->score;
strcpy(p4->name,temp->name);strcpy(p4->num,temp->num);p4->score=temp->score;}}
void menu()
{printf("\n\n\n\n\t\t\t菜單\n\t\t\t\n\t\t\t請選擇\n\t\t\t********************\n\t\t\t*1創建資料\n\t\t\t*2查找\n\t\t\t*3顯示全部\n\t\t\t*4添加\n\t\t\t*5刪除\n\t\t\t*6排名\n\t\t\t*7退出\n\t\t\t********************\n");
}
int main()
{student *head;char str[20],ch; int count=0;
while(1)
{menu();ch=getch();
system("cls");
if(ch!='1'&&ch!='7'&&count==0){printf("\n\n\n\n\n\n\t\t\t 你還沒有創建資料\n");printf("\n\t\t 按任意鍵回主菜單\n");getch();system("cls");continue;}
else count=1;
switch(ch)
{case '1':head=creat();break;
case '2':printf("輸入要查找的姓名或學號:");gets(str);listfind(head,str);break;
case '3':listprint(head);break;
case '4':listadd(head);break;
case '5':printf("輸入要刪除的姓名或學號:");gets(str);listdel(head,str);break;
case '6':listsort(head);listprint(head);break;
case '7':printf("\n\n\n\n\n\n\t\t\t 謝謝使用\n\n\n\n\n");getchar();return 0;
}printf("\n\t\t 按任意鍵回主菜單\n");getch();system("cls");
}
}
B. c語言課設小型圖書管理系統設計(c++)
試著做了下這個題,結果和你一樣前7項全解決了,就剩下第八個了,明天再試試,能做出來給你發代碼。要是我做不出來,還請你給我指點一二。
/*終於完成了,第八項也OK了。
你自己多運則指行幾組數據測試下,應該沒問題了
*/
// 20130718.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
#define N 5
void swap(string & a,string & b)
{
string c= a;
a= b;
b= c;
}
void swap1(int & a,int & b)
{
int c=a;
a=b;
b=c;
}
class A
{
private:
string name[N];
string writer[N];
int num1[N];//書號
int price[N];
int num2[N];//數量
public:
void input();
void add();
void modify();
void cut();
void look();
void seek();
void display();
};
void A::input()
{
for(int i=0;i<N;i++)
{
int j;
cout<<"請依次輸入書名,作者,編號,單價及數量,用空格鍵分開"<<endl;
cin>>name[i];
cout<<"書名錄入成功"<<endl;
cin>>writer[i];
cout<<"作者錄入成功"<<endl;
cin>>num1[i];
cout<<"編號錄入成功"<<endl;
cin>>衫賣price[i];
cout<<"單價錄入成功"<<endl;
cin>>num2[i];
cout<<"數量錄入成功"<<endl;
cout<<"是或盯逗否繼續輸入"<<endl;
cout<<"1:繼續"<<endl;
cout<<"2:退出"<<endl;
cin>>j;
if(j==1) ;
else break;
}
}
void A::add()
{
for(int i=0;i<N;i++)
{
if(name[i]==writer[i])
{
int j;
cout<<"請依次輸入書名,作者,編號,單價及數量,用空格鍵分開"<<endl;
cin>>name[i];
cout<<"書名錄入成功"<<endl;
cin>>writer[i];
cout<<"作者錄入成功"<<endl;
cin>>num1[i];
cout<<"編號錄入成功"<<endl;
cin>>price[i];
cout<<"單價錄入成功"<<endl;
cin>>num2[i];
cout<<"數量錄入成功"<<endl;
cout<<"添加成功,是否繼續添加"<<endl;
cout<<"1:是"<<endl;
cout<<"2:否"<<endl;
cin>>j;
if(j==1);
else break;
}
}
}
void A::modify()
{
int i,j;
cout<<"請輸入要修改書的書號"<<endl;
cin>>j;
for(i=0;i<N;i++)
{
if(num1[i]!=j);
else
cout<<"書籍已找到"<<endl;
cout<<"請重新輸入書名,作者,編號,單價及數量,用空格鍵分開"<<endl;
cin>>name[i];
cout<<"書名錄入成功"<<endl;
cin>>writer[i];
cout<<"作者錄入成功"<<endl;
cin>>price[i];
cout<<"單價錄入成功"<<endl;
cin>>num2[i];
cout<<"數量錄入成功"<<endl;
break;
}
}
void A::cut()
{
int i,j;
cout<<"請輸入要刪除的書的書號"<<endl;
cin>>j;
for(i=0;i<N;i++)
{
if(num1[i]==j)
{ name[i]=writer[i]=" ";
num1[i]=price[i]=num2[i]=0;
cout<<"初始化完成"<<endl;
break;
}
}
}
void A::look()
{
int i;
for(i=0;i<N;i++)
{
cout<<name[i]<<writer[i]<<num1[i]<<price[i]<<num2[i]<<endl;
}
}
void A::seek()
{
int i,j;
cout<<"請輸入要查找的書的書號"<<endl;
cin>>j;
for(i=0;i<N;i++)
{
if(j==num1[i])
cout<<num1[i]<<name[i]<<writer[i]<<price[i]<<num2[i]<<endl;
else
break;
}
}
void A::display()//排序未解決,難。。。。。
{
int i,j,k/*,P,N1,N2*/;
for(i=1;i<N;i++)
{
for(j=0;j<i;j++)
{
if(price[i]>price[j])
{
swap1(price[i],price[j]);
swap(name[i],name[j]);
swap(writer[i],writer[j]);
swap1(num1[i],num1[j]);
swap1(num2[i],num2[j]);
}
}
}
for(k=0;k<N;k++)
{
cout<<setw(5)<<price[k]<<setw(10)<<name[k]<<setw(5)<<writer[k]<<setw(3)<<num1[k]<<setw(3)<<num2[k]<<endl;
}
cout<<"排序完成"<<endl;
}
int main()
{
A a;
int i;
do
{
cout<<"1:信息錄入"<<endl;
cout<<"2:添加記錄"<<endl;
cout<<"3:信息修改"<<endl;
cout<<"4:信息刪除"<<endl;
cout<<"5:信息瀏覽"<<endl;
cout<<"6:信息查詢"<<endl;
cout<<"7:信息排序"<<endl;
cout<<"8:退出系統"<<endl;
cin>>i;
switch(i)
{
case 1:a.input();break;
case 2:a.add();break;
case 3:a.modify();break;
case 4:a.cut();break;
case 5:a.look();break;
case 6:a.seek();break;
case 7:a.display();break;
case 8:cout<<"成功退出"<<endl;break;
default:cout<<"輸入錯誤"<<endl;
}
}while(i!=8);
return 0;
}
/*終於完成了,第八項也OK了。
你自己多運行幾組數據測試下,應該沒問題了
*/
C. c語言設計
#include<stdio.h>
#include<conio.h>
bool IsOddEven(int value)
{
bool t=true;
int n,i;
i = value%2;
while(value)
{
value /= 10;
n = value%2;
if(n != (++i %2))
{
t = false;
break;
}
}
return t;
}
void main()
{
int t;
printf("請閉穗輸入一個整數:");
scanf("%d",&t);
if(IsOddEven(t))
printf("yes\n");
else
printf("轎頃卜no\乎孫n");
}
D. C語言程序設計
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineMAX_NUM50
#defineMAX_LINE256
structstudent{
charname[10];
floatmath;
姿尺賣floatchinese;
floateng;
floataver;
chargrade;
};
constchar*student_file="student.txt";
constchar*grade_file="grade.txt";
intinit(structstudent*s,intn);
intfun(structstudents[],intn);
voidsort(structstudent*s,intn);
voiddisplay(structstudent*s,intn);
voidsave_student(structstudent*s,intn,constchar*file);
voidload_student(constchar*file);
intmain(intargc,char*argv[])
{
intnum=0;
structstudentst[MAX_NUM];
num=init(st,MAX_NUM);
printf(" 等級為D的人數:%d ",fun(st,num));
save_student(st,num,student_file);
sort(st,num);
save_student(st,num,grade_file);
display(st,num);
printf(" 所有學生信息 ");
load_student(student_file);
printf(" 排序後的學生信息 ");
load_student(grade_file);
return0;
}
/*初始化*/
intinit(structstudent*s,intn)
{
intnum=0,i=0;
if(0==s||0>=n||MAX_NUM<n)
return0;
printf("請輸入學生數量[不大於50]:");
fflush(stdout);
scanf("%d",&num);
while(0>=num||n<num)
{
fflush(stdin);
printf("無效的參數,請輸入學生數量[不大於50]:");
fflush(stdout);
scanf("%d",&num);
}
for(i=0;i<num;++i)
{
printf("請輸入第%d個學生的信息:",i+1);
fflush(stdout);
scanf("%9s%f%f%f",s[i].name,&s[i].math,&s[i].chinese,&s[i].eng);
fflush(stdin);
}
returnnum;
}
/*計算平均成績並統計等級為D的人數*/
intfun(structstudents[],intn)
{
inti=0,cnt=0;
if(0==s||0>=n||MAX_NUM<n)
return0;
for(i=0;i<n;++i)
{
s[i].aver=(s[i].math+s[i].chinese+s[i].eng)/3.0;
if(70<=s[i].aver&&85>s[i].aver)
s[i].grade='B';
elseif(60<=s[i].aver&&70>s[i].aver)
s[i].grade='C';
elseif(60>s[i].aver)
s[i].grade='D';
else
s[i].grade='A';
if('D'==s[i].grade)
跡逗++cnt;
}
returncnt;
}
/*降序排列*/
voidsort(structstudent*s,intn)
{
inti=0,j=0;
structstudenttmp;
if(0==s||0>=n||MAX_NUM<n)
return0;
for(i=困慧0;i<n-1;++i)
{
for(j=0;j<n-1-i;++j)
{
if(s[j].aver<s[j+1].aver)
{
tmp=s[j];
s[j]=s[j+1];
s[j+1]=tmp;
}
}
}
}
voiddisplay(structstudent*s,intn)
{
inti=0;
if(0==s||0>=n||MAX_NUM<n)
return0;
printf("所有學生的信息如下: ");
for(i=0;i!=n;++i)
{
printf("%d.name[%s]math[%.1f]chinese[%.1f]eng[%.1f]aver[%.1f]grade[%c] ",
i+1,s[i].name,s[i].math,s[i].chinese,s[i].eng,s[i].aver,s[i].grade);
}
}
/*保存學生信息到指定文件*/
voidsave_student(structstudent*s,intn,constchar*file)
{
inti=0;
FILE*fp=0;
charbuf[MAX_LINE]={0};
if(0==s||0>=n||MAX_NUM<n||0==file)
return0;
if(fp=fopen(file,"w"))
{
for(i=0;i!=n;++i)
{
sprintf(buf,"%s%.1f%.1f%.1f%.1f%c ",
s[i].name,s[i].math,s[i].chinese,s[i].eng,s[i].aver,s[i].grade);
fwrite(buf,1,strlen(buf),fp);
memset(buf,0x00,MAX_LINE);
}
fclose(fp);
}
}
/*載入指定文件中的信息*/
voidload_student(constchar*file)
{
charbuf[MAX_LINE]={0};
FILE*fp=0;
if(0==file)
return;
if(fp=fopen(file,"r"))
{
while(fgets(buf,MAX_LINE,fp))
printf("%s",buf);
fclose(fp);
}
}
E. 求C語言的程序設計
#include<stdio.h>
int main()
{
float a,b,c;
printf("請輸入三個實數:\n");
scanf("%f%f%f",&a,&b,&c);
if(a+b<c || a+c<b || b+c<a)
{
printf("0\枝搭n");
}
else
{
if(a==b==c)
printf("1\n");
else if(a==b || b==c || c==a)
printf("2\慶戚n");
else printf("譽搭陵3\n");
}
return 0;
}
F. 運用C語言中有關內容,獨立設計上述游戲,並編程實現以下功能
************************************
#include "iostream.h"
#include<string.h>
#include <stdlib.h>
#include <ctime>
#include<conio.h>
int temp,ying,shu,ping;
int main1(int n1)
{
srand((unsigned)time(0));
n1=rand()%3;
return n1;
}
int main()
{
int now1;
now1=0;
cout<<"--------------------------------------------\n";
cout<<"----------- ---------------\n";
cout<<"----------- LuinSoftware ---------------\n";
cout<<"----------- ---------------\n";
cout<<"----------- CEO: ---------------\n";
cout<<"---- Email:[email protected] -----\n";
cout<<"----------- QQ:361198638 ---------------\n";
cout<<"--------------------------------------------\n";
cout<<"\a\n";
cout<<"歡迎進入石頭剪子布小游戲世界!\n";
cout<<"按任意鍵開始拿拆沒..."<<endl;
getch();
do
{
int cho;
cout<<"消納\n";
cout<<"//////////////新一局/////////////////\n";
cout<<"1.出剪子\n";
cout<<"2.出石頭\n";
cout<<"3.出布\n";
cout<<"*************************************\n";
cout<<"請選擇";
cin>>cho;
if(cho<1||cho>3)
{
cout<<"請輸入1-3的數字!\n";
continue;
}
int good;
good=main1(temp);
switch (good)
{
case 0:
cout<<"對方出了剪子\n";
if(cho==1)
{
cout<<"勢均力敵\n";
ping++;
}
else if(cho==2)
{
cout<<"您贏了御埋!\n";
ying++;
}
else if(cho==3)
{
cout<<"您輸了!\n";
shu++;
}
break;
case 1:
cout<<"對方出了石頭\n";
if(cho==1){
cout<<"您輸了\n";
shu++;}
else if(cho==2)
{
cout<<"勢均力敵\n";
ping++;
}
else if(cho==3)
{
cout<<"您贏了!\n";
ying++;
}
break;
case 2:
cout<<"對方出了布\n";
if(cho==1)
{
cout<<"您贏了!\n";
ying++;
}
else if(cho==2)
{
cout<<"您輸了!\n";
shu++;
}
else if(cho==3)
{
cout<<"勢均力敵\n";
ping++;
}
}
cout<<"戰況:贏"<<ying<<"次;輸:"<<shu<<"次;平:"<<ping<<"次.\a\n";
cout<<"_____________________________________\n";
}
while(1);
cout<<temp<<"\n";
return 1;
}
*************************************
G. c語言程序設計實例
// 電話本Dlg.cpp : 實現文件
//
#include stdafx.h
#include 電話本.h
#include 電話本Dlg.h
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// C電話本Dlg 對話框
C電話本Dlg::C電話本Dlg(CWnd* pParent /*=NULL*/)
: CDialog(C電話本Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);
}
void C電話本Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(C電話本Dlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT1, C電話本Dlg::OnName)
ON_EN_CHANGE(IDC_EDIT2, C電話本Dlg::OnPhoto)
ON_EN_CHANGE(IDC_EDIT3, C電話本Dlg::OnAddress)
ON_EN_CHANGE(IDC_EDIT4, C電話本Dlg::OnShow)
ON_BN_CLICKED(IDC_BUTTON1, C電話本Dlg::OnFill)
ON_BN_CLICKED(IDC_BUTTON2, C電話本Dlg::OnInsert)
ON_BN_CLICKED(IDC_BUTTON3, C電話本Dlg::OnFind)
ON_BN_CLICKED(IDC_BUTTON4, C電話本Dlg::OnDelete)
ON_BN_CLICKED(IDC_BUTTON5, C電話本Dlg::OnEdit)
ON_BN_CLICKED(IDC_BUTTON7, C電話本Dlg::OnSave)
END_MESSAGE_MAP()
// C電話本Dlg 消息處理程序
BOOL C電話本Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動
// 執行此操作
SetIcon(m_hIcon, TRUE); // 設置大圖標
SetIcon(m_hIcon, FALSE); // 設置小圖標
// TODO: 在此添加額外的初始化代碼
ifstream InFile(Wab.fsd,ios::in);
listWabList::iterator ItWab;
char Tmp1[8],Tmp2[16],Tmp3[32];
Wab.clear();
while(InFile Tmp1 Tmp2 Tmp3)
{
OutWab.Name=Tmp1;
OutWab.Photo=Tmp2;
OutWab.Address=Tmp3;
Wab.push_front(OutWab);
}
C電話本Dlg::OnFill();
return TRUE; // 除非將焦點設置到控制項,否則返回 TRUE
}
// 如果向對話框添加最小化按鈕,則需要下面的代碼
// 來繪制該圖標。對於使用文檔/視圖模型的 MFC 應用程序,
// 這將由框架自動完成。
void C電話本Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用於繪制的設備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_castWPARAM(dc.GetSafeHdc()), 0);
// 使圖標在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(rect);
int x = (rect.Width() - cxIcon 1) / 2;
int y = (rect.Height() - cyIcon 1) / 2;
// 繪制圖標
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//當用戶拖動最小化窗口時系統調用此函數取得游標顯示。
//
HCURSOR C電話本Dlg::OnQueryDragIcon()
{
return static_castHCURSOR(m_hIcon);
}
void C電話本Dlg::OnName()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT1);
pMaxLen-SetLimitText(6);
GetDlgItemText(IDC_EDIT1,InWab.Name);
}
void C電話本Dlg::OnPhoto()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT2);
pMaxLen-SetLimitText(11);
GetDlgItemText(IDC_EDIT2,InWab.Photo);
}
void C電話本Dlg::OnAddress()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT3);
pMaxLen-SetLimitText(30);
GetDlgItemText(IDC_EDIT3,InWab.Address);
}
void C電話本Dlg::OnShow()
{
// TODO: 如果該控制項是 RICHEDIT 控制項,則它將不會
// 發送該通知,除非重寫 CDialog::OnInitDialog()
// 函數並調用 CRichEditCtrl().SetEventMask(),
// 同時將 ENM_CHANGE 標志「或」運算到掩碼中。
// TODO: 在此添加控制項通知處理程序代碼
}
void C電話本Dlg::OnFill()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
OutWab=*ItWab;
OutTemp=OutWab.Name Space OutWab.Photo Space OutWab.Address;
OutAll=OutAll OutTemp Enter;
}
SetDlgItemText(IDC_EDIT4,OutAll);
}
void C電話本Dlg::OnInsert()
{
Wab.push_front(InWab);
C電話本Dlg::OnFill();
}
void C電話本Dlg::OnFind()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
}
void C電話本Dlg::OnDelete()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
break;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
if(ItWab!=Wab.end()) Wab.erase(ItWab);
}
void C電話本Dlg::OnEdit()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
break;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
if(ItWab!=Wab.end())
{
Wab.erase(ItWab);
Wab.push_front(InWab);
}
}
void C電話本Dlg::OnSave()
{
CString Tmp1,Tmp2,Tmp3;
ofstream OutFile(Wab.fsd,ios::out);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
Tmp1=ItWab-Name;
Tmp2=ItWab-Photo;
Tmp3=ItWab-Address;
OutFile Tmp1 ' ' Tmp2 ' ' Tmp3 endl;
}
}
只是個用C語言編寫的電話本程序 很有實用意義
H. C語言程序設計:通訊錄程序設計
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "stdio.h"
#include "dos.h"
FILE *fp;
int i; //i是全局變數 可代替length
struct address
{ char postnum[10];
char a[40]; //家庭地址
};
struct birthday
{ int year;
int month;
int day;
};
struct ffriend
{ int num; //序號
char name[20];
char sex;
char telephone[13];
struct birthday birthday;
struct address address;
}
friends[50];
void Load()
{
int j;
long k;
fp=fopen("friend.txt","a+"); //打開文件friend.txt
if(fp!=NULL)
{
for(i=1;i<50;i++)
{
j=fgetc(fp);
if(j==EOF) //判斷是否到了文件尾
return;
k=i-1;
fseek(fp,k*sizeof(struct ffriend),SEEK_SET);
fread(&friends[i],sizeof(struct ffriend),1,fp); //從文件中讀取一條記錄
}
}
else
{
fp=fopen("friend.txt","w");
i=1;
}
}
void Show(int j)
{
// friends[j].num=i;
printf("\n\n\t編號-Nnumber: %3d",friends[j].num);
printf("\n\t姓名-Name:%-20s",friends[j].name);
printf("\n\t性別-Sex:%c",friends[j].sex);
printf("\n\t聯系電話-Telephone:%-13s",friends[j].telephone);
printf("\n\t出生日期-Birthday:%4d-%2d-%3d",friends[j].birthday.year,friends[j].birthday.month,friends[j].birthday.day);
printf("\n\t郵編-Postnum:%-10s",friends[j].address.postnum);
printf("\n\t通信地址-address:%-40s",friends[j].address.a);
}
void Append(int j)
{
fflush(stdin); //區內清除文件緩沖區,文件以寫方式打開時將緩沖容寫入文件
//stdin是一個標准FILE*(文件型指針)指向通常是用鍵盤的輸入的輸入流
friends[j].num=j;
printf("\n\t\t\t序號-Number:%d",j);
printf("\n\t\t\t姓名-Name:");
scanf("%s",friends[j].name);
fflush(stdin); //為什麼沒有fflush(stdin); 的效果會自動跳過呢?
printf("\t\t\t性別-Sex(m/w):"); //為什麼輸入漢字男女也會自動跳過聯系電話呢
scanf("%c",&friends[j].sex);
printf("\t\t\t聯系電話-telephone:");
scanf("%s",friends[j].telephone);
printf("\t出生日期-birthday");
printf("\n\t\t\t年份-year:");
scanf("%d",&friends[j].birthday.year);
printf("\t\t\t月份-month:");
scanf("%d",&friends[j].birthday.month);
printf("\t\t\t日-day:");
scanf("%d",&friends[j].birthday.day);
printf("\t\t\t郵編-Postnumber:");
scanf("%s",friends[j].address.postnum);
printf("\t\t\t通信地址-Address:");
scanf("%s",friends[j].address.a);
getchar();
}
void Delete()
{
int k;
printf("\n\tDelete 序號-Number:");
scanf("%d",&k);
if(k<=i)
{
for(int j=k;j<i+1;j++) /*插入位置後的元素順序後移*/
{
strcpy(friends[j].name,friends[j+1].name); /*交換元素內容*/
friends[j].sex=friends[j+1].sex;
strcpy(friends[j].telephone,friends[j+1].telephone);
friends[j].birthday.year=friends[j+1].birthday.year;
friends[j].birthday.month=friends[j+1].birthday.month;
friends[j].birthday.day=friends[j+1].birthday.day;
strcpy(friends[j].address.postnum,friends[j+1].address.postnum);
strcpy(friends[j].address.a,friends[j+1].address.a);
}
i--;
}
else
{
printf("輸入的序號太大!");
}
}
void Modify(int j)
{
Append(j);
}
void Save()
{
int j;
fp=fopen("friend.txt","w");
for(j=1;j<=i;j++)
{
fwrite(&friends[j],sizeof(struct ffriend),1,fp);
}
fclose(fp);
}
void main()
{
int j;
char grade;
char searchname[10];
Load();
i--;
do
{
printf("\t\t\t\t簡易通訊錄\n\n");
printf("功能選擇(Function choose)\n");
printf("\1A.讀取(Read)\n");
printf("\2B.增添(Append)\n");
printf("\6C.插入(Insert)\n");
printf("\5D.刪除(Delete)\n");
printf("\5E.查詢(Search)\n");
printf("\6F.修改(Modify)\n");
printf("\2G.保存(Save)\n");
printf("\1H.退出(Quit)\n");
printf("請選擇(Choice)\n注:輸入A~H的大寫字母\n");
scanf("%c",&grade);
switch(grade)
//加個大寫和小寫 都可以啊
{
case 'A': j=1; //顯示功能
while(getchar()!=0x1b&&j<=i) //增添按Esc鍵退出
{
Show(j++);
printf("\n請按回車鍵繼續!");
}
if(j-1<1)
{
printf("\n\t空文檔,無任何記錄-Empty Note\n");
printf("\n請按回車鍵繼續!");
getchar();
}
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'B': //增加功能
while(i<=50) //增加按ESC鍵退出的功能
{
i++;
Append(i);
printf("\t是否繼續增加?y/n");
//修改 加上else if 其他就跳出或強制列印出出錯
char a=getchar();
if(a=='n'||a=='N')
break;
}
if(i==51)
printf("\n\t文檔已滿,無法增加記錄-note full");
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
getchar();
break;
case 'C':
int k;
printf("\n\t輸入要插入的位置:");
scanf("%d",&k);
j=i+1;
friends[j].num=j;
for(j;j>k;j--) //插入功能
{
strcpy(friends[j].name,friends[j-1].name); //數據後移
friends[j].sex=friends[j-1].sex;
strcpy(friends[j].telephone,friends[j-1].telephone);
friends[j].birthday.year=friends[j-1].birthday.year;
friends[j].birthday.month=friends[j-1].birthday.month;
friends[j].birthday.day=friends[j-1].birthday.day;
strcpy(friends[j].address.postnum,friends[j-1].address.postnum);
strcpy(friends[j].address.a,friends[j-1].address.a);
}
Append(k);
i++;
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'D': //刪除功能 //增添隨意刪除多條記錄的功能
Delete();
if(i<1)
printf("\n沒有記錄-No records\n");
printf("\n請按回車鍵繼續!");
getchar();
getchar();
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'E': //查詢功能
printf("\n\t請輸入要查詢記錄的相關姓名:"); //增添查詢按列號的
scanf("%s",searchname);
for(j=1;j<=i;j++)
{
if(strcmp(searchname,friends[j].name)==0) //比較字元串
{
Show(j);
break;
}
}
if(i<1)
{
printf("\n 沒有您所查詢的記錄-No records");
printf("\n 請按回車鍵繼續!");
getchar();
}
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
getchar();
break;
case 'F': //修改功能 / /增添按序列號查詢
printf("\n\t請輸入要修改記錄的相關姓名:");
gets(searchname);
scanf("%s",searchname);
for(j=1;j<=i;j++)
{
if(strcmp(searchname,friends[j].name)==0) //比較字元串
{
Modify(j);
// getchar(); //加個輸出確定修改嗎
}
}
if(i<1) //修改功能
{ printf("\n 沒有您所要修改的記錄-No records");
printf("\n 請按回車鍵繼續!");
getchar();
getchar();
}
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'G':
Save(); //存檔功能
getchar();
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
break;
case 'H': //退出功能
char x;
printf("是否保存?yes/no\n");
scanf("%s",&x);
//getchar();
// getchar();
if(x=='y')
{
Save();
exit(0);
}
else
exit(0);
default:
printf("\n\t輸入有誤,請輸入正確的序列號!");
printf("\n請按回車鍵繼續!");
getchar();
printf("\n\t\t\t\t\t\t\t此次操作結束");
printf("\n\t------------------------------------------------------------\t\t\n");
}
}while(1);
}
I. C語言 程序設計
#include <stdio.h> //3、給數據加密程序。
#include <math.h>
int encrypt(int a,int b,int c,int d); //加密函數
int deciphering(int a,int b,int c,int d); //解密函數
main()
{
int number,k;
int g,s,b,q;
printf("please enter a number(四位數):");
scanf("%d",&number);
printf("---------------------------------\n"); //菜單
printf("-------------菜單選項------------\n");
printf("---------------------------------\n");
printf("1.對輸入的四位數進行加密操作。\n");
printf("2.對輸入的四位數進行解密操作。\n");
printf("3.退出程序。\n");
printf("---------------------------------\n");
while(k!=3) //循環選擇操作
{
printf("please enter your chance:");
scanf("%d",&k);
g=number%10;
s=number/10%10;
b=number/100%10;
q=number/1000;
switch(k)
{
case 1: //加密選項
{
encrypt(g,s,b,q);
printf("加密後的數字為:%d\n",encrypt(g,s,b,q));
}
break;
case 2: //解密選項
{
deciphering(g,s,b,q);
printf("解密後的數字為:%d\n",deciphering(g,s,b,q));
}
break;
case 3: //退出選項
{
printf("close the code!\n");
}
break;
default:
{
}
break;
}
}
}
int encrypt(int a,int b,int c,int d) //加密函數
{
int encryptnumber=0;
int temp1,temp2;
if(a+5>=10) //悶神判斷是否超過十,此處可以簡化為函數,不過我沒做
{
a=abs(a+5-10);
}
else
{
a=a+5;
}
if(b+5>=10)
{
b=abs(b+5-10);
}
else
{
b=b+5;
}
if(c+5>=10)
{
c=abs(c+5-10);
}
else
{
c=c+5;
}
if(d+5>模枝=10)
{
d=abs(d+5-10);
}
else
{
d=d+5;
}
temp1=a;
a=d;
d=temp1;
temp2=b;
b=c;
c=temp2;
encryptnumber=d*1000+c*100+b*10+a;
//printf("加密後的數字為:%d\n",encryptnumber);
return encryptnumber;
}
int deciphering(int a,int b,int c,int d) //解密函數
{
int encrypt(int a,int b,int c,int d); //函數嵌套
int decipheringnumber=0;
int encryptnum=encrypt(a,b,c,d); //取螞碼虧得加密函數的結果
int temp1,temp2;
int gg,ss,bb,qq;
gg=encryptnum%10;
ss=encryptnum/10%10;
bb=encryptnum/100%10;
qq=encryptnum/1000;
temp1=gg;
gg=qq;
qq=temp1;
temp2=bb;
bb=ss;
ss=temp2;
if(gg-5<=0) //判斷是否超過十,此處可以簡化為函數,不過我沒做
{
gg=abs(gg-5+10);
}
else
{
gg=gg-5;
}
if(ss-5<=0)
{
ss=abs(ss-5+10);
}
else
{
ss=ss-5;
}
if(bb-5<=0)
{
bb=abs(bb-5+10);
}
else
{
bb=bb-5;
}
if(qq-5<=0)
{
qq=abs(qq-5+10);
}
else
{
qq=qq-5;
}
decipheringnumber=qq*1000+bb*100+ss*10+gg;
return decipheringnumber;
}
J. 什麼是c語言程序設計
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言訂定了一套完整的國際標准語法,稱為ANSI C,作為C語言最初的標准。
c語言程序設計 就是用c語言的功能函數開發的程序。