[ create a new paste ] login | about

Link: http://codepad.org/XZRH6pIB    [ raw code | output | fork ]

C, pasted on Jun 17:
// 2012年度 プログラミング言語 第9回演習
// 別途講義中の指示に従いファイルを作成し提出すること

// /*?? */を適切なプログラムに置き換えて各演習のプログラムを完成させること.


/*----------------------------------------------------------------------*/
// ●演習9-1 リストを用いた成績処理
// ◎ソースコード
#include <stdio.h>
#include <stdlib.h>

#define NAME_LEN 20
#define SUB_NO 4

typedef struct student {
	struct student *next;
	int st_id;
	char st_name[NAME_LEN];
	int english;
	int mathematics;
	int chemistry;
	int physics;
} student_t;

void printdata (student_t *st_p) {
	printf("%d\t%s\t%d\t%d\t%d\t%d\n", 
		/*?? (1) ここに*st_pの各メンバを出力する記述をする */);
}

void printAverage (student_t *st_p) {
	double ave = (/*?? (2) ここに各科目の総和を計算する式を記述する*/)/(double)SUB_NO;
	printf("The average score of %d %s is %4.1f.\n", st_p->st_id, st_p->st_name, ave);
}

void main() {
	student_t* head;//リストの先頭
	student_t* stup;//入力されたデータの一時保管用
	student_t* temp;//リスト操作用のテンポラリ

	int found;
	int command;
	int key_id;
	int stn = 0;//読み込んだ人数を数える

	double subjectAverage[SUB_NO];
	for(int i = 0; i < SUB_NO; i++) subjectAverage[i] =0;

	head = NULL;

	printf("%s %s %s\n", CLASS, ID, NAME);

	while(1) {
		/*?? (3) malloc関数を使ってヒープ領域を確保しstupに代入 */
		printf("Please input data. : No.%d \n", stn+1);
		printf("Student number (Typing \"0\" to exit.):");
		scanf_s("%d", &stup->st_id);
		if (stup->st_id == 0) break;
		printf("Name:");
		scanf_s("%s", stup->st_name, NAME_LEN);
		printf("English:");
		scanf_s("%d", &stup->english);
		printf("Mathematics:");
		scanf_s("%d", &stup->mathematics);
		printf("Chemistry:");
		scanf_s("%d", &stup->chemistry);
		printf("Physics:");
		scanf_s("%d", &stup->physics);
		stup->next = head;
		stn++;

		if (head == NULL) head = stup;//一番最初のデータを入力した時の処理
		else if (head->st_id >= stup->st_id) {
			//リストのhead側(先頭)の学番より入力された学番が小さい場合は,
			//リストのhead側(先頭)に入力されたデータを追加する.

			/*?? (4) headの中身を現在の先頭のものに入れ替える */
		} else {
			//リストのhead側(先頭)の学番より入力された学番が大きい場合は,
			//リスト内を操作して入力されたデータを適切な場所に追加する.
			//つまり,学生番号順に並ぶように追加する場所を調べている
			temp = head;
			while (temp->next != NULL ) {
				//tempには現在操作中の構造体データが入っている
				if (temp->next->st_id >= stup->st_id){
					//tempの次に操作するデータの学番が入力された
					//学番より大きい場合はwhileから抜ける
					break;
				}
				//次の構造体データに移動
				temp = temp->next;
			}
			//現在操作中のデータの後に入力されたデータを挿入する.
			stup->next = temp->next;
			temp->next = stup;
		}
	}

	temp = NULL; //tempを再初期化
	printf("%d students data loaded.\n", stn);
	printf("\n--------------------------------\n");

	while(1) {
		printf("\nPlease input the number.\n");
		printf("1 : List of Result\n");
		printf("2 : Personal Result\n");
		printf("3 : Personal Average\n");
		printf("4 : Subject Average\n");
		printf("0 : exit\n");
		scanf_s("%d", &command);

		if (command == 1){
			printf("List of Result\n");
			printf("Num.\tName\tEng.\tMath.\tChem.\tPhys.\n");
			for(/*?? (5) リストの走査条件 */) {
				//上記により,リストの要素を先頭(head側)から順に処理をする
				//着目している要素はtempで示している.
				printdata(temp);
			}
		} else if (command == 2){
			printf("Please input student number.\n");
			scanf_s("%d", &key_id);
			found = 0;
			for(/*?? (5) リストの走査条件 */) {
				if (temp->st_id == key_id) {
					printdata(temp);
					found = 1;
					break;
				}
			}
			if (found == 0) printf("Not Found. (%d)\n", key_id);
		} else if (command == 3) {
			printf("Please input student number.\n");
			scanf_s("%d", &key_id);
			found = 0;
			for(/*?? (5) リストの走査条件 */) {
				if (temp->st_id == key_id) {
					printAverage(temp);
					found = 1;
					break;
				}
			}
			if (found == 0) printf("Not Found. (%d)\n", key_id);
		} else if (command == 4) {
			printf("Eng.\tMath.\tChem.\tPhys.\n");
			for(/*?? (5) リストの走査条件 */) {
				subjectAverage[0] += temp->english;
				subjectAverage[1] += temp->mathematics;
				subjectAverage[2] += temp->chemistry;
				subjectAverage[3] += temp->physics;
			}
			for (int i = 0; i < SUB_NO; i++) {
				printf("%4.1f\t", subjectAverage[i] / (double)stn);
				subjectAverage[i] = 0.0;
			}
		} else if (command == 0) {
			break;
		} else {
			printf("invalid number\n");
		}
		printf("\n--------------------------------\n");
		temp = NULL; //tempを再初期化
	}
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
In function 'printdata':
Line 28: error: expected expression before ')' token
In function 'printAverage':
Line 32: error: expected expression before ')' token
In function 'main':
Line 47: error: 'for' loop initial declaration used outside C99 mode
Line 51: error: 'CLASS' undeclared (first use in this function)
Line 51: error: (Each undeclared identifier is reported only once
Line 51: error: for each function it appears in.)
Line 51: error: 'ID' undeclared (first use in this function)
Line 51: error: 'NAME' undeclared (first use in this function)
Line 115: error: expected expression before ')' token
Line 115: error: expected expression before ')' token
Line 124: error: expected expression before ')' token
Line 124: error: expected expression before ')' token
Line 136: error: expected expression before ')' token
Line 136: error: expected expression before ')' token
Line 146: error: expected expression before ')' token
Line 146: error: expected expression before ')' token
Line 152: error: 'for' loop initial declaration used outside C99 mode
Line 36: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: