)。直接编"og:type" content="webpage">)。直接编" /> )。直接编"> 经典C语言程序例子_软件运维_内存溢出

阅读 7

经典C语言程序例子,第1张

题目01:在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同的单词。

直接编译,程序执行结果如下图所示:

题目02:编写一个int string_len(char *s),返回字符串s的字符长度(不包括\0)。

直接编译,程序执行结果如下图所示:

扩展资料:

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。// 电话本

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语言编写的电话本程序 很有实用意义

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

struct Message

{

char name[10]

int number

char sort[4]

int price

char company[4]

struct Message *next

}

void Interface(void)

{

printf("\n*******************************\n")

printf(" 1:添加记录 2: 显示记录 \n")

printf(" 3:查询记录 4: 代号的排序 \n")

printf(" 5:删除记录 6: 保存记录 \n")

printf(" 7:退出 \n")

printf("*********************************\n")

return

}

struct Message * AddMessage(struct Message *head)

{

struct Message *message

message = (struct Message *) malloc (sizeof (struct Message)) printf("请输入

名称: \n")

scanf("%s", message->name)

fflush(stdin)

printf("请输入代号: \n")

scanf("%d", &message->number)

fflush(stdin)

printf("请输入类别: \n")

scanf("%d", &message->sort)

fflush(stdin)

printf("请输入公司: \n")

scanf("%d", &message->company)

fflush(stdin)

printf("请输入价格: \n")

scanf("%d", &message->price)

message->next = NULL

if (head == NULL)

{

return message

}

else

{

message->next = head

return message

}

}

void ShowMessage(struct Message *head)

{

if (head == NULL)

{

printf("没有信息显示!\n")

}

else

{

while (head != NULL)

{

printf("股票名称: %s\n", head->name)

printf("代号: %d\n", head->number)

printf("类别: %s\n", head->sort)

printf("公司: %s\n", head->company)

printf("价格: %d\n", head->price)

head = head ->next

}

}

}

void SearchMessage(struct Message *head)

{

char name[10] printf("请输入你要

查询的名称: \n")

scanf("%s", name)

while (head != NULL)

{

if (strcmp(head->name, name) == 0)

{

printf("股票名称: %s\n", head->name)

printf("代号: %d\n", head->number)

printf("类别: %s\n", head->sort)

printf("公司: %s\n", head->company)

printf("价格: %d\n", head->price)

break

}

else

{

head = head->next

}

}

if (head == NULL)

{

printf("没有你要查询的信息\n")

}

}

void Sort(struct Message *head)

{

/* struct Message *temp

temp = (struct Message *)malloc(sizeof(struct Message))

memset(temp, 0, sizeof(struct Message))

struct Message *nextTo

nextTo = head->next

if (head != NULL &&nextTo != NULL)

{

while (head != NULL)

{

while (nextTo != NULL)

{

if (head->number >nextTo->number)

{

temp = head

head = nextTo

head ->next = temp

temp ->next = nextTo ->next

nextTo = temp

}

nextTo = nextTo->next

}

head = head->next

}

}

else

{

return

}

return*/

printf("暂时不能实现\n")

}

struct Message * RemoveMessage(struct Message *head)

{

struct Message *h = head

char name[10]

struct Message *nextTo, *p

printf("请输入你要删除的名称: \n")

scanf("%s", name)

if (head == NULL)

{

printf("还没有添加任何信息,无法删除!\n")

return head

}

else if (strcmp(head->name, name) == 0)

{

//p = head

head = head->next

return head

//free(p)

}

else

{

nextTo = head->next

while (strcmp(nextTo->name, name) != 0 &&nextTo != NULL)

{

head = nextTo

nextTo = nextTo ->next

}

if (strcmp(nextTo->name, name) == 0)

{

head->next = nextTo ->next

free(nextTo)

return h

}

else

{

printf("没有你要删除的信息!\n")

return h

}

}

return h

}

void SaveMessage(struct Message *head)

{

if (head == NULL)

{

return

}

FILE *fp

fp = fopen("file.txt", "ab+")

if (fp == NULL)

{

printf("打开文件出错!\n")

return

}

while (head != NULL)

{

fwrite(head, sizeof(struct Message), 1, fp)

head = head ->next

}

printf("保存成功!\n")

fclose(fp)

}

int main(void)

{

int choice

struct Message *head = NULL

while (1)

{

Interface()

printf("请选择功能项: \n")

scanf("%d", &choice)

switch (choice)

{

case 1:

head = AddMessage(head)

break

case 2:

ShowMessage(head)

break

case 3:

SearchMessage(head)

break

case 4:

Sort(head)

break

case 5:

head = RemoveMessage(head)

break

case 6:

SaveMessage(head)

break

case 7:

exit(1)

break

default:

printf("选择有误,请重新选项!\n")

}

}

return 0


}

欢迎分享,转载请注明来源:

内存溢出

原文地址:

http://outofmemory.cn/yw/11350397.html
图标
(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
打码赚钱 打码赚钱 一级用户组
网易lofter有小程序吗
上一篇
2023-05-15
excel怎么算rsd
2023-05-15

发表评论
请登录后评论... 登录
提交

    评论列表(0条)
保存
{label} {label}