目前共有24篇帖子。 內容轉換:不轉換▼
 
點擊 回復
1636 23
今天上课写的C程序
180.84.33.*
1樓 發表于:2015-11-27 10:03

#include <stdio.h>
//#include <stdlib.h>

/*int *fun()
{
    //int *p = (int *)malloc(sizeof(int));
    int a;
    int *p = &a;
    *p = 12;
    return p;
}*/

void invert(int *array, int n)
{
    int *i, *j, temp;
    i = array;
    j = array + n - 1;
    while (i < j)
    {
        temp = *i;
        *i = *j;
        *j = temp;
        i++;
        j--;
    }
}

void main()
{
    /*int a[] = {15, -27, 63, 49, 115, 663, 774};
    int *const p = &a[2];
    printf("%d\n", (*p)++);
    printf("%d\n", *p);*/

/*    int *p2 = fun();
    int *p3;
    *p2 += 2;
    p3 = fun();
    printf("%d\n", *p3);
    printf("%d\n", *p2);*/
    //free(p2);
    //free(p3);

    int a[] = {2, 4, 7, 3, 6, 9, 11, 14, 8};
    int len = sizeof(a) / sizeof(int);
    int i;
    for (i = 0; i < len; i++)
        printf("%-3d", *(a + i));
    printf("\n");

    invert(a, 4);
    for (i = 0; i < len; i++)
        printf("%-3d", *(a + i));
    printf("\n");
}

180.84.33.*
2樓 發表于:2015-11-27 10:04

学校机房内的电脑虽然默认情况下不能上网,但是把网卡的IP地址改为自动获取(DNS不需要改为自动获取),大概半分钟后就能上网了。
180.84.33.*
3樓 發表于:2015-11-27 10:05

一派護法 十九級
4樓 發表于:2015-11-27 10:24

当函数要返回一个指针的时候,这个指针不能指向该函数内部定义的局部变量,但可以指向静态变量或者malloc函数分配的空间。
如果函数真的返回了指向局部变量的指针的话,那么在主函数中就不得对指向的空间的值进行任何写操作,只能读取其值,否则会出错。
202.115.90.*
5樓 發表于:2015-11-27 16:26

#include <stdio.h>
void main()
{
int x,y,z;
for(x=0;x<=100;x++)
for(y=0;y<=100;y++)
{
z=100-x-y;
if(x*5+y*3+z/3==100)
printf("x=%d,y=%d,z=%d\n",x,y,z);
}
}
202.115.90.*
6樓 發表于:2015-11-27 16:46

#include <stdio.h>

void main()
{
int a = 10;
printf("%d\n", a = a + 2); //12
printf("%d\n", a += 2); //14
}
202.115.90.*
7樓 發表于:2015-11-27 16:50

#include <stdio.h>

void main()
{
char str[] = "abcdefg";
char *pStr = str;
*pStr++ = 'R';
printf("%s\n", str); //Rbcdefg
printf("%s\n", pStr); //bcdefg
}
202.115.90.*
8樓 發表于:2015-11-27 17:24

// Page 211
#include <stdio.h>
#include <string.h>

void insert(char *s1, const char *s2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int pos = -1; // when not found
int i;

/* Find the position */
for (i = 0; i < len1; i++)
{
if (*(s1 + i) == *s2)
{
pos = i; // found
break;
}
}

/* Move */
if (pos != -1)
{
for (i = len1; i >= pos - 1; i--)
{
// At first when i == len1, the character is '\0'
// and then '\0' will be moved to the end
*(s1 + i + len2 - 1) = *(s1 + i);
}
}

/* Copy chars from s2 to s1 */
if (pos == -1)
{
pos = len1;
*(s1 + len1) = *s2; // copy the first character of s2
*(s1 + len1 + len2) = '\0';
}
// copy the rest
for (i = 1; i < len2; i++)
*(s1 + pos + i) = *(s2 + i);

// abcdefghijklmn
// f123
// abcdef ghijklmn
// abcdef123ghijklmn

// abcdef
// d45
// abcd ef
// abcd45ef

// abcdef
// 45
// abcdef45
}

void main()
{
char s1[50] = "abcdef";
char s2[50] = "d45";
insert(s1, s2);
printf("%s\n", s1);

strcpy(s1, "abcdef");
strcpy(s2, "45");
insert(s1, s2);
printf("%s\n", s1);

strcpy(s1, "abcdefghijklmn");
strcpy(s2, "f123");
insert(s1, s2);
printf("%s\n", s1);
}
202.115.90.*
9樓 發表于:2015-11-27 17:26

回复:8楼

这是我完全自己写的版本,没有看书上的。

202.115.90.*
10樓 發表于:2015-11-27 17:29

不得不说书上211页那个版本的程序比我写的简洁得多。
一派護法 十九級
11樓 發表于:2015-11-28 14:02

回復:8樓
move下的i >= pos - 1那里最好加一个条件:&& i >= 0
202.115.90.*
12樓 發表于:2015-12-3 14:22

#include <stdio.h>

int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}

void main()
{
int a, b, x;
int (*pt)();
pt = max;
scanf("%d%d", &a, &b);
x = (*pt)(a, b);
printf("%d\n", x);
}
一派護法 十九級
13樓 發表于:2015-12-3 14:39

一派護法 十九級
14樓 發表于:2015-12-3 14:43

202.115.90.*
15樓 發表于:2015-12-3 14:46

// 11.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#pragma comment(lib, "comctl32.lib")
#include "commctrl.h"


#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY11, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY11);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY11);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MY11;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

InitCommonControls();

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
202.115.90.*
16樓 發表于:2015-12-4 16:49

// 3.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <stdio.h>

void alert(const char* str)
{
MessageBox(NULL, str, "Title", MB_ICONWARNING);
}

int confirm(const char *str)
{
return (MessageBox(NULL, str, "Title", MB_ICONQUESTION | MB_YESNO) == IDYES);
}

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

char str[50];
sprintf(str, "Hello World!\nnCmdShow: %d", nCmdShow);

if (confirm(str))
alert("Haha");
else
alert("hoho");

return 0;
}


202.115.90.*
17樓 發表于:2015-12-10 15:25

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
int num;
char name[100];
float score;
struct student *next;
};

struct student *createTable(int n)
{
struct student *head, *p, *q;
int i;
head = NULL;
q = NULL;
for (i = 0; i < n; i++)
{
p = (struct student *)malloc(sizeof(struct student));
scanf("%d%f\n", &p->num, &p->score);
//printf("num: %d, score: %f\n", p->num, p->score);
gets(p->name);
//printf("name: %s\n", p->name);
p->next = NULL;
if (head == NULL)
head = p;
else
q->next = p;
q = p;
}
return head;
}

void traverseTable(struct student *head)
{
struct student *p;
p = head;
while (p != NULL)
{
printf("%d,%s,%4.2f\n", p->num, p->name, p->score);
p = p->next;
}
}

void freeTable(struct student *head)
{
struct student *p, *q;
p = head;
while (p != NULL)
{
q = p->next;
free(p);
p = q;
}
}

void main()
{
struct student *p, *head;
head = createTable(4);
p = (struct student *)malloc(sizeof(struct student));
strcpy(p->name, "New Item");
p->num = 10;
p->score = 8.6f;
p->next = head->next->next;
head->next->next = p;
traverseTable(head);
freeTable(head);
}
202.115.90.*
18樓 發表于:2015-12-10 15:28

1 1.1
a
2 2.2
b
3 3.3
c
4 4.4
d
1,a,1.10
2,b,2.20
10,New Item,8.60
3,c,3.30
4,d,4.40
Press any key to continue
一派護法 十九級
19樓 發表于:2015-12-10 15:34

一派護法 十九級
20樓 發表于:2015-12-10 15:47

奇怪了,这段程序在app下每行开头的空格又是正常显示的。。。。
202.115.90.*
21樓 發表于:2015-12-11 16:52

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
int num;
char name[20];
char gender;
int age;
float score;
};

void main()
{
/*FILE f, *fp;
struct student stu;
stu.age = 90;
stu.gender = 'm';
strcpy(stu.name, "曹磊");
stu.num = 68;
stu.score = 74.6f;
fp = fopen("haha.dat", "wb");
fwrite(&stu, sizeof(struct student), 1, fp);
f = *fp;
fclose(fp);

//printf("ptr: %s\n", f._ptr);
printf("cnt: %d\n", f._cnt);
//printf("base: %s\n", f._base);
printf("flag: %d\n", f._flag);
printf("file: %d\n", f._file);
printf("charbuf: %d\n", f._charbuf);
printf("bufsiz: %d\n", f._bufsiz);
printf("tmpfname: %s\n", f._tmpfname);*/

struct student stu;
FILE f;
int t = 0;
FILE *fp;
if ((fp = fopen("stu.dat", "rb")) == NULL)
{
printf("Can't open file\n");
exit(1);
}
printf("num\tname\tgender\tage\tscore\n");
while (fread(&stu, sizeof(struct student), 1, fp) == 1)
printf("%-8d%-10s%-5c%-5d%-4.2f\n", stu.num, stu.name, stu.gender, stu.age, stu.score);
//printf("ptr: %s\n", fp->_ptr);
//printf("base: %s\n", fp->_base);
//printf("tmpfname: %s\n", fp->_tmpfname);
fclose(fp);

if ((fp = fopen("f.dat", "rb")) == NULL)
{
printf("Can't open file\n");
exit(1);
}
printf("\nbufsize\tcharbuf\tcnt\tfile\tflag\n");
while (fread(&f, sizeof(FILE), 1, fp) == 1)
printf("%d\t%d\t%d\t%d\t%d\n", f._bufsiz, f._charbuf, f._cnt, f._file, f._flag);
fclose(fp);

/*char numstr[20], ch;
FILE file;
FILE *fp, *fp2;
if ((fp = fopen("stu.dat", "ab")) == NULL)
{
printf("Can't open file\n");
exit(1);
}
fp2 = fopen("f.dat", "ab");
do
{
printf("Enter number: ");
gets(numstr);
stu.num = atoi(numstr);
printf("Enter name: ");
gets(stu.name);
printf("Enter sex: ");
stu.gender = getchar();
getchar();
printf("Enter age: ");
gets(numstr);
stu.age = atoi(numstr);
printf("Enter score: ");
gets(numstr);
stu.score = (float)atof(numstr);
fwrite(&stu, sizeof(struct student), 1, fp);
printf("Have another student record(y/n)?");
ch = getchar();
getchar();

file = *fp;
fwrite(&file, sizeof(FILE), 1, fp2);
} while (ch == 'Y' || ch == 'y');
fclose(fp);
fclose(fp2);*/
}
202.115.90.*
22樓 發表于:2015-12-11 17:09

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
int num;
char name[20];
char gender;
int age;
float score;
};

void main()
{
struct student stu;
FILE *fp;
int i;
if ((fp = fopen("stu.dat", "rb")) == NULL)
{
printf("Can't open file\n");
exit(1);
}

printf("文件中保存的所有记录如下:\n");
printf("Num\tName\tGender\tAge\tScore\n");
while (fread(&stu, sizeof(struct student), 1, fp) == 1)
printf("%-8d%-10s%-5c%-5d%-4.2f\n", stu.num, stu.name, stu.gender, stu.age, stu.score);
printf("请输入要查看的记录编号:");
scanf("%d", &i);

printf("第%d条记录的内容是:\n", i);
fseek(fp, (i - 1) * sizeof(struct student), SEEK_SET);
if (fread(&stu, sizeof(struct student), 1, fp) == 1)
printf("%-8d%-10s%-5c%-5d%-4.2f\n", stu.num, stu.name, stu.gender, stu.age, stu.score);
fclose(fp);
}


文件中保存的所有记录如下:
Num Name Gender Age Score
115 Tony 1 15 48.60
227 Tom 2 18 66.90
43 RPG 2 17 49.60
请输入要查看的记录编号:3
第3条记录的内容是:
43 RPG 2 17 49.60
Press any key to continue
202.115.90.*
23樓 發表于:2015-12-11 17:09

num name gender age score
115 Tony 1 15 48.60
227 Tom 2 18 66.90
43 RPG 2 17 49.60

bufsize charbuf cnt file flag
4096 0 4060 3 10
Press any key to continue
202.115.90.*
24樓 發表于:2015-12-11 17:11

stu.dat的内容如下:
sTony烫烫烫烫烫烫烫?烫?ffBB鉚om烫烫烫烫烫烫烫?烫?吞匓+RPG烫烫烫烫烫烫烫烫2烫?ffFB

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:1636 回複數:23
評論數: ?
作者: 180.84.33.*
最後回復:202.115.90.*
最後回復時間:2015-12-11 17:11
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。